Well if you were to put this code onto a batch file:
it doesn't choose a random number (or if it does it always has the same result)
it does't seem to lose or win if you choose to fight
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
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!
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
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.
Related
I am currently coding (in batch) a mini tycoon game. However,
I'm having some difficulties when executing this command:
:cantbuy
echo Insufficient Funds!
pause
goto shop
:buy1
set /p money<"m2df.dll"
set /p cps<"m3df.dll"
if %money% GTR 100 goto canbuy1
goto cantbuy
:canbuy1
set /a "newmoney=%money%-100"
set /a "newcps=%cps%+2"
set cps=%newcps%
set money=%newmoney%
echo %money%>m2df.dll
echo %cps%>m3df.dll
echo Bought!
pause
goto shop
The program seems to exit straight after I input "1"
Any ideas how to fix this?
Update:
Found It Out. I decided to put a pause after everything to see what was wrong, and the syntax was bad and it went straight to another command crashing the script. here's the fixed script:
:cantbuy
echo Insufficient Funds!
pause
goto shop
:buy1
set /p money=<"m2df.dll"
set /p cps=<"m3df.dll"
if %money% GEQ 100 goto canbuy1
goto cantbuy
:canbuy1
set /a "newmoney=%money%-100"
set money=%newmoney%
set /a "newcps=%cps%+2"
set cps=%newcps%
echo %cps% >m3df.dll
echo %money% >m2df.dll
echo Bought!
pause
goto shop
I was creating a simple random number guesser in batch but I got an error in my code and don't know what it is please help. Here is the code:
#echo off
title Number Guesser
:menu
echo ------------------
echo Number Guesser
echo ------------------
echo 1. Easy
echo 2. Medium
echo 3. Hard
echo 4. Exit
set /p dif=Select difficulty number:
if %dif% == 1 goto easygen
if %dif% == 2 goto medgen
if %dif% == 3 goto hardgen
if %dif% == 4 exit
goto menu
:easygen
set /a num=%random%
if %num% gtr 20 goto gen
cls
goto play
:medgen
set /a num=%random%
if %num% gtr 50 goto gen
cls
goto play
:hardgen
set /a num=%random%
if %num% gtr 100 goto gen
cls
goto play
:play
set /p guess=Guess:
if %guess% == %num% goto win
if %guess% gtr %num% echo Lower!
if %guess% lss %num% echo Higher!
:win
cls
echo Well Done
echo 1. Play again!
echo 2. Quit
set /p cmd=What do you want to do:
if %cmd% == 1 goto menu
if %cmd% == 2 exit
I get the error once I choose the difficulty (dif) I have no idea what isn't working. It just closes.
If you open up cmd then navigate to the file and run it, instead of double clicking the batch file, you'll see an error.
The system cannot find the batch label specified - gen
Like the other commenter said, you don't have :gen defined. It says goto gen, but gen doesn't exist, so it freaks out and closes.
Okay, so, I'm making a crude text-based batch game demo, its a sword-fighting game.... I've never used any sort of coding before, and I taught myself as best I could... I know this is a pretty primitive and easy to learn language, but I don't quite understand what I did wrong... It doesn't process damage to you OR the enemy, and the stat randomizer sometimes give out numbers equal to or below 0... Ive been tweaking, and changing code in ways I don't really understand, trying to fix it, so the code may be a little weird at some parts, feel free the constructively criticize, improve parts that MAY work, and PLEASE find the problems stated... (PS. its also programmed to recognize me as the "creator" and my girlfriend, and calls her by her nickname, but that works fine, so Ill remove it from this code.)
the code:
#echo off
color 02
cls
setlocal enabledelayedexpansion
set file=AdventureTextMusic.mp3
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
echo Sound.URL = "%file%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs
start /min sound.vbs
title New Adventurer
echo.
echo Hello Adventurer!
echo.
echo may I ask your name?
echo.
set /p name=
echo.
if %name% equ Michelle goto bunni
if %name% equ Stuart goto kitty
if %name% neq Stuart goto random
:bunni
cls
echo.
echo Bunni? Welcome to my game! I worked hard on it, I don't know if it will be good... We'll see I guess!!
set /a gold=%random% %% 25-1
echo.
echo Here's %gold% gold to get you started! I hope it helps!
echo.
set name=Bunni
echo Good luck %name%!
pause
goto continue
:kitty
cls
echo.
echo Creator? Bug fixing I assume? I hope all is well in the land of Adventure Text! Good luck!
set /a gold=%random% %% 25-1
echo.
echo Here's %gold% gold to get you started! I hope it helps!
pause
goto continue
:random
cls
echo.
echo That name suits you! Welcome %name%! To the land of Adventure Text!
set /a gold=%random% %% 15-1
echo.
echo Here's %gold% gold to get you started! I hope it helps!
echo.
echo Good luck %name%!
pause
goto continue
:continue
cls
echo The controls are simple, when it asks for your choice of attack, pick an option.
echo.
echo Slash does your weapons default damage, minus enemy armor strength, and takes no stamina
echo.
echo Slice does your weapons default damage, ignoring armor strength, and takes 2 stamina
echo.
echo whereas Stab does 1.5 damage, ignoring armor strength, but takes 4 stamina
echo.
pause
cls
echo.
echo Would you like to begin, %name%?
pause
title %name%'s Quest
cls
set /a health=%random% %% 15-1 +2
set /a armor=%random% %% 2-1 +2
set /a damage=%random% %% 4-2 +2
set /a stamina=%random% %% 15-8 +2
echo.
echo Health: %health% Gold: %gold% Armor Strength: %armor% Damage per Hit: %damage% Stamina: %stamina%
pause
cls
echo.
set /a number=%random% %% 2-1
if %number% equ 1 goto Grunt
if %number% equ 2 goto Guard
:Guard
set enemy=Guard
set EHealth=12
set EArmor=1
set EDamage=1
goto battle1
:Grunt
set enemy=Grunt
set EHealth=10
set EArmor=0
set EDamage=2
goto battle1
:battle1
echo An enemy %enemy% has spotted you!
echo Enemy Health: %EHealth% Enemy Armor Strength: %EArmor% Enemy Damage: %EDamage%
pause
cls
echo.
goto turn1
:turn1
echo What would you like to do?
echo 1) Slash
echo 2) Slice
echo 3) Stab
set /p attack=
if %attack% equ 1 goto slash
if %attack% equ 2 goto slice
if %attack% equ 3 goto stab
:turn2
echo Health: !health! Stamina: !stamina!
if !EHealth! leq 0 goto end
echo Enemy Health: !EHealth!
echo Enemy turn!
pause
cls
goto enemy
:enemy
set /a EAttack=%random% %% 3-1
if %EAttack% equ 1 goto Eslash
if %EAttack% equ 2 goto Eslice
if %EAttack% equ 3 goto Estab
if %EAttack% neq 3 goto enemy
:Eslash
set health-=!EDamage!-!armor!
if !health1 leq 0 goto lose
goto turn1
:Eslice
set health-=!EDamage!
if !health! leq 0 goto lose
goto turn1
:Estab
set EDamage*=1.5
set health-=!EDamage!
set EDamage/=1.5
if !health! leq 0 goto lose
goto turn1
:slash
set EHealth-=!damage!-!EArmor!
goto turn2
:slice
if %stamina% lss 2 cls
if %stamina% lss 2 echo.
if %stamina% lss 2 echo You do not have enough stamina to Slice
if %stamina% lss 2 goto turn1
set EHealth-=!damage!
set stamina-=2
goto turn2
:stab
if %stamina% lss 4 cls
if %stamina% lss 4 echo.
if %stamina% lss 4 echo You do not have enough stamina to Stab
if %stamina% lss 4 goto turn1
set damage*=1.5
set EHealth-=!damage!
set damage/=1.5
set stamina-=4-2
goto turn2
:lose
cls
echo.
echo Im sorry %name%... but you lost...
pause
cls
echo This game is in pre-alpha, I will likely be updating it to add the full story.
echo Created by: SteweeBee
pause
taskkill /f /im "wscript.exe"
exit
:end
cls
echo.
echo ---- ---- ---- [----] ------- ----
echo \ \ / \ / / I I I \ I I
echo \ \ / \ / / I I I I\ \I I
echo \ \_/ /\ \_/ / I I I I \ I I
echo \ / \ / I I I I \ I
echo ------ ------ [----] I___I \___I
pause
cls
echo This game is in pre-alpha, I will likely be updating it to add the full story.
echo Created by: SteweeBee
pause
taskkill /f /im "wscript.exe"
exit
set /a health=%random% %% 15-1 +2
The line above is the same as
set /a health=%random% %% 16
and I think you want this:
set /a health=(%random% %% 15-1) +2
The line below uses delayed expansion but you don't use code that requires it (like in loops) - You could just use normal expansion like %health%
!health!
This code will work, unless another character is added when typing and then it will fail. The variable attack should also be initialised before the set /p because set /p will remember what it was the last time through if enter is pressed without input. Input like 1 2 with the space will cause it to fail too (ditto with &).
set /p attack=
if %attack% equ 1 goto slash
if %attack% equ 2 goto slice
if %attack% equ 3 goto stab
It should really have a check for invalid input at the end - just a goto :get_input to loop back for input will do.
%random% seems to go in order.
#ECHO OFF
SET /A RAND=%RANDOM% %%100
ECHO %RAND%
ECHO.
If you keep running this it increments until it reaches 100 and then the number start over. If it were random it would jump around.
#ECHO OFF
SET /A RAND=%RANDOM%
ECHO %RAND%
ECHO.
SET /A RAND=%RANDOM%%%100+1
this may work.
If I understoof the question right, here's what you're looking for.
echo off
title Number from 1 to 100.
color 0a
cls
:loop
cls
set /a rand=%random% %%101
echo %rand%
pause >nul
goto loop
hey if the problem still persists
use this code
this generates a number between a and b
tweak it to your needs
#echo off
color 02
echo enter value of A
set /p a=
echo.
echo enter value of B
set /p b=
:main
set no=%random%
if %no% GEQ %a% goto sub
if not %no% GEQ %a% goto main
:sub
if %no% LEQ %b% goto end
if not %no% LEQ %b% goto main
:end
echo %no%
goto main
I found this tutorial to make a game on Notepad++ and make a batch file to run on CMD. Well here's what I have so far:
:ChooseWeapon
cls
echo "I almost forgot. Here are the weapons I have avaliable, choose one and begin your quest."
echo.
set /p weapon=What is your weapon? (Sword, Double-Bladed Axe, Dagger):
The point to this is to choose a weapon you wish to use by typing what you want. Now, the thing is I'm able to type "ghregff" and it would say that is my weapon. How do I make it so you have to choose either: Sword, Double-Bladed Axe, or Dagger?
You could make it a choice menu like below, and if they choose a number not 1 2 or 3, it will kick them back to enter a selection again.
#echo off
:chooseweapon
cls
echo "I almost forgot. Here are the weapons I have avaliable, choose one and begin your quest."
echo.
echo What is your weapon? (Sword, Double-Bladed Axe, Dagger):
echo 1 - Sword
echo 2 - Double-Bladed Axe
echo 3 - Dagger
echo.
set /P Weapon="Enter a choice: "
echo --------------------------------------------------------------------
for %%I in (1 2 3) do if #%Weapon%==#%%I goto wp%%I
goto chooseweapon
:wp1
Set weapon=Sword
goto end
:wp2
Set weapon=Double-Bladed Axe
goto end
:wp3
Set weapon=Dagger
goto end
:end
echo %weapon%
pause
#echo off
set "NumberWeapons=3"
:ChooseWeapon
color 07
cls
echo."I almost forgot. Here are the weapons I have avaliable, choose one and begin your quest."
echo.
echo.1 Sword
echo.2 Double-Bladed Axe
echo.3 Dagger
set /p Weapon=What is your weapon? (1-%NumberWeapons%):
echo.
if %Weapon% lss 1 goto :ChoiceError
IF %Weapon% gtr %NumberWeapons% goto :ChoiceError
GOTO :Start
:ChoiceError
COLOR CF
ECHO.Error! Choose a valid choice.
pause
goto :ChooseWeapon
:Start
echo.Replace this line with the rest of your stuff & pause
#ECHO OFF
SETLOCAL
SET setprompt=What is your weapon?
CALL :chooselist Sword, "Double-Bladed Axe", Dagger
ECHO(Choice made was %response%
CALL :chooselist2 Sword, "Double-Bladed Axe", Dagger
ECHO(Choice made was %response%
CALL :choosemenu Sword, "Double-Bladed Axe", Dagger
ECHO(Choice made was %response%
GOTO :EOF
:chooselist
SET valid=%*
SET "nchoices="
CALL :choose "%setprompt% (%valid:"=%):"
GOTO :eof
:chooselist2
SET valid=%*
SET "nchoices="
FOR %%Z IN (%*) DO ECHO %%~Z
CALL :choose "%setprompt%:"
GOTO :eof
:choosemenu
SET valid=%*
SET /a nchoices=0
FOR %%Z IN (%*) DO SET /a nchoices+=1&CALL ECHO %%nchoices%% : %%~Z
CALL :choose "%setprompt%:"
GOTO :eof
:choose
SET /p "response=%~1 "
IF NOT DEFINED response GOTO choose
IF DEFINED nchoices FOR /l %%Z IN (1,1,%nchoices%) DO IF "%%Z"=="%response%" CALL :setresp %valid%&GOTO :eof
SET /a cmatch=0
CALL :countresp %valid%
IF NOT %cmatch%==1 GOTO choose
GOTO :eof
:setresp
IF %response% neq 1 SET /a response-=1&shift&GOTO setresp
SET response=%~1
GOTO :eof
:countresp
SET $1=%~1
IF NOT DEFINED $1 (
IF %cmatch%==1 (SET response=%$3%)
GOTO :eof
)
CALL SET $2=%%$1:*%response%=%%
IF /i "%response%%$2%"=="%$1%" SET /a cmatch+=1&SET "$3=%~1"
shift&GOTO countresp
GOTO :eof
This is a flexible piece of code that allows you to make your choice in ne of three manners:
Using chooselist, the parameter-list is appended to setprompt and the user may choose to enter any response in full, or simply sufficient to unambiguously define the requirement. "s" would define Sword, for instance, but "do" would be required for "Double-Bladed Axe" and "da" for "Dagger" since "d" is ambiguous.
Using chooselist2 is similar, it simply lists the choices each on its separate line and asks for input.
Finally, choosemenu does the same, but numbers the line and the choice can be made using either the number or an unambiguous start-string.
The response will always be in response
The easiest way seems to be doing it through the findstr command.
:Choice
set /p weapon=What is your weapon? (Sword, Double-Bladed Axe, Dagger):
echo %weapon%| findstr /r "^Sword$ ^Double-Bladed Axe$ ^Dagger$">nul
if errorlevel 1 (
echo %weapon% was sadly not an option.
goto :Choice
)
echo you chose %weapon%.
(you need ^ and $. ^ means start of line, $ means end of line)
OR
if you want to use that system several times you could use findstr with parameters and a return value
:MakeChoice
set /p answer= :
echo %answer%| findstr /r "^%~2$ ^%~3$ ^%~4$ ^%~5$ ">nul
if errorlevel 1 (
echo "%answer%" is sadly not a valid choice, choose again.
goto :MakeChoice
)
set %~1=%answer%
goto :eof
You can than call the function this way:
echo choose a weapon(Sword, Axe, Dagger)
call :MakeChoice chosenOption "Sword" "Double-Bladed Axe" "Dagger"
echo you chose %chosenOption%
This works with 1-4 variables, but if you want more, you can always add ^%~6$ ^%~7$... after ^%~5$.