Need help with an structure and compare inputs
Posted: 04 Jan 2023 10:18
Supp guys?
I was trying to create an basic tic tac toe in batch, but I'm stuck at the point when user do their moves. When they input the reference number position that thei wanna do, I got an invalid input, can you gays take a look at the code?
========================================
@echo off
set "board=123456789"
CLS
ECHO.
ECHO.
echo Welcome to Tic Tac Toe!
echo.
echo 1 ^| 2 ^| 3
echo ---------
echo 4 ^| 5 ^| 6
echo ---------
echo 7 ^| 8 ^| 9
echo.
GOTO setup
:setup
set /p "player1=Enter player 1's name: "
set /p "player2=Enter player 2's name: "
set "currentplayer=%player1%"
set "turn=X"
GOTO input
:main
GOTO input
:input
echo %currentplayer%'s turn.
set /p "choice=Enter a number to place your %turn% on the board: "
if not defined board%choice:~-1% goto invalid
set "board=%board:%%choice%=%turn%"
GOTO checkwin
:checkwin
if "%board:1=%"=="%board%" goto draw
if "%board:2=%"=="%board%" goto draw
if "%board:3=%"=="%board%" goto draw
if "%board:4=%"=="%board%" goto draw
if "%board:5=%"=="%board%" goto draw
if "%board:6=%"=="%board%" goto draw
if "%board:7=%"=="%board%" goto draw
if "%board:8=%"=="%board%" goto draw
if "%board:9=%"=="%board%" goto draw
if "%board:123=%"=="%board%" goto xwin
if "%board:456=%"=="%board%" goto xwin
if "%board:789=%"=="%board%" goto xwin
if "%board:159=%"=="%board%" goto xwin
if "%board:753=%"=="%board%" goto xwin
if "%board:147=%"=="%board%" goto owin
if "%board:258=%"=="%board%" goto owin
if "%board:369=%"=="%board%" goto owin
if "%board:159=%"=="%board%" goto owin
if "%board:753=%"=="%board%" goto owin
goto switch
:switch
if "%turn%"=="X" (
set "turn=O"
set "currentplayer=%player2%"
)
if "%turn%"=="O" (
set "turn=X"
set "currentplayer=%player1%"
)
goto main
:invalid
echo Invalid choice. Try again.
goto input
:draw
echo It's a draw!
goto end
win
echo %player1% wins!
goto end
win
echo %player2% wins!
goto end
:end
echo.
echo Play again? (y/n)
set /p "choice=Enter y to play again or n to exit: "
if "%choice%"=="y" goto start
if "%choice%"=="n" exit
goto end
EXIT
===============================================
Would apreciate any help,
Thx.
I was trying to create an basic tic tac toe in batch, but I'm stuck at the point when user do their moves. When they input the reference number position that thei wanna do, I got an invalid input, can you gays take a look at the code?
========================================
@echo off
set "board=123456789"
CLS
ECHO.
ECHO.
echo Welcome to Tic Tac Toe!
echo.
echo 1 ^| 2 ^| 3
echo ---------
echo 4 ^| 5 ^| 6
echo ---------
echo 7 ^| 8 ^| 9
echo.
GOTO setup
:setup
set /p "player1=Enter player 1's name: "
set /p "player2=Enter player 2's name: "
set "currentplayer=%player1%"
set "turn=X"
GOTO input
:main
GOTO input
:input
echo %currentplayer%'s turn.
set /p "choice=Enter a number to place your %turn% on the board: "
if not defined board%choice:~-1% goto invalid
set "board=%board:%%choice%=%turn%"
GOTO checkwin
:checkwin
if "%board:1=%"=="%board%" goto draw
if "%board:2=%"=="%board%" goto draw
if "%board:3=%"=="%board%" goto draw
if "%board:4=%"=="%board%" goto draw
if "%board:5=%"=="%board%" goto draw
if "%board:6=%"=="%board%" goto draw
if "%board:7=%"=="%board%" goto draw
if "%board:8=%"=="%board%" goto draw
if "%board:9=%"=="%board%" goto draw
if "%board:123=%"=="%board%" goto xwin
if "%board:456=%"=="%board%" goto xwin
if "%board:789=%"=="%board%" goto xwin
if "%board:159=%"=="%board%" goto xwin
if "%board:753=%"=="%board%" goto xwin
if "%board:147=%"=="%board%" goto owin
if "%board:258=%"=="%board%" goto owin
if "%board:369=%"=="%board%" goto owin
if "%board:159=%"=="%board%" goto owin
if "%board:753=%"=="%board%" goto owin
goto switch
:switch
if "%turn%"=="X" (
set "turn=O"
set "currentplayer=%player2%"
)
if "%turn%"=="O" (
set "turn=X"
set "currentplayer=%player1%"
)
goto main
:invalid
echo Invalid choice. Try again.
goto input
:draw
echo It's a draw!
goto end
win
echo %player1% wins!
goto end
win
echo %player2% wins!
goto end
:end
echo.
echo Play again? (y/n)
set /p "choice=Enter y to play again or n to exit: "
if "%choice%"=="y" goto start
if "%choice%"=="n" exit
goto end
EXIT
===============================================
Would apreciate any help,
Thx.