0

I'm trying to make armor and weapon buying and selling systems in my game, but I'm a beginner programmer and have no idea how to use the 'for' statement. I have some functional code at the moment, but it's absolutely enormous and junky. I want to try to use a for statement to shorten it, but I don't know how. Can someone help me? Thanks. I'm currently using this junk code:

cls
echo What armor do you want to change to?
echo[
echo 1. None
echo 2. Leather
echo 3. Wooden
echo 4. Stone
echo 5. Bronze
echo 6. Iron
echo 7. Titanium
echo 8. Adamantium
echo[
choice /c 12345678 /m "What'll it be, %name%?"
if %errorlevel%==8 (
cls
set armor=Adamantium
echo It is done!
pause
goto start
)
if %errorlevel%==7 (
cls
set armor=Titanium
echo It is done!
pause
goto start
)
if %errorlevel%==6 (
cls
set armor=Iron
echo It is done!
pause
goto start
)
if %errorlevel%==5 (
cls
set armor=Bronze
echo It is done!
pause
goto start
)
if %errorlevel%==4 (
cls
set armor=Stone
echo It is done!
pause
goto start
)
if %errorlevel%==3 (
cls
set armor=Wooden
echo It is done!
pause
goto start
)
if %errorlevel%==2 (
cls
set armor=Leather
echo It is done!
pause
goto start
)
cls
set armor=None
echo It is done!
pause
goto start
  • 1
    The `for` command is for iterating things (i.e., do something for each item in a collection). I don't think it does what you think it does. I would recommend reading the `for /?` help first. (I would also recommend starting with PowerShell rather than `cmd` batch/shell script code; it has has better code constructs and does away with `goto`.) – Bill_Stewart Aug 05 '20 at 21:28
  • What you want to do is create a key value pair. I know we have a good Q&A here on how to do that. I just can't find it right now. – Squashman Aug 05 '20 at 21:34
  • You want to use an [_array_](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990), or an [_associative array_](https://stackoverflow.com/questions/35639223/find-all-ocurences-of-letter-change-to-diffrent-letter/35642840#35642840) when the subscripts are not numbers. – Aacini Aug 06 '20 at 21:22

3 Answers3

1

Essentially what you can do is what some people call a key value pair or a lookup map. This can be done two ways that I know of.

The first option sets all the key value pairs into a single variable. Then two string substitutions are done to extract the correct value from the key index set by the choice command.

@echo off
cls
SET keymap=1-None;2-Leather;3-Wooden;4-Stone;5-Bronze;6-Iron;7-Titanium;8-Adamantium
echo What armor do you want to change to?
echo(
echo 1. None
echo 2. Leather
echo 3. Wooden
echo 4. Stone
echo 5. Bronze
echo 6. Iron
echo 7. Titanium
echo 8. Adamantium
echo(
choice /c 12345678 /m "What'll it be?"
set "index=%errorlevel%"
CALL SET armor=%%keymap:*%index%-=%%
SET armor=%armor:;=&rem.%
echo Armor = %armor%
pause

The second option creates an array of the key value pairs. The array index is set by the choice command again and then used to get the corresponding array value.

@echo off
setlocal enabledelayedexpansion
set "m=0"
for %%G in (None Leather Wooden Stone Bronze Iron Titanium Adamantium) do (
    set /A m+=1
    set "armor[!m!]=%%G"
)
echo(
echo 1. None
echo 2. Leather
echo 3. Wooden
echo 4. Stone
echo 5. Bronze
echo 6. Iron
echo 7. Titanium
echo 8. Adamantium
echo(
choice /c 12345678 /m "What'll it be?"
set "index=%errorlevel%"

set "armor=!armor[%index%]!"
echo Armor = %armor%
pause
endlocal
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • 2
    Good method, plus 1..If I may, In the second example, you can add `echo !m!. %%G` as last line in the loop and then get rid of the entire `echo N. Name` list, before `choice` – Gerhard Aug 06 '20 at 05:53
  • 2
    you can also get rid of `set "index=%errorlevel%"` and change to `set "armor=!armor[%index%]!"` tp `set armor=!armor[%errorlevel%]!` – Gerhard Aug 06 '20 at 07:54
  • Both work. I ended up using the keymap version because 'setlocal enabledelayedexpansion' was causing some issues. Apparently if you endlocal before you save the data it doesn't save, and it gets rid of all the exclimation marks in your sentences, along with potential sentences afterward. But they both work; thanks! – Joshua R. Liu Aug 08 '20 at 14:57
1

To give a slightly shorter version of @Squashman's answer, and this is just a shortened version, Do NOT mark it as the correct answer as the original was not posted my me.

@echo off & setlocal enabledelayedexpansion
set "m=0"
for %%G in (None Leather Wooden Stone Bronze Iron Titanium Adamantium) do (
    set /A m+=1
    set "armor[!m!]=%%G"
    echo !m!. %%G
)
echo(
choice /c 12345678 /m "What'll it be?"
set armor=!armor[%errorlevel%]!
echo %armor% & pause
goto :start
Gerhard
  • 22,678
  • 7
  • 27
  • 43
1

Another version! ;)

@echo off & setlocal EnableDelayedExpansion

set "armors=None Leather Wooden Stone Bronze Iron Titanium Adamantium"
set "m=1" & set "armor[!m!]=%armors: =" & set /A m+=1 & set "armor[!m!]=%"

for /L %%i in (1,1,%m%) do echo %%i. !armor[%%i]!
echo/
choice /n /c 12345678 /m "What'll it be?"
set "armor=!armor[%errorlevel%]!"
echo %armor%
Aacini
  • 65,180
  • 12
  • 72
  • 108