Batch if statement error - batch-file

I am attempting to make a minesweeper in batch but i am having strange errors. When I run the file it lays 5 mines. Then it goes through the rest of the squares and checks to see how many mines are adjacent to each square. My problem is that it only senses an out put to the south-east. The south-east code is no different than the code for any other directions. This is very confusing to me. It seems like no one else has had this error. This is what an example output would be:
M 0 1 0 0
0 1 0 M 0
M 0 M 0 0
0 0 1 0 0
0 0 0 M 0
An answer would be greatly appreciated.
#echo off
setlocal enableDelayedExpansion
setlocal ENABLEEXTENSIONS
title Batch Minesweeper
color 80
set setX=1
set setY=1
set testX=1
set testY=1
set plrX=1
set plyY=1
set mines=0
set mineNum=0
set N=
set NE=
set E=
set SE=
set S=
set SW=
set W=
set NW=
set rect1_1=
set rect2_1=
set rect3_1=
set rect4_1=
set rect5_1=
set rect1_2=
set rect2_2=
set rect3_2=
set rect4_2=
set rect5_2=
set rect1_3=
set rect2_3=
set rect3_3=
set rect4_3=
set rect5_3=
set rect1_4=
set rect2_4=
set rect3_4=
set rect4_4=
set rect5_4=
set rect1_5=
set rect2_5=
set rect3_5=
set rect4_5=
set rect5_5=
set mine1_1=0
set mine2_1=0
set mine3_1=0
set mine4_1=0
set mine5_1=0
set mine1_2=0
set mine2_2=0
set mine3_2=0
set mine4_2=0
set mine5_2=0
set mine1_3=0
set mine2_3=0
set mine3_3=0
set mine4_3=0
set mine5_3=0
set mine1_4=0
set mine2_4=0
set mine3_4=0
set mine4_4=0
set mine5_4=0
set mine1_5=0
set mine2_5=0
set mine3_5=0
set mine4_5=0
set mine5_5=0
cls
echo Batch Minesweeper
echo.
echo [1] - Play
echo [2] - Info
echo [3] - Exit
echo.
echo Batch Minesweeper made by Matt Pauly
choice /c 123 /n /m ""
if %errorlevel%==1 goto startGame
if %errorlevel%==1 goto info
if %errorlevel%==1 goto end
:startGame
cls
set /a ranX=%RANDOM% * 5 / 32768 + 1
set /a ranY=%RANDOM% * 5 / 32768 + 1
if not %mines%==5 (
if !mine%ranX%_%ranY%!==0 (
set mine%ranX%_%ranY%=M
set /a mines=%mines% + 1
goto startGame
)
goto startGame
)
goto setup
:setup
cls
echo Loading...
set curVal=%setX%_%setY%
if %curVal%==1_6 goto display
if %setX%==6 (
set setX=1
set /a setY=%setY% + 1
goto setup
)
if !mine%setX%_%setY%!==M (
set /a setX=%setX% + 1
goto setup
)
set /a testY=%setY% + 1
set testX=%setX%
set N=!mine%testX%_%testY%!
set /a testY=%setY% + 1
set /a testX=%setX% + 1
set NE=!mine%testX%_%testY%!
set testY=%setY%
set /a testX=%setX% + 1
set E=!mine%testX%_%testY%!
set /a testY=%setY% + 1
set /a testX=%setX% - 1
set SE=!mine%testX%_%testY%!
set testY=%setY%
set /a testX=%setX% - 1
set S=!mine%testX%_%testY%!
set /a testY=%setY% - 1
set /a testX=%setX% - 1
set SW=!mine%testX%_%testY%!
set /a testY=%setY% + 1
set testX=%setX%
set W=!mine%testX%_%testY%!
set /a testY=%setY% - 1
set /a testX=%setX% + 1
set NW=!mine%testX%_%testY%!
set mineNum=0
if defined N if "%N%"=="M" set /a mineNum=%mineNum% + 1
if defined NW if "%NW%"=="M" set /a mineNum=%mineNum% + 1
if defined W if "%W%"=="M" set/a mineNum=%mineNum% + 1
if defined SW if "%S%"=="M" set /a mineNum=%mineNum% + 1
if defined S if "%S%"=="M" set /a mineNum=%mineNum% + 1
if defined SE if "%SE%"=="M" set /a mineNum=%mineNum% + 1
if defined E if "%E%"=="M" set /a mineNum=%mineNum% + 1
if defined NE if "%NE%"=="M" set /a mineNum=%mineNum% + 1
set mine%setX%_%setY%=%mineNum%
set /a setX=%setX% + 1
goto setup
:display
cls
::echo 5 %rect1_5% %rect2_5% %rect3_5% %rect4_5% %rect5_5%
::echo.
::echo 4 %rect1_4% %rect2_4% %rect3_4% %rect4_4% %rect5_4%
::echo.
::echo 3 %rect1_3% %rect2_3% %rect3_3% %rect4_3% %rect5_3%
::echo.
::echo 2 %rect1_2% %rect2_2% %rect3_2% %rect4_2% %rect5_2%
::echo.
::echo 1 %rect1_1% %rect2_1% %rect3_1% %rect4_1% %rect5_1%
::echo.
::echo a b c d e
echo 5 %mine1_5% %mine2_5% %mine3_5% %mine4_5% %mine5_5%
echo.
echo 4 %mine1_4% %mine2_4% %mine3_4% %mine4_4% %mine5_4%
echo.
echo 3 %mine1_3% %mine2_3% %mine3_3% %mine4_3% %mine5_3%
echo.
echo 2 %mine1_2% %mine2_2% %mine3_2% %mine4_2% %mine5_2%
echo.
echo 1 %mine1_1% %mine2_1% %mine3_1% %mine4_1% %mine5_1%
echo.
echo a b c d e
pause
:info
:end
exit

Your major problem is trailing spaces on lines. In a conventional set statement, any trailing spaces are included in the value assigned, and "M" and "M " are completely different things, so if you have set X=!something! then the value assigned is different from that assigned by set X=!something!.
You also have a problem where you are confusing your X with your Y. For instance, setting W is done with X and Y+1 - the same as setting N. This should be X-1 and Y.

Related

How do i get a random letter output in batch

How do i get a random letter output in batch that dose not need to update each time it just makes a new random letter soo something like this
#echo off
title random letter gen
echo welcome
echo press any key to get a random letter
!!!!!!!!!!!!i want the %letter% to give a random letter!!!!!!!!!!
echo %letter%
echo press a key to get a new letter
pause
goto a
:a
echo %letter%
pause
goto a
Here's a quick way to generate a random letter. %=ExitCodeAscii% contains the ASCII value of %ERRORLEVEL%. More information.
rem // generate a random number between 65 and 90
set /a "ascii = %random% * 26 / 32768 + 65"
rem // set errorlevel to that number
cmd /c exit /b %ascii%
rem // get letter corresponding to that exit code
echo %=ExitCodeAscii%
Eggsedan,
I found this string of code on Superuser.com. It is designed to be a password generator but you could edit to only allow it to make a single character.
https://superuser.com/questions/349474/how-do-you-make-a-letter-password-generator-in-batch
Hopefully this will meet your needs.
#Echo Off
color 0a
set /P lengthnumberuser="What length do you want your password to be? "
pause
cls
Setlocal EnableDelayedExpansion
Set _RNDLength=%lengthnumberuser%
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 Password is: is !_RndAlphaNum!
pause
You can also use The Exit code ASCII output to output lowercase and numbers.
Here's a Random String generator using numbers lowercase and uppercase chars:
#ECHO OFF& SETLOCAL ENABLEDELAYEDEXPANSION
:RandomStrings
::: numbers: char 48 + (0-9)
::: lowercase: char 97 + (0-25)
::: uppercase: char 65 + (0-25)
SET "RANDSTUFF="
SET "CharCount=8"
FOR /L %%A in (1,1,%CharCount%) DO (
SET /A "Chance=%RANDOM% %% 3"
CALL :RandomStrings.!Chance!
)
ECHO.!RANDSTUFF!
EXIT /B
:RandomStrings.0
SET /A "nAscii=%RANDOM%*9/32768+48"
CMD /c EXIT /b %nAscii%
SET RANDSTUFF=!RANDSTUFF!%=ExitCodeASCII%
EXIT /B
:RandomStrings.1
SET /A "lAscii=%RANDOM%*26/32768+97"
CMD /c EXIT /b %lAscii%
SET RANDSTUFF=%RANDSTUFF%%=ExitCodeASCII%
EXIT /B
:RandomStrings.2
SET /A "uAscii=%RANDOM%*26/32768+65"
CMD /c EXIT /b %uAscii%
SET RANDSTUFF=%RANDSTUFF%%=ExitCodeASCII%
EXIT /B
here is one that asks you the lenght of the string
#echo off
Title string generator V1
:DebLoop
set /a RN=%random%/100 *3 *4 /3 *9 /3 /9 +10 -5 *3 *2
set UCN=0
:UCSLN
echo please select the lenght of the string to generate
set /p "UCSL=User Chosen String Lenght>"
if not %UCSL% GTR 0 (
echo invalid input, please chose a number above 0
goto UCSLN
) else (
set /a UCSL=%UCSL%+1
echo generating string, please wait
:UCSL
set CN=0
set N=1
set /a RN=%random%/100 *3 *4 /3 *9 /3 /9 +10 -5 *3 *2
if %RN% LEQ 0 goto UCSL
set OL=%OL%%L%
set /a UCN=%UCN%+1
if %UCN%==%UCSL% (
echo string generated:
echo %OL%>string.txt
string.txt
timeout 2 >nul
goto DebLoop
)
)
:GenLoop
set /a N=%N%+1
if %N% GEQ 62 set N=0
set /a CN=%CN%+1
if %CN%==%RN% (
set CH=%N%
goto DefLoop
)
goto GenLoop
:DefLoop
if %CH% GEQ 1 if %CH% LEQ 26 goto UpperCase
if %CH% GEQ 27 if %CH% LEQ 52 goto LowerCase
if %CH% GEQ 58 if %CH% LEQ 62 goto UpperThanFive
if %CH% GEQ 57 if %CH% LEQ 52 goto LowerThanFive
:UpperCase
if %CH% LEQ 13 goto UCFirst13
if %CH% GEQ 14 goto UCLast13
:LowerCase
if %CH% LEQ 39 goto LCFirst13
if %CH% GEQ 40 goto LCLast13
:UCFirst13
if %CH%==1 set L=A
if %CH%==2 set L=B
if %CH%==3 set L=C
if %CH%==4 set L=D
if %CH%==5 set L=E
if %CH%==6 set L=F
if %CH%==7 set L=G
if %CH%==8 set L=H
if %CH%==9 set L=I
if %CH%==10 set L=J
if %CH%==11 set L=K
if %CH%==12 set L=L
if %CH%==13 set L=M
:UCLast13
if %CH%==14 set L=N
if %CH%==15 set L=O
if %CH%==16 set L=P
if %CH%==17 set L=Q
if %CH%==18 set L=R
if %CH%==19 set L=S
if %CH%==20 set L=T
if %CH%==21 set L=U
if %CH%==22 set L=V
if %CH%==23 set L=W
if %CH%==24 set L=X
if %CH%==25 set L=Y
if %CH%==26 set L=Z
:LCFirst13
if %CH%==27 set L=a
if %CH%==28 set L=b
if %CH%==29 set L=c
if %CH%==30 set L=d
if %CH%==31 set L=e
if %CH%==32 set L=f
if %CH%==33 set L=g
if %CH%==34 set L=h
if %CH%==35 set L=i
if %CH%==36 set L=j
if %CH%==37 set L=k
if %CH%==38 set L=l
if %CH%==39 set L=m
:LCLast13
if %CH%==40 set L=n
if %CH%==41 set L=o
if %CH%==42 set L=p
if %CH%==43 set L=q
if %CH%==44 set L=r
if %CH%==45 set L=s
if %CH%==46 set L=t
if %CH%==47 set L=u
if %CH%==48 set L=v
if %CH%==49 set L=w
if %CH%==50 set L=x
if %CH%==51 set L=y
if %CH%==52 set L=z
:LowerThanFive
if %CH%==53 set L=1
if %CH%==54 set L=2
if %CH%==55 set L=3
if %CH%==56 set L=4
if %CH%==57 set L=5
:UpperThanFive
if %CH%==58 set L=6
if %CH%==59 set L=7
if %CH%==60 set L=8
if %CH%==61 set L=9
if %CH%==62 set L=0
goto UCSL
pause

How can i make a IF statement change 2 variables?

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

Rubik's cube scrambler [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I made this awesome (horrible) Rubik's cube scrambler a while ago, but when I look at the code, it's just too much, I know there is a very simpler way of doing this. If there is anyone who can modify certain bits to make the code more compact, I would really appreciate it.
This is purely for my understanding in batch, and hopefully others can learn from anything you answer.
Here is the code:
#echo off
mode con: cols=62 lines=10
title Cube Scrambler
color 0a
Setlocal EnableDelayedExpansion
set ct = 0
:strt
set scram=BDEFLMRSUbdflru
set /a ct = ct +1
set /a a=%random%%%14
set /a b=%random%%%14
set /a c=%random%%%14
set /a d=%random%%%14
set /a e=%random%%%14
set /a f=%random%%%14
set /a g=%random%%%14
set /a h=%random%%%14
set /a i=%random%%%14
set /a j=%random%%%14
set /a k=%random%%%14
set /a l=%random%%%14
set /a m=%random%%%14
set /a n=%random%%%14
set /a o=%random%%%14
set a=!scram:~%a%,1!
set b=!scram:~%b%,1!
set c=!scram:~%c%,1!
set d=!scram:~%d%,1!
set e=!scram:~%e%,1!
set f=!scram:~%f%,1!
set g=!scram:~%g%,1!
set h=!scram:~%h%,1!
set i=!scram:~%i%,1!
set j=!scram:~%j%,1!
set k=!scram:~%k%,1!
set l=!scram:~%l%,1!
set m=!scram:~%m%,1!
set n=!scram:~%n%,1!
set o=!scram:~%o%,1!
set /a z1=%random%%%2 +1
set /a z2=%random%%%2 +1
set /a z3=%random%%%2 +1
set /a z4=%random%%%2 +1
set /a z5=%random%%%2 +1
set /a z6=%random%%%2 +1
set /a z7=%random%%%2 +1
set /a z8=%random%%%2 +1
set /a z9=%random%%%2 +1
set /a z10=%random%%%2 +1
set /a z11=%random%%%2 +1
set /a z12=%random%%%2 +1
set /a z13=%random%%%2 +1
set /a z14=%random%%%2 +1
set /a z15=%random%%%2 +1
if %z1% == 2 goto 2x2
set x1='
:2x2
if %z2% == 2 goto 3x3
set x2='
:3x3
if %z3% == 2 goto 4x4
set x3='
:4x4
if %z4% == 2 goto 5x5
set x4='
:5x5
if %z5% == 2 goto 6x6
set x5='
:6x6
if %z6% == 2 goto 7x7
set x6='
:7x7
if %z7% == 2 goto 8x8
set x7='
:8x8
if %z8% == 2 goto 9x9
set x8='
:9x9
if %z9% == 2 goto 10x10
set x9='
:10x10
if %z10% == 2 goto 11x11
set x10='
:11x11
if %z11% == 2 goto 12x12
set x11='
:12x12
if %z12% == 2 goto 13x13
set x12='
:13x13
if %z13% == 2 goto 14x14
set x13='
:14x14
if %z14% == 2 goto 15x15
set x14='
:15x15
if %z15% == 2 goto ans
set x15='
:ans
echo.
echo Scramble: %ct%
echo.
echo.
echo %a%%x1% %b%%x2% %c%%x3% %d%%x4% %e%%x5% %f%%x6% %g%%x7% %h%%x8% %i%%x9% %j%%x10% %k%%x11% %l%%x12% %m%%x13% %n%%x14% %o%%x15%
echo.
echo.
echo { Enter } = New scramble
echo Made by: Ruan Swanepoel
pause>nul
cls
set x1=
set x2=
set x3=
set x4=
set x5=
set x6=
set x7=
set x8=
set x9=
set x10=
set x11=
set x12=
set x13=
set x14=
set x15=
goto strt
Maybe something like this approach using FOR loops?
#ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
mode con: cols=62 lines=10
title Cube Scrambler
color 0a
set "ct=0"
set "discontinue="
:strt
set "scram=BDEFLMRSUbdflru"
set /a "ct+=1"
for %%x in (a b c d e f g h i j k l m n o) do (
set /a "xx=!random!%%15"
call set "%%x=%%scram:~!xx!,1%%"
)
for /L %%y in (1, 1, 15) do (
set /a "yy=!random!%%2"
if !yy! EQU 1 (
set "x%%y= "
) else (
set "x%%y='"
)
)
:ans
echo(
echo Scramble: %ct%
echo(
echo(
echo %a%%x1% %b%%x2% %c%%x3% %d%%x4% %e%%x5% %f%%x6% %g%%x7% %h%%x8% %i%%x9% %j%%x10% %k%%x11% %l%%x12% %m%%x13% %n%%x14% %o%%x15%
echo(
echo(
echo { Enter } = New scramble
echo Made by: Ruan Swanepoel
set /P "discontinue="
if defined discontinue goto :endlocal
cls
goto strt
:endlocal
color
title %CD%
rem mode con: cols=80 lines=25
ENDLOCAL
goto :eof
It could probably be trimmed down a bit more (especially if I wanted to tweak the spacing), but I got it down to 29% of what it was, so there's that.
#echo off
setlocal enabledelayedexpansion
mode con cols=62 lines=10
title Cube Scrambler
color 0A
set ct=0
:begin
set scram=BDEFLMRSUbdflru
set /a ct+=1
for /l %%A in (1,1,15) do (
set /a mod_15=!random!%%15
for /F "delims=" %%B in ("!mod_15!") do (
set mov[%%A]=!scram:~%%B,1!
)
set /a inv[%%A]=!random!%%2
if !inv[%%A]! equ 0 (
set "inv[%%A]= "
) else (
set "inv[%%A]=' "
)
)
echo.
echo Scramble: !ct!
echo.
for /l %%A in (1,1,15) do (
set /p"=!mov[%%A]!!inv[%%A]!"<nul
)
echo.
echo.
echo { Enter } = New scramble
echo Made by: Ruan Swanepoel
pause>nul
cls
goto begin
This version is even smaller (just above 18% of the original) at the expense of some readability:
#echo off
setlocal enabledelayedexpansion
mode con cols=45 lines=10
set c=0
set s=BDEFLMRSUbdflru
:a
set /a c+=1
title Cube Scramble !c!
for /l %%A in (1,1,15) do (
set /a r=!random!%%15,i%%A=!random!%%2
for /F %%B in ("!r!") do set m%%A=!s:~%%B,1!
if !i%%A! equ 0 (set i%%A= ) else (set i%%A=' )
)
echo.
for /l %%A in (1,1,15) do set /p=!m%%A!!i%%A!<nul
echo.
echo Enter: Rescramble
pause>nul
cls
goto a

Repeating Answer in Batch Random Number Generator

In my code*, I have a simple random number generator and a few ifs to check what number the generator produces. However, even when run in another instance of CMD, in another Batch file, and another instance of this Batch file, it still produces the same number: 4. Every time. I am not using this code in a loop, although it can be looped back to its section. I do, however, have another line of code to generate another number within a different set of parameters that works just fine.
*My code:
:genClass
set /a class=(5 * !random!) / 32768 + 1
if !class!==1 (
set class=Knight
goto genKnightArmor
)
if !class!==2 (
set class=Warrior
goto genWarriorArmor
)
if !class!==3 (
set class=Archer
goto genArcherArmor
)
if !class!==4 (
set class=Thief
goto genThiefArmor
)
if !class!==5 (
set class=Mage
goto genMageArmor
)
:genKnightArmor
set /a armor=(2 * !random!) / 32768 + 1
if !armor!==1 set armor=Light Armor
if !armor!==2 set armor=Heavy Armor
echo !name!
echo !gender!
echo !class!
echo !armor!
pause
exit
:genWarriorArmor
set /a armor=(2 * !random!) / 32768 + 1
if !armor!==1 set armor=Light Armor
if !armor!==2 set armor=Heavy Armor
echo !name!
echo !gender!
echo !class!
echo !armor!
pause
exit
:genArcherArmor
set /a armor=(2 * !random!) / 32768 + 1
if !armor!==1 set armor=Light Armor
if !armor!==2 set armor=Heavy Armor
echo !name!
echo !gender!
echo !class!
echo !armor!
pause
exit
:genThiefArmor
set /a armor=(2 * !random!) / 32768 + 1
if !armor!==1 set armor=Light Armor
if !armor!==2 set armor=Heavy Armor
echo !name!
echo !gender!
echo !class!
echo !armor!
pause
exit
:genMageArmor
set /a armor=(3 * !random!) / 32768 + 1
if !armor!==1 set armor=Light Armor
if !armor!==2 set armor=Heavy Armor
if !armor!==3 set armor=Robes and Hood
echo !name!
echo !gender!
echo !class!
echo !armor!
pause
exit
EDIT: With Monacraft's solution, it now generates a number of 2 and also generates the armor type without error. However, the code to generate the class is still messed up and, as stated before, continues to generate the number 2.
EDIT: I have edited the code once again to change the arithmetic and string variable names. This did not clear anything up, although the class generator is now continuously generating the number 3. I've noticed that, when edited, the generator produces a new number, although, after the first generation, it yields the same number.
EDIT: Fixed mistakes in my code. Such as matching the variables and changing the number 4 in the random generator to 5.
Ok, the main problem with your random number generator is the use of ! instead of %. Unless your using a for loop, you won't need these.
Instead of this:
set /a num=(4 * !random!) / 32768 + 1
Simply use this:
set /a num=(4 * %random%) / 32768 + 1
I tested this in cmd, and it worked fine.
To use ! you need to EnableDelayedExpansion and then use this is a for loop.
Hope this helped with your problem,
Mona.
set /a num=(4 * %random%) / 32768 + 1
if !class!==1 (
Maybe i'm wrong, but, set num and if class does not seem correct
The problem is your expression
set /a num=(4 * !random!) / 32768 + 1
Batch mathematics is integer-only, so this will fail.
Try
set /a num=(%random% %% 4) + 1
Better yet, try in your main code
call :roll armor 3
and add at the end of the code
REM Roll 1..%2
:ROLL
set /a %1=(%random% %% %2) + 1
GOTO :EOF
Note carefully where the colons are - they are critical.
call :roll somevar n
will assign a random number 1..n to the variable somevar
Here's another way to do the selection:
#echo off
:genClass
set name=Slartibartfast
set gender=Alien
set /a num1=%random% %% 5 + 1
set /a num2=%random% %% 2 + 1
if %num1%==1 (
set class=Knight
if %num2%==1 set armor=Light Armor
if %num2%==2 set armor=Heavy Armor
)
set /a num2=%random% %% 2 + 1
if %num1%==2 (
set class=Warrior
if %num2%==1 set armor=Light Armor
if %num2%==2 set armor=Heavy Armor
)
set /a num2=%random% %% 2 + 1
if %num1%==3 (
set class=Archer
if %num2%==1 set armor=Light Armor
if %num2%==2 set armor=Heavy Armor
)
set /a num2=%random% %% 2 + 1
if %num1%==4 (
set class=Thief
if %num2%==1 set armor=Light Armor
if %num2%==2 set armor=Heavy Armor
)
set /a num2=%random% %% 3 + 1
if %num1%==5 (
set class=Mage
if %num2%==1 set armor=Light Armor
if %num2%==2 set armor=Heavy Armor
if %num2%==3 set armor=Robes and Hood
)
echo %name%
echo %gender%
echo %class%
echo %armor%
echo.
pause
goto :genclass

Batch variable inside of variable

I've got a set of variables called p1 to p9
I've got 2 vars to set the range I'm interested in.
I want to list all the vars from minimum range +1 to the max.
It looks like that (and it won't work)
set currentvar=5
set maxvar=9
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result = %maxvar% - %currentvar%
echo Found %result% vars in the range.
:LOOP
if %currentvar% LSS %maxvar% (
set /a currentvar=%currentvar% + 1
echo %p %currentvar% % //IT WON'T WORK AND I DON'T KNOW HOW TO MAKE IT WORK...
goto LOOP
) else (
goto END
)
:END
The result I'd like to see:
fff
ggg
hhh
iii
this might work for you:
#ECHO OFF &SETLOCAL
set /a currentvar=5
set /a maxvar=9
set /a RangeStart=currentvar+1
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result=maxvar-currentvar
echo Found %result% vars in the range.
for /l %%a in (%RangeStart% 1 %maxvar%) do call echo(%%p%%a%%
You need to Enabledelayedexpansion:
#echo off
Setlocal Enabledelayedexpansion
set currentvar=5
set maxvar=9
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result = %maxvar% - %currentvar%
echo Found %result% vars in the range.
:LOOP
if %currentvar% LSS %maxvar% (
set /a currentvar=%currentvar% + 1
echo !p%currentvar%! &REM This is how you make a comment in batch
goto LOOP
) else (
goto END
)
:END
Endlocal
And that should do what you want to do. Also to make a comment use a new line and type :: or REM.
Mona

Resources