Making a batch DND assistant - batch-file

I don't know if any of you guys know anything about batch (I am sure someone does), but in regards to this post, I am trying to create a dumbed down DND Assist (something that you would tell you stats to and that would assist you in completing an action irl faster than rolling dice and doing the math yourself.
Currently I have the random num generator somewhat working (although I would like to improve it)
This version is set to choose a random number between 1 and 20, I would like to figure out a way to have the program notice if you roll a 1 or a 20 (Crit hits / crit fail)
Also I need something later that will show me how to save certain values as variables,
#echo off
:Start
Set /a ans="%RANDOM% %% 20"+1
echo %ans%
pause
goto Start
(In regards to saving variables, when the program is ran, it will tell me that I am missing an operation.)
The coloring portion was just for the hecks of it, if someone can show me a way to streamline that section please tell.
Please use lamens terms, Im still not very good at understanding any of this.
(CURRENT ASSIST PROGRAM PROGRESS)
#echo off
cls
:BEGIN
Echo HI THERE! AND WELCOME TO MY GAME!!
Echo Lets begin by setting your prefered color!
:A
set choice=
set /p choice= RED, WHITE, OR BLUE?!?
if not '%choice%'=='' set '%choice%'=='Red, White, Blue'
if '%choice%'=='RED' goto RED
if '%choice%'=='WHITE' goto WHITE
if '%choice%'=='BLUE' goto BLUE
if '%choice%'=='Red' goto RED
if '%choice%'=='White' goto WHITE
if '%choice%'=='Blue' goto BLUE
if '%choice%'=='red' goto RED
if '%choice%'=='white' goto WHITE
if '%choice%'=='blue' goto BLUE
if '%choice%'=='9' goto 1Bs
echo "%choice%" is not a good color bro, do a different one
goto A
:RED
color 4
goto START SCREEN
:WHITE
color 7
goto START SCREEN
:BLUE
color 1
goto START SCREEN
:START SCREEN
cls
TITLE CHOOSER GAME BOI
Echo ---THE DND GAME---
echo Welcome to the DND game, we will first choose your Attributes
echo Strength (How hard you hit) (STR)
echo Constitution (Your health) (CNST)
echo Knowledge (Better Rolls against Vendors and Questions) (KNLG)
echo Dexterity (Your chances of dodging and Hitting) (DXT)
echo You have a total of 10 points to apply to each Attribute
echo Your points HAVE to equal 10 otherwise you will have to restart
:ATTRSET
set MXPNTS=10
set choice=
set /p STR= STR (1-10)
set choice=
set /p CNST= CNST (1-10)
set CNST=CNST
set choice=
set /p KNLG= KNLG (1-10)
set KNLG=KNLG
set choice=
set /p DXT= DXT (1-10)
set DXT=DXT
set /a ATTRTTL=STR+CNST+KNLG+DXT
echo ATTRTTL
if NOT ATTRTTL=MXPNTS goto ATTRSET
if ATTRTTL=MXPNTS goto testyay
pause
:testyay
pause

I would personally suggest:
#echo off
cls
:begin
echo HI THERE! AND WELCOME TO MY GAME!!
echo Lets begin by setting your preferred color!
:a
set /p choice= RED, WHITE, OR BLUE?!?
if "%choice%" == "" (
echo Please enter something!
cls
goto :A
)
for %%A IN (red white blue) do if /I "%choice%" == "%%A" (call :%%A & goto :start_screen)
if "%choice%" == "9" (goto 1Bs)
echo "%choice%" is not a good color bro, do a different one.
goto :a
:red
color 4
exit /b
:white
color 7
exit /b
:blue
color 1
exit /b
:start_screen
cls
title CHOOSER GAME BOI
echo ---THE DND GAME---
echo Welcome to the DND game, we will first choose your Attributes
echo Strength (How hard you hit) (STR)
echo Constitution (Your health) (CNST)
echo Knowledge (Better Rolls against Vendors and Questions) (KNLG)
echo Dexterity (Your chances of dodging and Hitting) (DXT)
echo You have a total of 10 points to apply to each Attribute
echo Your points HAVE to equal 10 otherwise you will have to restart
:ATTRSET
set "mxpnts=10"
set /p "str=STR (1-10) "
set /p "cnst=CNST (1-10) "
set /p "knlg=KNLG (1-10) "
set /p "dxt=DXT (1-10) "
set /a "attrttl=str+cnst+knlg+dxt"
echo %ATTRTTL%
if not "%attrttl%" == "%mxpnts%" (goto :attrset) else (goto :testyay)
pause
:testyay
pause
Capitalization detected! Successfully removed! As batch is a case insensitive language, uppercase letters would make noise and make reader close the tab with your question and move on - or at least me.
The whole thing about the choice variable you did was not needed. Just a for loop looping through the color words and checking (case insensitive) if the user input was red, white or blue.
I have decided to call subroutines, not goto to them to save some lines - I usually do it to my programs: you had put 3 separate commands goto START SCREEN which could be simplified calling the subroutine (which means goto to it, but then return) and then goto where you want.
Remember: it is not good for your files/folders to contain a space in their name. This can cause quite many misbehaviours. It is just the same in all languages: when you name variables, functions, subroutines or whatever, don't include spaces! I have renamed it to start_screen.
That's all, follow excellent suggestions by Squashman in comments and read the help of some commands in cmd, typing command /? and you should be fine.

Related

Batch counter program shutting down

So I am making a counter and I am not sure how to make it work.. I have this right now with some other functions for customization purposes:
set /a current_value=current_value+incremental_value
but it does not work unfortunately..
The whole purpose is to use the pause >nul function so when ever the user presses a key then the screen will show a number go up by the incremental value chosen earlier..
This is the whole script:
#echo off
cls
title Counter
:Incremental_Value
cls
echo./----------------------------------------------\
echo.I Set the Incremental Value then press Enter I
echo.\----------------------------------------------/
echo.
set /p %incremental_value%= [
:Starter_Value
cls
set current_value=%starter_value%
echo./------------------------------------------\
echo.I Set the Starter Value then press Enter I
echo.\------------------------------------------/
echo.
set /p %starter_value%= [
goto Counter
:Counter
cls
echo./-------------------\
echo.I %current_value% I
echo.\-------------------/
echo.
pause >nul
set /a current_value=current_value+incremental_value
goto Counter
Edit: I fixed the shutting down problem, but when you first get to the Counter screen the number does not appear. Once you hit a key it becomes zero (if you set the starting value to zero) then it wont add the incremental value if you continue to press the key.
A very simple issue you had was the improper use of the set /p command. When using set /p, you do not specify the string as set /p %String%= but rather set /p String=. For more information on the set command try typing set /? into a command prompt.
Another issue, not problem is that you have :Incremental_Value & :Starter_Value but you never call or goto them in the script. The only place you properly did this was with goto Counter. Unless you are going to individually goto/call them later, just remove them; or use goto :Starter_Value - exc.
In the future, try using echo( instead of echo. to call a blank space in the window.
Counter.bat
#echo off
title Counter With Incremental Progression
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º Set the Starter Value then press Enter º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo(
set /p starter_value=Value:
cls
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º Set the Incremental Value then press Enter º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo(
set /p incremental_value=Value:
Set "current_value=%starter_value%"
:Counter
cls
echo Current Number: %current_value%
echo(
pause >nul
set /a "current_value=current_value+incremental_value"
goto Counter
PS: Switch the file encoding to ANSI for fun boxes - :-)

Why is my batch file showing all variables

I am coding a text-based batch rpg, but the code reads all variables formatted as :VARIABLE
echo
set
if
etc.
as if it was a sequence of code.
CODE:
:start
#echo off
set /p name=Welcome to the game, what is your name?:
if %name%==ChrisPBacon goto crispy
ping localhost -n 2 >nul
cls
goto selectclass
:WARRIORSELECT1
cls
set /p question1=Your class is Warrior, is this correct Y/N?:
if %question1%==Y cls goto skillselect1
if %question1%==N cls goto selectclass
:PALADINSELECT1
cls
set /p question1=Your class is Paladin, is this correct Y/N?:
if %question1%==Y cls goto skillselect1
if %question1%==N cls goto selectclass
:MAGESELECT1
cls
set /p question1=Your class is Mage, is this correct Y/N?:
if %question1%==Y cls goto skillselect1
if %question1%==N cls goto selectclass
:CLERICSELECT1
cls
set /p question1=Your class is Cleric, is this correct Y/N?:
if %question1%==Y cls goto clerichealer
if %question1%==N cls goto selectclass
:selectclass
echo Welcome to the game, %name%!
ping localhost -n 2 >nul
echo Warrior
echo Paladin
echo Mage
echo Cleric
set /p CLASS=What class would you like to be?:
if %CLASS%==Warrior goto WARRIORSELECT1
if %CLASS%==Paladin goto PALADINSELECT1
if %CLASS%==Mage goto MAGESELECT1
if %CLASS%==Cleric goto CLERICSELECT1
:clerichealer
set /p surecleric=Are you sure you wish to be the Cleric? This class does 0
damage and can only heal Y/N!:
if %surecleric%==Y cls goto skillselect1
if %surecleric%==N cls goto selectclass
:skillselect1
echo Welcome to the skill selection menu, %Name% the %CLASS% From here you
can select your initial skills, with 10 points to spend at first and more
can be gained by levelling up!
echo Weight (W)
echo Attack Damage (AD)
echo Magic Damage (MD)
echo Healing Effectiveness (HE)
set /p point1 What attribute do you wish to level up with your 1st point?
if point1==W echo Increased weight by +10!
if point1==AD echo Increased Attack Damage by +20!
if point1==MD echo Increased Magic Damage by +15!
if point1==HE echo Increased Healing Effectiveness by +10 Health!
:crispy
echo Welcome Admin!
echo
echo 1) Force Delete current save
echo 2) Implement Preset save
set /p bacon=What do you wish to do?
In particular, when any class but Cleric is selected, it loops through all classes asking if this is your selected class. When Cleric is selected, it loops back to Welcome to the game, %name% etc.
The thing that makes it loop when you select cleric is that it just goes on, even when you enter "n" (instead of "N"), as SteveFest said.
But there are many issues with your code:
It doesn't account for incorrect or empty input
The if comparison is case-sensitive
no quotes for the if comparison
It's obvious it's going to get much worse with time if you keep using too many goto statements
Take a look in this snippet.
:CLERICSELECT1
cls
set /p question1=Your class is Cleric, is this correct Y/N?:
if %question1%==Y cls goto clerichealer
if %question1%==N cls goto selectclass
:selectclass
If your selection is not Y or N, it will automatically go on, and reach :selectclass. This problem occurs throughout the entire script, so fix them first.
Another thing that most new batch scripts make is the multicommand in one if statement. You could use
if "a"=="b" cls && goto blah
or
if "a"=="b" (
cls
goto blah
)
Notice the quotes " I've added, as quotes will help deal with variables with spaces.
Optionally, you can add the /i flag to if, which means case-insensitive search.
When we want to echo a blank line, we don't only use echo, instead, we use echo(, though there are some less safer methods.
Some variables are not wrapped in % signs.
Multi-line echo requires multi echos unless you want for loops
There was one missing equal sign in one of the set /p statement
Storing passwords directly in a batch file isn't safe, even after making it a .exe file, as most bat to exe converter does make a temporary bat file in the temp directory, which can be easily accessed by malicious users.

Something wrong with my batch script?

I'm having to do a project on figurative language in Maroon 5 music, and decided to make a quiz. I am trying to be able to count how many questions were right. Here is my work so far, and there is a bug somewhere, but I don't know what it is.
#echo off
color 0f
:START
set Q=Q
cls
echo Maroon 5 Figuratve Language
echo ---------------------------
echo Options (Type a number and press ENTER):
echo 1) Take quiz
echo 2) Change color scheme
set /p Choice=
if %Choice% == 1 GOTO GAME
if %Choice% == 2 GOTO COLOR
:COLOR
cls
echo Choose a color scheme.
echo 1) Black BG, White text
echo 2) White BG, black text
set /p color=
if %color% == 1 GOTO 0F
if %color% == 2 GOTO F0
:0F
color 0f
GOTO start
:F0
color f0
GOTO start
:GAME
set qr=0
cls
goto Q1
:Q1
set qn=1
cls
echo What type of figurative language is "I can smell your scent from miles?"
echo a) Simile
echo b) Hyperbole
echo c) Metaphor
echo d) Personification
set /p Q1=
if %Q1% == b goto CORRECT
if NOT %Q1% == b goto INCORRECT
:CORRECT
set /a qr=%qr%+1
set /a qn=%qn%+1
goto %Q%%qn%
:INCORRECT
cls
echo Incorrect...
pause
set /a qn=%qn%+1
goto %Q%%qn%
:Q2
echo testing
echo %qn%
echo %qr%
pause
I can see a bug (there might be more in the code but this one is the first I can see ^^)! :D It's in the line goto %Q%%qn%. I've answered a similar question time ago: How to put variable value inside another variable name in batch?
The point is that as your variables are being evaluated at parse time the interpreter doesn't know what's the value of %Q% and %qn%. You have to add SETLOCAL EnableDelayedExpansion at the beginning of your script and surround your variables with !...! instead of %...%. This will force the interpreter to evaluate the values at run time considering any changes made to the variables so far.

My text based game script is not working

I am making a text based rpg and the script is not working. It has something to do with the stamina. I also have a quest that is dependent on you having a certain amount of gold and that script is not working as well. I will include pictures.
:harvest
cls
echo Press 1) to harvest
set /p input17=enter:
if %input17%==1 set /a wheat= %wheat% + 5
if %input17%==1 set /a carrots= %carrots% +4
if %input17%==1 set /a stamina= %stamina% - 25 (this line)
if %stamina% < 0 goto nostamina (this line)
echo.
echo You get some wheat and some carrots.
echo.
echo check your inventory for accurate numbers.
echo.
echo Press 1) to go back.
pause >nul
goto insidehouse
:insidehouse
cls
echo You are now inside of your house.
echo.
echo Press 1) to harvest.
echo Press 2) to sell all crops.
echo Press 3) to go into your inventory.
echo Press 4) to sleep eight hours.
echo Press 5) to check for quests.
set /p input16=enter:
if %input16% EQU 1 goto harvest
if %input16% EQU 2 goto market
if %input16% EQU 3 goto Inventory1
if %input16% EQU 4 goto sleep
if %input16% EQU 5 (and) if %gold% LSS 0 goto shopping (this line)
You haven't provided much code to work with so, I can only guess at a solution.
My best guess is that you are atempting to update a variable from inside a for loop. If this is the case you need to add this line to the top of your batch file: setlocal enabledelayedexpansion. You will also need to access the affected variables like this !var! instead of this %var%.
setlocal enabledelayedexpansion causes expansion of variables to delayed in your batch file. What this will mean in the context of your program is that variables can be updated from within a for loop.

How to find keywords of the users input in the command line?

What I'm looking for is a batch file line of code that will scan what the user inputs to find key words and direct them in the right place. That way when a trainee has a question he/she could just ask the batch file and it will direct them to the proper menu. Is this possible? if so, How would one go about doing this?
:menu
set /p c=Please type you question:
findstr /m "How to ringout a product on our system?" "%c%"
if %c%=="ringout" (
goto :Ringingout
) Else (
goto :Sorry
)
:Ringingout
cls
echo In order to right something out maksure you do the following:
echo - Log in
echo - click on scan in the bottom left had corner on the tender page
echo - scan items
echo - click continue
Pause
goto :Menu
:Sorry
cls
echo Sorry I don't recognize your question, please re-word it.
pause
goto :Menu
#ECHO OFF
SETLOCAL
SET "keywords=word anotherword someword"
FOR %%k IN (%keywords%) DO set "#%%k="
SET /p query="Please enter your query ? "
FOR %%k IN (%keywords%) DO CALL :analyse %%k
SET #
GOTO :EOF
:analyse
CALL SET "found=%%query:%1=%%"
IF "%found%"=="%query%" GOTO :EOF
SET #%1=FOUND!
GOTO :eof
Here's a general way to do it.
If one of the keywords is entered, the variable #keyword will be set to FOUND! so you can use if defined #keyword to process from there.
It's not protected against destructive user-inputs - that's not what this question is about...
You'd be better off collecting all the questions in a document (like .html) and letting the user search that document for what they need. But if this is just an exercise, you can re-write your logic like so to make your program work:
:menu
set "c="
set /p "c=Please type your question: "
echo %c% | findstr /i /b "ringout" >nul
if errorlevel 1 goto Sorry else goto Ringingout
:Ringingout
and so on.

Resources