Link to home
Start Free TrialLog in
Avatar of Paul Tomasi
Paul TomasiFlag for United Kingdom of Great Britain and Northern Ireland

asked on

One for the Experts... 31

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:

1111
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:

1111
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:

1111
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
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Hey Paul,

  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)
Heh, why not....  will try and look next week.

Steve
Avatar of Bill Prew
Bill Prew

Hello Paul, et al,

You know me, I'll have to poke at it, but probably not until next month.  A few questions though:

  1. 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?
  2. Am I correct in assuming that you have to hit 0 exactly, can not go negative on final play?
  3. 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
Avatar of Paul Tomasi

ASKER

Good day everybody. Bill Prew poses some interesting questions and I have re-thought the game (it's been a very long time since I actually played it) so instead of counting down from 31 to 0, the initial total starts at 0 and increases by players' choices (1 through 6) as play progresses. The winner is the player who lands on 31. If it's possible (I can't remember) to force a player to not land on 31 but go higher, then a draw is declared.
I feel like contributing to this too.

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...
Interesting....  I suppose you would have to use and watch for flag files or registry entries for from one windows to the next.  If you used the presence of a flag file to show player details and to say it is your turn etc. they could be on a network drive or even a synced dropbox / onedrive folder.... we could share a folder between us all and play batch file game together :-)

Not going to have time to do this justice at the moment mind.

Steve
Hey all!  Nice to see you again :)

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

Open in new window

Choice problem fixed

@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

Open in new window

Hiya all. I've been unwell. Hiya ReneGe, I'm reviewing your code...
Adding this comment so that question does not close.
Cheers
Hey Paul, did you have a chance to look at my script?
Cheers
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.