Rubik的立方体搅拌器

时间:2015-07-12 11:21:43

标签: batch-file

我刚才制作了这个非常棒的(可怕的)Rubik's cube scrambler,但是当我查看代码时,它太多了,我知道有一种非常简单的方法可以做到这一点。如果有人可以修改某些位以使代码更紧凑,我会非常感激。

这纯粹是为了我的批量理解,希望其他人可以从你回答的任何事情中学习。

以下是代码:

@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

2 个答案:

答案 0 :(得分:1)

它可能会被削减一点(特别是如果我想调整间距),但我把它降低到原来的29%,所以就是这样。

@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

<小时/> 这个版本甚至更小(仅略高于原版的18%),但牺牲了一些可读性:

@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

答案 1 :(得分:1)

使用FOR loops可能就像这种做法?

    for($i = $limit; $i < count($minText); $i++){
       echo $minText[$i].".";
   }