-1

So i'm working on a "Minesweeper level generator" and this is the code to set the text to be displayed in positions. (□ is Clear, O is a bomb (working on the number indicators))

for %%v in (a1,a2,a3,a4,a5,a6,a7,a8,b1,b2,b3,b4,b5,b6,b7,b8,c1,c2,c3,c4,c5,c6,c7,c8,d1,d2,d3,d4,d5,d6,d7,d8,e1,e2,e3,e4,e5,e6,e7,e8,f1,f2,f3,f4,f5,f6,f7,f8,g1,g2,g3,g4,g5,g6,g7,g8,h1,h2,h3,h4,h5,h6,h7,h8) do (
if %bombs% gtr 0 (if %random% gtr 16383 (set e_a1=O && set /a "bombs-=1") else (set e_%%v=□))
)

or this:

for %%v in (a1,a2,a3,a4,a5,a6,a7,a8,b1,b2,b3,b4,b5,b6,b7,b8,c1,c2,c3,c4,c5,c6,c7,c8,d1,d2,d3,d4,d5,d6,d7,d8,e1,e2,e3,e4,e5,e6,e7,e8,f1,f2,f3,f4,f5,f6,f7,f8,g1,g2,g3,g4,g5,g6,g7,g8,h1,h2,h3,h4,h5,h6,h7,h8) do (
set _r=
set /a "_r=(%RANDOM%*6/32768)+1"
if %bombs% gtr 0 (if %_r% gtr 3 (set e_a1=O && set /a "bombs-=1") else (set e_%%v=□))
)

It should work fine, but when I tried this out in cmd, I found that all the assigned random numbers are the same each time.

Why does it do that and how can I fix it?

  • 1
    `Enable DelayedExpansion`, and use `!RANDOM!` and `!_r!`. – Compo Jan 25 '21 at 02:04
  • Tried replacing %random% with !random! (and enabled delayed expansion) – Sean Armecin Jan 25 '21 at 02:29
  • Now it's now always 15 – Sean Armecin Jan 25 '21 at 02:29
  • 1
    Without you posting sufficient code for us to reproduce your issue, it is unlikely we will be able to assist you further. Please post enough code for us to be able to run ourselves. We need the code in which `bombs` was defined and an indication of nesting levels, and the code you're using to show/view the expanded variables values. Please see [mcve], for a better understanding of the expectations. – Compo Jan 25 '21 at 02:43
  • @SeanArmecin, what is always 15? If you updated any code, then [edit] your question so we know what the current state of the code is. – Squashman Jan 25 '21 at 03:19

1 Answers1

0
FOR /L %%c IN (1,1,8) DO FOR %%b IN (a b c d e f g h) DO CALL SET /a #%%b%%c=1 + (%%random%% %%%% 6)
SET #

# prefixed to variablename for ease of display of results

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Fixed! New code: `for /L %%c in (1,1,8) do for %%b in (a b c d e f g h) do ( set /a "_r=(%RANDOM%*6/32768)+1" if !bombs! gtr 0 (if !_r! gtr 3 (set e_%%b%%c=O && set /a "bombs-=1") else (set e_%%b%%c=□)) else (set e_%%b%%c=□) ) ` Thanks to your help! – Sean Armecin Jan 25 '21 at 03:24
  • @SeanArmecin, while Magoo's code does provide for better readability, it is not what solved your code issues. The use of Delayed Expansion is what solved your code issue so I am not sure why you chose to mark this as the answer to your question. No offense to you Magoo. Your code is always solid. – Squashman Jan 25 '21 at 03:33