Page 1 of 1

How to add a break in a batch code

Posted: 19 Jun 2015 05:40
by 117Mickey
Hi just asking how to make batch not automatically move on to the next line. Example.
This is a game where you act as a teacher and you go around finding students who are playing games. For example i click on 'north' and it reacts as normal. when everything gets to the end, it automatically jumps to 'east'. is there someway i can place a break in between these choices. Thanks

@echo off
echo Welcome to my adventure game where you catch students playing games.
echo.

:menu
cls
echo Choose an action
echo.
echo Move North - N
echo Move East - E
echo Move South - S
echo Move West - W
pause

SET /P M=
IF %M%==N GOTO North
IF %M%==E GOTO East
IF %M%==S GOTO South
IF %M%==W GOTO West

:North
cls
echo You have chosen to venture North
set /a a =%random% %% 3
goto :ChoiceN%a%
cls

:ChoiceN0
cls
echo You caught troll playing games
ping 192.0.2.2 -n 1 -w 2000 > nul
echo What will you do?
echo.
echo Educate
echo Flee

SET /P Q=
IF %Q%==Educate GOTO educate
IF %Q%==educate GOTO educate
IF %Q%==Flee GOTO Menu
IF %Q%==flee GOTO Menu
pause

:ChoiceN1
cls
echo You caught Lion playing games
ping 192.0.2.2 -n 1 -w 2000 > nul
echo What will you do?
echo.
echo Educate
echo Flee

SET /P W=
IF %W%==Educate GOTO educate
IF %W%==educate GOTO educate
IF %W%==Flee GOTO Menu
IF %W%==flee GOTO Menu
pause

:ChoiceN2
cls
echo You caught John playing games
ping 192.0.2.2 -n 1 -w 2000 > nul
echo What will you do?
echo.
echo Educate
echo Flee

SET /P E=
IF %E%==Educate GOTO educate
IF %E%==educate GOTO educate
IF %E%==Flee GOTO Menu
IF %E%==flee GOTO Menu
pause

:ChoiceN3
cls
echo You caught Bob playing games
ping 192.0.2.2 -n 1 -w 2000 > nul
echo What will you do?
echo.
echo Educate
echo Flee

SET /P R=
IF %R%==Educate GOTO educate
IF %R%==educate GOTO educate
IF %R%==Flee GOTO Menu
IF %R%==flee GOTO Menu
pause

:East
cls
echo You have chose to venture East
pause
set /a b =%random% %% 3
goto :ChoiceE%b%

:educate
cls
echo You educated the student
ping 192.0.2.2 -n 1 -w 2000 > nul
echo The student replied back with lies
ping 192.0.2.2 -n 1 -w 1000 > nul
echo You shout at the student for lieing.
ping 192.0.2.2 -n 1 -w 1000 > nul
echo The student has lost confididince in playing games again
ping 192.0.2.2 -n 1 -w 3000 > nul
pause
goto menu

Re: How to add a break in a batch code

Posted: 19 Jun 2015 07:28
by foxidrive
Can you clarify what aspect you are having trouble with?

One thing you are missing is a branch on each input section, which handles invalid input or no input (when the input is coded to handle no input and invalid input).

Like the last line I added here:

Code: Select all

echo Choose an action
echo.
echo Move North - N
echo Move East - E
echo Move South - S
echo Move West - W
pause

SET /P M=
IF %M%==N GOTO North
IF %M%==E GOTO East
IF %M%==S GOTO South
IF %M%==W GOTO West

goto :menu
 


Look at the help for if /? to see how to handle input that is not case sensitive too.

Re: How to add a break in a batch code

Posted: 19 Jun 2015 17:29
by 117Mickey
Hi, here is an edited version, here i put in what to do in an invalid situation.
The branch for 'ggg' is at the very bottom. Copy and paste this into text document and convert it to a batch file, run it. If you were to input N it goes alright. If you type in random like 'fhdfhsfdf' it still goes to the "North". how can I disable this.


@echo off
echo Welcome to my adventure game where you catch students playing games.
echo.

:menu
cls
echo Choose an action
echo.
echo Move North - N
echo Move East - E
echo Move South - S
echo Move West - W
pause

SET /P M=
IF %M%==N GOTO North
IF %M%==E GOTO East
IF %M%==S GOTO South
IF %M%==W GOTO West
IF NOT %M%== GOTO GGG

:North
cls
echo You have chosen to venture North
set /a a =%random% %% 3
goto :ChoiceN%a%
cls

:ChoiceN0
cls
echo You caught random playing games
ping 192.0.2.2 -n 1 -w 2000 > nul
echo What will you do?
echo.
echo Educate
echo Flee

SET /P Q=
IF %Q%==Educate GOTO educate
IF %Q%==educate GOTO educate
IF %Q%==Flee GOTO Menu
IF %Q%==flee GOTO Menu
pause

:ChoiceN1
cls
echo You caught random1 playing games
ping 192.0.2.2 -n 1 -w 2000 > nul
echo What will you do?
echo.
echo Educate
echo Flee

SET /P W=
IF %W%==Educate GOTO educate
IF %W%==educate GOTO educate
IF %W%==Flee GOTO Menu
IF %W%==flee GOTO Menu
pause

:ChoiceN2
cls
echo You caught random2 playing games
ping 192.0.2.2 -n 1 -w 2000 > nul
echo What will you do?
echo.
echo Educate
echo Flee

SET /P E=
IF %E%==Educate GOTO educate
IF %E%==educate GOTO educate
IF %E%==Flee GOTO Menu
IF %E%==flee GOTO Menu
pause

:ChoiceN3
cls
echo You caught random3 playing games
ping 192.0.2.2 -n 1 -w 2000 > nul
echo What will you do?
echo.
echo Educate
echo Flee

SET /P R=
IF %R%==Educate GOTO educate
IF %R%==educate GOTO educate
IF %R%==Flee GOTO Menu
IF %R%==flee GOTO Menu
pause
goto Menu

:educate
cls
echo You educated the student
ping 192.0.2.2 -n 1 -w 2000 > nul
echo The student replied back with lies
ping 192.0.2.2 -n 1 -w 1000 > nul
echo You shout at the student for lieing.
ping 192.0.2.2 -n 1 -w 1000 > nul
echo The student has lost confididince in playing games again
ping 192.0.2.2 -n 1 -w 3000 > nul
pause
goto menu

:GGG
echo That is not a valid input
ping 192.0.2.2 -n 1 -w 2000 > nul
goto Menu

Re: How to add a break in a batch code

Posted: 19 Jun 2015 22:32
by 117Mickey
Anyway, I figured it out, just one questions about, how can I make it so if the user enters something other than 'n,e,s,w' it says incorrect input. I have written but it doesn't accept. it doesn't work.

echo Choose an action
echo.
echo Move North - N
echo Move East - E
echo Move South - S
echo Move West - W

SET /P Z=
IF %Z%==N GOTO North
IF %Z%==n GOTO North
IF %Z%==E GOTO East
IF %Z%==e GOTO East
IF %Z%==S GOTO South
IF %Z%==s GOTO South
IF %Z%==W GOTO West
IF %Z%==w GOTO West
IF NOT==%Z% GOTO Incorrect

:Incorrect
echo Incorrect input
goto Menu

Re: How to add a break in a batch code

Posted: 19 Jun 2015 23:43
by foxidrive
Your check for invalid input is not going to work, and you didn't read if /? help as I suggested, but I didn't phrase the reason why very well.

There is a switch that makes the comparison case-insensitive.

Re: How to add a break in a batch code

Posted: 20 Jun 2015 00:49
by 117Mickey
I don't undertsand what u mean, sorry

Re: How to add a break in a batch code

Posted: 20 Jun 2015 01:35
by foxidrive
117Mickey wrote:I don't undertsand what u mean, sorry


Tell me which part you don't understand and I'll try and explain it.

Re: How to add a break in a batch code

Posted: 20 Jun 2015 03:07
by 117Mickey
The suggestion you told me, if/ and what that means. Also the case sensitive thing. ????

Re: How to add a break in a batch code

Posted: 20 Jun 2015 06:56
by foxidrive
117Mickey wrote:The suggestion you told me, if/ and what that means. Also the case sensitive thing. ????


When you type the command name followed by a /? then it shows you the help.

You can also type help on the command prompt. I'm surprised that your lessons haven't covered that, and I assume you are studying it - and your amusing coding theme is aimed at your teachers.

case insensitive. That means you can type N or n and have it compared in just one statement.
It's the opposite of case sensitive.

Re: How to add a break in a batch code

Posted: 20 Jun 2015 18:14
by 117Mickey
ok, I understand thanks, and also I am not learning this in school, this is just something I made at home. And thanks for the case sensitive thing, I fixed that. Thanks. Last thing is how to insert an image ascii into this as well as how to make an invalid entry thing. IF NOT %Z%== GOTO Wrong doesn't work.

Re: How to add a break in a batch code

Posted: 20 Jun 2015 19:03
by foxidrive
Show us your changed code - it helps to see where you are up to before offering code.

Re: How to add a break in a batch code

Posted: 21 Jun 2015 03:20
by 117Mickey
Hi,

just wanted to say I figured it out, it was actually simple. Thanks for your help. Just one more thing, how do i make so the user 'wins' after it moves in a specific way.

Re: How to add a break in a batch code

Posted: 22 Jun 2015 02:02
by 117Mickey
hi thanks for your help, this will be the end of the thread. ThX