2048: Difference between revisions

8,475 bytes added ,  9 years ago
no edit summary
No edit summary
No edit summary
Line 35:
* Lose condition.
 
=={{header|Batch File}}==
This code uses XCOPY.EXE as key input. Check [http://stackoverflow.com/questions/18942356/batch-file-choice-syntax/18942798#18942798 HERE] for details.
<lang dos>
::
::2048 Task from Rosetta Code Wiki
::Batch File Implementation
::
::Directly OPEN the Batch File to play.
::I think this code is slow in some systems and needs improvements...
::
 
@echo off
title 2048 Game
setlocal enabledelayedexpansion
 
::GENERATING A GAME
:begin
set score=0
set won=
set lose=
for %%a in (1,2,3,4) do (
for %%b in (1,2,3,4) do (
set a%%a%%b=0
)
)
set list=a11a12a13a14a21a22a23a24a31a32a33a34a41a42a43a44
set blanktiles=16
call :addtile
call :addtile
::/GENERATING A GAME
 
::MAIN GAME LOOP
:gameplay
call :display
echo.Your Move:
set "k="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
if not defined k set "k=%%L"
)
set k=%k:~-1%
set moved=0&set list=&set blanktiles=0&set x1=&set test=0&set inp=
if /i "!k!"=="n" (goto :begin)
if /i "!k!"=="p" (exit)
if /i "!k!"=="s" (set inp=1)
if /i "!k!"=="w" (set inp=2)
if /i "!k!"=="a" (set inp=3)
if /i "!k!"=="d" (set inp=4)
if not "!inp!"=="" (
for %%a in (a1 a2 a3 a4) do (set x=%%a&call :proc%inp%)
)
if !won!==1 (
goto :yes
)
if !moved!==0 (
if !blanktiles!==0 (
if "!x1!"=="" (
set test=1&set lose=1
for %%a in (a1 a2 a3 a4) do (set x=%%a&call :adder3)
if !lose!==1 (goto :no)
) else (
set test=1&set lose=1
for %%a in (a1 a2 a3 a4) do (set x=%%a&call :adder1)
if !lose!==1 (goto :no)
)
)
goto :gameplay
)
call :addtile
goto :gameplay
::/MAIN GAME LOOP
 
::ADDTILE FUNCTION
:addtile
set /a rnd1=(%random%%%%blanktiles%)*3
set /a rnd2=%random%%%10
set pick=!list:~%rnd1%,3!
set %pick%=2
if %rnd2%==1 (
set %pick%=4
)
set list=!list:%pick%=!
set /a blanktiles-=1
goto :EOF
::/ADDTILE FUNCTION
 
::PROCESSOR DOWN
:proc1
set cyc=4
:cyc1
if !%x%%cyc%!==0 (
set /a tm=%cyc%-1
for /l %%b in (!tm!,-1,1) do (
if not !%x%%%b!==0 (
set "%x%%cyc%=!%x%%%b!"
set "%x%%%b=0"
set moved=1
goto :break1
)
)
)
:break1
if %cyc%==2 (
goto :adder1
) else (
set /a cyc-=1
goto :cyc1
)
:adder1
if !%x%3!==!%x%4! (
if !test!==1 (
set lose=0
goto :EOF
) else (
set /a %x%4*=2
set %x%3=!%x%2!
set %x%2=!%x%1!
set %x%1=0
set /a "score+=!%x%4!"
if !%x%4!==2048 (set won=1)
if not !%x%4!==0 (set moved=1)
)
)
if !%x%2!==!%x%3! (
if !test!==1 (
set lose=0
goto :EOF
) else (
set /a %x%3*=2
set %x%2=!%x%1!
set %x%1=0
set /a "score+=!%x%3!"
if !%x%3!==2048 (set won=1)
if not !%x%3!==0 (set moved=1)
)
) else (
if !%x%1!==!%x%2! (
if !test!==1 (
set lose=0
goto :EOF
) else (
set /a %x%2*=2
set %x%1=0
set /a "score+=!%x%2!"
if !%x%2!==2048 (set won=1)
if not !%x%2!==0 (set moved=1)
)
)
)
if !test!==0 (
if !%x%1!==0 (set list=!list!%x%1&set /a blanktiles+=1)
if !%x%2!==0 (set list=!list!%x%2&set /a blanktiles+=1)
if !%x%3!==0 (set list=!list!%x%3&set /a blanktiles+=1)
if !%x%4!==0 (set list=!list!%x%4&set /a blanktiles+=1)
)
goto :EOF
::/PROCESSOR DOWN
 
::PROCESSOR UP
:proc2
set cyc=1
:cyc2
if !%x%%cyc%!==0 (
set /a tm=%cyc%+1
for /l %%b in (!tm!,1,4) do (
if not !%x%%%b!==0 (
set "%x%%cyc%=!%x%%%b!"
set "%x%%%b=0"
set moved=1
goto :break2
)
)
)
:break2
if %cyc%==3 (
goto :adder2
) else (
set /a cyc+=1
goto :cyc2
)
:adder2
if !%x%1!==!%x%2! (
set /a %x%1*=2
set %x%2=!%x%3!
set %x%3=!%x%4!
set %x%4=0
set /a "score+=!%x%1!"
if !%x%1!==2048 (set won=1)
if not !%x%1!==0 (set moved=1)
)
if !%x%2!==!%x%3! (
set /a %x%2*=2
set %x%3=!%x%4!
set %x%4=0
set /a "score+=!%x%2!"
if !%x%2!==2048 (set won=1)
if not !%x%2!==0 (set moved=1)
) else (
if !%x%3!==!%x%4! (
set /a %x%3*=2
set %x%4=0
set /a "score+=!%x%3!"
if !%x%3!==2048 (set won=1)
if not !%x%3!==0 (set moved=1)
)
)
if !%x%1!==0 (set list=!list!%x%1&set /a blanktiles+=1)
if !%x%2!==0 (set list=!list!%x%2&set /a blanktiles+=1)
if !%x%3!==0 (set list=!list!%x%3&set /a blanktiles+=1)
if !%x%4!==0 (set list=!list!%x%4&set /a blanktiles+=1)
goto :EOF
::/PROCESSOR UP
 
::PROCESSOR LEFT
:proc3
set x1=%x:~1,1%
set x=%x:~0,1%
set cyc=1
:cyc3
if !%x%%cyc%%x1%!==0 (
set /a tm=%cyc%+1
for /l %%b in (!tm!,1,4) do (
if not !%x%%%b%x1%!==0 (
set "%x%%cyc%%x1%=!%x%%%b%x1%!"
set "%x%%%b%x1%=0"
set moved=1
goto :break3
)
)
)
:break3
if %cyc%==3 (
goto :adder3
) else (
set /a cyc+=1
goto :cyc3
)
:adder3
if !test!==1 (
set x1=%x:~1,1%
set x=%x:~0,1%
)
if !%x%1%x1%!==!%x%2%x1%! (
if !test!==1 (
set lose=0
goto :EOF
) else (
set /a %x%1%x1%*=2
set %x%2%x1%=!%x%3%x1%!
set %x%3%x1%=!%x%4%x1%!
set %x%4%x1%=0
set /a "score+=!%x%1%x1%!"
if !%x%1%x1%!==2048 (set won=1)
if not !%x%1%x1%!==0 (set moved=1)
)
)
if !%x%2%x1%!==!%x%3%x1%! (
if !test!==1 (
set lose=0
goto :EOF
) else (
set /a %x%2%x1%*=2
set %x%3%x1%=!%x%4%x1%!
set %x%4%x1%=0
set /a "score+=!%x%2%x1%!"
if !%x%2%x1%!==2048 (set won=1)
if not !%x%2%x1%!==0 (set moved=1)
)
) else (
if !%x%3%x1%!==!%x%4%x1%! (
if !test!==1 (
set lose=0
goto :EOF
) else (
set /a %x%3%x1%*=2
set %x%4%x1%=0
set /a "score+=!%x%3%x1%!"
if !%x%3%x1%!==2048 (set won=1)
if not !%x%3%x1%!==0 (set moved=1)
)
)
)
if !test!==0 (
if !%x%1%x1%!==0 (set list=!list!%x%1%x1%&set /a blanktiles+=1)
if !%x%2%x1%!==0 (set list=!list!%x%2%x1%&set /a blanktiles+=1)
if !%x%3%x1%!==0 (set list=!list!%x%3%x1%&set /a blanktiles+=1)
if !%x%4%x1%!==0 (set list=!list!%x%4%x1%&set /a blanktiles+=1)
)
goto :EOF
::/PROCESSOR LEFT
 
::PROCESSOR RIGHT
:proc4
set x1=%x:~1,1%
set x=%x:~0,1%
set cyc=4
:cyc4
if !%x%%cyc%%x1%!==0 (
set /a tm=%cyc%-1
for /l %%b in (!tm!,-1,1) do (
if not !%x%%%b%x1%!==0 (
set "%x%%cyc%%x1%=!%x%%%b%x1%!"
set "%x%%%b%x1%=0"
set moved=1
goto :break4
)
)
)
:break4
if %cyc%==2 (
goto :adder4
) else (
set /a cyc-=1
goto :cyc4
)
:adder4
if !%x%3%x1%!==!%x%4%x1%! (
set /a %x%4%x1%*=2
set %x%3%x1%=!%x%2%x1%!
set %x%2%x1%=!%x%1%x1%!
set %x%1%x1%=0
set /a "score+=!%x%4%x1%!"
if !%x%4%x1%!==2048 (set won=1)
if not !%x%4%x1%!==0 (set moved=1)
)
if !%x%2%x1%!==!%x%3%x1%! (
set /a %x%3%x1%*=2
set %x%2%x1%=!%x%1%x1%!
set %x%1%x1%=0
set /a "score+=!%x%3%x1%!"
if !%x%3%x1%!==2048 (set won=1)
if not !%x%3%x1%!==0 (set moved=1)
) else (
if !%x%1%x1%!==!%x%2%x1%! (
set /a %x%2%x1%*=2
set %x%1%x1%=0
set /a "score+=!%x%2%x1%!"
if !%x%2%x1%!==2048 (set won=1)
if not !%x%2%x1%!==0 (set moved=1)
)
)
if !%x%1%x1%!==0 (set list=!list!%x%1%x1%&set /a blanktiles+=1)
if !%x%2%x1%!==0 (set list=!list!%x%2%x1%&set /a blanktiles+=1)
if !%x%3%x1%!==0 (set list=!list!%x%3%x1%&set /a blanktiles+=1)
if !%x%4%x1%!==0 (set list=!list!%x%4%x1%&set /a blanktiles+=1)
goto :EOF
::/PROCESSOR RIGHT
 
::WIN!!!
:yes
call :display
echo Congrats^^! You SOLVED the 2048 puzzle^^!
echo.
echo Press N for NEW GAME, or P to EXIT...
set "qwerty="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
if not defined qwerty set "qwerty=%%L"
)
set qwerty=%qwerty:~-1%
if /i "!qwerty!"=="n" (goto :begin)
if /i "!qwerty!"=="p" (exit)
goto :yes
::/WIN!
 
::LOSE :(
:no
call :display
echo Game Over due to Out of Moves... Sorry :^(
echo.
echo Press N for NEW GAME, or P to EXIT...
set "qwerty="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
if not defined qwerty set "qwerty=%%L"
)
set qwerty=%qwerty:~-1%
if /i "!qwerty!"=="n" (goto :begin)
if /i "!qwerty!"=="p" (exit)
goto :no
::/LOSE :(
 
::DISPLAY
:display
for %%a in (1 2 3 4) do (
for %%b in (1 2 3 4) do (
set a=%%a
set b=%%b
call :beautify
)
)
cls
echo.
echo 2048 Game
echo Batch File Implementation
echo.
echo.
echo.
echo. +----+----+----+----+
echo. ^|%da11%^|%da21%^|%da31%^|%da41%^| W - Slide UP
echo. +----+----+----+----+ S - Slide DOWN
echo. ^|%da12%^|%da22%^|%da32%^|%da42%^| A - Slide LEFT
echo. +----+----+----+----+ D - Slide RIGHT
echo. ^|%da13%^|%da23%^|%da33%^|%da43%^| N - New GAME
echo. +----+----+----+----+ P - EXIT
echo. ^|%da14%^|%da24%^|%da34%^|%da44%^|
echo. +----+----+----+----+
echo.
echo.Score: !score!
echo.
goto :EOF
::/DISPLAY
 
::"BEAUTIFY" DISPLAY
:beautify
if !a%a%%b%!==0 (set da%a%%b%= &goto :EOF)
if !a%a%%b%! lss 10 (set da%a%%b%= !a%a%%b%!&goto :EOF)
if !a%a%%b%! lss 100 (set da%a%%b%= !a%a%%b%!&goto :EOF)
if !a%a%%b%! lss 1000 (set da%a%%b%= !a%a%%b%!&goto :EOF)
set da%a%%b%=!a%a%%b%!
goto :EOF
::/"BEAUTIFY" DISPLAY
</lang>
=={{header|C++}}==
<lang cpp>
535

edits