So I'm programming a mini-game about writing an id code, and then letting someone guess it. The problem i am facing, is that whenever someone tries to change the font color using the game syntax, just shows an error: "( was unexpected at this time." and just instantly crashes. This is my code at the moment:
#echo off
cls
:play
set /p id=Identification code:
if %id% == color:0 (
color 0
cls
goto play
) else if %id% == color:a (
color a
cls
goto play
) else if %id% == color:b (
color b
cls
goto play
) else if %id% == color:c (
color c
cls
goto play
) else if %id% == color:d (
color d
cls
goto play
) else if %id% == color:e (
color e
cls
goto play
) else if %id% == color:f (
color f
cls
goto play
) else (
cls
echo Identification stored.
pause
cls
set /p conf=Enter identification:
if %conf% == %id% (
cls
echo Access granted.
pause
exit
) else if %conf% == #cheat.code:(acss_grnt.admn)$ (
color c
cls
echo Cheating system..
timeout 3 >nul
tree
dir
timeout 1 >nul
cls
echo Access granted.
pause
exit
else (
cls
echo Access denied.
pause
exit
)
You should be aware from COLOR /? "Color attributes are specified by TWO hex digits"; you are just using one. You have not added any code to determine what the current foreground colour is. "The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute the COLOR command with a foreground and background color that are the same". Don't assume the foreground color attribute, if you're assuming it is currently 8 then add 8 after the background attribute in your COLOR commands.
You do not need to use all those ELSE IF combinations:
#ECHO OFF
:PLAY
SET/P "ID=Identification code: "
IF /I "%ID%"=="color:0" (
COLOR 08
GOTO PLAY
)
IF /I "%ID%"=="color:a" (
COLOR A8
GOTO PLAY
)
IF /I "%ID%"=="color:b" (
COLOR B8
GOTO PLAY
)
IF /I "%ID%"=="color:c" (
COLOR C8
GOTO PLAY
)
IF /I "%ID%"=="color:d" (
COLOR D8
GOTO PLAY
)
IF /I "%ID%"=="color:e" (
COLOR E8
GOTO PLAY
)
IF /I "%ID%"=="color:f" (
COLOR F8
GOTO PLAY
)
I have removed all of those CLS commands, just begin :PLAY with CLS instead.
You are missing a closing parenthesis for this block :
else if %conf% == #cheat.code:(acss_grnt.admn)$ (
color c
cls
echo Cheating system..
timeout 3 >nul
tree
dir
timeout 1 >nul
cls
echo Access granted.
pause
exit
You are also missing a closing parenthesis for this else block :
else (
cls
echo Identification stored.
pause
cls
set /p conf=Enter identification:
You should try to keep the structure simple computers/programs are there to avoid tiresome repetitions so the color thing is put in one logical line with a line continuation char ^ at the end and using conditional execution on success && or fail || of the result of the findstring.
I don't know if I could always follow your logic, so try it out.
#echo off
Set "Wait=timeout /T 3 /nobreak>NUL"
:play
cls
set "id="
set /p id=Identification code:
If "%id%"=="" Echo no input&timeout 3>NUL&goto play
IF /i "%id:~0,6%"=="color:" Echo:%id%|findstr /I "^color:[0a-f]$" >NUL 2>&1 ^
&&(color %id:~-1%&Goto play)||(Echo wrong color&%Wait%&goto play)
cls
echo Identification stored.
%Wait%
cls
set "conf="
set /p conf=Enter identification:
if "%conf%"=="%id%" (
cls
echo Access granted.
%Wait%
exit
) else if /i "%conf%"=="#cheat.code:(acss_grnt.admn)$" (
color c&cls&echo Cheating system..
%Wait%
tree&dir
timeout 1 >nul
cls&echo Access granted.
%Wait%
exit
)
cls&echo Access denied.
%Wait%
exit
Related
So I'm making a very ambitious game using batch to challenge myself. I've been working on the "shop" segment of this game but for some reason when I enter 'b' or 'B' it doesn't send me back to the menu, but reads it as I wanted to buy a medkit and sends me to the "medkit purchased" line of code. I've tried to add pause >nul and pause between the shop line of code and the purchased line of code, but it didn't help. I also tried double checking to see if I typed menu right many times. There has to be something I'm missing, help appreciated, I've been messing with it for almost 2 hours now.
Edit: I have not entered the other events yet, only medkit and go back to menu.
Edit2: I've added some more variables and instead of taking me to them it takes me to the medkit code again.
Edit: I removed the percents, you guys can stop bugging me bout it. The issue still is not fixed and I do not have choice.exe so choice commands don't work.
Here is the code:
:shop
cls
echo Welcome to the shop!
echo Money: $%money%
echo.
echo 1. Buy Med Kit -$30
echo 2. Buy Shotgun Ammo (1) -$15
echo 3. Buy Assult Rifle Ammo (5) -$20
echo 4. Buy Missle (1) -$50
echo 5. View Armor
echo 6. View Modifiers
echo.
echo Enter 'B' to go back.
echo.
set /p %shopOp%=
if '%shopOp%' == '1' goto medkit1
if '%shopOp%' == '2' goto sg1
if '%shopOp%' == '3' goto ar1
if '%shopOp%' == '4' goto rpg1
if '%shopOp%' == '5' goto armors1
if '%shopOp%' == '6' goto mods1
if '%shopOp%' == 'B' goto menu
if '%shopOp%' == 'b' goto menu
:medkit1
cls
set /a money=%money%-30
if %money% LSS 0 goto noBuy1
set /a medK=%medK%+1
echo You bought 1 medkit for $30!
echo You now have %medK% med kits now!
echo.
echo Press enter to continue
pause >nul
goto shop
:noBuy1
cls
set /a money=%money%+30
echo You dont have enough money!
echo You have $%money%. You need $30.
echo.
echo Press enter to continue
pause >nul
goto shop
Okay so you don't have choice. How about xcopy?
A version adapted from the link in my comment that substitutes the choice utility with xcopy to return keypress:
#Echo off
Set Echo.ID=Call :ID.Label "%%~1" "%%~2"
Setlocal EnableDelayedExpansion
Title Menu Navigation Example
:Bar
Set "Left.Date="
IF /I "!Left.Bar!" == "Yes" Goto :Date
Call :[+Menu] "Bar" "Drink Fight Chat Leave"
Goto :Bar
:Date
Set "Left.Bar="
IF /I "!Left.Date!" == "Yes" Goto :Bar
Call :[+Menu] "Date" "Dinner Flirt Leave"
Goto :Date
:[+Menu] <Location / Core Menu option> <Sub Actions / Menu Items as list>
::: - Build menu and choice list for display
CLS
Set "CHOICES="
Set "Actions="
For %%A in (%~2) Do (
Set "Option=%%~A"
Set Actions=!Actions! "%%~A"
Set CHOICES=!CHOICES! "!Option:~0,1!"
Set "Option=[!Option:~0,1!]!Option:~1,100!"
Echo !Option!
)
::: - Return key literal from xcopy command and call relevent menu item with Identifying Params
Set "Option="
Set Key=
For /F "Delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do (
If Not Defined Key Set "Key=%%A"
)
Set "Key=!Key:~-1!"
::: - Restrict keypress to available options, fall through to 'Goto :[Menu]' when no Call is made (Invalid keypress)
For %%O in (%~2) Do (
Set "Option=%%~O"
Set "Option=!Option:~0,1!"
If /I "!Option!" == "!Key!" Call :[%~1_%%~O-!Key!] "%%~1 %%~O" "%~1"
)
Goto :[+Menu]
:[Bar_Drink-D]
%Echo.ID%
Goto :!Loc!
:[Bar_Fight-F]
%Echo.ID%
Goto :!Loc!
:[Bar_Chat-C]
%Echo.ID%
Goto :!Loc!
:[Bar_Leave-L]
%Echo.ID%
Set "Left.Bar=Yes"
Goto :!Loc!
:[Date_Dinner-D]
%Echo.ID%
Goto :!Loc!
:[Date_Flirt-F]
%Echo.ID%
Goto :!Loc!
:[Date_Leave-L]
Set "Left.Date=Yes"
%Echo.ID%
Goto :!Loc!
:ID.Label <%~1> <%~2>
Title %~1
::: - Identify return Label
Set "Loc=%~2"
Exit /B
I am trying to make a fully immersive text adventure using a batch file.
Here is my problem: I want the answers to be a text input, so that the players type in a response which dictates where they will go.
For a lot of questions I need there to be multiple possible inputs. For example when you get to an enemy there are tons of different things you could do, however I can only figure out how to get it to recognise one input.
With other words, I want system to take user input and do actions accordingly.
Here is my code for this section so far:
:forest1
echo you awake in a forest, you do not know where you are or why you are there.
echo infront of you is a small goblin like creature
:recoil1
echo What do you do?
set /p answer=
if %answer%==run (
goto run1
) else (
if %answer%==attack (
goto attack1
) else (
if %answer%==befriend(
goto befriend1
) else (
if %answer%==scream(
goto scream1
) else (
if %answer%==dance (
goto dance1
) else (
echo Nothing happened
timeout /t 1
goto forest1
)
Your way should be modified like this to work:
#echo off
rem Your code before the code you provided above ^^
:forest1
echo You awake in a forest, you do not know where you are or why you are there.
echo In front of you is a small goblin like creature
goto :recoil1
:recoil1
set /p "answer=What do you do? "
if "%answer%" == "run" (
goto :run1
) else (
if "%answer%" == "attack" (
goto :attack1
) else (
if "%answer%" == "befriend" (
goto :befriend1
) else (
if "%answer%" == "scream" (
goto :scream1
) else (
if "%answer%" == "dance" (
goto :dance1
) else (
echo Nothing happened.
timeout /t 1
goto :forest1
)
)
)
)
)
You see: this is complicated a bit; you missed lots of parenthesis!
So, use choice command with some modifications:
#echo off
rem Your code before the code you provided above ^^
:forest1
echo You awake in a forest, you do not know where you are or why you are there.
echo In front of you is a small goblin like creature
goto :recoil1
:recoil1
echo What do you do? Here is a list of options:
echo r - run away
echo a - attack the goblin
echo b - be friend with the goblin
echo s - scream
echo d - dance
echo n - do nothing
choice /C:rabsdn /N
if errorlevel 6 (
echo Nothing happened.
timeout /t 1
goto :forest1
)
if errorlevel 5 goto :dance1
if errorlevel 4 goto :scream1
if errorlevel 3 goto :befriend1
if errorlevel 2 goto :attack1
if errorlevel 1 goto :run1
which is clearer, faster and more readable, isn't it?
Note: the if with the errorlevel should be in descending order because if errorlevel n means if the errorlevel is greater than or equal to n!
Modify the options to better suit for you.
Why not try use if in the for looping to do this job?
#echo off
:forest1
cls & echo/ & if defined answer set answer=<nul
echo/ you awake in a forest, you do not know where you are or why you are there.
echo/ infront of you is a small goblin like creature
:recoil1
set /p "answer= What do you do? "
for %%i in (run attack befriend scream dance) do if /i "%answer%" == "%%i" goto :%answer%1
echo/ Nothing happened
timeout /t 1 & goto forest1
:run1
echo/ Here I'm in Label run1 & exit /b
:attack1
echo/ Here I'm in Label attack1 & exit /b
:befriend1
echo/ Here I'm in Label befriend1 & exit /b
:scream1
echo/ Here I'm in Label scream1 & exit /b
:dance1
echo/ Here I'm in Label dance1 & exit /b
I am trying to write a batch script to get a string in a variable and check if it is empty, and if it is empty then it is directed to a loop. The below code represents the problem
:loop
set /p cide=
IF NOT "a%1"=="a" (set cide="%1")
IF [%1]==[] goto loop
ELSE
echo IDE entered
TIMEOUT 5 > NUL
The program starts to loop again even if i give a string.
I tried to put IF [%cide%]==[] goto loop or IF %cide%==[] goto loop it gave an error stating "ELSE" not recognized.
Any help is appreciated. Thanks
You can try something like that :
#echo off
:loop
cls & Color 0A
echo Type what you want !
set /p "cide="
IF "%cide%"=="" (
Cls & Color 0C
echo You must enter something
Timeout /T 2 /NoBreak>nul
goto loop
) ELSE (
Goto Next
)
:Next
Cls
echo %cide% is entered
pause
#echo off
:loop
cls & Color 0A
echo Type what you want !
set /p "cide="
IF [%cide%]==[] (
Cls & Color 0C
echo You must enter something
choice /d y /t 2 > nul
goto loop
) ELSE (
Goto Next
)
:Next
Cls
echo %cide% is entered
pause
Im am trying to make a simple coin game in batch. The problem I have is once the delay is completed (ping) "goto was unexpected at this time" is all I get before the window closes. Any help would be appreciated!
#echo off
color f0
title Nim In Batch
::: Main Menu
:main
set /a coins=12
set /a time=0
cls
echo Nim In Batch!
echo.
choice /n /c pr /m "Play Or See Rules?"
if %errorlevel%==1 (
goto play
)
if %errorlevel%==2 (
goto rules
)
::: Rules
:rules
cls
echo Objective: Be the one to claim the last coin
echo.
echo Rules:
echo -You Play First
echo -You Must Remove 1,2 Or 3 Coins Each Turn
echo -Turns Switch Once Coins Are Removed
echo -Coins Must Be Removed In Order
echo.
echo Ready?
pause>nul
goto main
::: Game
:play
cls
echo (%coins%) Coins
echo.
choice /n /c 123 /m "How Many Coins Do You Wish To Remove?"
if %errorlevel%==1 (
set /a Coins=%coins%-1
)
if %errorlevel%==2 (
set /a Coins=%coins%-2
)
if %errorlevel%==3 (
set /a Coins=%coins%-3
)
cls
echo (%coins%) Coins
echo.
echo Computer Is Thinking
ping 1.1.1.1 -n 1 -w 3250 >NUL
if %errorlevel%==1 (
set /a Coins=%coins%-3
)
if %errorlevel%==2 (
set /a Coins=%coins%-2
)
if %errorlevel%==3 (
set /a Coins=%coins%-1
)
if %coin%==0 (
goto end
) else (
goto play
)
:end
cls
echo Computer Wins!
echo He Collected The Final Coin!
echo.
pause
goto main
You forget an s in %coins% here :
if %coins%==0 (
goto end
) else (
goto play
)
I am having a very weird and problem that I cannot explain.
When I run my program a random bracket appears in the code.
Output
(No #echo off at the beginning)
C:\Users\wodahS>set /p ProgramTask=wodahS:
wodahS: How are you feeling Aimi?
C:\Users\wodahS>if ["How are you feeling Aimi?"] == [""] goto PROGRAM
C:\Users\wodahS>timeout 1 /nobreak 1>nul
) was unexpected at this time.
C:\Users\wodahS> )
Code
if "%ProgramTask%" == "I wont be able to talk to you for a while" (
:UnableTalk
cls
echo.
echo Aimi: Why not??
set /a Happiness-= 3
set /p Reason=%Us%:
timeout 1/nobreak >nul
echo.
if "%Reason%" == "I've got homework to do" (
)
if "%Reason%" == "I've got work to do" (
)
if "%Reason%" == "We're travelling" (
cls
echo Aimi: What?!?
timeout 1 /nobreak >nul
echo Aimi: Where To??
timeout 1 /nobreak >nul
:TravelDest
set /p TravelDes=%Us%:
timeout 1 /nobreak >nul
if "%TravelDes%" == "School" (
)
if "%TravelDes%" == "Town" (
)
if "%TravelDes%" == "Poland" (
cls
color 0d
echo Aimi: What??!?!? Are You Serious??
timeout 1 /nobreak >nul
echo Aimi: OMG!! Finally!!!
set /a Excitement+=25
timeout 1 /nobreak >nul
if %BondS% GTR 50 (
echo Aimi: Does that mean that I'm going to meet your uncle?!
timeout 1 /nobreak >nul
set /p MeetUncleQ=%Us%:
if "%MeetUncleQ%" == "Yeah" (
echo Aimi: Oh My God!! Im so excited!!
set /a Excitement+=10
)
)
)
if "%TravelDes%" == "My friend's house" (
)
echo Aimi: What? I dont understand.
timeout 1 /nobreak >nul
echo.
goto TravelDest
)
if "%Reason%" == "Never Mind" (
echo Aimi: Ummm... Okay?
set /a Curiousity+=2
timeout 1 /nobreak >nul
goto PROGRAM
)
echo Aimi: What? I dont understand.
timeout 1 /nobreak >nul
goto UnableTalk
)
You can't use a label :UnableTalk, :TravelDest within a block command (a series of lines within parentheses) as it terminates any open blocks.
Are you aware of the not and /i (case-insensitive) options of the if command? That would save you the outermost block.
if not "%ProgramTask%" == "I wont be able to talk to you for a while" goto skiptheblock
See if /? from the prompt for details.