Batch - Something Fails With: . was unexpected at this time [closed] - windows

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to make a text-based game using Batch, and this part used to work but it suddenly doesn't. As soon as I press enter after typing a name, the program terminates suddenly and displays the error: . was unexpected at this time
I'm pretty sure this shouldn't even happen, and I know the error has to be somewhere in the following code:
`
:CFGNAME
cls & title N.A.M.E & color 0d
echo --------------------------------------
echo State your identity.
echo --------------------------------------
echo.
echo You need to specify things like your
echo name and gender.
echo.
echo.
echo.
echo Gender: %gender%
echo.
echo Enter a name. It may be up to sixteen
echo characters long, and can contain any
echo letter, and only letters. Most other
echo glyphs will crash the game.
echo.
echo.
echo -Enter a
set /p tmpName=- name:
echo %tmpName%> "data\temp\~playername.tmp"
for %%? in ("data\temp\~playername.tmp") do ( set /A nameLength=%%~z? - 2 )
del "data\temp\~playername.tmp"
set ucdName=%tmpName%
set ucdName=%ucdName:A= %
set ucdName=%ucdName:a= %
set ucdName=%ucdName:B= %
set ucdName=%ucdName:b= %
set ucdName=%ucdName:C= %
set ucdName=%ucdName:c= %
set ucdName=%ucdName:D= %
set ucdName=%ucdName:d= %
set ucdName=%ucdName:E= %
set ucdName=%ucdName:e= %
set ucdName=%ucdName:F= %
set ucdName=%ucdName:f= %
set ucdName=%ucdName:G= %
set ucdName=%ucdName:g= %
set ucdName=%ucdName:H= %
set ucdName=%ucdName:h= %
set ucdName=%ucdName:I= %
set ucdName=%ucdName:i= %
set ucdName=%ucdName:J= %
set ucdName=%ucdName:j= %
set ucdName=%ucdName:K= %
set ucdName=%ucdName:k= %
set ucdName=%ucdName:L= %
set ucdName=%ucdName:l= %
set ucdName=%ucdName:M= %
set ucdName=%ucdName:m= %
set ucdName=%ucdName:N= %
set ucdName=%ucdName:n= %
set ucdName=%ucdName:O= %
set ucdName=%ucdName:o= %
set ucdName=%ucdName:P= %
set ucdName=%ucdName:p= %
set ucdName=%ucdName:Q= %
set ucdName=%ucdName:q= %
set ucdName=%ucdName:R= %
set ucdName=%ucdName:r= %
set ucdName=%ucdName:S= %
set ucdName=%ucdName:s= %
set ucdName=%ucdName:T= %
set ucdName=%ucdName:t= %
set ucdName=%ucdName:U= %
set ucdName=%ucdName:u= %
set ucdName=%ucdName:V= %
set ucdName=%ucdName:v= %
set ucdName=%ucdName:W= %
set ucdName=%ucdName:w= %
set ucdName=%ucdName:X= %
set ucdName=%ucdName:x= %
set ucdName=%ucdName:Y= %
set ucdName=%ucdName:y= %
set ucdName=%ucdName:Z= %
set ucdName=%ucdName:z= %
set "ucdName=!ucdName: =!"
echo %ucdName%x> "data\temp\~checknamecontents.tmp"
for %%? in ("data\temp\~checknamecontents.tmp") do ( set /A invalidChars=%%~z? - 2 )
set /a invalidChars-=1
del "data\temp\~checknamecontents.tmp"
if %invalidChars% gtr 0 (
cls & title I.N.V.A.L.I.D & color 0c
echo.
echo Your name
echo must only contain letters!
echo.
echo The name you entered cannot be
echo used because if your name was
echo not fully alphabetical, pass-
echo words generated using that name
echo would be improperly generated.
echo It has %invalidChars%
echo invalid character(s).
echo.
echo Press any key to retry...
echo.
pause > nul
goto CFGNAME
)
:CHKNAMELENGTH
if %nameLength% gtr 16 (
cls & title I.N.V.A.L.I.D & color 0c
echo.
echo Your name must be up to 16
echo characters in length!
echo.
echo The name you entered cannot be
echo used because %nameLength% is
echo more than 16.
echo.
echo.
echo.
echo.
echo.
echo Press any key to retry...
echo.
pause > nul
goto CFGNAME
) else (
if %nameLength% lss 3 (
cls & title I.N.V.A.L.I.D & color 0c
echo.
echo Your name must be at least 3
echo characters in length!
echo.
echo The name you entered cannot be
echo used because %nameLength% is
echo less than 3.
echo.
echo.
echo.
echo.
echo.
echo Press any key to retry...
echo.
pause > nul
goto CFGNAME
) else (
set name=%tmpName%
goto CFGID
)
)
:CFGID
set IDEasterEgg=*
if /i "%tmpName%"=="Undyne" (
set IDEasterEgg=* Ngaaah!
)
if /i "%tmpName%"=="Frisk" (
set IDEasterEgg=* -_-
)
if /i "%tmpName%"=="Mario" (
set IDEasterEgg=* Lets-a-go!
)
if /i "%tmpName%"=="Waluigi" (
set IDEasterEgg=Waaa!
)
if /i "%tmpName%"=="Link" (
set IDEasterEgg=* Nice job, "princess."
)
if /i "%tmpName%"=="Navi" (
set IDEasterEgg=* Hey! Listen!
)
if /i "%tmpName%"=="Yoda" (
set IDEasterEgg=* Backwards I talk. Hm.
)
if /i "%tmpName%"=="Darth Vader" (
set IDEasterEgg=* No. I am your father.
)
if /i "%tmpName%"=="Mabbel" (
set IDEasterEgg=* GRAPPLING HOOK!!!
)
if /i "%tmpName%"=="Stanley" (
set IDEasterEgg=* Illegal pyrotechnics, anyone?
)
if /i "%tmpName%"=="Steve" (
set IDEasterEgg=* It's. A cow.
)
if /i "%tmpName%"=="Notch" (
set IDEasterEgg=* <3
)
if /i "%tmpName%"=="Caillou" (
set IDEasterEgg=* But Mommy!
)
if /i "%tmpName%"=="Rosie" (
set IDEasterEgg=* STUPID! STUPID! :D
)
if /i "%tmpName%"=="Testificate Man" (
set IDEasterEgg=* How inconsiderate!!!
)
if /i "%tmpName%"=="Dr Inconsiderate" (
set IDEasterEgg=* Slightly less than average!
)
if /i "%tmpName%"=="Shaggy" (
set IDEasterEgg=* Zoinks!
)
if /i "%tmpName%"=="Scooby-Doo" (
set IDEasterEgg=* Rello, rorld!!
)
if /i "%tmpName%"=="Santa Claus" (
set IDEasterEgg=* Ho ho ho!
)
if /i "%tmpName%"=="Easter Bunny" (
set IDEasterEgg=* You've found an Easter egg.
)
if /i "%tmpName%"=="Ed" (
set IDEasterEgg=* Head: Looks like flat pear.
)
if /i "%tmpName%"=="Edd" (
set IDEasterEgg=* Headwear: Very mysterious...
)
if /i "%tmpName%"=="Eddy" (
set IDEasterEgg=* Smile: Freakishly huge.
)
if /i "%tmpName%"=="Pacman" or "%tmpName%"=="Pac Man" (
set IDEasterEgg=* Wafers: A LOT
)
if /i "%tmpName%"=="Ms Pacman" or "%tmpName%"=="Ms Pac Man" (
set IDEasterEgg=* Children: Jr. Pac-Man
)
if /i "%tmpName%"=="Blinky" (
set IDEasterEgg=* Personality: Leader
)
if /i "%tmpName%"=="Clyde" (
set IDEasterEgg=* Personality: Teasing
)
if /i "%tmpName%"=="Inky" (
set IDEasterEgg=* Personality: Shy
)
if /i "%tmpName%"=="Pinky" (
set IDEasterEgg=* Personality: Energetic
)
if /i "%tmpName%"=="MatPat" (
set IDEasterEgg=* But that's just a theory.
)
:CFGID
cls & title Y.O.U S.U.R.E? & color 02
echo --------------------------------------
echo Is this ID okay with you?
echo ----------------------------------
echo This will be your character for the
echo rest of the game. Are you sure you
echo have everything exactly how you
echo want it? If not, you can go back
echo and edit your info all you like.
echo Anything but Y will be interpreted
echo as a no!
echo --------------------------------------
echo.
echo * Name: %name%
echo.
echo * Gender: %gender%
echo.
echo %IDEasterEgg%
echo.
echo __ __ __ __ __ __ ^|
echo _/ \__/ \__/ \__/ \__/ \__/ \__/
echo.
set /p confirmCharacter=(Y, N):
`
If anybody can figure out what's causing the problem, I'd really appreciate it.

The line echo invalid character(s). is the problem. The ) closes your code block, resulting in . to be interpreted as a command. Escape it with a caret:
echo invalid character(s^).

Related

How to require user to activate my program?

I was wondering...
I have a program, and I want to charge money for it.
it runs on Windows, and is written mostly in VB and Batch-files...
how can I force the user to buy a product key for it, and activate it to use the Paid Version?
Thanks in advance!
~ #Cascading-style
This just a little example showing you that you can limit the number of execution of your program , so if the maximum of number of execution is reached the program will Auto delete by him self
#echo off
Setlocal enabledelayedexpansion
Title Count the number of times my BATCH file is run
Mode Con Cols=70 lines=7 & color 0E
Set /a MaxExecution=3
set /a count=1
set "FileCount=%tmp%\%~n0.dll"
If Not exist "%FileCount%" (
echo !count! > "%FileCount%"
) else (
For /F "delims= " %%a in ('Type "%FileCount%"') Do (
set /a count=!count! + %%a
echo !count! > "%FileCount%"
)
)
echo.
echo This Program is running for "!count!" time(s)
Call :SelfDelete
pause>nul & Exit /b
::**************************************************************
:SelfDelete
echo.
If !count! GTR !MaxExecution! (
Color 0C
echo The maximum execution of this "%~nx0" is set to "!MaxExecution!"
echo and it is reached & Timeout /T 5 /Nobreak>nul & Del %~f0
) else (
echo The counting is "!count!" and the max is set to "!MaxExecution!"
)
Goto :EOF
::**************************************************************

How to create a checklist with a bat file/batch script in the console window?

I'm looking to create a checklist in a console window to help users choose certain options they would like to install. This kind of output:
Please select options to install:
[x]Option 1
[ ]Option 2
>[x]Option 3
[x]Option 4
Where the user can move the cursor throughout the list and select the options.
I have a very, very vague idea of how I might do this code, testing with just two options. But if there is anyone who already has a solidified idea of how this would work and could share I would be very appreciative!
Test code for those who want to see it:
#echo off
call:main
end /b 0
:main
set /p choice= "Would you like to enable option one? (yes/no): "echo(
if %choice%==yes (
set option1=1
)
if %choice%==no (
set option1=0
)
call:mod1
set /p choice= "Would you like to enable option two? (yes/no): "echo(
if %choice%==yes (
set option2=1
)
if %choice%==no (
set option2=0
)
call:mod2
:mod1
if %option1%==1 (
echo [x] Option 1
)
if %option1%==0 (
echo [ ] Option 1
)
:mod2
if %option2%==1 (
echo [x] Option 1
)
if %option2%==0 (
echo [ ] Option 1
)
You could try this:
#echo off
setlocal EnableDelayedExpansion
for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A
set "getKeyMacro=powershell -noprofile "^
while (-not (37..40+13).contains($x)) {^
$x = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown').VirtualKeyCode^
}^
if ($x -eq 13) {^
'enter'^
}^
('left','up','right','down')[$x - 37]^
""
set "option1=0"
set "option2=0"
set "option3=0"
set "option4=0"
set "selected=1"
:select
cls
echo use ^<right^> arrow to continue, ^<up^> and ^<down^> to select, and ^<enter^> to toggle
FOR /L %%G IN (1,1,4) DO (
set "display=[ ]"
if !option%%G! equ 1 set "display=[x]"
if %%G equ !selected! set "display=^>!display!
echo !display! Option %%G
)
FOR /F "delims==" %%G IN ('%getKeyMacro%') DO set "key=%%G"
if "%key%"=="up" set /a "selected-=1"
if "%key%"=="down" set /a "selected+=1"
if %selected% lss 1 set "selected=1"
if %selected% gtr 4 set "selected=4"
if "%key%"=="enter" goto toggle
if "%key%"=="right" goto OK
goto select
:toggle
set /a "option%selected%+=1"
set /a "option%selected%=!option%selected%!%%2"
goto select
:OK
FOR /L %%G IN (1,1,4) DO (
if !option%%G! equ 1 (
echo %%G selected
)
)
pause
Note that this is very heavily dependent on Delayed Expansion, so you might want to read up on it here.
Second note: this needs powershell so you can use up and down arrow keys to select options, enter to toggle currently selected option, and right arrow to continue.
EDIT
Updated version, this allows you to set the display names for the options, but you need to specify the amount of options aswell:
#echo off
setlocal EnableDelayedExpansion
for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A
set "getKeyMacro=powershell -noprofile "^
while (-not (37..40+13).contains($x)) {^
$x = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown').VirtualKeyCode^
}^
if ($x -eq 13) {^
'enter'^
}^
('left','up','right','down')[$x - 37]^
""
set "option1=0"
set "option2=0"
set "option3=0"
set "option4=0"
set "option1name=Install thing 1"
set "option2name=Do thing 2"
set "option3name=Execute thing 3"
set "option4name=Run thing 4"
set "maxOptions=4"
set "selected=1"
:select
cls
echo use ^<right^> arrow to continue, ^<up^> and ^<down^> to select, and ^<enter^> to toggle
FOR /L %%G IN (1,1,%maxOptions%) DO (
set "display=[ ]"
if !option%%G! equ 1 set "display=[x]"
if %%G equ !selected! set "display=^>!display!
echo !display! !option%%Gname!
)
FOR /F "delims==" %%G IN ('%getKeyMacro%') DO set "key=%%G"
if "%key%"=="up" set /a "selected-=1"
if "%key%"=="down" set /a "selected+=1"
if %selected% lss 1 set "selected=1"
if %selected% gtr %maxOptions% set "selected=!%maxOptions%!"
if "%key%"=="enter" goto toggle
if "%key%"=="right" goto OK
goto select
:toggle
set /a "option%selected%+=1"
set /a "option%selected%=!option%selected%!%%2"
goto select
:OK
FOR /L %%G IN (1,1,%maxOptions%) DO (
if !option%%G! equ 1 (
echo %%G selected
)
)
pause
EDIT #2
Now uses the for loop by #Aacini to initiate the variables so this only needs to happen once, and so there is no need for a manual maxoption any more:
#echo off
setlocal EnableDelayedExpansion
set "getKeyMacro=powershell -noprofile "^
while (-not (37..40+13).contains($x)) {^
$x = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown').VirtualKeyCode^
}^
if ($x -eq 13) {^
'enter'^
}^
('left','up','right','down')[$x - 37]^
""
set "num=0"
for %%a in ("Install thing 1"
"Do thing 2"
"Execute thing 3"
"Run thing 4") do (
set /A num+=1
set "option!num!=0"
set "option!num!name=%%~a"
)
set "maxOptions=%num%"
set "selected=1"
:select
cls
echo use ^<right^> arrow to continue, ^<up^> and ^<down^> to select, and ^<enter^> to toggle
FOR /L %%G IN (1,1,%maxOptions%) DO (
set "display=[ ]"
if !option%%G! equ 1 set "display=[x]"
if %%G equ !selected! set "display=^>!display!
echo !display! !option%%Gname!
)
FOR /F "delims==" %%G IN ('%getKeyMacro%') DO set "key=%%G"
if "%key%"=="up" set /a "selected-=1"
if "%key%"=="down" set /a "selected+=1"
if %selected% lss 1 set "selected=1"
if %selected% gtr %maxOptions% set "selected=!%maxOptions%!"
if "%key%"=="enter" goto toggle
if "%key%"=="right" goto OK
goto select
:toggle
set /a "option%selected%+=1"
set /a "option%selected%=!option%selected%!%%2"
goto select
:OK
FOR /L %%G IN (1,1,%maxOptions%) DO (
if !option%%G! equ 1 (
echo %%G selected
)
)
pause
#echo off
setlocal EnableDelayedExpansion
rem Define the text for the options
set "num=0"
for %%a in ("First option"
"Second option"
"Third option"
"Fourth option") do (
set /A num+=1
set "msg[!num!]=%%~a"
set "opt[!num!]= "
)
set "digits=0123456789"
:select
(cls
echo Press digit to mark/unmark options, or X to end
echo/
for /L %%i in (1,1,%num%) do echo !digits:~%%i,1!. [!opt[%%i]!] !msg[%%i]!
)
choice /C !digits:~1,%num%!X /N > NUL
if %errorlevel% gtr %num% goto endSelect
set "sel=%errorlevel%"
if "!opt[%sel%]!" equ "X" (set "opt[%sel%]= ") else set "opt[%sel%]=X"
goto select
:endSelect
echo/
echo/
for /L %%i in (1,1,%num%) do (
if "!opt[%%i]!" equ "X" (
echo %%i selected
)
)

How do i check if input is any integer?

Simply asked, I need to check if a variable is numerical. I'm aware of the ability of:
set /a variable1=%variable%
setting non numerical strings to 0, but i need to be able to have 0 as an intiger as well as negative numbers.
This will be run very often, so a fast script is preferred. I've tried to echo the variable into a .txt, and use a for loop to scan through and return an error if anything other than 0-9 is detected, but the script is excessively long running, and frankly is a mess.
You could do something to this affect. Remove all numbers. If anything is left over it is not an integer. Not saying this is perfect but it is a step in the right direction.
set "tempvar="
FOR /F "tokens=* delims=-0123456789" %%G IN ("%variable1%") DO SET "tempvar=%%G"
IF DEFINED tempvar echo NOT AN INTEGER
As mentioned in question17584282
The easiest for digits should be:
IF %1 NEQ +%1 echo Notnumeric!
If negative numbers (hyphen) are also to be considered, this will work
SET number=%1
if %1 EQU +%1 echo positive number
if %1==-%number:-=% echo negative number
Learned from https://www.itprotoday.com/compute-engines/jsi-tip-9692-how-can-batch-script-determine-if-variable-or-parameter-integer
#echo off
:isInterer input [returnVar]
setlocal enableDelayedexpansion
set "input=%~1"
if "!input:~0,1!" equ "-" (
set "input=!input:~1!"
) else (
if "!input:~0,1!" equ "+" set "input=!input:~1!"
)
for %%# in (1 2 3 4 5 6 7 8 9 0) do (
if not "!input!" == "" (
set "input=!input:%%#=!"
)
)
if "!input!" equ "" (
set result=true
) else (
set result=false
)
endlocal & if "%~2" neq "" (set %~2=%result%) else echo %result%
try this.Some special symbols like ! and ^ could cause trouble though.
You can also use findstr:
#echo off
:isIntererFindstr input [returnVar]
setlocal enableDelayedexpansion
set "input=%~1"
if "!input:~0,1!" equ "-" (
set "input=!input:~1!"
) else (
if "!input:~0,1!" equ "+" set "input=!input:~1!"
)
echo !input!|findstr /r "[^0-9]" >nul 2>&1
if %errorlevel% equ 0 (
set result=false
) else (
set result=true
)
endlocal & if "%~2" neq "" (set %~2=%result%) else echo %result%

Cannot find bug in simple .CMD file

I cannot find my bug, after set /p sum=give nr %amount%: it closes.
Does anybody have an Idea why ?
thanks anyway;-)
::Made By QluPreX 29/01/2015
#echo off
cls
color a
:SET_NUM
set /p tot=how many numbers:
cls
set amount=1
set sum_tot=0
echo %tot%?
set /p y_or_n=is that correct (y/n) ? :
if %y_or_n%==y (
cls
goto:GIVE_NUM
) ELSE (
cls
goto:SET_NUM
)
:GIVE_NUM
set /p sum=give nr %amount%:
set /a sum_tot=%sum_tot%+%sum%
set /a amount=%amount%+1
if /I %amount%==%tot%(
goto:DISPLAY
)ELSE(
goto:GIVE_NUM
)
:DISPLAY
echo total is %sum_tot%
pause
No idea what you mean by set /p sum=give nr %amount%:
but
)ELSE(
Must be
) ELSE (
(spaces required)
equally,
if /I %amount%==%tot%(
must be
if /I %amount%==%tot% (

Windows Shell Script Issue - Response always invalid

Below is a .bat file with the code for a tic-tac-toe game for a class I am in. No matter what I change, I'm always greeted with "Invalid move. Please try again!" when entering a box location (eg, A1). The validation occurs in :ValidateResponse, starting in line 273. Can anyone see what I have done wrong causing this response? Thanks.
#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
You have faulty logic in :ValidateResponse. I only answered the question you asked. This will allow you to move on to the next problem. Keep at it!
REM Ensure that a valid square was specified (A1-A3, B1-B3 & C1 - C3)
IF /I %response%==A1 GOTO :ContinueValidating
IF /I %response%==A2 GOTO :ContinueValidating
IF /I %response%==A3 GOTO :ContinueValidating
IF /I %response%==B1 GOTO :ContinueValidating
IF /I %response%==B2 GOTO :ContinueValidating
IF /I %response%==B3 GOTO :ContinueValidating
IF /I %response%==C1 GOTO :ContinueValidating
IF /I %response%==C2 GOTO :ContinueValidating
IF /I %response%==C3 GOTO :ContinueValidating
SET ValidMove=False
GOTO :EOF
:ContinueValidating
Then you also need to change all of the lines below that like the one below to change " " to "" because the variable is not initialized to a space.
IF NOT "%A1%"==" " (
to
IF NOT "%A1%"=="" (

Resources