Batch - Cleaner way to find largest given variable? - batch-file

The following code works for my problem but as you can see it is heavily protracted. So i was wondering if there was a more efficient way to find the largest of 6 given variables.
Code
#echo off
Set /a picture0 = 4
Set /a picture1 = 10
Set /a picture2 = 9
Set /a picture3 = 6
Set /a picture4 = 71
Set /a picture5 = 6
Set /a picture6 = 5
Call :TestPicture0
Call :TestPicture1
Call :TestPicture2
Call :TestPicture3
Call :TestPicture4
Call :TestPicture5
Call :TestPicture6
#pause
GoTo :EOF
So I have the six variables I want to use right, and I call each of them separately because they are rather long.
:TestPicture0
Set /a Test01 = %picture0% - %picture0%
Set /a Result01 = 0
Set /a Test02 = %picture0% - %picture2%
Set /a Result02 = 0
Set /a Test03 = %picture0% - %picture3%
Set /a Result03 = 0
Set /a Test04 = %picture0% - %picture4%
Set /a Result04 = 0
Set /a Test05 = %picture0% - %picture5%
Set /a Result05 = 0
Set /a Test06 = %picture0% - %picture6%
Set /a Result06 = 0
if %Test01% GEQ 0 (Set /a Result01 = 1)else (Set /a Result01 = 0)
if %Test02% GEQ 0 (Set /a Result02 = 1)else (Set /a Result02 = 0)
if %Test03% GEQ 0 (Set /a Result03 = 1)else (Set /a Result03 = 0)
if %Test04% GEQ 0 (Set /a Result04 = 1)else (Set /a Result04 = 0)
if %Test05% GEQ 0 (Set /a Result05 = 1)else (Set /a Result05 = 0)
if %Test06% GEQ 0 (Set /a Result06 = 1)else (Set /a Result06 = 0)
Set /a Picture0Final = %Result06% + %Result05% + %Result04% + %Result03% + %Result02% + %Result01%
If %Picture0Final% EQU 6 (Echo Picture Zero) Else (Echo no)
GoTo :EOF
:TestPicture1
Set /a Test10 = %picture1% - %picture0%
Set /a Result10 = 0
Set /a Test12 = %picture1% - %picture2%
Set /a Result12 = 0
Set /a Test13 = %picture1% - %picture3%
Set /a Result13 = 0
Set /a Test14 = %picture1% - %picture4%
Set /a Result14 = 0
Set /a Test15 = %picture1% - %picture5%
Set /a Result15 = 0
Set /a Test16 = %picture1% - %picture6%
Set /a Result16 = 0
if %Test10% GEQ 0 (Set /a Result10 = 1)else (Set /a Result10 = 0)
if %Test12% GEQ 0 (Set /a Result12 = 1)else (Set /a Result12 = 0)
if %Test13% GEQ 0 (Set /a Result13 = 1)else (Set /a Result13 = 0)
if %Test14% GEQ 0 (Set /a Result14 = 1)else (Set /a Result14 = 0)
if %Test15% GEQ 0 (Set /a Result15 = 1)else (Set /a Result15 = 0)
if %Test16% GEQ 0 (Set /a Result16 = 1)else (Set /a Result16 = 0)
Set /a Picture1Final = %Result16% + %Result15% + %Result14% + %Result13% + %Result12% + %Result10%
If %Picture1Final% EQU 6 (Echo Picture one) Else (Echo no)
GoTo :EOF
:TestPicture2
Set /a Test20 = %picture2% - %picture0%
Set /a Result20 = 0
Set /a Test21 = %picture2% - %picture1%
Set /a Result21 = 0
Set /a Test23 = %picture2% - %picture3%
Set /a Result23 = 0
Set /a Test24 = %picture2% - %picture4%
Set /a Result24 = 0
Set /a Test25 = %picture2% - %picture5%
Set /a Result25 = 0
Set /a Test26 = %picture2% - %picture6%
Set /a Result26 = 0
if %Test20% GEQ 0 (Set /a Result20 = 1)else (Set /a Result20 = 0)
if %Test21% GEQ 0 (Set /a Result21 = 1)else (Set /a Result21 = 0)
if %Test23% GEQ 0 (Set /a Result23 = 1)else (Set /a Result23 = 0)
if %Test24% GEQ 0 (Set /a Result24 = 1)else (Set /a Result24 = 0)
if %Test25% GEQ 0 (Set /a Result25 = 1)else (Set /a Result25 = 0)
if %Test26% GEQ 0 (Set /a Result26 = 1)else (Set /a Result26 = 0)
Set /a Picture2Final = %Result26% + %Result25% + %Result24% + %Result23% + %Result21% + %Result20%
If %Picture2Final% EQU 6 (Echo Picture Two) Else (Echo no)
GoTo :EOF
:TestPicture3
Set /a Test30 = %picture3% - %picture0%
Set /a Result30 = 0
Set /a Test31 = %picture3% - %picture1%
Set /a Result11 = 0
Set /a Test32 = %picture3% - %picture2%
Set /a Result32 = 0
Set /a Test34 = %picture3% - %picture4%
Set /a Result34 = 0
Set /a Test35 = %picture3% - %picture5%
Set /a Result35 = 0
Set /a Test36 = %picture3% - %picture6%
Set /a Result36 = 0
if %Test30% GEQ 0 (Set /a Result30 = 1)else (Set /a Result30 = 0)
if %Test31% GEQ 0 (Set /a Result31 = 1)else (Set /a Result31 = 0)
if %Test32% GEQ 0 (Set /a Result32 = 1)else (Set /a Result32 = 0)
if %Test34% GEQ 0 (Set /a Result34 = 1)else (Set /a Result34 = 0)
if %Test35% GEQ 0 (Set /a Result35 = 1)else (Set /a Result35 = 0)
if %Test36% GEQ 0 (Set /a Result36 = 1)else (Set /a Result36 = 0)
Set /a Picture3Final = %Result36% + %Result35% + %Result34% + %Result32% + %Result31% + %Result30%
If %Picture3Final% EQU 6 (Echo Picture Three) Else (Echo no)
GoTo :EOF
:TestPicture4
Set /a Test40 = %picture4% - %picture0%
Set /a Result40 = 0
Set /a Test42 = %picture4% - %picture2%
Set /a Result42 = 0
Set /a Test43 = %picture4% - %picture3%
Set /a Result43 = 0
Set /a Test41 = %picture4% - %picture1%
Set /a Result41 = 0
Set /a Test45 = %picture4% - %picture5%
Set /a Result45 = 0
Set /a Test46 = %picture4% - %picture6%
Set /a Result46 = 0
if %Test40% GEQ 0 (Set /a Result40 = 1)else (Set /a Result40 = 0)
if %Test42% GEQ 0 (Set /a Result42 = 1)else (Set /a Result42 = 0)
if %Test43% GEQ 0 (Set /a Result43 = 1)else (Set /a Result43 = 0)
if %Test41% GEQ 0 (Set /a Result41 = 1)else (Set /a Result41 = 0)
if %Test45% GEQ 0 (Set /a Result45 = 1)else (Set /a Result45 = 0)
if %Test46% GEQ 0 (Set /a Result46 = 1)else (Set /a Result46 = 0)
Set /a Picture4Final = %Result46% + %Result45% + %Result41% + %Result43% + %Result42% + %Result40%
If %Picture4Final% GEQ 6 (Echo Picture Four) Else (Echo no)
GoTo :EOF
:TestPicture5
Set /a Test50 = %picture5% - %picture0%
Set /a Result50 = 0
Set /a Test51 = %picture5% - %picture1%
Set /a Result51 = 0
Set /a Test52 = %picture5% - %picture2%
Set /a Result52 = 0
Set /a Test53 = %picture5% - %picture3%
Set /a Result53 = 0
Set /a Test54 = %picture5% - %picture5%
Set /a Result54 = 0
Set /a Test56 = %picture5% - %picture6%
Set /a Result56 = 0
if %Test50% GEQ 0 (Set /a Result50 = 1)else (Set /a Result50 = 0)
if %Test51% GEQ 0 (Set /a Result51 = 1)else (Set /a Result51 = 0)
if %Test52% GEQ 0 (Set /a Result52 = 1)else (Set /a Result52 = 0)
if %Test53% GEQ 0 (Set /a Result53 = 1)else (Set /a Result53 = 0)
if %Test54% GEQ 0 (Set /a Result54 = 1)else (Set /a Result54 = 0)
if %Test56% GEQ 0 (Set /a Result56 = 1)else (Set /a Result56 = 0)
Set /a Picture5Final = %Result56% + %Result54% + %Result53% + %Result52% + %Result51% + %Result50%
If %Picture5Final% EQU 6 (Echo Picture Five) Else (Echo no)
GoTo :EOF
:TestPicture6
Set /a Test60 = %picture6% - %picture0%
Set /a Result60 = 0
Set /a Test61 = %picture6% - %picture1%
Set /a Result61 = 0
Set /a Test62 = %picture6% - %picture2%
Set /a Result62 = 0
Set /a Test63 = %picture6% - %picture3%
Set /a Result63 = 0
Set /a Test64 = %picture6% - %picture6%
Set /a Result64 = 0
Set /a Test65 = %picture6% - %picture6%
Set /a Result65 = 0
if %Test60% GEQ 0 (Set /a Result60 = 1)else (Set /a Result60 = 0)
if %Test61% GEQ 0 (Set /a Result61 = 1)else (Set /a Result61 = 0)
if %Test62% GEQ 0 (Set /a Result62 = 1)else (Set /a Result62 = 0)
if %Test63% GEQ 0 (Set /a Result63 = 1)else (Set /a Result63 = 0)
if %Test64% GEQ 0 (Set /a Result64 = 1)else (Set /a Result64 = 0)
if %Test65% GEQ 0 (Set /a Result65 = 1)else (Set /a Result65 = 0)
Set /a Picture6Final = %Result65% + %Result64% + %Result63% + %Result62% + %Result61% + %Result60%
If %Picture6Final% EQU 6 (Echo Picture Six) Else (Echo no)
GoTo :EOF
If yall know a way to make this better please let me know :D----<-<
*Edit: fixed some 0's to ones ... literally

you asked how to
find the largest of 6 given variables
There is no need to minus each and compare the result to 0. It isn't even neccessary to compare the variables.
#echo off
====SETLOCAL EnableDelayedExpansion EnableExtensions
set/a"picture0=4,picture1=10,picture2=9,picture3=6,picture4=71,picture5=6,picture6=5,MAX=0"
FOR /L %%# in (0 1 6) do if !picture%%#! GTR !max! set/a"max=!picture%%#!,max_var=%%#"
echo MAX is picture%max_var%=%max%
Limitations:
If %picture3% == %picture4% == 6, %max% will be picture3.
Works for 32-bits integers only. See Windows batch file IF failure - How can 30000000000000 equal 40000000000?
In this case, because the variables are in a "list", I used a FOR /L loop . But in case it's not, use FOR.

An alternative to #HackingAddict1337's answer:
A For /F loop using the Set command in conjunction with the Array variable prefix can be used to retrieve the highest value withought needing to know / reference the size of the Array:
Setlocal ENABLEdelayedexpansion
For /F "Tokens=1,2 Delims==" %%A in ('Set picture') Do If "!Max!" == "" (Set "Max=%%B" & Set "MaxV=%%A") Else If %%B GTR !Max! (Set "Max=%%B" & Set "MaxV=%%A")

Related

How can I deal with this error in my Batch script?

I'm not sure what is happening as batch script closes almost immediately when I try to run it. It's pretty simple it should test whether a number(num) is a prime Number. If not it continues until num1 is bigger than num.
echo off
title Prime Numbers
cls
set /a prime=7
set /a num1=2
set /a num2=2
:do1
if %prime% == %num1% * %num2% goto do_if_1
if else %prime% LSS %num1% * %num2% set /a num2=%num2%+1
if else %prime% GTR %num1% * %num2% goto do_if_2
if else %prime% LSS %num1% * 1 goto do_if_3
goto do1
:do_if_1
set /a prime=%prime%+1
set /a num1=2
set /a num2=2
goto do1
:do_if_2
set /a num1=%num1%+1
set /a num2=2
goto do2
:do_if_3
echo %prime%
goto do_if_1
Edit: 6n±1 method
In case it's useful to anyone, here's a much more efficient method for testing whether a number is prime or composite. It's a Batch language adaptation of the 6n±1 algorithm mentioned by several people on this post.
#echo off & setlocal enabledelayedexpansion
if "%~1"=="" goto usage
for /f "delims=0123456789" %%I in ("%~1") do goto usage
if %~1 LEQ 1 (
echo %~1 is neither prime nor composite.
exit /b 1
) else if %~1 LEQ 3 (
echo %~1 is prime.
exit /b 0
)
set /a "mod2or3 = ^!(%~1 %% 2) | ^!(%~1 %% 3), half = %~1 / 2"
if %mod2or3% EQU 1 (
echo %~1 is composite.
exit /b 1
)
for /L %%I in (5,6,%half%) do (
set /a "n6pm1 = ^!(%~1 %% %%I) | ^!(%~1 %% (%%I + 2))"
if !n6pm1! EQU 1 (
echo %~1 is composite.
exit /b 1
)
)
echo %~1 is prime.
goto :EOF
:usage
echo Usage: %~nx0 integer
goto :EOF
Original answer:
Thought I might accept the challenge.
#echo off
setlocal enabledelayedexpansion
if "%~1"=="" goto usage
for /f "delims=0123456789" %%I in ("%~1") do goto usage
set /a limit = %~1 - 1
for /L %%I in (2,1,%limit%) do (
set /a modulo = %~1 %% %%I
if !modulo! equ 0 (
echo %~1 is composite ^(divisible by %%I^).
goto :EOF
)
)
echo %~1 is prime.
goto :EOF
:usage
echo Usage: %~nx0 integer
You had commented under Dominique's answer, "So how do I fix this?" Take the first part of your script:
set /a prime=7
set /a num1=2
set /a num2=2
:do1
if %prime% == %num1% * %num2% goto do_if_1
Since you've been informed by both Squashman and myself that if %prime% == %num1% * %num2% is not going to work as you intend, you should now know that you need to insert an additional set /a above your if statement to perform %num1% * %num2%.
set /a prime=7
set /a num1=2
set /a num2=2
:do1
set /a product = num1 * num2
if %prime% == %product% goto do_if_1
Let's continue...
...
if %prime% == %product% goto do_if_1
if else %prime% LSS %num1% * %num2% set /a num2=%num2%+1
There are two immediate problems here. #1, I think you probably meant else if, not if else. #2, in batch scripting, else needs to be included as a part of the if command you're else-ing. This means else either needs to be appended to the end of the previous line, or you should use some parentheses. Rewrite it like this:
if %prime% == %product% (
goto do_it_1
) else if %prime% LSS %product% (
set /a num2 += 1
) else etc...
The first thing I advise you, is to get rid of the echo off, like this you can follow what your script is doing:
D:\>set /a prime=7
D:\>set /a num1=2
D:\>set /a num2=2
D:\>if 7 == 2 * 2 goto do_if_1
7 was unexpected at this time. => this is causing your problem!
D:\>if else 7 LSS 2 * 2 set /a num2=2+1
D:\>

Rubik's cube scrambler [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I made this awesome (horrible) Rubik's cube scrambler a while ago, but when I look at the code, it's just too much, I know there is a very simpler way of doing this. If there is anyone who can modify certain bits to make the code more compact, I would really appreciate it.
This is purely for my understanding in batch, and hopefully others can learn from anything you answer.
Here is the code:
#echo off
mode con: cols=62 lines=10
title Cube Scrambler
color 0a
Setlocal EnableDelayedExpansion
set ct = 0
:strt
set scram=BDEFLMRSUbdflru
set /a ct = ct +1
set /a a=%random%%%14
set /a b=%random%%%14
set /a c=%random%%%14
set /a d=%random%%%14
set /a e=%random%%%14
set /a f=%random%%%14
set /a g=%random%%%14
set /a h=%random%%%14
set /a i=%random%%%14
set /a j=%random%%%14
set /a k=%random%%%14
set /a l=%random%%%14
set /a m=%random%%%14
set /a n=%random%%%14
set /a o=%random%%%14
set a=!scram:~%a%,1!
set b=!scram:~%b%,1!
set c=!scram:~%c%,1!
set d=!scram:~%d%,1!
set e=!scram:~%e%,1!
set f=!scram:~%f%,1!
set g=!scram:~%g%,1!
set h=!scram:~%h%,1!
set i=!scram:~%i%,1!
set j=!scram:~%j%,1!
set k=!scram:~%k%,1!
set l=!scram:~%l%,1!
set m=!scram:~%m%,1!
set n=!scram:~%n%,1!
set o=!scram:~%o%,1!
set /a z1=%random%%%2 +1
set /a z2=%random%%%2 +1
set /a z3=%random%%%2 +1
set /a z4=%random%%%2 +1
set /a z5=%random%%%2 +1
set /a z6=%random%%%2 +1
set /a z7=%random%%%2 +1
set /a z8=%random%%%2 +1
set /a z9=%random%%%2 +1
set /a z10=%random%%%2 +1
set /a z11=%random%%%2 +1
set /a z12=%random%%%2 +1
set /a z13=%random%%%2 +1
set /a z14=%random%%%2 +1
set /a z15=%random%%%2 +1
if %z1% == 2 goto 2x2
set x1='
:2x2
if %z2% == 2 goto 3x3
set x2='
:3x3
if %z3% == 2 goto 4x4
set x3='
:4x4
if %z4% == 2 goto 5x5
set x4='
:5x5
if %z5% == 2 goto 6x6
set x5='
:6x6
if %z6% == 2 goto 7x7
set x6='
:7x7
if %z7% == 2 goto 8x8
set x7='
:8x8
if %z8% == 2 goto 9x9
set x8='
:9x9
if %z9% == 2 goto 10x10
set x9='
:10x10
if %z10% == 2 goto 11x11
set x10='
:11x11
if %z11% == 2 goto 12x12
set x11='
:12x12
if %z12% == 2 goto 13x13
set x12='
:13x13
if %z13% == 2 goto 14x14
set x13='
:14x14
if %z14% == 2 goto 15x15
set x14='
:15x15
if %z15% == 2 goto ans
set x15='
:ans
echo.
echo Scramble: %ct%
echo.
echo.
echo %a%%x1% %b%%x2% %c%%x3% %d%%x4% %e%%x5% %f%%x6% %g%%x7% %h%%x8% %i%%x9% %j%%x10% %k%%x11% %l%%x12% %m%%x13% %n%%x14% %o%%x15%
echo.
echo.
echo { Enter } = New scramble
echo Made by: Ruan Swanepoel
pause>nul
cls
set x1=
set x2=
set x3=
set x4=
set x5=
set x6=
set x7=
set x8=
set x9=
set x10=
set x11=
set x12=
set x13=
set x14=
set x15=
goto strt
Maybe something like this approach using FOR loops?
#ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
mode con: cols=62 lines=10
title Cube Scrambler
color 0a
set "ct=0"
set "discontinue="
:strt
set "scram=BDEFLMRSUbdflru"
set /a "ct+=1"
for %%x in (a b c d e f g h i j k l m n o) do (
set /a "xx=!random!%%15"
call set "%%x=%%scram:~!xx!,1%%"
)
for /L %%y in (1, 1, 15) do (
set /a "yy=!random!%%2"
if !yy! EQU 1 (
set "x%%y= "
) else (
set "x%%y='"
)
)
:ans
echo(
echo Scramble: %ct%
echo(
echo(
echo %a%%x1% %b%%x2% %c%%x3% %d%%x4% %e%%x5% %f%%x6% %g%%x7% %h%%x8% %i%%x9% %j%%x10% %k%%x11% %l%%x12% %m%%x13% %n%%x14% %o%%x15%
echo(
echo(
echo { Enter } = New scramble
echo Made by: Ruan Swanepoel
set /P "discontinue="
if defined discontinue goto :endlocal
cls
goto strt
:endlocal
color
title %CD%
rem mode con: cols=80 lines=25
ENDLOCAL
goto :eof
It could probably be trimmed down a bit more (especially if I wanted to tweak the spacing), but I got it down to 29% of what it was, so there's that.
#echo off
setlocal enabledelayedexpansion
mode con cols=62 lines=10
title Cube Scrambler
color 0A
set ct=0
:begin
set scram=BDEFLMRSUbdflru
set /a ct+=1
for /l %%A in (1,1,15) do (
set /a mod_15=!random!%%15
for /F "delims=" %%B in ("!mod_15!") do (
set mov[%%A]=!scram:~%%B,1!
)
set /a inv[%%A]=!random!%%2
if !inv[%%A]! equ 0 (
set "inv[%%A]= "
) else (
set "inv[%%A]=' "
)
)
echo.
echo Scramble: !ct!
echo.
for /l %%A in (1,1,15) do (
set /p"=!mov[%%A]!!inv[%%A]!"<nul
)
echo.
echo.
echo { Enter } = New scramble
echo Made by: Ruan Swanepoel
pause>nul
cls
goto begin
This version is even smaller (just above 18% of the original) at the expense of some readability:
#echo off
setlocal enabledelayedexpansion
mode con cols=45 lines=10
set c=0
set s=BDEFLMRSUbdflru
:a
set /a c+=1
title Cube Scramble !c!
for /l %%A in (1,1,15) do (
set /a r=!random!%%15,i%%A=!random!%%2
for /F %%B in ("!r!") do set m%%A=!s:~%%B,1!
if !i%%A! equ 0 (set i%%A= ) else (set i%%A=' )
)
echo.
for /l %%A in (1,1,15) do set /p=!m%%A!!i%%A!<nul
echo.
echo Enter: Rescramble
pause>nul
cls
goto a

Using multiple colors in matrix batch file

I've been messing around with batch files a bit, and have this code running. I did not write it, I found it, and it just runs random blue numbers in command prompt until you exit. My question is this, I want to make one where every character is a random color. so essentially, it'll look like a rainbow vomited skittles on my screen. But i digress. That is my challenge, and so far i have failed at solving it. Any ideas would be great!
#echo off
color 01
cls
:a
set /a a=%random%
if %a% geq 16384 set /a aa=1
if %a% lss 16384 set /a aa=0
set /a b=%random%
if %b% geq 16384 set /a bb=1
if %b% lss 16384 set /a bb=0
set /a c=%random%
if %c% geq 16384 set /a cc=1
if %c% lss 16384 set /a cc=0
set /a d=%random%
if %d% geq 16384 set /a dd=1
if %d% lss 16384 set /a dd=0
set /a e=%random%
if %e% geq 16384 set /a ee=1
if %e% lss 16384 set /a ee=0
set /a f=%random%
if %f% geq 16384 set /a ff=1
if %f% lss 16384 set /a ff=0
set /a g=%random%
if %g% geq 16384 set /a gg=1
if %g% lss 16384 set /a gg=0
set /a h=%random%
if %h% geq 16384 set /a hh=1
if %h% lss 16384 set /a hh=0
set /a i=%random%
if %i% geq 16384 set /a ii=1
if %i% lss 16384 set /a ii=0
set /a j=%random%
if %j% geq 16384 set /a jj=1
if %j% lss 16384 set /a jj=0
set /a k=%random%
if %k% geq 16384 set /a kk=1
if %k% lss 16384 set /a kk=0
set /a l=%random%
if %l% geq 16384 set /a ll=1
if %l% lss 16384 set /a ll=0
set /a m=%random%
if %m% geq 16384 set /a mm=1
if %m% lss 16384 set /a mm=0
set /a n=%random%
if %n% geq 16384 set /a nn=1
if %n% lss 16384 set /a nn=0
set /a o=%random%
if %o% geq 16384 set /a oo=1
if %o% lss 16384 set /a oo=0
set /a p=%random%
if %p% geq 16384 set /a pp=1
if %p% lss 16384 set /a pp=0
set /a q=%random%
if %q% geq 16384 set /a qq=1
if %q% lss 16384 set /a qq=0
set /a r=%random%
if %r% geq 16384 set /a rr=1
if %r% lss 16384 set /a rr=0
set /a s=%random%
if %s% geq 16384 set /a ss=1
if %s% lss 16384 set /a ss=0
set /a t=%random%
if %t% geq 16384 set /a tt=1
if %t% lss 16384 set /a tt=0
set /a u=%random%
if %u% geq 16384 set /a uu=1
if %u% lss 16384 set /a uu=0
set /a v=%random%
if %v% geq 16384 set /a vv=1
if %v% lss 16384 set /a vv=0
set /a w=%random%
if %w% geq 16384 set /a ww=1
if %w% lss 16384 set /a ww=0
set /a x=%random%
if %x% geq 16384 set /a xx=1
if %x% lss 16384 set /a xx=0
set /a y=%random%
if %y% geq 16384 set /a yy=1
if %y% lss 16384 set /a yy=0
set /a z=%random%
if %z% geq 16384 set /a zz=1
if %z% lss 16384 set /a zz=0
echo %aa% %bb% %cc% %dd% %ee% %ff% %gg% %hh% %ii% %jj% %kk% %ll% %mm% %nn% %oo% %pp% %qq% %rr% %ss% %tt% %uu% %vv% %ww% %xx% %yy% %zz%
goto a
I don't know if you got it yet but I had the same thought
and Decided to look it up I could not find anything but how to use colors and a random number generator so here's what I got
#echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
:a-0a0
set eddie=%time:~-2%
set first=%eddie:~0,1%
if %first%==0 set eddie=%eddie:~1,1%
set /a eddie=(%eddie%%%9)
call :ColorText 0%eddie% " %random%"
goto a-0a0
pause >null
:ColorText
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1

Batch if statement error

I am attempting to make a minesweeper in batch but i am having strange errors. When I run the file it lays 5 mines. Then it goes through the rest of the squares and checks to see how many mines are adjacent to each square. My problem is that it only senses an out put to the south-east. The south-east code is no different than the code for any other directions. This is very confusing to me. It seems like no one else has had this error. This is what an example output would be:
M 0 1 0 0
0 1 0 M 0
M 0 M 0 0
0 0 1 0 0
0 0 0 M 0
An answer would be greatly appreciated.
#echo off
setlocal enableDelayedExpansion
setlocal ENABLEEXTENSIONS
title Batch Minesweeper
color 80
set setX=1
set setY=1
set testX=1
set testY=1
set plrX=1
set plyY=1
set mines=0
set mineNum=0
set N=
set NE=
set E=
set SE=
set S=
set SW=
set W=
set NW=
set rect1_1=
set rect2_1=
set rect3_1=
set rect4_1=
set rect5_1=
set rect1_2=
set rect2_2=
set rect3_2=
set rect4_2=
set rect5_2=
set rect1_3=
set rect2_3=
set rect3_3=
set rect4_3=
set rect5_3=
set rect1_4=
set rect2_4=
set rect3_4=
set rect4_4=
set rect5_4=
set rect1_5=
set rect2_5=
set rect3_5=
set rect4_5=
set rect5_5=
set mine1_1=0
set mine2_1=0
set mine3_1=0
set mine4_1=0
set mine5_1=0
set mine1_2=0
set mine2_2=0
set mine3_2=0
set mine4_2=0
set mine5_2=0
set mine1_3=0
set mine2_3=0
set mine3_3=0
set mine4_3=0
set mine5_3=0
set mine1_4=0
set mine2_4=0
set mine3_4=0
set mine4_4=0
set mine5_4=0
set mine1_5=0
set mine2_5=0
set mine3_5=0
set mine4_5=0
set mine5_5=0
cls
echo Batch Minesweeper
echo.
echo [1] - Play
echo [2] - Info
echo [3] - Exit
echo.
echo Batch Minesweeper made by Matt Pauly
choice /c 123 /n /m ""
if %errorlevel%==1 goto startGame
if %errorlevel%==1 goto info
if %errorlevel%==1 goto end
:startGame
cls
set /a ranX=%RANDOM% * 5 / 32768 + 1
set /a ranY=%RANDOM% * 5 / 32768 + 1
if not %mines%==5 (
if !mine%ranX%_%ranY%!==0 (
set mine%ranX%_%ranY%=M
set /a mines=%mines% + 1
goto startGame
)
goto startGame
)
goto setup
:setup
cls
echo Loading...
set curVal=%setX%_%setY%
if %curVal%==1_6 goto display
if %setX%==6 (
set setX=1
set /a setY=%setY% + 1
goto setup
)
if !mine%setX%_%setY%!==M (
set /a setX=%setX% + 1
goto setup
)
set /a testY=%setY% + 1
set testX=%setX%
set N=!mine%testX%_%testY%!
set /a testY=%setY% + 1
set /a testX=%setX% + 1
set NE=!mine%testX%_%testY%!
set testY=%setY%
set /a testX=%setX% + 1
set E=!mine%testX%_%testY%!
set /a testY=%setY% + 1
set /a testX=%setX% - 1
set SE=!mine%testX%_%testY%!
set testY=%setY%
set /a testX=%setX% - 1
set S=!mine%testX%_%testY%!
set /a testY=%setY% - 1
set /a testX=%setX% - 1
set SW=!mine%testX%_%testY%!
set /a testY=%setY% + 1
set testX=%setX%
set W=!mine%testX%_%testY%!
set /a testY=%setY% - 1
set /a testX=%setX% + 1
set NW=!mine%testX%_%testY%!
set mineNum=0
if defined N if "%N%"=="M" set /a mineNum=%mineNum% + 1
if defined NW if "%NW%"=="M" set /a mineNum=%mineNum% + 1
if defined W if "%W%"=="M" set/a mineNum=%mineNum% + 1
if defined SW if "%S%"=="M" set /a mineNum=%mineNum% + 1
if defined S if "%S%"=="M" set /a mineNum=%mineNum% + 1
if defined SE if "%SE%"=="M" set /a mineNum=%mineNum% + 1
if defined E if "%E%"=="M" set /a mineNum=%mineNum% + 1
if defined NE if "%NE%"=="M" set /a mineNum=%mineNum% + 1
set mine%setX%_%setY%=%mineNum%
set /a setX=%setX% + 1
goto setup
:display
cls
::echo 5 %rect1_5% %rect2_5% %rect3_5% %rect4_5% %rect5_5%
::echo.
::echo 4 %rect1_4% %rect2_4% %rect3_4% %rect4_4% %rect5_4%
::echo.
::echo 3 %rect1_3% %rect2_3% %rect3_3% %rect4_3% %rect5_3%
::echo.
::echo 2 %rect1_2% %rect2_2% %rect3_2% %rect4_2% %rect5_2%
::echo.
::echo 1 %rect1_1% %rect2_1% %rect3_1% %rect4_1% %rect5_1%
::echo.
::echo a b c d e
echo 5 %mine1_5% %mine2_5% %mine3_5% %mine4_5% %mine5_5%
echo.
echo 4 %mine1_4% %mine2_4% %mine3_4% %mine4_4% %mine5_4%
echo.
echo 3 %mine1_3% %mine2_3% %mine3_3% %mine4_3% %mine5_3%
echo.
echo 2 %mine1_2% %mine2_2% %mine3_2% %mine4_2% %mine5_2%
echo.
echo 1 %mine1_1% %mine2_1% %mine3_1% %mine4_1% %mine5_1%
echo.
echo a b c d e
pause
:info
:end
exit
Your major problem is trailing spaces on lines. In a conventional set statement, any trailing spaces are included in the value assigned, and "M" and "M " are completely different things, so if you have set X=!something! then the value assigned is different from that assigned by set X=!something!.
You also have a problem where you are confusing your X with your Y. For instance, setting W is done with X and Y+1 - the same as setting N. This should be X-1 and Y.

Batch File - Get list value using index key

I have written a script to bat file that will print the random value from list.
but I am facing problem to access list value using it's index
My code is:
set list=A B C D a b c
echo %list[3]%
for /l %%a in (1,1,6) do (
#set /a bottomlimit = 0
#set /a upperlimit = 5
#set /a num = !bottomlimit! + !RANDOM! %% !upperlimit! - !bottomlimit! + 1
echo %list[!num!]%
TIMEOUT /T 5
)
Waiting for your valuable solution.
Just three options. Number one to handle your approach. Number two for a "pure" array in environment variables. Number three will mix the two, definition of the list as in option 1, but iterating the list to generate the array in option 2.
#echo off
setlocal enableextensions enabledelayedexpansion
REM OPTION 1 - The list
echo -------------------------------------------------
setlocal
set "list=A B C D a b c"
set /a bottomlimit=0
set /a upperlimit=6
for /l %%a in (1,1,6) do (
set /a "num=!bottomlimit! + ( !RANDOM! %% (!upperlimit! - !bottomlimit! + 1))"
set "pos=0"
for %%l in (!list!) do if defined pos if !pos!==!num! ( echo %%l & set "pos=" ) else ( set /a "pos+=1")
)
endlocal
REM OPTION 2 - The "pure" array
echo -------------------------------------------------
setlocal
set "list[0]=A"
set "list[1]=B"
set "list[2]=C"
set "list[3]=D"
set "list[4]=a"
set "list[5]=b"
set "list[6]=c"
set /a bottomlimit=0
set /a upperlimit=6
for /l %%a in (1,1,6) do (
set /a "num=!bottomlimit! + ( !RANDOM! %% (!upperlimit! - !bottomlimit! + 1))"
for %%n in (!num!) do echo !list[%%n]!
)
endlocal
REM OPTION 3 - The remix
echo -------------------------------------------------
setlocal
set "list=A B C D a b c"
set "pos=0"
for %%l in (!list!) do ( set "list[!pos!]=%%l" & set /a "pos+=1" )
set /a "bottomlimit=0"
set /a "upperlimit=!pos!-1"
for /l %%a in (1,1,6) do (
set /a "num=!bottomlimit! + ( !RANDOM! %% (!upperlimit! - !bottomlimit! + 1))"
for %%n in (!num!) do echo !list[%%n]!
)
endlocal
endlocal
exit /b
#echo off
setlocal EnableDelayedExpansion
set list=A B C D a b c
set /a counter=1
for %%a in (%list%) do (
set "list[!counter!]=%%~a"
set /a counter=counter+1
)
set list[
for /l %%a in (1,1,6) do (
#set /a bottomlimit = 0
#set /a upperlimit = 5
#set /a num = bottomlimit + !RANDOM! %% upperlimit - bottomlimit + 1
for %%# in (!num!) do echo !list[%%#]!
TIMEOUT /T 5
)
endlocal
You may use echo/%list:~3,1%, but you can't use echo/%list:~%num%,1%
Try this:
#echo off
set "list=ABCDabc"
echo/%list:~3,1%
set /a bottomlimit = 0
set /a upperlimit = 5
for /l %%a in (1,1,6) do (
CALL:CALC
CALL _temp.bat
Timeout /t 5
)
del _temp.bat
pause >nul
exit/b
:CALC
set /a num = %bottomlimit% + %random% %% %upperlimit% - %bottomlimit% + 1
echo/set "list=%list%">_temp.bat
echo/echo/%%list:~%num%,1%%>>_temp.bat
exit/b

Resources