Question

Well if you were to put this code onto a batch file:

  1. it doesn't choose a random number (or if it does it always has the same result)
  2. it does't seem to lose or win if you choose to fight
  3. i've been teaching myself coding and this is my first time on a forum so please go easy on me :)

here is my code:

@echo off
title template
color 0F
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 thanks for playing
pause
exit /b

:instructions
cls
echo instructions
echo.
echo This game is case-sensitive!
echo Just have fun with it!
pause
goto menu

:start_1
set /a s1=%random% * 3 / 32768 + 1
if %s1%==1 goto prefight_1
if %s1%==2 goto prefight_2
if %s1%==3 goto prefight_3

:prefight_1
cls
echo You have discovered 3 Turtles!
echo They dont see you!
set /p answer=would you like to (1)FIGHT or (2)RUN?
if %answer%==1 goto fight_1
if %answer%==2 goto run_1

:fight_1
set /a f1=%random% * 4 / 32768 + 1
if %f1%==1 goto lose_fight_1
if %f1%==2 goto win_fight_1
if %f1%==3 goto win_fight_1
if %f1%==4 goto win_fight_1

:prefight_2
cls
echo You have discovered 3 Turtles!
echo They see you!
set /p answer=would you like to (1)FIGHT or (2)RUN?
if %answer%==1 goto fight_2
if %answer%==2 goto run_1

:fight_2
set /a f2=%random% * 4 / 32768 + 1
if %f2% gtr 4 goto fight_2
if %f2% lss 1 goto fight_2
if %f2%==1 goto lose_fight_1
if %f2%==2 goto lose_fight_1
if %f2%==3 goto win_fight_1
if %f2%==4 goto win_fight_1

:prefight_3
cls
echo You have discovered 3 Turtles!
echo They see you!
echo They seem angry!
set /p answer=would you like to (1)FIGHT or (2)RUN?
if %answer%==1 goto fight_3
if %answer%==2 goto run_1

:fight_3
set /a f3=%random% * 4 / 32768 + 1
if %f3%==1 goto lose_fight_1
if %f3%==2 goto lose_fight_1
if %f3%==3 goto lose_fight_1
if %f3%==4 goto win_fight_1

:lose_fight_1
cls
echo Sorry,You LOST!
echo Thank you for playing!
echo made by: JEREMY
set /p answer==(1)continue or (2)quit?
if %answer%==1 goto start_1
if %answer%==2 goto menu
pause

:run_1
cls
echo You ran away
pause
goto start_1
Was it helpful?

Solution 2

I strongly suspect that you have set a variable random. If you've done that, then the value that you set in the environment overrides the magic variable.

You can clear it by set "random="

It's normal practice to use a setlocal after your @echo off. That way, any changes you make to the envirnment are backed out when the batch terminates. Without it, any changes made will remain until they are explicitly changed again.

Personally, I prefer

set /a value=%random% %% limit + 1

to generate a value 1..limit. If for no other reason, it's easier to type.


@echo off
setlocal
rem environment changes made after here will be backed-out when the batch finishes...
....whatever....

The setlocal command sets up a 'local' environment which exists only until the batch ends or an endlocal command is encountered.

see setlocal /? from the prompt for more info.

Your batch appears to work fine once I'd added a win_fight_1 routine.

Note however that batch will barf on set /p answer==(1)continue or (2)quit? but removing one of those = will fix it.

Beyond that, just watch whether you want to go to menu or start_1.

And the

if %f2% gtr 4 goto fight_2
if %f2% lss 1 goto fight_2

will be ineffective - certainly if you've used set /a f2=%random% %% 4 + 1

OTHER TIPS

First off it's better for humans to read when it's "equ" instead of "==". The set /p VARIABLE= is easier to use when clear so use echo like so:

:Jelly_Attack_1
cls
echo AH! There's a swarm of angry jellyfish!
echo Act fast!
echo.
echo You can (1) fight or (2) RUN
set /p VARIABLE=
if %VARIABLE% equ 1 goto Fight_Jelly_1
if %VARIABLE% equ 2 goto Run
if %VARIABLE% neq 1 goto TEST

The fight part should look something like this:

:Fight_Jelly_1
cls
echo The jellyfish are more annoying than harmful!
echo You have a great advantage!
pause
set /a FIGHTNO=%random%
if %FIGHTNO% gtr 4 goto Fight_Jelly_1
if %FIGHTNO% lss 1 goto Fight_Jelly_1
if %FIGHTNO% equ 1 goto FAILED
if %FIGHTNO% equ 2 goto Fight_Jelly_1_Win
if %FIGHTNO% equ 3 goto Fight_Jelly_1_Win
if %FightNO% geq 4 goto Fight_Jelly_1_Win

Then when wanting to give gold or a reward on winning do this:

:Fight_Jelly_1_Win
cls
echo The fight was easy and you found some gold!
pause
set gold=0 (if you didn't have a gold variable)
set /a gold=gold+5 (to add gold)
goto FRAMENAME

I hope I helped!

Fake random-- I confirmed that %random% is driven by system clock-- http://blogs.msdn.com/b/oldnewthing/archive/2010/06/17/10026183.aspx

use the clock MS to get a random number from 1 to 1000 here is one way to get milliseconds: https://answers.yahoo.com/question/index?qid=20100816021807AAz6eL3

Another possibility to introduce randomness is to start the game and then wait for user input i.e. "ready to start?" and when they start will introduce some randomness into the amount of time since cmd.exe started.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top