title: BATCH - RPS - Rock, Paper, Scissors - Keep failing after a few rounds
body: <p>I have been trying to make a rock, paper, scissors game in batch. So far, the script runs and I can make it play against itself or to play against me (I need to change the B var from /A and random number to /P and have user input).</p>
  
  <p>The thing is that after a few (different amount every time) rounds, the script crash and I do not have time to read the last line of text (which would help me find the error). I have tried with ECHO ON and OFF, I also tried to run 'RPS.bat >> log.txt' from a different DOS window (with the right directory chosen). That did log everything until it crashed, and did not get the last line either.</p>
  
  <p>I have been looking through it several times, but I cant find the error... :(</p>
  
  <p>I made the same game in PHP with currently no errors... (it is a totally different language, I know)</p>
  
  <p>So here is my script:</p>
  
  <pre><code>@ECHO OFF
  :TOP
  SET /A AWON=0
  SET /A BWON=0
  SET /A NWON=0
  
  :GAME
  SET /A A=%RANDOM% %% 3 + 1
  SET /P B=Number from 1 to 3
  
  IF %A% EQU 1 (
      GOTO A1
  ) ELSE IF %A% EQU 2 (
  GOTO A2
  ) ELSE IF %A% EQU 3 (
  GOTO A3
  ) ELSE (
  GOTO GAME
  )
  
  :A1
  IF %B% EQU 1 (
  GOTO DRAW
  ) ELSE IF %B% EQU 2 (
  GOTO BWIN
  ) ELSE IF %B% EQU 3 (
  GOTO AWIN
  ) ELSE (
      GOTO GAME
  )
  
  :A2
  IF %B% EQU 1 (
      GOTO ARAW
  ) ELSE IF %B% EQU 2 (
      GOTO DRAW
  ) ELSE IF %B% EQU 3 (
      GOTO BWIN
  ) ELSE (
      GOTO GAME
  )
  
  :A3
  IF %B% EQU 1 (
      GOTO BWIN
  ) ELSE IF %B% EQU 2 (
      GOTO AWIN
  ) ELSE IF %B% EQU 3 (
      GOTO DRAW
  ) ELSE (
      GOTO GAME
  )
  
  :AWIN
  SET /A AWON=%AWON% + 1
  ECHO -------------------------------------------------------------------------------
  ECHO A won this round!
  ECHO ---
  ECHO A won: %AWON%
  ECHO B won: %BWON%
  ECHO Draw: %NWON%
  PAUSE
  GOTO GAME
  
  :BWIN
  SET /A BWON=%BWON% + 1
  ECHO -------------------------------------------------------------------------------
  ECHO B won this round!
  ECHO ---
  ECHO A won: %AWON%
  ECHO B won: %BWON%
  ECHO Draw: %NWON%
  PAUSE
  GOTO GAME
  
  :DRAW
  SET /A NWON=%NWON% + 1
  ECHO -------------------------------------------------------------------------------
  ECHO This round was a draw!
  ECHO ---
  ECHO A won: %AWON%
  ECHO B won: %BWON%
  ECHO Draw: %NWON%
  PAUSE
  GOTO GAME
  </code></pre>
  
  <p>Thanks to Gray for helping me out with my stupid mistake ^^</p>
  
  <p>Also thanks for the script, but if I am not missing something here, it is not <em>quite</em> correct.</p>
  
  <pre><code>@ECHO OFF
  :GAME
  SET /P A=A: 
  SET /P B=B: 
  SET /A WINNER=(%A%-%B%) %% 3
  ECHO %A% - %B% = %WINNER%
  IF %WINNER% EQU 1 (
      echo A WON! 
  ) ELSE (
  IF %WINNER% EQU 0 (
      echo DRAW!
  ) ELSE (
      echo B WON!
  ))
  PAUSE
  GOTO GAME
  </code></pre>
  
  <p>Run that script (a 'shortened' version of Gray's script). Use the variables like as in my 'table', you can see that by choosing 1 (rock), A is not able to win (A=1 vs B=3 should be: A WON). Do you have any idea on how to fix this? I guess a 'hard-coded?' solution when A=1 and B=3 would be best? (I mean that when those are the values, the 'normal' script does not apply, but a special code gives the result instead).</p>
  
  <p>In my table, if the last sign is '+', then the script gives the correct answer, if the last sign is a '-', then the script gives an incorrect answer.</p>
  
  <pre><code>ITEM     VALUE
  
  ROCK     1
  PAPER    2
  SCISSORS 3
  
  A - B = WINNER
  
  1 - 1 =  0 DRAW  +
  1 - 2 = -1 B WON +
  1 - 3 = -2 A WON -
  2 - 1 =  1 A WON +
  2 - 2 =  0 DRAW  +
  2 - 3 = -1 B WON +
  3 - 1 =  2 B WON +
  3 - 2 =  1 A WON +
  3 - 3 =  0 DRAW  +
  </code></pre>
  
  <p>Also, if you have any tips on how to keep the script more tidy and use less lines code to do the same job, I would gladly listen!</p>
  
  <p>Thanks in advance! - Espen</p>