-1

I decided to have a little bit of fun with command prompt yesterday: I wanted to make a randomly generated "maze" with unicode, and got super excited. I have the bones of it pretty much done, but I'm having some issues with my coordinate system to make the image using characters. For some reason, my plethora of variables aren't displaying their values. I don't know what's wrong. It could be the syntax when I made the variables (there are variables in the variable name,) it could be the way I'm setting them up when I'm trying to display them, I don't know.

@echo off
:restart
set x=0
set y=0
:next
set /a chance=%random% %%2
if chance==1 (
    set %x%l%y%=00
)
if chance==2 (
    set %x%l%y%=11
)
set /a x=%x%+1
if x GEQ 10 (
    set x=0
    goto display
    set /a y=%y%+1
    if y GEQ 11 (
        goto display
    )
)
goto next
:display
echo %0l0%%1l0%%2l0%%3l0%%4l0%%5l0%%6l0%%7l0%%8l0%%9l0%
echo %0l1%%1l1%%2l1%%3l1%%4l1%%5l1%%6l1%%7l1%%8l1%%9l1%
echo %0l2%%1l2%%2l2%%3l2%%4l2%%5l2%%6l2%%7l2%%8l2%%9l2%
echo %0l3%%1l3%%2l3%%3l3%%4l3%%5l3%%6l3%%7l3%%8l3%%9l3%
echo %0l4%%1l4%%2l4%%3l4%%4l4%%5l4%%6l4%%7l4%%8l4%%9l4%
echo %0l5%%1l5%%2l5%%3l5%%4l5%%5l5%%6l5%%7l5%%8l5%%9l5%
echo %0l6%%1l6%%2l6%%3l6%%4l6%%5l6%%6l6%%7l6%%8l6%%9l6%
echo %0l7%%1l7%%2l7%%3l7%%4l7%%5l7%%6l7%%7l7%%8l7%%9l7%
echo %0l8%%1l8%%2l8%%3l8%%4l8%%5l8%%6l8%%7l8%%8l8%%9l8%
echo %0l9%%1l9%%2l9%%3l9%%4l9%%5l9%%6l9%%7l9%%8l9%%9l9%
echo:
pause
cls
goto restart

Because I have not replaced "00" and "11" with unicode yet, the desired output would look something like this:

00110000111100111111
11110011110000111100
00000011111111001100
11000000111100110011
00110000111100111111
00110000111100111111
11110011110000111100
00000011111111001100
11000000111100110011
00110000111100111111

Press any key to continue...

Instead it's this mess.

"C:\Users\***\Documents\array.bat"l0%1l0%2l0%3l0%4l0%5l0%6l0%7l0%8l0%9l0
"C:\Users\***\Documents\array.bat"l1%1l1%2l1%3l1%4l1%5l1%6l1%7l1%8l1%9l1
"C:\Users\***\Documents\array.bat"l2%1l2%2l2%3l2%4l2%5l2%6l2%7l2%8l2%9l2
"C:\Users\***\Documents\array.bat"l3%1l3%2l3%3l3%4l3%5l3%6l3%7l3%8l3%9l3
"C:\Users\***\Documents\array.bat"l4%1l4%2l4%3l4%4l4%5l4%6l4%7l4%8l4%9l4
"C:\Users\***\Documents\array.bat"l5%1l5%2l5%3l5%4l5%5l5%6l5%7l5%8l5%9l5
"C:\Users\***\Documents\array.bat"l6%1l6%2l6%3l6%4l6%5l6%6l6%7l6%8l6%9l6
"C:\Users\***\Documents\array.bat"l7%1l7%2l7%3l7%4l7%5l7%6l7%7l7%8l7%9l7
"C:\Users\***\Documents\array.bat"l8%1l8%2l8%3l8%4l8%5l8%6l8%7l8%8l8%9l8
"C:\Users\***\Documents\array.bat"l9%1l9%2l9%3l9%4l9%5l9%6l9%7l9%8l9%9l9

Press any key to continue . . .

I have plans to expand on this concept, but I cannot go any further without addressing this issue. Please help!

  • 4
    `%0` resolves to the currently running batch file name - so you are exacttly getting what your echo requires. Variable names can't begin with a digit. Also `if chance==1` will never be true to compare the **content** use `if %chance%==1` –  Feb 12 '19 at 22:57
  • As you're going to be replacing the numbers with unicode characters, I suppose it's a non issue. Technically, you're asking us to help you with an issue with a batch file you're never going to need. Anyhow, in the meantime, why not consider changing it to `l%y%%x%` from `%x%l%y%`. – Compo Feb 12 '19 at 23:46
  • Never use a digit as first character for name of an environment variable. Let an environment variable name always start with a non-digit character. Run in a command prompt window `call /?` to get output the help for this command explaining how to reference batch file arguments with `%0`, `%1`, `%2`, ... without or with a modifier. That is the reason why an environment variable name should never start with a digit. And run also `set /?` to get displayed the help for this command explaining also __arithmetic expression__. `set /a y=y+1` is better than `set /a y=%y%+1` and best is `set /a y+=1`. – Mofi Feb 13 '19 at 06:19

1 Answers1

1

I strongly suggest you to use the standard array notation when you use arrays, that is, with the indices enclosed in square braquets. You may read further details at Arrays, linked lists and other data structures in cmd.exe (batch) script

I suggest you to use for /L command to assemble loops.

For example:

@echo off
setlocal EnableDelayedExpansion

:restart
for /L %%x in (0,1,9) do (
   for /L %%y in (0,1,9) do (
      set /A chance=!random! %% 2
      if !chance! == 1 (
         set "p[%%x][%%y]=00"
      ) else (
         set "p[%%x][%%y]=11"
      )
   )
)

:display
for /L %%x in (0,1,9) do (
   set "out="
   for /L %%y in (0,1,9) do (
      set "out=!out!!p[%%x][%%y]!
   )
   echo !out!
)
echo/
pause
cls
goto restart
Aacini
  • 65,180
  • 12
  • 72
  • 108