-2

This is another question I have for my Yahtzee game. I need to set a "FOR" loop to run 6 times.

    :roll
    cls
    pause
    SET /A dice=%RANDOM% %%6 +1
      if %dice%==1 (
        echo %dice1a%
        echo %dice1b%
        echo %dice1c%
        echo %dice1d%
        echo %dice1e%
        echo %dice1f%
        echo %dice1g%
      )
      if %dice%==2  (
        echo %dice2a%
        echo %dice2b%
        echo %dice2c%
        echo %dice2d%
        echo %dice2e%
        echo %dice2f%
        echo %dice2g%
      )
      if %dice%==3  (
        echo %dice3a%
        echo %dice3b%
        echo %dice3c%
        echo %dice3d%
        echo %dice3e%
        echo %dice3f%
        echo %dice3g%
      )
      if %dice%==4  (
        echo %dice4a%
        echo %dice4b%
        echo %dice4c%
        echo %dice4d%
        echo %dice4e%
        echo %dice4f%
        echo %dice4g%
      )
      if %dice%==5  (
        echo %dice5a%
        echo %dice5b%
        echo %dice5c%
        echo %dice5d%
        echo %dice5e%
        echo %dice5f%
        echo %dice5g%
      )
      if %dice%==6  (
        echo %dice6a%
        echo %dice6b%
        echo %dice6c%
        echo %dice6d%
        echo %dice6e%
        echo %dice6f%
        echo %dice6g%
        )

This is the code I want to put in a for loop, and I want it to run 6 times to echo 6 dice faces. This is only a part of the code I am making. If you have anything useful to tell me (Like making a multistring variable, for instance), please comment a link, or tell me how to do it.

IronFlame
  • 29
  • 9
  • 1
    https://stackoverflow.com/questions/1355791/how-do-you-loop-in-a-windows-batch-file – sohaib Sep 18 '17 at 17:12
  • I suggest you to read [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Sep 19 '17 at 11:41

2 Answers2

0

There's something special to consider when doing batch files. It's called loop unrolling. That is, %var% will be expanded prior to command execution, which would render %RANDOM% static. To workaround this you need setlocal enabledelayedexpansion at the top of your script and use !var! instead of %var%:

for %%i in (1 2 3 4 5 6) do (
  set /a dice=!RANDOM! %%6+1
  echo dice%%i = !dice!
)

Note you don't need to change %%6 because it's just a way to mask the %.

yacc
  • 2,915
  • 4
  • 19
  • 33
  • 1
    Thanks for telling me these answers I don't have time to look at them yet but I will confirm if they work or not when I have the chance. – IronFlame Sep 20 '17 at 21:39
0

Instead of using all the IFs use delayedexpansion to reference the typefaces. Either by using a pseudo call and doubled percentsigns or with an initial setlocal enabledelayedexpansion and referencing var contents with !

Setlocal EnableDelayedExpansion
:roll
cls
pause
SET /A dice=%RANDOM% %%6 +1
echo !dice%dice%a!
echo !dice%dice%b!
echo !dice%dice%c!
echo !dice%dice%d!
echo !dice%dice%e!
echo !dice%dice%f!
echo !dice%dice%g!

Or

:roll
cls
pause
SET /A dice=%RANDOM% %%6 +1
call echo %%dice%dice%a%%
call echo %%dice%dice%b%%
call echo %%dice%dice%c%%
call echo %%dice%dice%d%%
call echo %%dice%dice%e%%
call echo %%dice%dice%f%%
cALL echo %%dice%dice%g%%