Batch counter program shutting down - batch-file

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 - :-)

Related

Making a batch DND assistant

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.

Batch: How to prevent looping code when holding enter

So iv'e made a little something here where there will be a list of numbers like 1-10 and whichever one you type (so far only 1) will show you information (so if i type and enter 1, it will go key1=key).
However, if I type 1 and click enter, it shows me that the Key1 = key which i intended but then if I hold down Enter key, it will keep looping the Key1=Key script and the homepage one, can this be stopped?
#echo off
:T
cls
echo Home Page
echo [1] NameA
echo.
echo [e] Exit
set /p word=
if "%word%"=="1" goto aaa
if "%word%"=="e" goto eee
goto T
:aaa
cls
echo Key1 = Key
pause
goto T
:eee
exit
set /p keeps the last value, if you just press enter. You should set the variable to empty (or a default value, if you prefer):
#echo off
:loop
REM set "word=" ; for "empty"
set "word=string"
set /p "word=Enter your choice (or just ENTER for '%word%': "
echo you choosed %word%
goto :loop
May I bring your attention to the choice command, which may (or may not) be a better choice.
#echo off
setlocal
:T
cls
echo Home Page
echo [1] NameA
echo.
echo [e] Exit
rem Undefine variable word before input.
set "word="
set /p "word="
rem No input = word is not set a new value, thus not defined.
if not defined word goto eee
if "%word%"=="1" goto aaa
if "%word%"=="e" goto eee
goto T
:aaa
cls
echo Key1 = Key
pause
goto T
:eee
exit
If you only press Enter at the set /p prompt, variable word is not set (a new value).
If word is undefined before the set /p input, then it will be undefined after,
if you only press Enter.
setlocal added to keep variables set local to the current script
execution.

set /a in If statement condition unable to work

Below are my simple calculator batch i trying to do, however on the set /a unable to do the job. What is the problem and my mistake?
#echo off
Title Calculator
:start
echo Press 1 for "+"
echo Press 2 for Exit
echo.
set /p input="Please choose your option: "
if %input%==1 (
echo.
set /p num1="Please enter first number: "
set /p num2="Please enter second number: "
set /a ans=%num1%+%num2%
echo Your answer is %ans%
pause
cls
goto start
)
if %input%==2 (
echo.
echo Thanks!
pause
exit
) else echo Invalid input!
pause
goto start
When i first run the batch is will return me the Missing operand. When i continue again the error disappear without giving me the answer, when the third time i continue, it return me the answer of the number that i wanted to add up.
For example:
1. 10+10 result is Missing operand
2. 1+1 result is empty
3. 2+2 result is 20 (which is the 2 number i enter at first time)
Please help what my error is. Thanks.
Here is your batch code with using delayed expansion and indents as wisely suggested by rojo:
#echo off
title Calculator
setlocal EnableExtensions EnableDelayedExpansion
:Begin
echo Press 1 for "+"
echo Press 2 for Exit
echo/
set "input="
set /P "input=Please choose your option: "
if "!input!"=="1" (
echo/
set /P "num1=Please enter first number: "
set /P "num2=Please enter second number: "
set /A ans=num1+num2
echo Your answer is !ans!
pause
cls
goto Begin
)
if "!input!"=="2" (
echo/
echo Thanks^^!
echo/
pause
exit /B
)
echo Invalid input^^!
echo/
pause
echo/
goto Begin
Variable input is always cleared before user is asked because otherwise the user could just hit key RETURN or ENTER to keep current value of variable input.
Delayed expansion is used on checking user input against 1 or 2 in case of user enters a character which would result in a syntax error on execution with not using delayed expansion. Better would be nevertheless the usage of command choice for first user prompt.
For an expression evaluated on runtime by using set /A the environment variables can be specified directly without being expanded at all. So instead of using
set /A ans=%num1%+%num2%
or
set /A ans=!num1!+!num2!
it is enough to write
set /A ans=num1+num2
because with parameter /A command set interprets num1 and num2 automatically as names of variables.
Delayed expansion is nevertheless needed to print the result value stored in variable ans because command processor expands otherwise %ans% by nothing respectively the value of previous run on parsing the entire block starting with ( and ending with ). This can be seen on running your batch file from within a command prompt window with first line changed to #echo on for debugging.
For more details run in a command prompt window set /? or help set and read the output help pages.
start is the name of a command. Therefore it is not good to use this word as name of a label although possible.
By the way: Always use set "variable=value" and never set variable="value" as this makes a big difference, see the answers on
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
How to set environment variables with spaces?

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.

Batch roguelike errors

I am trying to make a batch roguelike but I am running in to three main problems. First of all my message system is not working. Second there is a glitch where if i try to move somewhere where I can't the choice statement keeps writing the W, S, A, and D's at the bottom. Third and most importantly there is a very annoying flashing of the screen. I tried to minimize the time it takes to load but to no avail. Here is my code:
#echo off
setlocal enableextensions
mode con: cols=54 lines=30
set num=1
set nextVar=1
set oldVar=103
set message=a
cls
echo Loading...
:startup
if not %num%==1001 (
set b%num%=.
set /a num = %num% + 1
goto startup
)
cls
set b103=#
goto update
:update
choice /c wsad /n /m ""
if %errorlevel%==1 set /a nextVar = %oldVar% - 50
if %errorlevel%==2 set /a nextVar = %oldVar% + 50
if %errorlevel%==3 set /a nextVar = %oldVar% - 1
if %errorlevel%==4 set /a nextVar = %oldVar% + 1
if defined b%nextVar% (
set b%nextVar%=#
set b%oldVar%=.
set oldVar=%nextVar%
set message="a"
goto display
)
set message="You can not move there."
goto update
:display
set ln1=%b1%%b2%%b3%...
set ln2=%b51%%b52%%b53%...
set ln3=%b101%%b102%...
...
...
...
set ln8=%b351%%b352%...
set ln18=%b851%%b852%%b853%
set ln19=%b901%%b902%%b903%%b904%...
set ln20=%b951%%b952%%b953%%b954%%b955%%b956%...%b1000%
cls
if not "%message%"=="a" (
echo %message%
goto next
)
echo.
:next
echo.
echo %ln1%
echo %ln2%
echo %ln3%
echo %ln4%
echo %ln5%
echo %ln6%
echo %ln7%
echo %ln8%
echo %ln9%
echo %ln10%
echo %ln11%
echo %ln12%
echo %ln13%
echo %ln14%
echo %ln15%
echo %ln16%
echo %ln17%
echo %ln18%
echo %ln19%
echo %ln20%
echo.
goto update
I had to ... some sections to save space. If anyone can solve these problems in particularity the screen flashing that would be super helpful. Also if anyone has ideas on generation NetHack like dungeons that would also be appreciated. Thank you.
So problems 1 and 2 are related.
First of all my message system is not working. Second there is a glitch where if i try to move somewhere where I can't the choice statement keeps writing the W, S, A, and D's at the bottom.
Change the error goto statement for the update section. You want to go to the display section so the screen is redrawn (removing the old used choice prompt) and the error message is displayed.
set message="You can not move there."
goto display
Problem 3 is a bit more difficult due to the nature of batch.
Third and most importantly there is a very annoying flashing of the screen.
The flashing is caused primarily because of one command. The cls command. Instead of just redrawing the screen it is first causing the screen to turn black before the new output is drawn causing the flicker.
Step 1: Remove the cls command. This means that you will have to output 30 lines each time to fill the screen.
Reducing the flicker even more will require a bigger overhaul on your script. Instead of echoing each line use the natural ability of the command window to wrap lines to draw the whole screen. This will reduce the amount of echo calls to a couple. Your canvas is set to 54 x 30 so that equals 1620 characters to fill the whole screen at one time.
Step 2: Output the entire screen with a single or as few calls as possible to prevent drawing/scrolling delay and use the natural line wrapping ability of the console window.
Here is an example I put together to illustrate this second step. Let me know if you have any questions.
#echo off
( if "%~1"=="" start "" /HIGH %0 1 & exit /b 1 )
setlocal enabledelayedexpansion
mode con: cols=80 lines=25
set "mark=0"
set "switch=0"
:next
if %mark% geq 920 set "mark=0" & if %switch% equ 0 ( set "switch=1" ) else set "switch=0"
set top=........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
set bot=........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
if %switch% equ 0 set top=!top:~0,%mark%!^|!top:~%mark%!
if %switch% equ 1 set bot=!bot:~0,%mark%!^|!bot:~%mark%!
goto display
:display
echo %mark%
set /p "=%top:~0,920%" <nul
set /p "=%bot:~0,920%" <nul
ping 192.0.2.2 -n 1 -w 200 >nul
set /a mark+=1
goto next
Step 3: An additional step that helps would be to increase the processes priority level. I have also added this to my example script above on the second line.

Resources