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
Related
I wanted to ask this question for a long time. When I echo for example a box using hashtags, and put inside some text, this happens:
https://i.stack.imgur.com/0VnZq.png
And without the text, it looks normal:
https://i.stack.imgur.com/jyOw7.png
So I made a moving system in batch, and when I opened it it ended up looking like this.
https://i.stack.imgur.com/ZUxf4.png
Here's the code, thank you very much if you find an answer and a fix for this.
#echo off
#title batch rpg
cls
color 0f
:initsetup
set xc=3
set yc=3
:displaysetup
cls
set X1Y1=
set X1Y2=
set X1Y3=
set X1Y4=
set X2Y1=
set X2Y2=
set X2Y3=
set X2Y4=
set X3Y1=
set X3Y2=
set X3Y3=
set X3Y4=
set X4Y1=
set X4Y2=
set X4Y3=
set X4Y4=
:SCREEN
set X%xc%Y%yc%=#
echo ....
echo .%X1Y4%%X2Y4%%X3Y4%%X4Y4%.
echo .%X1Y3%%X2Y3%%X3Y3%%X4Y3%.
echo .%X1Y2%%X2Y2%%X3Y2%%X4Y2%.
echo .%X1Y1%%X2Y1%%X3Y1%%X4Y1%.
echo ....
choice /c wasd /n
set choice=%ERRORLEVEL%
goto choice%choice%
:choice1
set /A yc=%yc%+1
if %yc% GTR 4 (
set /A yc=%yc%-1
goto displaysetup
) ELSE (
goto displaysetup
)
:choice2
set /A xc=%xc%-1
if %xc% LSS 1 (
set /A xc=%xc%+1
goto displaysetup
) ELSE (
goto displaysetup
)
:choice3
set /A yc=%yc%-1
if %yc% LSS 1 (
set /A yc=%yc%+1
goto displaysetup
) ELSE (
goto displaysetup
)
:choice4
set /A xc=%xc%+1
if %xc% GTR 4 (
set /A xc=%xc%-1
goto displaysetup
) ELSE (
goto displaysetup
)
This is the new version according to your comments.
It still doesn't work.
I try to rename files like
2006 Gaspésie - Croisière à la baleine 1023.jpg
2006 Gaspésie - Croisière baleine 28.jpg
To
2006-06 Croisière baleine 001.jpg
2006-06 Croisière baleine 002.jpg
The problem is that the fields F1 and F2 are empty when I do echo and when I do rename.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set /p "NewFileName=New name file (without the extension): "
set /p "LenghtMask=Mask lenght of the number: "
set /a FileNo=0
for %%I in (*.JPG) do (
echo.
#echo off
set /a FileNo=!FileNo!+1
call :return_no_mask !FileNo! %LenghtMask% FileNoMask
#echo on
set "F1=%%~I"
echo F1 %F1%
set ExtensionName=%%~xI
set "F2=%NewFileName% %FileNoMask%%ExtensionName%"
echo F2 %F2%
rename "%F1%" "%F2%"
pause
)
goto:EOF
::--------------------------------------------------
::Return the number (fill with 0 to have the right lenght)
:: in Number to use
:: in Mask's lenght ex: 2
:: out Number
::--------------------------------------------------
:return_no_mask
set NoIn=%1
::---Search the lenght of the number
set /a LenghtNo=1
if "%NoIn%" GEQ "10" set /a LenghtNo=2
if "%NoIn%" GEQ "100" set /a LenghtNo=3
if "%NoIn%" GEQ "1000" set /a LenghtNo=4
if "%NoIn%" GEQ "10000" set /a LenghtNo=5
::---Fill with zero
set NoOut=
:test_add
if "%LenghtNo%" LSS "%2" (
set NoOut=0%NoOut%
set /a LenghtNo+=1
goto :test_add
)
set NoOut=%NoOut%%1
set %3=%NoOut%
EXIT /B 0
::--------------------------------------------------
::End of file
::--------------------------------------------------
:EOF
endlocal
Thank you
I recently had an issue with one of my batch programs, it is basically an incompleted platformer: My code is work in progress and i got stuck with my attemp of collision detection. I have a matrix M, that i can access within an EnableDelayedExpansion block like this !M[%%a][%%d]!
The first time it works, but the second usage does not work anymore:
echo A!M[%xx%,%yy%]!B yields AB.
I would appreciate any hint.
Thanks in advance!
#echo off
::creating a new line variable for multi line strings
set NLM=^
:: Two empty lines are required here
::set up initial grid
set height=9
set width=14
for /l %%a in (0,1,9) do (
for /l %%d in (0,1,14) do (
set M[%%a][%%d]=.
)
)
::create some vars at an initial value
set falling=0
set row=5
set turns=0
set x=8
set face=empty
:turn
set M[%row%][%x%]=^P
::set wall position
set x2=4
set y2=6
set M[%y2%][%x2%]=^#
::display current grid
setlocal EnableDelayedExpansion
set "grid="
for /l %%a in (0,1,9) do (
set line=!M[%%a][0]!
for /l %%d in (1,1,14) do (
set line=!line!!M[%%a][%%d]!
)
set grid=!grid! !NLM! !line!
)
cls
echo !grid!
endlocal
::prompt and make move
:: 4 = d
choice /c:wasd /n /m "" /t 1 /d s
set move=0
set /a xx=x
set /a yy=row
:move clearing Player
set M[%row%][%x%]=.
:: ^ this clears the characters trail so that there is no trail left behind after
:: moves, even when blocked. But at the start of grid Player is drawn again.
if %errorlevel%==4 ( if %x% LSS %width% ( set face=right
set /a xx=x+1) else ( set face=empty))
::if %errorlevel%==3 ( if %row% LSS %height% ( set face=down
::set /a move=0) else ( set face=empty))
if %errorlevel%==2 ( if %x% GTR 0 ( set face=left
set /a xx=x-1) else ( set face=empty))
if %errorlevel%==1 ( if %row% GTR 0 ( set face=up
set /a move=1) else ( set face=empty))
::falling!
set /a yy=yy-move
if %move%==0 (
set /a falling+=1
set face=down
) else (
set falling=0
)
set /a yy=yy+falling
::collision tests
setlocal enableDelayedExpansion
::echo !M[%xx%,%yy%]! > tmp.var
echo A!M[%xx%,%yy%]!B
endlocal
::set /P var1="" < tmp.var
::if %var1%==# (
:: set face=empty
::)
::now the actual movements
::if %face%==right set /a x+=1
::if %face%==left set /a x-=1
::if %face%==up set /a row=yy
::if %face%==down set /a row=yy
set /a x=xx
set /a row=yy
::increment turns, return to top
set /a turns+=1
goto :turn
I couldn't get %difference_two% to work It can find %difference_one% as 2 but when I make it divide 2 by 2 it doesn't display as anything.
Use the numbers 2,5,10,17,26
To get what I need.
#echo off
color 0a
title Solver
:numbers
cls
set /p first=First:
set /p second=Second:
set /p third=Third:
set /p fourth=Fourth:
set /p fifth=Fifth:
goto solve
:solve
cls
set /a second_minus_first= %second% - %first%
set /a third_minus_second= %third% - %second%
if %third_minus_second%==%second_minus_first% (
goto s
) else (
goto d
)
:d
cls
set /a fourth_minus_third= %fourth% - %third%
set /a difference= %third_minus_second% - %second_minus_first%
set /a difference_one= %fourth_minus_third% - %third_minus_second%
if %difference%==%difference_one% (
set /a difference_two= %difference_one% / 2
set /a thing= %first% - %difference_two%
cls
echo %difference_two%n Squared + %thing%
pause >nul
goto numbers
) else (
goto wrong
)
Try this:
#echo off
setlocal EnableDelayedExpansion
color 0a
title Solver
:numbers
cls
set /p first=First:
set /p second=Second:
set /p third=Third:
set /p fourth=Fourth:
set /p fifth=Fifth:
goto solve
:solve
cls
set /a second_minus_first= %second% - %first%
set /a third_minus_second= %third% - %second%
if %third_minus_second%==%second_minus_first% (
goto s
) else (
goto d
)
:d
cls
set /a fourth_minus_third= %fourth% - %third%
set /a difference= %third_minus_second% - %second_minus_first%
set /a difference_one= %fourth_minus_third% - %third_minus_second%
if %difference%==%difference_one% (
set /a difference_two= !difference_one! / 2
set /a thing= !first! - !difference_two!
cls
echo !difference_two!n Squared + !thing!
pause >nul
goto numbers
) else (
goto wrong
)
In batch, commands in a code block (between ( )) are interpreted as on the same line, so you need to use setlocal EnableDelayedExpansion and use ! instead of % inside them.
I know I can write an echo > C:/Folder/name.txt
But I have a generator to make 2 number for Cord and can't figure out a quick way to make a one line echo > with all the points
for shortness I'll simplify my code because the current code is not the issue
Echo off
Setlocal EnableDelayedExpansion
set R=1
set Number=1
:Loop
if %R% EQU 1 (set /p Max=How many Max Points? ) Else(
echo.)
set /p PX%number%=What is PointX%number%?
set /p PY%number%=What is PointY%number%?
if %Number% GEQ %Max% (goto :fin) Else(
set /a Number=%Number%+1 & set R=2 & Goto :loop)
This Is what Im trying to Optimize
:fin
echo (%PX1%,%PY1%),(%PX2%,%PY2%),(%PX3%,%PY3%) ... Ect >C:Folder/File.txt
Is there any way to make all the generated numbers on 1 line
Credit to Squashman for the Answer - Thank you!
Correct Code:
Echo off
Setlocal EnableDelayedExpansion
set R=1
set Number=1
set Line=Test
:Loop
if %R% EQU 1 (set /p Max=How many Max Points? ) Else (
echo.)
set /p PX%number%=What is PointX%number%?
set /p PY%number%=What is PointY%number%?
set line=%line%(!PX%number%!,!PY%number%!),
if %Number% GEQ %Max% (goto :fin) Else (
set /a Number=%Number%+1 & set R=2 & Goto :loop)
:fin
echo %Line:~0,-1% >C:Folder/File.txt
Credit to Squashman for the Answer - Thank you!
The logic is so much simpler if you get the max point count before the loop. And the FOR /L loop can auto increment your counter and eliminate the need for GOTO.
#echo off
setlocal enableDelayedExpansion
set /p "cnt=How many points? "
set "ln="
for /l %%N in (1 1 %cnt%) do (
echo(
set /p "PX%%N=What is PointX%%N? "
set /p "PY%%N=What is PointY%%N? "
set "ln=!ln!(!PX%%N!,!PY%%N!),"
)
echo(
echo !ln:~0,-1!