-1

This is my code if you want to run it yourself just copy it and save as .bat. Its meant to be a scissors Paper Rock to see if i could make a game in batch, it keeps closing after you make a choice between scissors paper rock.

@echo off  
:setting  
title Scissors, Paper, Rock  

`Choosing between Scissors, Paper and Rock`
:an  
set /a ran=%random%  
if /i %ran% GTR 3 goto an  
if /i %ran% LSS 1 goto an  
if %ran%==1 set %ans%=Scissors  
if %ran%==2 set %ans%=Paper  
if %ran%==3 set %ans%=Rock  

`Human Making a Choice`
:in  
echo Choose:  
echo [1] = Scissors  
echo [2] = Paper  
echo [3] = Rock  
set /p text=  
if text==1 goto 1  
else if text==2 goto 2  
else if text==3 goto 3  

`Outcome`
:1  
echo You chose Scissors. The Computer chooses %ans%  
if %ans%==Scissors echo Draw  
if %ans%==Paper echo Win  
if %ans%==Rock echo Loose  
pause  
goto out  

`Outcome`
:2  
echo You chose Paper. The Computer chooses %ans%  
if %ans%==Scissors echo Loose  
if %ans%==Paper echo Draw  
if %ans%==Rock echo Win  
pause  
goto out  

`Outcome`
:3  
echo You chose Rock. The Computer chooses %ans%  
if %ans%==Scissors echo Win  
if %ans%==Paper echo Loose  
if %ans%==Rock echo Draw  
pause  
goto out  

`Play again or not`
:out  
echo GG Want to play again?  
echo [1] = Yes  
echo [2] = No  
set /p text=  
if text==1 goto setting  
if text==2 pause  

Thanks if you guys can help its really appreciated because i'm really confused right now.

Drew
  • 3,814
  • 2
  • 9
  • 28
  • Sorry lose*...... – clashof clans Jul 22 '18 at 04:52
  • Open the command prompt and run the script from there instead of double-clicking it. You'll see exactly what's wrong (hint: it's lines 22 and 23). (And honestly, the backticks probably aren't helping things either.) – SomethingDark Jul 22 '18 at 05:05
  • Additional hint, You have these at the end of every line, twice! – Drew Jul 22 '18 at 05:07
  • yeah sorry the back ticks i couldnt post the question without explaining the code i couldnt work out how to do it back ticks was what i thought.... sorry – clashof clans Jul 22 '18 at 05:15
  • I suggest to carefully read the answer on [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) – Mofi Jul 23 '18 at 16:38

3 Answers3

1
  • Using choice.exe you can save some of the input validation as it prevents illegal inputs.
  • set /a allows several calculations in one command and doesn't require enclosing vars in %/!
  • the batch loops (with a timeout delay) and maintains a score.
  • fetches the users first name from net user

:: Q:\Test\2018\07\22\SO_51462128.cmd
@Echo off&SetLocal EnableDelayedExpansion
title Scissors, Paper, Rock

:: build array of ans[] 
Set "ans[1]=Scissors"&Set "ans[2]=Paper"&Set "ans[3]=Rock"
Set /A "wins=Draw=Loss=0"

:: Get User first name
For /f "tokens=1-3,*" %%A in (
  'net user "%USERNAME%" ^| findstr /i /B "..ll"'
) Do set "FirstName=%%C"

:restart
cls
Echo Hi %FirstName%, let's play.                %Score%

REM Choosing between Scissors, Paper and Rock
SET /A "min=1,max=3,Comp=(%RANDOM% %% max)+min"
Set "ans=!ans[%Comp%]!"

REM Human Making a Choice
echo Choose:
echo [1] = Scissors
echo [2] = Paper
echo [3] = Rock
Choice /CS /C 123e /M "e to exit"
set "User=%ErrorLevel%"
If %User%==4 exit /B
echo You chose !ans[%User%]!. The Computer chooses !ans[%Comp%]!
If %User%==%Comp% Call :Score Draw & Goto :ReStart
goto :Outcome%User%

:Outcome1 Scissors
if %ans%==Paper Call :Score Wins & Goto :ReStart
Call :Score Loss & Goto :ReStart

:Outcome2 Paper
if %ans%==Rock Call :Score Wins & Goto :ReStart
Call :Score Loss & Goto :ReStart

:Outcome3 Rock
if %ans%==Scissors Call :Score Wins & Goto :ReStart
Call :Score Loss & Goto :ReStart

:Score
Echo **%1**
set /A "%1+=1"
Set "Score=Wins:[%Wins%] Draw:[%Draw%] Loss:[%Loss%]"
timeout /t 5 >Nul

Sample output:

Hi LotPings, let's play.                Wins:[4] Draw:[4] Loss:[1]
Choose:
[1] = Scissors
[2] = Paper
[3] = Rock
e to exit [1,2,3,e]?
1

There are several techniques that are common in computer programs in order to write simpler and shorter code. One of the most used is the array, because it allows to use the same code with different values and just use the right value to select the appropriate array element. You may read about array management in Batch files at this answer.

The code below does not manage each item (Scissors, Paper or Rock) in individual way, but manages all of them in the same way and the appropriate item is selected via a numeric index or subscript. The program uses a simple trick in order to accomodate the cyclic nature of the three items. An arithmetic expression converts a 0 (meaning "Paper less than Scissors") to 3 (meaning "Paper greater than Rock") only when the opponent's item is a Rock.

@echo off
setlocal EnableDelayedExpansion
title Scissors, Paper, Rock

rem Define array of items in "natural" (cyclic) order
set "item[0]=Paper"
set "item[1]=Scissors"
set "item[2]=Rock"
set "item[3]=Paper"
set /A "Loss=0, Draw=0, Wins=0"

cls
:loop
echo/

rem Choosing between Scissors, Paper and Rock
set /A "comp=%random% %% 3"

rem Human Making a Choice
choice /N /C PSRe /M "Choose Scissors, Paper or Rock (E to exit): "
if %errorlevel% equ 4 exit /B

set /A "human=%errorlevel%-1, comp+=^!comp*^!(human-2)*3, human+=^!human*^!(comp-2)*3"
echo You chose !item[%human%]!. The Computer chooses !item[%comp%]!.

if %human% gtr %comp% (
   set /A Wins+=1
) else if %human% lss %comp% (
   set /A Loss+=1
) else (
   set /A Draw+=1
)

echo Loss: %Loss%  -  Draw: %Draw%  -  Wins: %Wins%
goto loop

In the code below the three different score results (Loss, Draw and Wins) are not managed as individual items anymore, but calculated in the same way as elements of the same score array. A very simple expression replaces the several if commands used for the score identification into a single value between 0 and 2.

@echo off
setlocal EnableDelayedExpansion
title Scissors, Paper, Rock

rem Define array of items in "natural" (cyclic) order
set "item[0]=Paper"
set "item[1]=Scissors"
set "item[2]=Rock"
set "item[3]=Paper"

rem Define the elements of the "score" results array
set /A "score[0]=0, score[1]=0, score[2]=0"

cls
:loop
echo

rem Choosing between Scissors, Paper and Rock
set /A "comp=%random% %% 3"

rem Human Making a Choice
choice /N /C PSRe /M "Choose Scissors, Paper or Rock (E to exit): "
if %errorlevel% equ 4 exit /B

set /A "human=%errorlevel%-1, comp+=^!comp*^!(human-2)*3, human+=^!human*^!(comp-2)*3, result=human-comp+1"
echo You chose !item[%human%]!. The Computer chooses !item[%comp%]!.

set /A "score[%result%]+=1"
echo Loss: %score[0]%  -  Draw: %score[1]%  -  Wins: %score[2]%
goto loop
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

Re did some code and added remarks.

@echo off
:setting
title Scissors, Paper, Rock

REM Choosing between Scissors, Paper and Rock
:an
:: Changed the way you generate the random number as it makes it much quick. This forced the answer to be 1, 2 or 3.
SET maxvalue=3
SET minvalue=1
SET /A ran=((%RANDOM%)%%(%maxvalue%))+(%minvalue%)
:: As you are comparing numbers, use the EQU, not ==. == is for comparing strings, not numerical values
:: When setting a variable, don't use the % sign, just type their name and call it with the % sign.
if %ran% equ 1 set ans=Scissors
if %ran% equ 2 set ans=Paper
if %ran% equ 3 set ans=Rock
if /i %ran% GTR 3 goto :an
if /i %ran% LSS 1 goto :an

REM Human Making a Choice
:in
echo Choose:
echo [1] = Scissors
echo [2] = Paper
echo [3] = Rock
set /p text=
if %text% equ 1 goto :1
if %text% equ 2 goto :2
if %text% equ 3 goto :3

REM Outcome
:1
echo You chose Scissors. The Computer chooses %ans%
if %ans%==Scissors echo Draw
if %ans%==Paper echo Win
if %ans%==Rock echo Loose
pause
goto :out

REM Outcome
:2
echo You chose Paper. The Computer chooses %ans%
if %ans%==Scissors echo Loose
if %ans%==Paper echo Draw
if %ans%==Rock echo Win
pause
goto :out

REM Outcome
:3
echo You chose Rock. The Computer chooses %ans%
if %ans%==Scissors echo Win
if %ans%==Paper echo Loose
if %ans%==Rock echo Draw
pause
goto :out

REM Play again or not
:out
echo GG Want to play again?
echo [1] = Yes
echo [2] = No
set /p Restart=
if %Restart% equ 1 goto :setting
if %Restart% equ 2 pause
Drew
  • 3,814
  • 2
  • 9
  • 28