Programming
--
Questions
--
Followers
Top Experts
One for the Experts...
31
Good day gentlemen (and any ladies that may also be present among us).
I have a cracking challenge that will tease your expertise.
I have titled this challenge '31' because it is based on a simple game I played as a child.
The game starts with a running total of 0. Two players take turns choosing a number between '1' and '6' which is added to the total. The player who chooses the number which brings the total to '31' wins.
The numbers '1' to '6' are chosen from a pool of four '1's, four '2's, four '3's, four '4's, four '5's and four '6's. Once a number is chosen, it is removed from the pool of available numbers.
Example:
Total = 0
Let's say we have the following numbers available:
2222
3333
4444
5555
6666
Player A chooses say, 5.
Total = 5 (because 0 + 5 = 5)
A '5' is removed from the pool of available numbers so we now have:
2222
3333
4444
555
6666
(Notice there are now only 3 '5's in the pool)
Let's say player B chooses '4'
Total = 9 (because 5 + 4 = 9)
And our number pool now looks like the following:
2222
3333
444
555
6666
If any set of numbers '1' through '6' is exhausted then that number cannot be chosen in subsequent turns.
Programming Considerations
The program must be written using only DOS commands as a .BAT or .CMD batch file and run in a DOS environment (Command or CMD).
This is a two-player game and so the players will be you - the human, and the computer.
At the start of the game, the computer will randomly choose who goes first. At the end of the game, there will be an option to play again at which point, players take it in turn to go first. A simple Wins score is maintained for this purpose.
The Wins score is displayed when the player quits the game.
NOTE:
Please indicate your participation by commenting.
Closing date for this challenge is midnight 10/4/2018
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
I will be participating, thanks.
Does your end date indicate October 4th 2018 or April 10th 2018? (I'm afraid I find anything except ISO formatted dates to be clear as mud ^^)
(Edit, Given today is March 10th, I'll hazard you used the UK date format, and It's April 10th.)
Thanks,
Ben (Q)
Steve
You know me, I'll have to poke at it, but probably not until next month. A few questions though:
- Is the requirement for a smart computer opponent with "win" logic, or just logic to pick randomly a number that is available and fits in the sequence?
- Am I correct in assuming that you have to hit 0 exactly, can not go negative on final play?
- If it can happen (haven't thought about the math) and either player can't play an available number without going past 0, is that a forfeit?
»bp






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I want to try something different...
I want to run the program (named say, 31.bat) which establishes computer vs computer or computer vs human players. Then, 31.bat spawns two instances of itself in two separate DOS sessions but with names/IDs passed as parameters so that both spawned 31.bat instances becomes 'aware' they are players, however neither of the two spawned instances of 31.bat are aware of the other 31.bat's player's name or ID other than know that 'an opponent exists', and then the game commences.
The challenge will be to have both instances of 31.bat run as separate threads and alternate play.
This means there will be (possibly) three DOS sessions running on the desktop. I haven't decided how I'm going to implement alternate play yet. I need more time to think...
Not going to have time to do this justice at the moment mind.
Steve

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Here's what I came out so far. It's working well but I'm out of time so can't finish it today.
Also, I currently don't have time to make player 1 the "Computer".
My current challenge: In the CHOICE function, when a number is not available anymore and removed from /C, %errorlevel% does not match the selected number.
For example:
Choice /C "123456", when pressing on 2, %errorlevel% = 2
Choice /C "13456", when pressing on 3, %errorlevel% = 2
Try my script making sure you leave one of each available. It will work fine.
Have fun optimizing it, fixing it, changing it :)
Cheers mates!
@ECHO OFF
SETLOCAL EnableDelayedExpansion
:Start
CLS
SET /a Total1=0
SET /a Total2=0
REM DEFINE NUMBERS TO PLAY WITH - MAX = 9
SET MaxNb=6
FOR /L %%A in (1,1,%MaxNb%) DO SET Nb%%A=%%A%%A%%A%%A%%A
REM SELECT PLAYERS NAME
SET /p Player1=ENTER PLAYER 1 NAME":
SET /p Player2=ENTER PLAYER 2 NAME":
REM RANDOMIZE FIRST PLAYER
SET /A WhoIsPlaying=%RANDOM% * (2 - 5 + 1) / 32768 + 2
:HOME
CLS
IF "%WhoIsPlaying%" == "1" (SET WhoIsPlaying=2) ELSE (SET WhoIsPlaying=1)
ECHO %Player1% [%Total1%] VS %Player2% [%Total2%]
ECHO ----------------------
ECHO !Player%WhoIsPlaying%! IS NOW PLAYING
ECHO.
REM DEFINING CHOICE OPTIONS
SET Choose=
FOR /L %%A in (1,1,%MaxNb%) DO IF DEFINED Nb%%A SET Choose=!Choose!%%A
REM CHOOSING A NUMBER
ECHO CHOOSE A NUMBER
ECHO --------------------
REM LISTING AVAILABLE NUMBERS
FOR /F "Tokens=2 delims==" %%A in ('SET Nb') DO ECHO %%A
ECHO.
CHOICE /C "%Choose%"
SET Selection=%errorlevel%
SET Nb%Selection%=!Nb%Selection%:~0,-1!
SET /a Total%WhoIsPlaying%=!Total%WhoIsPlaying%! + %Selection%
IF !Total%WhoIsPlaying%! == 31 (
ECHO.
ECHO The winner is: !Player%WhoIsPlaying%!
PAUSE
GOTO GameOver
)
IF !Total%WhoIsPlaying%! GTR 31 (
ECHO.
ECHO GAME OVER. IT'S A TIE [Total=!Total%WhoIsPlaying%!]
PAUSE
GOTO GameOver
)
GOTO HOME
:GameOver
ECHO.
CHOICE /M "Play again?"
IF %errorlevel% == 1 Goto Start
EXIT
@ECHO OFF
SETLOCAL EnableDelayedExpansion
:Start
CLS
SET /a Total1=0
SET /a Total2=0
REM DEFINE NUMBERS TO PLAY WITH - MAX = 9
SET MaxNb=6
FOR /L %%A in (1,1,%MaxNb%) DO SET Nb%%A=%%A%%A%%A%%A%%A
FOR /L %%A in (1,1,%MaxNb%) DO SET ChoiceList=!ChoiceList!%%A
REM SELECT PLAYERS NAME
SET /p Player1=ENTER PLAYER 1 NAME":
SET /p Player2=ENTER PLAYER 2 NAME":
REM RANDOMIZE FIRST PLAYER
SET /A WhoIsPlaying=%RANDOM% * (2 - 5 + 1) / 32768 + 2
:HOME
IF "%WhoIsPlaying%" == "1" (SET WhoIsPlaying=2) ELSE (SET WhoIsPlaying=1)
:HOME2
CLS
ECHO %Player1% [%Total1%] VS %Player2% [%Total2%]
ECHO ----------------------
ECHO !Player%WhoIsPlaying%! IS NOW PLAYING
ECHO.
REM DEFINING CHOICE OPTIONS
SET Choose=
FOR /L %%A in (1,1,%MaxNb%) DO IF DEFINED Nb%%A SET Choose=!Choose!%%A
REM CHOOSING A NUMBER
ECHO CHOOSE A NUMBER
ECHO --------------------
REM LISTING AVAILABLE NUMBERS
FOR /F "Tokens=2 delims==" %%A in ('SET Nb') DO ECHO %%A
ECHO.
CHOICE /C "%ChoiceList%" /N /M "Choose a number [%Choose%]:"
SET Selection=%errorlevel%
IF NOT DEFINED Nb%Selection% (
ECHO.
ECHO ****** THAT NUMBER IS NOT AVAILABLE ANYMORE ******
ECHO.
PAUSE
GOTO HOME2
)
SET Nb%Selection%=!Nb%Selection%:~0,-1!
SET /a Total%WhoIsPlaying%=!Total%WhoIsPlaying%! + %Selection%
IF !Total%WhoIsPlaying%! == 31 (
ECHO.
ECHO The winner is: !Player%WhoIsPlaying%!
ECHO.
PAUSE
GOTO GameOver
)
IF !Total%WhoIsPlaying%! GTR 31 (
ECHO.
ECHO GAME OVER. IT'S A TIE [Total=!Total%WhoIsPlaying%!]
ECHO.
GOTO GameOver
)
GOTO HOME
:GameOver
ECHO.
CHOICE /M "Play again?"
IF %errorlevel% == 1 Goto Start
EXIT






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Cheers
Cheers
Programming
--
Questions
--
Followers
Top Experts
Programming includes both the specifics of the language you’re using, like Visual Basic, .NET, Java and others, but also the best practices in user experience and interfaces and the management of projects, version control and development. Other programming topics are related to web and cloud development and system and hardware programming.
Create your account and start contributing!