Reputation: 33
choice /c 123456789 >NUL
if errorlevel = 9 if %ui9%==9 set "ui9=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 8 if %ui8%==8 set "ui8=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 7 if %ui7%==7 set "ui7=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 6 if %ui6%==6 set "ui6=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 5 if %ui5%==5 set "ui5=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 4 if %ui4%==4 set "ui4=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 3 if %ui3%==3 set "ui3=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 2 if %ui2%==2 set "ui2=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 1 if %ui1%==1 set "ui1=X" & set /a moves = %moves% + 1 & goto :medmodulebot
cls
echo That spot's already taken
ping localhost -n 3 >NUL
goto medmoduleuser
I'm making a tic-tac-toe bot. When entering a digit such as 9, then entering it again in the next turn, it will trigger the if errorlevel = 8 if %ui8%==8 set "ui8=X" & set /a moves = %moves% + 1 & goto :medmodulebot
instead of just going through the code until it hits the bottom... It seems that choice
is returning errorlevels oddly? What do I do to prevent this?
Upvotes: 0
Views: 44
Reputation: 2951
In addition to the comments, errorlevel testing could be avoided completely. By converting the choice value into the literal value with a for / F loop metavariable
@echo off & Setlocal ENABLEdelayedexpansion
Rem // script body
For /F "Delims=" %%C in ('Choice /N /C:123456789') do (
if !ui%%C!==%%C (
set "ui%%C=X"
set /a moves+=1
goto :medmodulebot
)
)
cls
echo That spot's already taken
ping localhost -n 3 >NUL
goto medmoduleuser
Upvotes: 1