- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试批量制作石头剪刀布游戏。到目前为止,脚本运行,我可以让它与自己对战或与我对战(我需要将 B var 从/A 和随机数更改为/P 并有用户输入)。
问题是,在几轮(每次不同的数量)之后,脚本崩溃了,我没有时间阅读最后一行文本(这将帮助我找到错误)。我尝试过 ECHO ON 和 OFF,我还尝试从不同的 DOS 窗口运行“RPS.bat >> log.txt”(选择了正确的目录)。这确实记录了所有内容,直到它崩溃,也没有得到最后一行。
我已经翻了好几次了,但我找不到错误... :(
我用 PHP 制作了同样的游戏,目前没有错误......(我知道这是一种完全不同的语言)
所以这是我的脚本:
@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
@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
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 +
最佳答案
您的错误可能是:
The system cannot find the batch label specified - ARAW
GOTO ARAW
在您的
A2
标签,当它看起来像你打算做
GOTO AWIN
@echo off
rem rock:1; paper:2; scissors:3
:GAME
SET /A CPU=%RANDOM% %% 3 + 1
SET /P PLAYER=Number from 1 to 3
cls
echo %CPU% vs %PLAYER%
rem set /A WINNER=(%CPU%-%PLAYER%) %% 3 //this does not work with batch - my mistake
SET /A WINNER=(%CPU% - %PLAYER% + 3) %% 3
IF %WINNER% EQU 1 (
echo CPU WON!
) ELSE (
IF %WINNER% EQU 0 (
echo DRAW!
) ELSE (
echo YOU WON!
))
GOTO GAME
@echo off
rem rock:1; paper:2; scissors:3
:GAME
SET /A CPU=%RANDOM% %% 3 + 1
SET /P PLAYER=Number from 1 to 3
goto CPUTYPE
:TYPE1
goto PLAYERTYPE
:TYPE2
cls
echo CPU:%CPUT% vs YOU:%PLAYERT%
rem set /A WINNER=(%CPU%-%PLAYER%) %% 3 //this does not work with batch - my mistake
SET /A WINNER=(%CPU% - %PLAYER% + 3) %% 3
IF %WINNER% EQU 1 (
echo CPU WON!
) ELSE (
IF %WINNER% EQU 0 (
echo DRAW!
) ELSE (
echo YOU WON!
))
GOTO GAME
:CPUTYPE
IF %CPU% EQU 1 (
SET CPUT="ROCK"
) ELSE (
IF %CPU% EQU 2 (
SET CPUT="PAPER"
) ELSE (
SET CPUT="SCISSORS"
))
GOTO TYPE1
:PLAYERTYPE
IF %PLAYER% EQU 1 (
SET PLAYERT="ROCK"
) ELSE (
IF %PLAYER% EQU 2 (
SET PLAYERT="PAPER"
) ELSE (
SET PLAYERT="SCISSORS"
))
GOTO TYPE2
@ECHO OFF
:GAME
SET /P A=A:
SET /P B=B:
SET /A WINNER=(%A% - %B% + 3) %% 3
ECHO W= %WINNER%
IF %WINNER% EQU 1 (
echo A WON!
) ELSE (
IF %WINNER% EQU 0 (
echo DRAW!
) ELSE (
echo B WON!
))
PAUSE
GOTO GAME
SET /A WINNER=(%CPU% - %PLAYER% + 3) %% 3
的解释:
%
或模运算符来做到这一点。如果您使用整数除法除以该数字,则模运算符基本上会为您提供余数。
1v1 -> (1 - 1 + 3) % 3 -> 3 % 3 -> 1 rem 0
2v2 -> (2 - 2 + 3) % 3 -> 3 % 3 -> 1 rem 0
3v3 -> (3 - 3 + 3) % 3 -> 3 % 3 -> 1 rem 0
1v3 -> (1 - 3 + 3) % 3 -> 1 % 3 -> 0 rem 1
2v1 -> (2 - 1 + 3) % 3 -> 4 % 3 -> 1 rem 1
3v2 -> (3 - 2 + 3) % 3 -> 4 % 3 -> 1 rem 1
1v2 -> (1 - 2 + 3) % 3 -> 2 % 3 -> 0 rem 2
2v3 -> (2 - 3 + 3) % 3 -> 2 % 3 -> 0 rem 2
3v1 -> (3 - 1 + 3) % 3 -> 5 % 3 -> 1 rem 2
关于if-statement - BATCH - RPS - Rock,Paper,Scissors - 几轮后继续失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17621743/
我是一名优秀的程序员,十分优秀!