Very new to this, so I apologize if this is something simple. I am running the following .bat script in Command Prompt for an assignment.
#ECHO off
TITLE "KnockKnock.bat - The KnockKnock joke game!"
COLOR 0E
CLS
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
SET /p reply="Knock Knock! C:>"
CLS
IF NOT %reply% == "Who is there?" (
ECHO "Sorry, but you are not playing the game right!"
GOTO :EOF)
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
SET /p reply="Orange! C:>"
CLS
IF NOT %reply% == "Orange who?" (
ECHO "Sorry, but you are not playing the game right!"
GOTO :EOF)
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO "Orange you glad you've written your first Windows shell script?"
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
The script will prompt "Knock Knock!" as it should, but upon responding "Who is there?" (w/o quotations), I am presented with the error "is was unexpected at this time". What am I doing wrong?
Again, I realize this is probably very elementary, so I appreciate any help.
Thanks.
The problem is that when the %reply% variable is substituted with its value, cmd tries to interpret this:
IF NOT Who is there? == "Who is there?" (
Rather than this:
IF NOT "Who is there?" == "Who is there?" (
To fix it, add quotes around %reply%, like this:
IF NOT "%reply%" == "Who is there?" (
ECHO "Sorry, but you are not playing the game right!"
GOTO :EOF)
Related
When you get to the user input part, no matter what I type (desk, fire, door) it always goes to fireplace. Is there something wrong with my if syntax?
#echo off
color C
title RG Text Game
echo -------------------------------------------------
echo Welcome to the Game!
echo.
echo -------------------------------------------------
echo.
pause
echo.
echo Blah bah blah story story
echo What do you want to do?
echo Choices: fire/desk/door
set /p choice=
if %choice%=="fire" GOTO fireplace
if %choice%=="desk" GOTO desk
if %choice%=="door" GOTO door
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
:desk
echo.
echo You go to the desk.
echo.
:door
echo.
echo You go to the door.
echo.
Doublequote %choice% or it will not be equal: desk is not equal as "desk".
And exit your Label block with a goto:eof or exit/b.
Use the /i switch with IF so you can also use DESK or DesK
if /i "%choice%"=="fire" GOTO fireplace
if /i "%choice%"=="desk" GOTO desk
if /i "%choice%"=="door" GOTO door
goto:error
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
exit/b
:desk
echo.
echo You go to the desk.
echo.
exit/b
:door
echo.
echo You go to the door.
echo.
exit/b
You need to protect the entry points of each of your target blocks, otherwise when it's done executing the target block, it will "fall into" the next block.
Before any label line (e.g., :fireplace), you'll need a GOTO to make sure that the program flow doesn't "fall into" the next routine:
#echo off
color C
title RG Text Game
echo -------------------------------------------------
echo Welcome to the Game!
echo.
echo -------------------------------------------------
echo.
pause
echo.
echo Blah bah blah story story
echo What do you want to do?
echo Choices: fire/desk/door
set /p choice=
if /I "%choice%" EQU "fire" GOTO fireplace
if /I "%choice%" EQU "desk" GOTO desk
if /I "%choice%" EQU "door" GOTO door
GOTO END
:fireplace
echo.
echo You come to the fireplace.
echo.
pause
GOTO END
:desk
echo.
echo You go to the desk.
echo.
GOTO END
:door
echo.
echo You go to the door.
echo.
:END
Note also the changes to the IF statements. These allow you to handle the case of typing FIRE or Fire instead of just fire.
In addition to the tips from Jeff and SachaDee,
if you name all your labels equal to the choice you could use a loop
For %%A in (fire desk door) Do If /i "%choice%" equ "%%A" Goto %choice%
Or with a llimited number of choices you could use choice.exe and work with a single letter answer (no enter needed then) and evaluate the returned errorlevel.
So recently I started learning Batch-file, and today I decided to try and make a game with it. I have a few errors, and can't figure out what is wrong with the code.
Heres the code:
#echo off
title Batch Rpg
color 07
echo Welcome to Batch Rpg!
echo.
echo.
pause
:menu
cls
echo.
echo -Menu-
echo.
echo.
echo.
echo 1) Begin
echo.
echo 2) How to play
echo.
echo 3) Exit
echo.
echo.
echo.
echo ---------
echo.
set /p c=Choice:
if %c%==1 goto prestart1
if %c%==2 goto howtoplay
if %c%==3 goto cfr_exit
if NOT %c%==1 if NOT %c%==2 if NOT %c%==3 goto menu
:cfr_exit
cls
echo.
echo Are you sure you want to exit?
echo.
set /p c=(Y/N):
if %c%=="Y" exit
if %c%=="N" goto menu
if NOT %c%=="Y" if NOT %c%=="N" goto cfr_exit2
:cfr_exit2
cls
echo.
echo You must enter a valid option.
echo.
pause
goto cfr_exit
:howtoplay
cls
echo.
echo -How to play-
echo.
echo.
echo.
echo This game is very simple. There will be a number with an option after it, type the option in and it will perform an action(As the option would say).
echo.
pause
goto menu
:prestart1
cls
echo.
echo Welcome to land of Fageryth!
echo.
echo What is your name, adventurer?
echo.
set /p playername=Name:
goto prestart2
:prestart2
cls
echo.
echo What would be your more valued statistic, Attack damage, or Hit points?
echo.
echo.
echo.
echo 1)Attack damage(Atk)
echo.
echo 2)Hit points(Hp)
echo.
echo.
echo.
echo ---------
echo.
set /p playermorevaluedstat=Choice:
if %playermorevaluedstat%==1
set playeratk=6
set playerhp=25
if %playermorevaluedstat%==2
set playeratk=4
set playerhp=30
if NOT %playermorevaluedstat%==1 if NOT %playermorevaluedstat%==2 goto prestart2
cls
echo playeratk
echo playerhp
pause
I'm having trouble with the :prestart2 section of my code. With the end of it, I tried to make it where if the variable wasn't equal to 1 or 2, which were the options, then it send the player back up to the start of the section again, and also, when it finishes checking, I'm trying to make it display the two variables playeratk and playerhp but instead it just closes out. I am really lost here and would appreciate the help!
A couple things before we start:
First, when troubleshooting your batch scripts, get rid of (comment out) your echo off and cls lines so that you can see where it's going wrong.
Second, you should always double-quote your variables to make sure you're not inadvertently including spaces when setting and comparing them. It doesn't seem to actually be causing problems in your code, but it's a good habit to get into:
set "var1=something"
set "var2=a string with spaces"
if "%var1%"=="something" echo %var1%
Now, the problem in your code is with the two if statements stretching over multiple lines. If you're going to do that, you have to put them inside of parentheses.
set /p playermorevaluedstat=Choice:
if %playermorevaluedstat%==1 (
set playeratk=6
set playerhp=25
)
if %playermorevaluedstat%==2 (
set playeratk=4
set playerhp=30
)
I made this code this afternoon and only started to start doing this about a day ago. I can seem to find out why when playing my game I can just press enter on my questions and it will go through to the next level even though I set questions for it.
#echo off
title 'Hospital Nightmare'
color 47
if "%1" neq "" (goto %1)
pause
:Menu
cls
echo '1 Start'
echo '2 Instructions'
echo '3 Exit'
set /p answer='Type the number of your option and press enter'
if %answer%==1 goto 'Start_1'
if %answer%==2 goto 'Instructions'
if %answer%==3 goto 'Exit'
:'Exit'
#echo off
echo Happy Dreams
pause
exit /b
:'Instructions'
#echo off
cls
echo 'Instructions'
echo.
echo Self Explanitory you
echo IDIOT!
pause
goto Menu
:'Start_1'
#echo off
cls
echo You awake in a small room
echo that smells like sterilisation
echo and surgical equiptment.
echo.
echo Even though it's small,
echo many things are inside.
echo this is strange?
echo.
echo Screams echo around you
echo and a noise is coming from
echo around the corner!
echo.
set /p answer='Press 1 to find out what it is!'
if %answer%==1 goto 'Fight_1'
:'Fight_1'
#echo off
cls
echo "YOU, YES YOU THERE! DON'T
echo LOOK AT ME LIKE IM STUPID
echo YOU KNOW WHAT I WANT, NOW
echo FIGHT ME!!"
echo.
echo What is this guy going on
echo about??
echo.
set /p answer='would you like to fight or run?'
if %answer%==Fight goto 'Fight_1'
if %answer%==Run goto 'Run_1'
:'Run_1'
#echo off
cls
echo You escape safely!
:'Fight_1'
#echo off
cls
echo "GOOD LUCK"
echo "YOU'RE GONNA NEED
echo IT!"
echo.
echo You wait for thhe first punch
echo.
set /p answer= Type the number 1 and press enter to continue:
if %answer%==1 goto 'Fight_1_Loop'
:'Fight_1_Loop'
set /a num=%random%
if %num% gtr 4 goto 'Fight_1_Loop'
if %num% lss 1 goto 'Fight_1_Loop'
if %num%==1 goto 'Lose_Fight_1'
if %num%==2 goto 'Win_Fight_1'
if %num%==3 goto 'Win_Fight_1'
if %num%==4 goto 'Win_Fight_1'
:'Lose_Fight_1'
#echo off
cls
echo "FIGHT ME EH? YOU
echo GOT NO CHANCE!"
echo.
echo You are knocked
echo out cold! :'(
pause
goto Menu
:'Win_Fight_1'
#echo off
cls
echo "I WAS ONLY JOKING
echo YOU'RE HUGE MAN
echo LEAVE ME ALONE!"
echo.
set /p answer='Would you like to save?'
if %answer%==Yes goto 'Save'
if %answer%==No goto 'Start_2'
:'Save'
#echo off
cls
echo YOU IDIOT YOU CAN'T SAVE
echo I'M NEW TO THIS AHAHAHA!
:'Start_2'
#echo off
cls
echo That guy was crazy.
echo Now where am I? and what did
echo he want with me?
echo.
echo This place is an old hospital
set /p answer='Before we keep going what's your name?'
if %answer%=="Meerkat" goto 'Hallway'
:'Hallway'
#echo off
cls
echo You turn left to into a
echo hallway to find that it
echo is very long and wide.
echo.
echo LIKE MY D*CK!!!
echo.
echo "Now is not the time to
echo joke narrator."
echo.
echo Ok
echo.
echo Well umm...
echo You turn left...
echo To find...
echo A...
echo F*CK IT! You find
echo a HOBO OCTOPUS!!!
echo.
echo "Dude wtf."
echo.
echo "Ok I'll do it."
echo.
echo YESS! Let the battle begin!
echo.
set /p answer='Will you fight this fight?'
if %answer%==Yes goto 'Fight_2'
if %answer%==No goto 'Scene_1'
:'Scene_1'
#echo off
cls
echo YOU DON'T WANT TO FIGHT
echo AN OCTOPUS?
echo.
echo WHAT ARE YOU? OCTOPUSSY?
pause
goto Menu
:'Fight_2'
#echo off
cls
You need to set the variable to blank as you get the prompt:
:Menu
cls
echo '1 Start'
echo '2 Instructions'
echo '3 Exit'
set /p answer='Type the number of your option and press enter' || SET answer=NO_ANSWER
if %answer%==NO_ANSWER GOTO Menu
if %answer%==1 goto 'Start_1'
if %answer%==2 goto 'Instructions'
if %answer%==3 goto 'Exit'
GOTO Menu
The final GOTO Menu also ensures that the value that was inserted was not just nonsense, for example sdhghdhf or some other invalid value. It will act as the "default" case.
Just copy this kind of structure to every "decision" and it should work fine.
i'm new to the coding of batch files and got an error wich i can not solve..
the problem occurs at the :secName , I made a printscreen at the flash(just before it exits cmd), and it said: "so your name is [what %name% is]"and then "the syntax of the command is incorrect.
goto was unexpected at this time."
i've got this coding done now:
#echo off
goto Welcome
title The Game Of Everything
:welcome
echo.
echo Welcome!
echo What is your name?
set /p name=Enter:
goto secName
:secName
cls
echo.
echo so your name is %name%?
set /p secName:yes or no?:
if %secName%== yes goto begin
if %secName%==no goto welcome
:begin
cls
echo.
echo Welcome %name%, to "The Game Of Everything"!
echo I hope you enjoy this mini-game, downloadable on your computer!
echo.
echo It isn't the prettiest, but who gives a toss right?
echo press any key to continue...
pause>nul
exit
change this:
set /p secName:yes or no?:
to this
set /p secName=yes or no?:
My code is not completely finished but it was working.Now it closes after the first section instead of going to section two.I know that this is probably a dumb mistake but I would appreciate the help!(The lines are just for organization)
#echo off
title First batch file
color 1f
::-----------------------
----------
:one
cls
echo Please enter your
name below.
echo.
echo.
set /p name=Name:
echo.
echo.
echo Hello %name%!
echo.
echo.
pause >nul
goto two
::-----------------------
----------
:two
cls
echo Would you like to
create a new account?
echo (yes or no)
echo.
echo.
set /p yorn=>>
if yorn equ yes then goto
new
if yorn equ no then goto
login
pause >nul
::-----------------------
---------
:new
cls
echo Please enter your
user name.
echo.
echo.
set /p username=Username:
cls
echo.
echo Please enter your
password.
echo.
echo.
set /p password=Password:
cls
echo.
echo Please confirm your
password.
echo.
echo.
set /p cpassword=Confirm
password:
cls
echo.
echo.
if %password% equ
%cpassword% goto
confirmscreen
if %password% new
%cpassword% goto new
pause >nul
:------------------------
---------
:confirmscreen
cls
echo Confirm:
echo.
echo.
echo Username: %username%
echo.
echo Password: %password%
echo.
echo.
set /p yorn2=Yes or No?:
if yorn2 equ yes goto
login
if yorn2 equ no goto new
pause >nul
:------------------------
---------
#echo off
title First batch file
color 1f
::---------------------------------
:one
cls
echo Please enter your name below.
echo.
echo.
set /p name=Name:
echo.
echo.
echo Hello %name%!
echo.
echo.
pause >nul
goto two
::---------------------------------
:two
set /p yorn=Would you like to create a new account? (yes or no) :
if %yorn% equ yes then goto new
if %yorn% equ no then goto login
goto:two