I am trying to make a simple RPG game using batch, and for "attacks" that take away both enemy health and "SP". Here is my current code.
#echo off
set /a HP=10
set /a SP=10
echo hp = %HP%
echo SP = %SP%
pause nul
set /p DC= 10 max
set /a NHP=%HP% - %DC%
set /a HP=%NHP%
echo %HP%
pause nul
set /a DCE=10
echo you have %HP% hp
Echo You can 1. Hit (-1 hp to enemy) 2. Kick (-1 hp to enemy) 3 special hit (-2 hp to enemy and -5 SP to you)
Note that HP=Health points and SP= Special points or points that you use for special attacks
And also, The beginning code is just to test a system to take away points., So if you have a better system for that. It would be helpful
set /a EHP = 10
set /a BaseDamage = 1
set /a SpecialDamage = 2
set /a SpecialCost = 5
set /p input = .
if %input% == 1 or %input% == hit (
echo you hit the enemy
set /a EHP = %EHP% - %BaseDamage%
)
if %input% == 2 or %input% == kick (
echo you kick the enemy
set /a EHP = %EHP% - %BaseDamage%
)
if %input% == 3 or %input% == special hit (
echo [insert dialoge here]
set /a EHP = %EHP% - %SpecialDamage%
set /a SP = %SP% - %SpecialCost%
)
goto a
by the way you should probably add :a before echo You can 1.Hit 2.Kick 3.special hit
Related
So, couple of problems I'm having.
When the TutorialFight runs, it works almost as I want it, except that the hero never gets hit.
When choosing Magic or Run, the if statements do not display the echos, and goes to the TutorialError.
Note: %stat% is the hero's stat, while %estat% is the enemy's stat.
:TutorialIntro
title Tutorial^^!
cls
set /a ehealth=15
set /a emana=15
set /a ebstat=5
set /a eostat=5
set /a ecstat=5
set /a elevel=1
set /a health=15
set /a mana=15
set /a bstat=5
set /a ostat=5
set /a cstat=5
set /a level=1
echo You have run into a
echo Level %elevel% Sock Puppet.
echo -I made this from a combination of a sock, glue, and some paper.
echo If you lose to this thing, I'm not looking you in the eye again.
echo You know, if I could actually look at you in the first place.
echo.
echo Level %level% Hero
echo %health% HP
echo %mana% MP
echo %bstat% Brawn
echo %ostat% Obscurity
echo %cstat% Cowardice
pause
goto Tutorial
:Tutorial
title Fight^^!
cls
if %ehealth% leq 0 goto TutorialWin
if %health% leq 0 goto TutorialLoss
echo Level %elevel% Sock Puppet.
echo (%ehealth% HP)
echo.
echo V.S.
echo.
echo Level %level% Hero
echo %health% HP
echo %mana% MP
echo %bstat% Brawn
echo %ostat% Obscurity
echo %cstat% Cowardice
echo.
set /p answer= Fight(1), Magic(2), Run(3)
if %answer%==1 goto TutorialFight
if %answer%==2 echo Sorry. You don't know magic yet.
if %answer%==3 echo No. You are not going to run from a sock.
goto TutorialError
:TutorialFight
set /a loss=0
set /a eloss=0
set /a num=%random% * ((%cstat%-%ecstat%) - 1 + 1) / 32768 + 1
if %num% lss (%ecstat%/2) set /a loss=%random% * (%ebstat% - (%ebstat%/2) + 1) / 32768 + (%ebstat%)/2)
if %num% geq (%ecstat%/2) set /a eloss=%random% * (%bstat% - (%bstat%/2) + 1) / 32768 + (%bstat%)/2)
set /a health=(%health% - %loss%)
set /a ehealth=(%ehealth% - %eloss%)
goto Tutorial
:TutorialError
title Huh?
cls
echo Try that again. I believe in you.
pause
goto Tutorial
:TutorialWin
title Victory
cls
echo Yay^^!
echo Now I think you are ready for the big pond^^!
pause
goto Intro
:TutorialLoss
title ...
cls
echo ...
echo.
echo Let's forget that ever happened.
pause
goto Menu
Use an additional calculation:
Set/A halfecstat=ecstat / 2
Then change the line from:
if %num% lss (%ecstat%/2)
to:
If %num% Lss %halfecstat%
Also I've noted that the calculation which follows it has unbalanced parentheses so you may want to give that another careful look too.
I am designing a text game in a batch file. When my code reaches the point where I need to input a number to decide what action to perform, the Command Prompt window simply closes.
#echo off
set /a EHP = 10
set /a BaseDamage = 1
set /a SpecialDamage = 2
set /a SpecialCost = 5
set /p input = .
if %input% == 1 or %input% == hit (
echo you hit the enemy
set /a EHP = %EHP% - %BaseDamage%
)
if %input% == 2 or %input% == kick (
echo you kick the enemy
set /a EHP = %EHP% - %BaseDamage%
)
if %input% == 3 or %input% == special hit (
echo [insert dialoge here]
set /a EHP = %EHP% - %SpecialDamage%
set /a SP = %SP% - %SpecialCost%
)
goto a
I couldn't get %difference_two% to work It can find %difference_one% as 2 but when I make it divide 2 by 2 it doesn't display as anything.
Use the numbers 2,5,10,17,26
To get what I need.
#echo off
color 0a
title Solver
:numbers
cls
set /p first=First:
set /p second=Second:
set /p third=Third:
set /p fourth=Fourth:
set /p fifth=Fifth:
goto solve
:solve
cls
set /a second_minus_first= %second% - %first%
set /a third_minus_second= %third% - %second%
if %third_minus_second%==%second_minus_first% (
goto s
) else (
goto d
)
:d
cls
set /a fourth_minus_third= %fourth% - %third%
set /a difference= %third_minus_second% - %second_minus_first%
set /a difference_one= %fourth_minus_third% - %third_minus_second%
if %difference%==%difference_one% (
set /a difference_two= %difference_one% / 2
set /a thing= %first% - %difference_two%
cls
echo %difference_two%n Squared + %thing%
pause >nul
goto numbers
) else (
goto wrong
)
Try this:
#echo off
setlocal EnableDelayedExpansion
color 0a
title Solver
:numbers
cls
set /p first=First:
set /p second=Second:
set /p third=Third:
set /p fourth=Fourth:
set /p fifth=Fifth:
goto solve
:solve
cls
set /a second_minus_first= %second% - %first%
set /a third_minus_second= %third% - %second%
if %third_minus_second%==%second_minus_first% (
goto s
) else (
goto d
)
:d
cls
set /a fourth_minus_third= %fourth% - %third%
set /a difference= %third_minus_second% - %second_minus_first%
set /a difference_one= %fourth_minus_third% - %third_minus_second%
if %difference%==%difference_one% (
set /a difference_two= !difference_one! / 2
set /a thing= !first! - !difference_two!
cls
echo !difference_two!n Squared + !thing!
pause >nul
goto numbers
) else (
goto wrong
)
In batch, commands in a code block (between ( )) are interpreted as on the same line, so you need to use setlocal EnableDelayedExpansion and use ! instead of % inside them.
I'm not sure what is happening as batch script closes almost immediately when I try to run it. It's pretty simple it should test whether a number(num) is a prime Number. If not it continues until num1 is bigger than num.
echo off
title Prime Numbers
cls
set /a prime=7
set /a num1=2
set /a num2=2
:do1
if %prime% == %num1% * %num2% goto do_if_1
if else %prime% LSS %num1% * %num2% set /a num2=%num2%+1
if else %prime% GTR %num1% * %num2% goto do_if_2
if else %prime% LSS %num1% * 1 goto do_if_3
goto do1
:do_if_1
set /a prime=%prime%+1
set /a num1=2
set /a num2=2
goto do1
:do_if_2
set /a num1=%num1%+1
set /a num2=2
goto do2
:do_if_3
echo %prime%
goto do_if_1
Edit: 6n±1 method
In case it's useful to anyone, here's a much more efficient method for testing whether a number is prime or composite. It's a Batch language adaptation of the 6n±1 algorithm mentioned by several people on this post.
#echo off & setlocal enabledelayedexpansion
if "%~1"=="" goto usage
for /f "delims=0123456789" %%I in ("%~1") do goto usage
if %~1 LEQ 1 (
echo %~1 is neither prime nor composite.
exit /b 1
) else if %~1 LEQ 3 (
echo %~1 is prime.
exit /b 0
)
set /a "mod2or3 = ^!(%~1 %% 2) | ^!(%~1 %% 3), half = %~1 / 2"
if %mod2or3% EQU 1 (
echo %~1 is composite.
exit /b 1
)
for /L %%I in (5,6,%half%) do (
set /a "n6pm1 = ^!(%~1 %% %%I) | ^!(%~1 %% (%%I + 2))"
if !n6pm1! EQU 1 (
echo %~1 is composite.
exit /b 1
)
)
echo %~1 is prime.
goto :EOF
:usage
echo Usage: %~nx0 integer
goto :EOF
Original answer:
Thought I might accept the challenge.
#echo off
setlocal enabledelayedexpansion
if "%~1"=="" goto usage
for /f "delims=0123456789" %%I in ("%~1") do goto usage
set /a limit = %~1 - 1
for /L %%I in (2,1,%limit%) do (
set /a modulo = %~1 %% %%I
if !modulo! equ 0 (
echo %~1 is composite ^(divisible by %%I^).
goto :EOF
)
)
echo %~1 is prime.
goto :EOF
:usage
echo Usage: %~nx0 integer
You had commented under Dominique's answer, "So how do I fix this?" Take the first part of your script:
set /a prime=7
set /a num1=2
set /a num2=2
:do1
if %prime% == %num1% * %num2% goto do_if_1
Since you've been informed by both Squashman and myself that if %prime% == %num1% * %num2% is not going to work as you intend, you should now know that you need to insert an additional set /a above your if statement to perform %num1% * %num2%.
set /a prime=7
set /a num1=2
set /a num2=2
:do1
set /a product = num1 * num2
if %prime% == %product% goto do_if_1
Let's continue...
...
if %prime% == %product% goto do_if_1
if else %prime% LSS %num1% * %num2% set /a num2=%num2%+1
There are two immediate problems here. #1, I think you probably meant else if, not if else. #2, in batch scripting, else needs to be included as a part of the if command you're else-ing. This means else either needs to be appended to the end of the previous line, or you should use some parentheses. Rewrite it like this:
if %prime% == %product% (
goto do_it_1
) else if %prime% LSS %product% (
set /a num2 += 1
) else etc...
The first thing I advise you, is to get rid of the echo off, like this you can follow what your script is doing:
D:\>set /a prime=7
D:\>set /a num1=2
D:\>set /a num2=2
D:\>if 7 == 2 * 2 goto do_if_1
7 was unexpected at this time. => this is causing your problem!
D:\>if else 7 LSS 2 * 2 set /a num2=2+1
D:\>
I have been having troubles with batch-codes that I would expect to work, but don't...
Below is what I have written...
#echo off
cls
:loop
set /p "input=Input a number: "
set /a "number=%input%" 2>nul
REM check if input valid
if "%input%" NEQ "%number%" (
cls
Echo Please Enter a valid number! &Echo.&Echo.
goto :loop
)
Set /a Even=number%%2
if %Even% EQU 0 (
Echo Substituting Even Number in: x / 2
Echo set /p"=(%number%) / 2 = "
set /a answer=number/2
) Else (
Echo Substituting Odd Number in: 3x - 1
<nul set /p"=3(%number%)-1 = "
set /a answer=number*3
set /a answer=answer-1
)
Echo %answer%
Echo.
Echo.
goto :loop
Echo Unexpected Error . . .
pause
Exit
Whenever I input a number into the console, it does the math, like I want it to, but prints the number -1, and every time i input another number, the number goes to -2, -3, -4, so on.
Put a setlocal enableextensions at the beginning after the #echo off, e.g.
#echo off
setlocal enableextensions
cls
Also, I think you would also need to use delayed variable expansion (usually denoted by !var!), which would change your script above to something like this:
#echo off
setlocal enableextensions enabledelayedexpansion
cls
:loop
set /p "input=Input a number: "
set /a number=!input! 2>nul
REM check if input valid
if "!input!" NEQ "!number!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
REM Make sure that it is an integer put in (just in case)
set /a int=!number! %% 1
if "!input!" NEQ "!int!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
Set /a Even=!number! %% 2
if !Even! EQU 0 (
Echo Substituting Even Number in: x / 2
set /a answer=!number! / 2
) Else (
Echo Substituting Odd Number in: 3x - 1
set /a answer=!number! * 3 - 1
)
Echo !answer!
Echo.
Echo.
goto :loop
I also would like to point out that I also fixed a few other bugs (set /p isn't of any use in this script at all, especially in where it is used, and also you need the modulus to find even/odd).