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.
Need help with an structure and compare inputs
Moderator: DosItHelp
Re: Need help with an structure and compare inputs
Any help with this part of the code?
: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
Just need it to identify position from the player input, and it have to be storing it to compare with win or draw chances
: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
Just need it to identify position from the player input, and it have to be storing it to compare with win or draw chances
Re: Need help with an structure and compare inputs
To perform substring modification using variables as the search replace strings, you need to have delayed expansion enabled and use expansion like so:
Ther is however are much more preferable way to take input for single character choices, which is the Choice command.
Here's a remarked implementation of tic tac toe for you to study.
Code: Select all
set "board=!board:%choice%=%turn%!"
Here's a remarked implementation of tic tac toe for you to study.
Code: Select all
@Echo off
Rem possible moves.
For /L %%i in (1 1 9)Do Set "c%%i=%%i"
REM define escape character for use of VT sequences. https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
for /F %%a in ('Echo(prompt $E^| cmd')Do Set \E=%%a
REM define grid display macro. DE not enabled yet, delayed variables will expand with 'runtime' value once DE is enabled.
Set "Grid=%\E%[1;1H !c7! ^| !c8! ^| !c9! %\E%[1E-----------%\E%[1E !c4! ^| !c5! ^| !c6! %\E%[1E-----------%\E%[1E !c1! ^| !c2! ^| !c3!%\E%[0J%\E%[1E"
REM define test string of possible win cell combinations. Uses same cell Array indices as Grid.
Set "Check=!c1!!c2!!c3!,!c4!!c5!!c6!,!c7!!c8!!c9!,!c1!!c4!!c7!,!c2!!c5!!c8!,!c3!!c6!!c9!,!c1!!c5!!c9!,!c3!!c5!!c7!"
REM define character Array for Players
Set "p1=X"
Set "p2=O"
Setlocal EnableDelayedExpansion
Echo(%GRID%
REM randomly assign Starting player
Set /A "Starter=!Random! %%2 + 1","Player=Starter-1"
REM define list of available moves for use with choice command
Set "Options=123456789"
For /L %%i in (1 1 9)Do (
REM use modulo to alternate between p1 and p2
Set /A player=player %%2 + 1"
REM expand current player #
For %%v in (!player!)Do (
REM notify game turn info
Title Player %%v: !p%%v!'s move. Choose tile:!Options!
REM Capture input of Choice in For variable
For /F "delims=" %%G in ('Choice /N /C:!Options!')Do (
REM remove input - cell position - from Options list
Set "Options=!Options:%%G=!"
REM Assign Player character to cell index for display via Grid
Set "c%%G=!p%%v!"
)
Echo(%GRID%
REM utilize findstr to test expanded check list for winning combination by current player
Echo("%Check%"|%SystemRoot%\System32\findstr.exe /li "!p%%v!!p%%v!!p%%v!" > nul && (
REM notify Win and quit
Echo(Player: !Player! won.
Pause
Endlocal & Goto:Eof
) ) )
REM Game has Drawn; Quit
Echo(Game Drawn
Pause
Endlocal & Goto:Eof
Last edited by T3RRY on 16 Feb 2023 01:56, edited 1 time in total.
Re: Need help with an structure and compare inputs
Thx a lot bro