以下は、私が所属しているクラスの三目並べゲームのコードを含む .bat ファイルです。何を変更しても、常に「無効な手です。もう一度やり直してください!」というメッセージが表示されます。ボックスの場所 (例: A1) を入力するとき。検証は、273 行目から始まる :ValidateResponse で行われます。私が何を間違ってこの応答を引き起こしたのか、誰にもわかりますか? ありがとう。
@ECHO off
REM ***************************************************************************
REM
REM Script Name: TicTacToe.bat
REM Author:
REM Date:
REM
REM Description: This is a Windows shell script implementation of the popular
REM child's game called "Tic-Tac-Toe".
REM
REM ***************************************************************************
REM ****** Script Initialization Section ******
REM Set the color scheme to yellow on black
COLOR 0E
REM Display the name of the game in the Windows command console's title bar
TITLE = T I C - T A C - T O E
REM Clear the display
CLS
REM ****** Main Processing Section ******
REM This label is called whenever the game needs to be restarted
:StartOver
REM Global variables used throughout the script
SET Player=X
SET Winner=None
SET /A NoMoves = 0
SET /A NoMoves = 0
REM Reset all the squares on the game board to show blanks
CALL :InitializeBlanks
REM Display the Welcome screen and prompt the players for instructions
CALL :Welcome
REM Process the player's instruction
IF /I "%reply%" == "" CLS & GOTO :StartOver
IF /I %reply% == Play CLS & CALL :Play
IF /I %reply% == Quit CLS & GOTO :EOF
IF /I %reply% == Help CLS & CALL :Help
IF /I %reply% == About CLS & CALL :About
GOTO :StartOver
GOTO :EOF
REM ****** Main Processing Section ******
REM Reset all squares on the game board to blanks
:InitializeBlanks
SET A1=
SET A2=
SET A3=
SET B1=
SET B2=
SET B3=
SET C1=
SET C2=
SET C3=
GOTO :EOF
REM Display the Welcome screen when called
:Welcome
REM Clear the display
CLS
REM Add 8 blanks lines to the display
FOR /L %%i IN (1,1,8) DO ECHO.
ECHO W E L C O M E T O T I C - T A C - T O E
ECHO.
ECHO.
ECHO Windows shell script style!
REM Add 9 blanks lines to the display
FOR /L %%i IN (1,1,9) DO ECHO.
REM Display a menu of options
ECHO Options: [Play] [Quit] [Help] [About]
ECHO.
REM Prompt the player to make a selection
SET /p reply=Enter selection:
GOTO :EOF
REM Display the game board
:DisplayBoard
REM Clear the display
CLS
ECHO.
ECHO.
ECHO T I C - T A C - T O E
ECHO.
ECHO.
ECHO.
ECHO 1 2 3
ECHO. Rules:
ECHO.
ECHO ^| ^| 1. Player X always goes first.
ECHO A %A1% ^| %A2% ^| %A3%
ECHO _____^|_____^|_____ 2. To make a move enter the
ECHO ^| ^| coordinates of the appropriate
ECHO B %B1% ^| %B2% ^| %B3% square.
ECHO _____^|_____^|_____
ECHO ^| ^| 3. Remember to switch turns when
ECHO C %C1% ^| %C2% ^| %C3% instructed by the game.
ECHO ^| ^|
ECHO.
ECHO.
ECHO.
ECHO Player %player%'s turn:
ECHO.
GOTO :EOF
REM Display the help screen when called
:HELP
REM Clear the display
CLS
REM Add 5 blank lines to the display
FOR /L %%i IN (1,1,5) DO ECHO.
ECHO HELP INSTRUCTIONS
ECHO.
ECHO.
ECHO This is a text-based implementation of the TIC-TAC-TOE game. In this game
ECHO the computer controls the action. Player X always goes first. The game
ECHO tells each player when it is his turn. When prompted to take a turn players
ECHO must type the coordinates of the square into which they wish to place their
ECHO marker (the X or O character). For example, to place a marker in the
ECHO upper left hand box, players would enter A1.
ECHO.
ECHO The game tracks the progress of each game and will automatically determine
ECHO when a game has been won, lost, or tied.
REM Add 6 blank lines to the display
FOR /L %%i IN (1,1,6) DO ECHO.
REM Make the player press a key to continue
PAUSE
GOTO :EOF
:About
REM Clear the display
CLS
REM Add 7 blank lines to the display
FOR /L %%i IN (1,1,7) DO ECHO.
ECHO About The TIC TAC TOE GAME
ECHO.
ECHO Written by
ECHO.
ECHO Jerry Lee Ford, Jr.
ECHO.
ECHO ------------------------
ECHO.
ECHO Copyright 2003
REM Add 7 blank lines to the display
FOR /L %%i IN (1,1,7) DO ECHO.
REM Make the player press a key to continue
PAUSE
GOTO :EOF
REM This procedure controls the actual play of the game
:Play
REM If player X has won then find out if a new game should be started
IF "%Winner%"=="X" (
CALL :DisplayGameResults
REM Make the player press a key to continue
PAUSE
GOTO :StartOver
)
REM If player O has won then find out if a new game should be started
IF "%Winner%"=="O" (
CALL :DisplayGameResults
REM Make the player press a key to continue
PAUSE
GOTO :StartOver
)
REM If the players tied find out if a new game should be started
IF "%Winner%"=="Nobody" (
CALL :DisplayGameResults
REM Make the player press a key to continue
PAUSE
GOTO :StartOver
)
REM display the game board
CALL :DisplayBoard
REM Collect current player's selection
SET /P response=Select a box:
REM Validate the specified selection
CALL :ValidateResponse
REM If the selection is valid
IF %ValidMove%==True (
REM Add 1 to the total number of valid selections made in the game
SET /A NoMoves = NoMoves += 1
REM Associate the player's selection with the right square
CALL :FillInSquare
REM If the player's selection is invalid
) ELSE (
REM Clear the display
CLS
REM Add 11 blank lines to the display
FOR /L %%i IN (1,1,11) DO ECHO.
ECHO Invalid move. Please try again!
REM Add 11 blank lines to the display
FOR /L %%i IN (1,1,11) DO ECHO.
REM Make the player press a key to continue
PAUSE
)
REM If a total of 9 valid selections have been made the board is full
IF %NoMoves% == 9 (
SET Winner=Nobody
) ELSE (
CALL :SeeIfWon
)
REM Its now time to switch players
IF %ValidMove%==True (
IF "%player%"=="X" (
SET Player=O
) ELSE (
SET Player=X
)
)
REM Loop back to the beginning and keep playing
GOTO :Play
GOTO :EOF
REM Ensure that the selection supplied by the player is valid
:ValidateResponse
REM By default assume a valid selection was made
SET ValidMove=True
REM Hitting enter without entering a selection is invalid
IF /I %response% == "" (
SET ValidMove=False
GOTO :EOF
)
REM Ensure that a valid square was specified (A1-A3, B1-B3 & C1 - C3)
IF /I NOT %response%==A1 (
IF /I NOT %response%==A2 (
IF /I NOT %response%==A3 (
IF /I NOT %response%==B1 (
IF /I NOT %response%==B2 (
IF /I NOT %response%==B3 (
IF /I NOT %response%==C1 (
IF /I NOT %response%==C2 (
IF /I NOT %response%==C3 (
SET ValidMove=False
GOTO :EOF
)
)
)
)
)
)
)
)
)
REM Previously selected squares are invalid
IF /I %response%==A1 (
IF NOT "%A1%"==" " (
SET ValidMove=False
)
)
IF /I %response%==A2 (
IF NOT "%A2%"==" " (
SET ValidMove=False
)
)
IF /I %response%==A3 (
IF NOT "%A3%"==" " (
SET ValidMove=False
)
)
IF /I %response%==B1 (
IF NOT "%B1%"==" " (
SET ValidMove=False
)
)
IF /I %response%==B2 (
IF NOT "%B2%"==" " (
SET ValidMove=False
)
)
IF /I %response%==B3 (
IF NOT "%B3%"==" " (
SET ValidMove=False
)
)
IF /I %response%==C1 (
IF NOT "%C1%"==" " (
SET ValidMove=False
)
)
IF /I %response%==C2 (
IF NOT "%C2%"==" " (
SET ValidMove=False
)
)
IF /I %response%==C3 (
IF NOT "%C3%"==" " (
SET ValidMove=False
)
)
GOTO :EOF
REM Associate the player's selection with the appropriate square
:FillInSquare
IF /I %response%==A1 SET A1=%player%
IF /I %response%==A2 SET A2=%player%
IF /I %response%==A3 SET A3=%player%
IF /I %response%==B1 SET B1=%player%
IF /I %response%==B2 SET B2=%player%
IF /I %response%==B3 SET B3=%player%
IF /I %response%==C1 SET C1=%player%
IF /I %response%==C2 SET C2=%player%
IF /I %response%==C3 SET C3=%player%
Goto :EOF
REM Display the results of the game
:DisplayGameResults
REM Clear the display
CLS
REM Set the default message to indicate a tie
SET messagetext=Tie - No Winner
REM If either player won set a variable containing a custom message
IF "%Winner%"=="X" SET messagetext=Player X has won!!!
IF "%Winner%"=="O" SET messagetext=Player O has won!!!
REM Add 5 blank lines to the display
FOR /L %%i IN (1,1,5) DO ECHO.
REM Display the final board and display a message indicating game results
ECHO ^| ^|
ECHO %A1% ^| %A2% ^| %A3% E N D O F G A M E
ECHO _____^|_____^|_____
ECHO ^| ^|
ECHO %B1% ^| %B2% ^| %B3% %messagetext%
ECHO _____^|_____^|_____
ECHO ^| ^|
ECHO %C1% ^| %C2% ^| %C3%
ECHO ^| ^|
REM Add 9 blank lines to the display
FOR /L %%i IN (1,1,9) DO ECHO.
GOTO :EOF
REM Check up, down, & diagonally to see if the player has won
:SeeIfWon
REM Check across
IF /I "%A1%"=="%player%" (
IF /I "%A2%"=="%player%" (
IF /I "%A3%"=="%player%" (SET Winner=%player%)
)
)
IF /I "%B1%"=="%player%" (
IF /I "%B2%"=="%player%" (
IF /I "%B3%"=="%player%" (SET Winner=%player%)
)
)
IF /I "%C1%"=="%player%" (
IF /I "%C2%"=="%player%" (
IF /I "%C3%"=="%player%" (SET Winner=%player%)
)
)
REM Check diagonally
IF /I "%A1%"=="%player%" (
IF /I "%B2%"=="%player%" (
IF /I "%C3%"=="%player%" (SET Winner=%player%)
)
)
IF /I "%A3%"=="%player%" (
IF /I "%B2%"=="%player%" (
IF /I "%C1%"=="%player%" (SET Winner=%player%)
)
)
REM Check up and down
IF /I "%A1%"=="%player%" (
IF /I "%B1%"=="%player%" (
IF /I "%C1%"=="%player%" (SET Winner=%player%)
)
)
IF /I "%A2%"=="%player%" (
IF /I "%B2%"=="%player%" (
IF /I "%C2%"=="%player%" (SET Winner=%player%)
)
)
IF /I "%A3%"=="%player%" (
IF /I "%B3%"=="%player%" (
IF /I "%C3%"=="%player%" (SET Winner=%player%)
)
)
GOTO :EOF