Im making an rpg game in batch, and here is the code for the stat system so far
:SPSPEND
CLS
Echo You have %SP% stat points to spend, every stat point used increases that skill by 5.
Echo 1. Strength=%Strength%
Echo 2. Agility=%Agility%
Echo 3. Magic=%Magic%
Echo 4. Vitality=%MaxHP%
Echo 5. Defence=%Defence%
Echo 6. Archery=%Archery%
set /p choice=Choose what to put points into:
if %choice%=="1" Set /a Strength==%strength%+5
if %choice%=="2"
if %choice%=="3"
if %choice%=="4"
if %choice%=="5"
if %choice%=="6"
cls
set /a SP==%SP%-1
goto :rest
Not sure how to format soz. i need help with two things : how can i make it so that it doesnt close out and actually works, and how can i make a failsafe incase a person enters something other than what i already have set up.
Thanks in advance
I believe this is what you want.
I have tested it and am fairly certain it will work
:SPSPEND
CLS
if %SP% leq 0 goto end
Echo You have %SP% stat points to spend, every stat point used increases that skill by 5.
Echo 1. Strength=%Strength%
Echo 2. Agility=%Agility%
Echo 3. Magic=%Magic%
Echo 4. Vitality=%MaxHP%
Echo 5. Defence=%Defence%
Echo 6. Archery=%Archery%
set /p choice=Choose what to put points into:
if "%choice%"=="1" Set /a Strength=%Strength%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="2" Set /a Agility=%Agility%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="3" Set /a Magic=%Magic%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="4" Set /a MaxHP=%MaxHP%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="5" Set /a Defence=%Defence%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="6" Set /a Archery=%Archery%+5& set /a SP=%SP%-1& goto SPSPEND
echo invalid choice
pause
goto SPSPEND
cls
:end
pause
goto :rest
Related
The Problem:
I'm trying to create a batch file which randomly generates passwords, but I cannot seem to get the generated passwords to format properly.
Before, I just chose to generate numbers, but I realized that it wasn't very secure for users, and I needed to include letters and numbers. I attempted, but it output the wrong thing (kind of).
I want the generated passwords to be at least 8 characters long, and 32 characters maximum. I tried it, but it completely broke the code and it wouldn't output anything at all. Also, it outputs the same passwords for each one on every option. For example, if I chose option 1 (generate 2 passwords), it would output ud8aqas and ud8aqas. If I chose option 2 (generate 5 passwords), it would output p9dkda, p9dkda, p9dkda, p9dkda, and p9dkda, and so on.
The code that I want to use:
#echo off
:Start2
cls
goto Start
:Start
title DotPass Password Generator [BETA]
echo Thanks for using DotPass! If you have any questions, feel free to email us at mhartmix#gmail.com.
echo Please maximise this program so you can see all of the details we have shown!
echo ------------------------------------------------------------------------------------------------------
echo DotPass will generate you a new PIN (or password, if that's what you're going to use it for).
echo You can use these for any of your online accounts! Remember to always note the PIN/password down somewhere!
echo -------------------------------------------------------------------------------------------------------
echo Note: The passwords/PIN's can only be seen by you, not us. To read more, go to github.com/TechGeekMic/DotPass/blob/Main/security.md
echo Passwords generated are never stored on the cloud, or locally, so that's why you should make a note of your randomly generated PIN/password.
echo -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
echo How many passwords do you want DotPass to generate?
echo 1) 2 Generated Random Password
echo 2) 5 Generated Random Passwords
echo 3) 10 Generated Random Passwords
echo Type the number of your choice below. ( i.e 2 )
set input=
set /p input= Choice:
if %input%==1 goto A if NOT goto Start2
if %input%==2 goto B if NOT goto Start2
if %input%==3 goto C if NOT goto Start2
:A
cls
Setlocal EnableDelayedExpansion
Set _RNDLength=12
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
echo Your two randomly generated passwords are !_RndAlphaNum! and !_RndAlphaNum!
)
echo What do you want do to? Here are a few options.
echo 1) Go back to the beginning
echo 2) Exit DotPass
echo 3) Shutdown your computer
echo Type the number of your choice below. ( i.e 1 )
set input=
set /p input= Choice:
if %input%==1 goto Start2 if NOT goto Start 2
if %input%==2 goto Exit if NOT goto Start 2
if %input%==3 goto Shutdown if NOT goto Start 2
:Shutdown
shutdown
:Exit
exit
:B
cls
Setlocal EnableDelayedExpansion
Set _RNDLength=12
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
echo Your five randomly generated passwords for you are !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum! and !_RndAlphaNum!
)
echo What do you want do to? Here are a few options.
echo 1) Go back to the beginning and start again
echo 2) Exit DotPass
echo Type the number of your choice below. ( i.e 1 )
set input=
set /p input= Choice:
if %input%==1 goto Start2 if NOT goto Start 2
if %input%==2 goto Exit if NOT goto Start 2
if %input%==3 goto Shutdown if NOT goto Start 2
:C
cls
Setlocal EnableDelayedExpansion
Set _RNDLength=12
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
echo Your ten randomly generated passwords for you are !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum! and !_RndAlphaNum!
echo What do you want do to? Here are a few options.
echo 1) Start again
echo 2) Exit DotPass
set input=
set /p input= Choice:
if %input%==1 goto Start2 if NOT goto Start 2
if %input%==2 goto Exit if NOT goto Start 2
if %input%==3 goto Shutdown if NOT goto Start 2
You can try it out for yourself as long as you don't publish it or modify it.
I am very new to batch scripts! Can someone help?
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
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.
%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$.