Problem with using choice command - batch - batch-file

i am trying to make character ("o") be able to enter both of the buldings, i've tried everything:
detecting %pos% so when its 5 it goes to :fishing store
adding "e" to choices so if i press e and %pos% equals 5 them goto :fishing store
(none of them worked, beacause i am doing something wrong and i don't know what)
ill be greatfull if someone will respond quickly
here is the code:
#echo off
title outside
mode 45,10
set/a pos=11
set "y7=[fishing] [ fanta's ]"
set "y6=[ store ] [ house ]"
set "y5=H-------H H---------H"
set "y4=H_II____H H_II______H"
set "y3=H___[ ]_H__o__H___[ ]___H__________"
set "y2=::::::::::::::::::::::::H\/\/H\/\/H"
set "y1=::::::::::::::::::::::::H/\/\H/\/\H~~~~~~~~~~"
echo %y7%
echo %y6%
echo %y5%
echo %y4%
echo %y3%
echo %y2%
echo %y1%
goto movement
:movement
choice /c ad /n
if %errorlevel% equ 1 goto left
if %errorlevel% equ 2 goto right
:left
if %pos% gtr 1 set /a pos-=1
goto position
:right
if %pos% lss 32 set /a pos+=1
goto position
:position
if %pos% EQU 1 set "y3=o___[ ]_H_____H___[ ]___H__________"
if %pos% EQU 2 set "y3=Ho__[ ]_H_____H___[ ]___H__________"
if %pos% EQU 3 set "y3=H_o_[ ]_H_____H___[ ]___H__________"
if %pos% EQU 4 set "y3=H__o[ ]_H_____H___[ ]___H__________"
if %pos% EQU 5 set "y3=H___[o]_H_____H___[ ]___H__________"
if %pos% EQU 6 set "y3=H___[ ]oH_____H___[ ]___H__________"
if %pos% EQU 7 set "y3=H___[ ]_o_____H___[ ]___H__________"
if %pos% EQU 8 set "y3=H___[ ]_Ho____H___[ ]___H__________"
if %pos% EQU 9 set "y3=H___[ ]_H_o___H___[ ]___H__________"
if %pos% EQU 10 set "y3=H___[ ]_H__o__H___[ ]___H__________"
if %pos% EQU 11 set "y3=H___[ ]_H___o_H___[ ]___H__________"
if %pos% EQU 12 set "y3=H___[ ]_H____oH___[ ]___H__________"
if %pos% EQU 13 set "y3=H___[ ]_H_____o___[ ]___H__________"
if %pos% EQU 14 set "y3=H___[ ]_H_____Ho__[ ]___H__________"
if %pos% EQU 15 set "y3=H___[ ]_H_____H_o_[ ]___H__________"
if %pos% EQU 16 set "y3=H___[ ]_H_____H__o[ ]___H__________"
if %pos% EQU 17 set "y3=H___[ ]_H_____H___[o]___H__________"
if %pos% EQU 18 set "y3=H___[ ]_H_____H___[ ]o__H__________"
if %pos% EQU 19 set "y3=H___[ ]_H_____H___[ ]_o_H__________"
if %pos% EQU 20 set "y3=H___[ ]_H_____H___[ ]__oH__________"
if %pos% EQU 21 set "y3=H___[ ]_H_____H___[ ]___o__________"
if %pos% EQU 22 set "y3=H___[ ]_H_____H___[ ]___Ho_________"
if %pos% EQU 23 set "y3=H___[ ]_H_____H___[ ]___H_o________"
if %pos% EQU 24 set "y3=H___[ ]_H_____H___[ ]___H__o_______"
if %pos% EQU 25 set "y3=H___[ ]_H_____H___[ ]___H___o______"
if %pos% EQU 26 set "y3=H___[ ]_H_____H___[ ]___H____o_____"
if %pos% EQU 27 set "y3=H___[ ]_H_____H___[ ]___H_____o____"
if %pos% EQU 28 set "y3=H___[ ]_H_____H___[ ]___H______o___"
if %pos% EQU 29 set "y3=H___[ ]_H_____H___[ ]___H_______o__"
if %pos% EQU 30 set "y3=H___[ ]_H_____H___[ ]___H________o_"
if %pos% EQU 31 set "y3=H___[ ]_H_____H___[ ]___H_________o"
:fanta's house
cls
echo you are at fanta's house
pause
:fishing store
cls
echo you are at fishing store
pause
:print
cls
echo %y7%
echo %y6%
echo %y5%
echo %y4%
echo %y3%
echo %y2%
echo %y1%
if %pos% EQU 5 choice /c ead /n >nul
if %errorlevel% equ 1 goto :fishing store
if %pos% EQU 17 choice /c ead /n >nul
if %errorlevel% equ 1 goto :fanta's house
if %errorlevel% equ 2 goto :left
if %errorlevel% equ 3 goto :right
goto movement

Firstly, make sure your file extension is .cmd and not .bat
Then something like this should help guide you.
Just replace your print section with this:
:print
cls
echo %y7%
echo %y6%
echo %y5%
echo %y4%
echo %y3%
echo %y2%
echo %y1%
if %pos% EQU 5 choice /c ead /n >nul
if %errorlevel% equ 1 goto :fishingstore
if %errorlevel% equ 2 goto :left
if %errorlevel% equ 3 goto :right
if %pos% EQU 17 choice /c ead /n >nul
if %errorlevel% equ 1 goto :fantashouse
if %errorlevel% equ 2 goto :left
if %errorlevel% equ 3 goto :right
goto movement
As you seemed to have changed your original code, try this exact full version:
#echo off
title outside
mode 45,10
set/a pos=11
set "y7=[fishing] [ fanta's ]"
set "y6=[ store ] [ house ]"
set "y5=H-------H H---------H"
set "y4=H_II____H H_II______H"
set "y3=H___[ ]_H__o__H___[ ]___H__________"
set "y2=::::::::::::::::::::::::H\/\/H\/\/H"
set "y1=::::::::::::::::::::::::H/\/\H/\/\H~~~~~~~~~~"
echo %y7%
echo %y6%
echo %y5%
echo %y4%
echo %y3%
echo %y2%
echo %y1%
goto movement
:movement
choice /c ad /n
if %errorlevel% equ 1 goto left
if %errorlevel% equ 2 goto right
:left
if %pos% gtr 1 set /a pos-=1
goto position
:right
if %pos% lss 32 set /a pos+=1
goto position
:position
if %pos% EQU 1 set "y3=o___[ ]_H_____H___[ ]___H__________"
if %pos% EQU 2 set "y3=Ho__[ ]_H_____H___[ ]___H__________"
if %pos% EQU 3 set "y3=H_o_[ ]_H_____H___[ ]___H__________"
if %pos% EQU 4 set "y3=H__o[ ]_H_____H___[ ]___H__________"
if %pos% EQU 5 set "y3=H___[o]_H_____H___[ ]___H__________"
if %pos% EQU 6 set "y3=H___[ ]oH_____H___[ ]___H__________"
if %pos% EQU 7 set "y3=H___[ ]_o_____H___[ ]___H__________"
if %pos% EQU 8 set "y3=H___[ ]_Ho____H___[ ]___H__________"
if %pos% EQU 9 set "y3=H___[ ]_H_o___H___[ ]___H__________"
if %pos% EQU 10 set "y3=H___[ ]_H__o__H___[ ]___H__________"
if %pos% EQU 11 set "y3=H___[ ]_H___o_H___[ ]___H__________"
if %pos% EQU 12 set "y3=H___[ ]_H____oH___[ ]___H__________"
if %pos% EQU 13 set "y3=H___[ ]_H_____o___[ ]___H__________"
if %pos% EQU 14 set "y3=H___[ ]_H_____Ho__[ ]___H__________"
if %pos% EQU 15 set "y3=H___[ ]_H_____H_o_[ ]___H__________"
if %pos% EQU 16 set "y3=H___[ ]_H_____H__o[ ]___H__________"
if %pos% EQU 17 set "y3=H___[ ]_H_____H___[o]___H__________"
if %pos% EQU 18 set "y3=H___[ ]_H_____H___[ ]o__H__________"
if %pos% EQU 19 set "y3=H___[ ]_H_____H___[ ]_o_H__________"
if %pos% EQU 20 set "y3=H___[ ]_H_____H___[ ]__oH__________"
if %pos% EQU 21 set "y3=H___[ ]_H_____H___[ ]___o__________"
if %pos% EQU 22 set "y3=H___[ ]_H_____H___[ ]___Ho_________"
if %pos% EQU 23 set "y3=H___[ ]_H_____H___[ ]___H_o________"
if %pos% EQU 24 set "y3=H___[ ]_H_____H___[ ]___H__o_______"
if %pos% EQU 25 set "y3=H___[ ]_H_____H___[ ]___H___o______"
if %pos% EQU 26 set "y3=H___[ ]_H_____H___[ ]___H____o_____"
if %pos% EQU 27 set "y3=H___[ ]_H_____H___[ ]___H_____o____"
if %pos% EQU 28 set "y3=H___[ ]_H_____H___[ ]___H______o___"
if %pos% EQU 29 set "y3=H___[ ]_H_____H___[ ]___H_______o__"
if %pos% EQU 30 set "y3=H___[ ]_H_____H___[ ]___H________o_"
if %pos% EQU 31 set "y3=H___[ ]_H_____H___[ ]___H_________o"
:print
cls
echo %y7%
echo %y6%
echo %y5%
echo %y4%
echo %y3%
echo %y2%
echo %y1%
if %pos% EQU 5 choice /c ead /n >nul
if %errorlevel% equ 1 goto :fishingstore
if %pos% EQU 17 choice /c ead /n >nul
if %errorlevel% equ 1 goto :fantashouse
if %errorlevel% equ 2 goto :left
if %errorlevel% equ 3 goto :right
goto movement
goto :EOF
:fantashouse
cls
echo you are at fanta's house
choice /c ce /m "Continue or exit?"
if %errorlevel% equ 2 goto :print
pause
goto :EOF
:fishingstore
cls
echo you are at fishing store
choice /c ce /m "Continue or exit?"
if %errorlevel% equ 2 goto :print
pause
goto :EOF

#ECHO OFF
SETLOCAL
title outside
rem mode 45,10
set/a pos=11
:position
set "y3=H___[ ]_H_____H___[ ]___H__________"
SET /a y7=pos+1
CALL SET "y3=%%y3:~0,%pos%%%o%%y3:~%y7%%%"
set "y7=[fishing] [ fanta's ]"
set "y6=[ store ] [ house ]"
set "y5=H-------H H---------H"
set "y4=H_II____H H_II______H"
set "y2=::::::::::::::::::::::::H\/\/H\/\/H"
set "y1=::::::::::::::::::::::::H/\/\H/\/\H~~~~~~~~~~"
cls
echo %y7%
echo %y6%
echo %y5%
echo %y4%
echo %y3%
echo %y2%
echo %y1%
:: goto movement
:movement
SET "choices=ad"
IF %pos% equ 5 SET "choices=ade"
IF %pos% equ 19 SET "choices=ade"
choice /c %choices% /n
if %errorlevel% equ 1 goto left
if %errorlevel% equ 2 goto right
IF %pos% equ 19 GOTO fanta's
IF %pos% equ 5 GOTO fishing
:left
if %pos% geq 1 set /a pos-=1
goto position
:right
if %pos% lss 32 set /a pos+=1
goto position
:fanta's house
cls
echo you are at fanta's house
pause
goto position
:fishing store
cls
echo you are at fishing store
pause
goto position
GOTO :EOF
A somewhat re-written version.
Since all that changes on the map is the position of o, place the o on the appropriate position on the line y3.
the first character in a string is "character 0" (hence, it's legitimate for pos to reach 0 - and gtr becomes geq in left:). Batch can substring using the syntax var:~start,length where start is the starting character number (or character-count from end, if negative) and length is number of characters (if negative, number of characters to not include).
So, we need to select that part of y3 from the start to pos, then the o and then the remainder of y3 starting at pos+1.
Hence - calculate pos+1 as batch can't apply a calculation in the substring formula and put the result in a temporary variable. I chose y7 since it's about to be reset anyway. and then use the parser to complete the extraction.
The parser examines CALL SET "y3=%%y3:~0,%pos%%%o%%y3:~%y7%%%" and replaces, left-to-right (let's assume pos=11, hence y7=12) %% with % and %var% with var's value since the set is being called:
set "y3=% y3:~0, 11 % o % y3:~ 12 %" (spaces inserted to show parser's steps)
set "y3=%y3:~0,11%o%y3:~12%" (as executed)
which places the o appropriately, depending on pos.
Our next step is whether o is at position 5 or 19 (not 17), when we need to allow e as well as a and d in the choice. Simply append the e in these circumstances, and choice will yield errorlevel 1 for a, 2 for d and 3 for e (but only if e is one of the choices).
Then - well, if choice has returned errorlevel 1 or 2, go left/right and if it's neither of those, then it must be 3, so e was pressed and we can look at pos to see which of the two routines to jump to.
Note that a label can't contain spaces, so if you tried fanta's house fanta's shop and fanta's tennis court then only the label fanta's would be interpreted (it's unusual, but not invalid, to use ' in a label). cmd would search for the next label fanta's following the goto as its destination, which might give the illusion of working correctly - but that's a trap...

Related

Batch file to check IP address, and set location based octet

I've pieced together a batch file based on info I've found here. Oddly, an earlier version appeared to work correctly at home, once I took it to work, it didn't. And all my modifications are failing to fix it. My boss and I are stuck.
#echo off
setlocal
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:"IPv4 Address"`) do (
for /f "tokens=1-4 delims=. " %%a in ("%%f") do (
set octetA=%%a set octetB=%%b set octetC=%%c set octetD=%%d
if %octetB% equ 10 goto :setschool
)
)
:setschool
if %octetC% geq 0 if %octetC% leq 3 set school=DIC
if %octetC% geq 16 if %octetC% leq 19 set school=AT
if %octetC% geq 48 if %octetC% leq 51 set school=BE
if %octetC% geq 64 if %octetC% leq 67 set school=BH
if %octetC% geq 80 if %octetC% leq 83 set school=CN
if %octetC% geq 112 if %octetC% leq 115 set school=LC
if %octetC% geq 128 if %octetC% leq 131 set school=LX
if %octetC% geq 144 if %octetC% leq 147 set school=RG
if %octetC% geq 160 if %octetC% leq 163 set school=UN
if %octetC% geq 176 if %octetC% leq 179 set school=WA
if %octetC% geq 192 if %octetC% leq 195 set school=WI
if %octetC% geq 208 if %octetC% leq 211 set school=BOE
echo %school%
goto :eof
It errors out at the first 'if', because octetB hasn't been set to anything yet. Yet the code just before it should have set it. All of our wired DHCP addresses start with 10.10., which is why it's checking for 10, which should mean it ignores our wireless, auto configed IPs, and virtual nics. If I manually run the lines before the 'if', it produces what I expect, with the octets being set correctly.
I suspect that the output of ipconfig is different at home and at work. Do you run the same operating systems on them? With the same language setting? Do the ipconfig outputs on both machines contain a IPv4 Address line when checked manually?
But I cannot tell anything without knowing the "earlier version that worked" you mentioned.
You can try using a simpler search String that is common on both machines, like IPv4. And maybe you want to add the /I switch to the findStr call to set it to "ignore case".
Try checking for equality with if "%%b" == "10" goto setschool (remove the colon, replace %octetB%). I think %octetB% is only available inside the for loop if you work with delayed expansion of variables.
You need to put each set command on its own line inside the for loop.
With these changes it works for me at least under Win10.
This is how I find which IP subnet our machines are using:
ipconfig^|findstr /i /c:"ip address"^|findstr /c:"10.11.1"&&set loc=AAA&&goto :continue
ipconfig^|findstr /i /c:"ipv4 address"^|findstr /c:"10.11.1"&&set loc=AAA&&goto ipconfig^|findstr /i /c:"ip address"^|findstr /c:"192.168.4."&&set loc=BBB&&goto :continue
:continue
ipconfig^|findstr /i /c:"ipv4 address"^|findstr /c:"192.168.4."&&set loc=BBB&&goto :continue
set loc=unknown
:continue
I think "ip address" is from WinXP or Win7.
You need delayed expansion.
untested
#echo off
setlocal enabledelayedexpansion
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:"IPv4 Address"`) do (
for /f "tokens=1-4 delims=. " %%a in ("%%f") do (
set octetA=%%a & set octetB=%%b & set octetC=%%c & set octetD=%%d
if "!octetB!" equ "10" goto :setschool
)
)
Here's a Remarked batch file example, showing the fix for your issues, as per my initial comment:
#Echo Off
Rem Undefine any existing octet variables.
For /F "Delims==" %%G In ('Set octet 2^>NUL') Do Set "%%G="
Rem Retrieve the IPv4 string from the IPConfig command.
Rem And set each octet to individual variables.
For /F "Tokens=2 Delims=:" %%G In (
'"%__AppDir__%ipconfig.exe 2>NUL | %__Appdir__%find.exe "IPv4""'
) Do For /F "Tokens=1-4 Delims=. " %%H In ("%%G"
) Do Set "octet1=%%H" & Set "octet2=%%I" & Set "octet3=%%J" & Set "octet4=%%K"
Rem Check to see if any octet variables are defined, i.e. IPv4 found.
Rem If not print message, wait for response and end the script.
Set octet >NUL 2>&1 || (Echo IPConfig failed to retrieve an IP address.
Pause & GoTo :EOF)
Rem From here you can make your comparisons:
Rem First ensure that a variable named school is not defined.
Set "school="
Rem Check to see if your second octet was 10.
Rem Then make your comparisons using the value of the third octet.
If %octet2% Equ 10 If Defined octet3 (
If %octet3% GEq 0 If %octet3% LEq 3 Set "school=DIC"
If %octet3% GEq 16 If %octet3% LEq 19 Set "school=AT"
If %octet3% GEq 48 If %octet3% LEq 51 Set "school=BE"
If %octet3% GEq 64 If %octet3% LEq 67 Set "school=BH"
If %octet3% GEq 80 If %octet3% LEq 83 Set "school=CN"
If %octet3% GEq 112 If %octet3% LEq 115 Set "school=LC"
If %octet3% GEq 128 If %octet3% LEq 131 Set "school=LX"
If %octet3% GEq 144 If %octet3% LEq 147 Set "school=RG"
If %octet3% GEq 160 If %octet3% LEq 163 Set "school=UN"
If %octet3% GEq 176 If %octet3% LEq 179 Set "school=WA"
If %octet3% GEq 192 If %octet3% LEq 195 Set "school=WI"
If %octet3% GEq 208 If %octet3% LEq 211 Set "school=BOE")
Rem As your comparisons do not currently cover all possible octet values,
Rem This is a check to see if the school variable was actually defined.
Rem If not print message, otherwise print the content of the school variable.
If Not Defined school (Echo The school variable was not defined.
Echo Reason: & If %octet2% Neq 10 (Echo The second octet was not 10.
) Else If Defined octet3 (Echo The third octet failed the comparisons.
) Else Echo Your returned IP address was corrupted.) Else Echo %school%
Rem Wait to ensure that any messages can be read, if not run from cmd.exe.
Pause
Rem End script.
GoTO :EOF
The script includes some additions designed to provide reasonable feedback if errors occur.
Feel free to use this unRemarked version for completion or in your production environment.
#Echo Off
For /F "Delims==" %%G In ('Set octet 2^>NUL')Do Set "%%G="
For /F "Tokens=2 Delims=:" %%G In (
'"%__AppDir__%ipconfig.exe 2>NUL|%__Appdir__%find.exe "IPv4""'
)Do For /F "Tokens=1-4Delims=. " %%H In ("%%G"
)Do Set "octet1=%%H"&Set "octet2=%%I"&Set "octet3=%%J"&Set "octet4=%%K"
Set octet>NUL 2>&1||(Echo IPConfig failed to retrieve an IP address.
Pause&GoTo :EOF)
Set "school="
If %octet2% Equ 10 If Defined octet3 (
If %octet3% GEq 0 If %octet3% LEq 3 Set "school=DIC"
If %octet3% GEq 16 If %octet3% LEq 19 Set "school=AT"
If %octet3% GEq 48 If %octet3% LEq 51 Set "school=BE"
If %octet3% GEq 64 If %octet3% LEq 67 Set "school=BH"
If %octet3% GEq 80 If %octet3% LEq 83 Set "school=CN"
If %octet3% GEq 112 If %octet3% LEq 115 Set "school=LC"
If %octet3% GEq 128 If %octet3% LEq 131 Set "school=LX"
If %octet3% GEq 144 If %octet3% LEq 147 Set "school=RG"
If %octet3% GEq 160 If %octet3% LEq 163 Set "school=UN"
If %octet3% GEq 176 If %octet3% LEq 179 Set "school=WA"
If %octet3% GEq 192 If %octet3% LEq 195 Set "school=WI"
If %octet3% GEq 208 If %octet3% LEq 211 Set "school=BOE")
If Not Defined school (Echo The school variable was not defined.
Echo Reason:&If %octet2% Neq 10 (Echo The second octet was not 10.
)Else If Defined octet3 (Echo The third octet failed the comparisons.
)Else Echo Your returned IP address was corrupted.)Else Echo %school%
Pause
GoTO :EOF

Is it possible to make a randomly generated variable in batch? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Currently I am making a game in batch that has randomly generated worlds. I'm currently working on biome generation, and I'm trying to figure out a way to have each variable generated, so the variables will look like %biomenum[0,0]%, %biomenum[0,1]%, and going maybe as far as %biomenum[512,512]% and as low as %biomenum[-512,-512]
The code that I have currently generates the biome for 9 different chunks, but it doesn't make the variables itself, and typing this out for each chunk between -512, -512 and 512, 512 doesn't seem all that plausible
rem 0x 0y
set /A biomenum0x0y = %RANDOM% * 16 / 32768 + 1
rem 0x 1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum0x1y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum0x1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum0x1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum0x1y = biomenum0x0y - 1
rem 1x 1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum1x1y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum1x1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum1x1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum1x1y = biomenum0x0y - 1
rem 1x 0y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum1x0y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum1x0y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum1x0y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum1x0y = biomenum0x0y - 1
rem 1x -1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum1xneg1y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum1xneg1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum1xneg1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum1xneg1y = biomenum0x0y - 1
rem 0x -1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum0xneg1y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum0xneg1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum0xneg1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum0xneg1y = biomenum0x0y - 1
rem -1x -1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenumneg1xneg1y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenumneg1xneg1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenumneg1xneg1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenumneg1xneg1y = biomenum0x0y - 1
rem -1x 0y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenumneg1x0y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenumneg1x0y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenumneg1x0y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenumneg1x0y = biomenum0x0y - 1
rem -1x 1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenumneg1x1y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenumneg1x1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenumneg1x1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenumneg1x1y = biomenum0x0y - 1
echo %biomenumneg1xneg1y% is the biome for -1,-1
echo %biomenumneg1x0y% is the biome for -1, 0
echo %biomenumneg1x1y% is the biome for -1, 1
echo %biomenum0xneg1y% is the biome for 0, -1
echo %biomenum0x0y% is the biome for 0, 0 -the starting chunk-
echo %biomenum0x1y% is the biome for 0, 1
echo %biomenum1xneg1y% is the biome for 1, -1
echo %biomenum1x0y% is the biome for 1, 0
echo %biomenum1x1y% is the biome for 1, 1
How would I be able to automate the creation of variables, so I don't have to type out all 1,048,576 chunks?
*Note: This question was edited so it made more sense. The original was a very long explanation that I made on 3 hours of sleep, and I apologize for that. Hopefully this is a bit more easy to understand.
Your explanation is pretty confusing, both in what you want to get and the way to obtain it...
When you have a series of variables with the same name and you want to select one variable based on the value of another variable (called index), then the name for such a data structure is array. If the array elements are selected by just one index variable (one dimensional array), then it is usually called vector. If the array elements are selected by two or more indices (multi-dimensional array), then it is called matrix.
In computer programs the usual way to repeat the same code over a range of values that are all processed in the same way is via the FOR statement/construct/command. The FOR control variable is placed in the code in the place of the varying value. The usual way to process a series of array elements is via a FOR command and place its control variable as the index of the array element.
The way to process array elements in Batch file programs is described at this answer.
The next Batch file obtain a result equivalent than your example code, but using a two-dimensional array and two nested FOR commands:
#echo off
setlocal EnableDelayedExpansion
rem 0x 0y
set /A biomenum0x0y = %RANDOM% * 16 / 32768 + 1
rem The rest!
for %%x in (neg1 0 1) do (
for %%y in (neg1 0 1) do (
set /A nextbiomenum = !RANDOM! * 4 / 32768 - 1, biomenum%%xx%%yy = biomenum0x0y + nextbiomenum %% 2
)
)
echo %biomenumneg1xneg1y% is the biome for -1,-1
echo %biomenumneg1x0y% is the biome for -1, 0
echo %biomenumneg1x1y% is the biome for -1, 1
echo %biomenum0xneg1y% is the biome for 0, -1
echo %biomenum0x0y% is the biome for 0, 0 -the starting chunk-
echo %biomenum0x1y% is the biome for 0, 1
echo %biomenum1xneg1y% is the biome for 1, -1
echo %biomenum1x0y% is the biome for 1, 0
echo %biomenum1x1y% is the biome for 1, 1
The results obtained by this code are not exactly the same than your code. In your code you add 1 when random is 1 and subtract 1 when random is 4 (and ignore 2 and 3). In my simplification I generate a number between -1 and 2 (instead of 1 to 4) and add 1 when random is 1 and subtract 1 when random is -1 (and ignore 0 and 2); this is done taking the remainder (mod, % operator) of the number divided by 2. The statistical result is, however, the same...
Antonio
PS - I suggest you to use the standard array notation with the subscripts enclosed in square braquets, (like %biomenum[1,0]% or %biomenum[1][0]%instead of %biomenum1x0y% ) for the reasons explained at this post.

Environment Variable " " not defined

: arena1.2
cls
set /a EnemyName= (%random% %% 20)+1
if %EnemyName% equ 1 set %EnemyName1% Racket
if %EnemyName% equ 2 set %EnemyName1% Amok
if %EnemyName% equ 3 set %EnemyName1% Banter
if %EnemyName% equ 4 set %EnemyName1% Drollery
if %EnemyName% equ 5 set %EnemyName1% Keumray
if %EnemyName% equ 6 set %EnemyName1% Quaemi
if %EnemyName% equ 7 set %EnemyName1% Raksul
if %EnemyName% equ 8 set %EnemyName1% Nafl
if %EnemyName% equ 9 set %EnemyName1% Zoda
if %EnemyName% equ 10 set %EnemyName1% Finje
if %EnemyName% equ 11 set %EnemyName1% Liffin
if %EnemyName% equ 12 set %EnemyName1% Reafdaw
if %EnemyName% equ 13 set %EnemyName1% Gleadey
if %EnemyName% equ 14 set %EnemyName1% Gant
if %EnemyName% equ 15 set %EnemyName1% Prooq
if %EnemyName% equ 16 set %EnemyName1% Nazu
if %EnemyName% equ 17 set %EnemyName1% Jape
if %EnemyName% equ 18 set %EnemyName1% Ridiculous
if %EnemyName% equ 19 set %EnemyName1% Vicious
if %EnemyName% equ 20 set %EnemyName1% Ludicrous
if %EnemyName% equ 21 set %EnemyName1% Bedlam
set /a EnemyTitle= (%random% %% 20)+1
if %EnemyTitle% equ 1 set %enemyt% The Hammer
if %EnemyTitle% equ 2 set %enemyt% The Destroyer
if %EnemyTitle% equ 3 set %enemyt% The Mountain
if %EnemyTitle% equ 4 set %enemyt% The Filthy
if %EnemyTitle% equ 5 set %enemyt% The Ugly
if %EnemyTitle% equ 6 set %enemyt% The Handsome
if %EnemyTitle% equ 7 set %enemyt% The Inconsiderate
if %EnemyTitle% equ 8 set %enemyt% The Friendly
if %EnemyTitle% equ 9 set %enemyt% The Biter
if %EnemyTitle% equ 10 set %enemyt% The Muzzled
if %EnemyTitle% equ 11 set %enemyt% The Claw
if %EnemyTitle% equ 12 set %enemyt% The Scarred
if %EnemyTitle% equ 13 set %enemyt% The Burned
if %EnemyTitle% equ 14 set %enemyt% The Flammable
if %EnemyTitle% equ 15 set %enemyt% The Extinguisher of Life
if %EnemyTitle% equ 16 set %enemyt% The Dumb
if %EnemyTitle% equ 17 set %enemyt% The Smart
if %EnemyTitle% equ 18 set %enemyt% The Wise
if %EnemyTitle% equ 19 set %enemyt% The Old
if %EnemyTitle% equ 20 set %enemyt% The Necromancer
if %EnemyTitle% equ 21 set %enemyt% The Revived
echo.
echo And in this corner, %EnemyName1% %enemyt%!
echo.
pause
1
your if is ok. It's your set that is wrong. It should read set EnemyName1=Racket – Stephan 10 mins ago
thanks this works.

I am having trouble figuring out any problems with this pseudo-random encounter engine

I just can't figure it out, I have tried and tried.
::Random Encounters
:ree5000
if %level% geq 1 goto reelvl1
:reelvl1
set /a monsterlvl=(4 * %random%) / 32768 + 1
if %monsterlvl% equ 1 goto monsterlvl1
if %monsterlvl% equ 2 goto monsterlvl2
if %monsterlvl% equ 3 goto monsterlvl3
if %monsterlvl% equ 4 goto monsterlvl4
:monsterlvl1
set /a monstertype=(5 * %random%) / 32768 + 1
if %monstertype% equ 1 set monsdis=Goblin
if %monstertype% equ 2 set monsdis=Imp
if %monstertype% equ 3 set monsdis=Rat
if %monstertype% equ 4 set monsdis=Wolf
if %monstertype% equ 5 set monsdis=Slime
set /a mobhealth=(100 * %random%) / 32768 + 1
if %monstertype% equ 1 set mobhealth=%mobhealth% + 10
if %monstertype% equ 2 set mobhealth=%mobhealth% - 20
if %monstertype% equ 3 set mobhealth=%mobhealth% + 15
if %monstertype% equ 4 set mobhealth=%mobhealth% + 50
if %monstertype% equ 5 set mobhealth=%mobhealth% - 50
if %mobhealth% leq 20 set mobhealth=50
set /a mobstr=(15 * %random%) / 32768 + 1
set /a mobdef=(5 * %random%) / 32768 + 1
goto mobencounter
Is the syntax wrong? Am I doing something wrong?
I just want to know how I can fix this and avoid the problem in the future.

Batch Alternate Data Streams [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am almost done with my game and have employed "Alternate Data Streams" to save some high scores in a ADS. Now i tried to do the same with a color option, to make the game be the same color scheme you set it every time you open it until you want to change it. Here is the code that i am working on:
echo.
echo Color Options - background/text
echo ------------------
echo 0) Black
echo 1) Blue
echo 2) green
echo 3) Aqua
echo 4) Red
echo 5) Purple
echo 6) Yellow
echo 7) White
echo 8) Grey
echo ------------------
set /p BcolorSetting=Background:
set /p TcolorSetting=Text:
echo %BcolorSetting%%TcolorSetting% >>"%~f0:colors"
color <"%~f0:colors"
pause
If you want to see the whole thing it's...
#echo off
REM Produced by Calder Hutchins
REM This is a game
title Memory Game
:begin
set point=0
cls
echo.
echo Memeory Game
echo ------------------
echo 1) Play
echo 2) Instructions
echo 3) High Scores
echo 4) Options
echo ------------------
set /p pick=^>
if %pick%==1 goto one
if %pick%==2 goto two
if %pick%==3 goto three
if %pick%==4 goto four
if %pick%==99 goto test
goto begin
:one
cls
REM Determines the number
if %point% LSS 6 set /a rand=%random% %% (100 - 1 + 1)+ 1
if %point% LSS 12 if %point% GTR 5 set /a rand=%random% %% (500 - 100 + 1)+ 100
if %point% LSS 18 if %point% GTR 11 set /a rand=%random% %% (1000 - 500 + 1)+ 500
if %point% LSS 24 if %point% GTR 17 set /a rand=%random% %% (2000 - 1000 + 1)+ 1000
if %point% LSS 30 if %point% GTR 23 set /a rand=%random% %% (9000 - 1500 + 1)+ 1500
if %point% LSS 36 if %point% GTR 29 set /a rand=%random% %% (19000 - 5000 + 1)+ 5000
if %point% LSS 42 if %point% GTR 35 set /a rand=%random% %% (32000 - 10000 + 1)+ 10000
if %point% LSS 48 if %point% GTR 41 set /a rand=%random% %% (999 - 100 + 1)+ 100
if %point% LSS 48 if %point% GTR 41 set /a randtwo=%random% %% (999 - 100 + 1)+ 100
if %point% GTR 47 set /a rand=%random% %% (9999 - 1000 + 1)+ 1000
if %point% GTR 47 set /a randtwo=%random% %% (9999 - 1000 + 1)+ 1000
echo.
REM Prints the number
if %point% LSS 42 echo %rand%
if %point% GTR 41 set rand=%rand%%randtwo%
if %point% GTR 41 echo %rand%
echo.
ping localhost -n 3 >nul
cls
echo.
echo.
echo.
set /p yourOption=Guess:
REM Determines correct or wrong
if %youroption%==%rand% set /a point=%point% +1 & goto one
cls
echo.
echo You scored: %point%
echo.
set /p name=Type name:
echo %name% - %point% >>"%~f0:scores"
goto begin
:two
cls
echo.
echo The objective of the game is to get as many points as possible. To get points you must correctly retype the numbers that appear on the screen. The numbers show for a short period of time. As you get more points the numbers get longer! When you have lost you will be prompted to enter your name. You can view the highscores too!
echo.
pause
goto begin
:three
cls
echo.
more<"%~f0:scores" | sort
echo.
pause
goto begin
:four
cls
echo.
echo Settings/Options
echo ------------------
echo 1) color
echo ------------------
set /p pickSetting=^>
if %pickSetting%==1 goto oneSetting
goto four
:oneSetting
cls
echo.
echo Color Options - background/text
echo ------------------
echo 0) Black
echo 1) Blue
echo 2) green
echo 3) Aqua
echo 4) Red
echo 5) Purple
echo 6) Yellow
echo 7) White
echo 8) Grey
echo ------------------
set /p BcolorSetting=Background:
set /p TcolorSetting=Text:
echo %BcolorSetting%%TcolorSetting% >>"%~f0:colors"
color <"%~f0:colors"
pause
goto begin
Thank-you in advance guys!
Fortunately the FOR /F can read ADS:
for /f "usebackq" %%C in ("%~f0:colors") do COLOR %%C
It appears that you will have to CD a certain directory. Then, write to a text file within the directory. In the start, read the text file.
Then, overwrite the text file and write the color in it at the point you are stuck in.
If you need to start with settings in any app, you always need a save file.
Writing can be like this:
echo %BcolorSetting%%TcolorSetting% >>"colorsetting.txt"
And when retrieving, do it in the very beginning. Before begin.
It is read like this
set /p %~f0:colors= <colorsetting.txt
Assuming you use those variables. I hope this helps.

Resources