Advertisement
Guest User

2048 in Batch script

a guest
Mar 29th, 2016
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.13 KB | None | 0 0
  1. @echo off&setlocal enabledelayedexpansion&title 2048&mode con: cols=32 lines=46&color F0
  2. :startgame
  3. if exist "2048JWinslow.dat" (for /f "delims=" %%g in (2048JWinslow.dat)do %%g) else set/ascore=0,winstate=0,bestscore=0&for /l %%g in (0,1,15)do set board[%%g]=0
  4. set/amoves=0,h=0&for /l %%g in (0,1,15)do if !board[%%g]!==0 set/ah+=1
  5. if %h%==16 call :tilespawn&call :tilespawn
  6. :startloop
  7. if %score% gtr %bestscore% set bestscore=%score%
  8. call :drawboard 0&call :savegame&choice /c wasdxn /n /m ""
  9. if %errorlevel%==1 call :rotateclockwise&call :rotateclockwise&call :rotateclockwise&call :compress&call :merge&call :compress&call :compress&call :rotateclockwise
  10. if %errorlevel%==2 call :compress&call :merge&call :compress&call :compress
  11. if %errorlevel%==3 call :rotateclockwise&call :compress&call :merge&call :compress&call :compress&call :rotateclockwise&call :rotateclockwise&call :rotateclockwise
  12. if %errorlevel%==4 call :rotateclockwise&call :rotateclockwise&call :compress&call :merge&call :compress&call :compress&call :rotateclockwise&call :rotateclockwise
  13. if %errorlevel%==5 goto end
  14. if %errorlevel%==6 (set/ascore=0,winstate=0&set moves=0&for /l %%g in (0,1,15)do set board[%%g]=0
  15. call :tilespawn&call :tilespawn&goto startloop)
  16. set/aboardchanged=0,tiles=0&for /l %%g in (0,1,15)do (if !board[%%g]! neq !tmpboard[%%g]! set boardchanged=1
  17. if !board[%%g]! neq 0 set /a tiles+=1
  18. if !board[%%g]!==2048 if %winstate%==0 set winstate=1)
  19. if %boardchanged%==1 call :tilespawn&set/amoves+=1
  20. if %winstate%==1 goto youwin
  21. set/ayoulost=0,v=0
  22. :1
  23. set/av+=1
  24. set/aw=%v%%%4,x=%v%/4,y=%v%-1,z=%v%-4
  25. if %tiles%==15 (if %w% neq 0 if !board[%v%]!==!board[%y%]! set youlost=1
  26. if %x% neq 0 if !board[%v%]!==!board[%z%]! set youlost=1)
  27. if %tiles%==15 if %v% neq 15 goto 1
  28. if %tiles%==15 if %boardchanged%==1 if %youlost%==0 goto youlost
  29. goto startloop
  30. :tilespawn
  31. set/arandtile=%random%%%16
  32. if !board[%randtile%]! gtr 0 goto tilespawn
  33. set/aboard[%randtile%]=%random%%%10/9*2+2&exit /b
  34. :compress
  35. set x=-1
  36. :2
  37. set/aw=0,x+=1,y=-1&for /l %%g in (0,1,3) do set tmparray[%%g]=0
  38. :3
  39. set/ay+=1&set/az=%x%*4+!y!
  40. if !board[%z%]! neq 0 set/atmparray[%w%]=!board[%z%]!&set/aw+=1
  41. if %y% neq 3 goto 3
  42. for /l %%g in (0,1,3)do set/az=%x%*4+%%g&set/aboard[!z!]=!tmparray[%%g]!
  43. if %x% neq 3 goto 2
  44. exit /b
  45. :merge
  46. set x=-1
  47. :4
  48. set/ax+=1,y=-1
  49. :5
  50. set/ay+=1&set/aw=%x%*4+!y!&set/az=!w!+1
  51. if !board[%w%]!==!board[%z%]! set/aboard[%w%]*=2,score+=!board[%w%]!*2&set board[%z%]=0
  52. if %y% neq 2 goto 5
  53. if %x% neq 3 goto 4
  54. exit /b
  55. :rotateclockwise
  56. set i=12
  57. for /l %%g in (0,1,15)do set/atmparray[%%g]=board[!i!],i-=4&if !i! lss 0 set/ai+=17
  58. for /l %%g in (0,1,15)do set/aboard[%%g]=!tmparray[%%g]!
  59. exit /b
  60. :youwin
  61. set winstate=2&call :drawboard 1&call :savegame&choice /c cnx /n /m ""
  62. if %errorlevel%==1 goto startloop
  63. if %errorlevel%==2 goto startgame
  64. if %errorlevel%==3 exit /b
  65. :youlost
  66. call :drawboard 2&for /l %%g in (0,1,15)do set board[%%g]=0
  67. set/ascore=0,winstate=0&call :savegame&choice /c nx /n /m ""
  68. if %errorlevel%==1 goto startgame
  69. if %errorlevel%==2 exit /b
  70. :drawboard
  71. for /l %%g in (0,1,15)do (set "board2[%%g]=      !board[%%g]!"&if !board[%%g]!==0 set "board2[%%g]=      "
  72. if %moves% gtr 0 if %%g==%randtile% set board2[%%g]=   + !board[%%g]!
  73. set board2[%%g]=!board2[%%g]:~-6!)
  74. cls&echo  ___     ___    _  _      ___&echo ^|__ \   / _ \  ^| ^|^| ^|    / _ \&echo    ) ^| ^| ^| ^| ^| ^| ^|^| ^|_  ^| (_) ^|&echo   / /  ^| ^| ^| ^| ^|__   _^|  ^> _ ^<&echo  / /_  ^| ^|_^| ^|    ^| ^|   ^| (_) ^|&echo ^|____^|  \___/     ^|_^|    \___/&echo(&echo Join the numbers and get to the&echo            2048 tile^^!&echo(&if %1==0 echo     (Press N for new game)&echo(
  75. if %1==1 echo             You Win!&echo Press C to continue, N to reset
  76. if %1==2 echo            Game Over&echo     Press N to try again...
  77. echo  Score: %score%&echo  Best: %bestscore%&echo  +------+------+------+------+&echo  ^|%board2[0]%^|%board2[1]%^|%board2[2]%^|%board2[3]%^|&echo  +------+------+------+------+&echo  ^|%board2[4]%^|%board2[5]%^|%board2[6]%^|%board2[7]%^|&echo  +------+------+------+------+&echo  ^|%board2[8]%^|%board2[9]%^|%board2[10]%^|%board2[11]%^|&echo  +------+------+------+------+&echo  ^|%board2[12]%^|%board2[13]%^|%board2[14]%^|%board2[15]%^|&echo  +------+------+------+------+&echo(&echo HOW TO PLAY: Use the WASD keys&echo to move the tiles. When two&echo tiles with the same number&echo touch, they merge into one^^!&echo -------------------------------&echo NOTE: This is only a port of&echo the original 2048, found at&echo http://git.io/2048 . Official&echo apps for iOS and Android are&echo also available. Other versions&echo are derivatives or fakes, and&echo should be used with caution.&echo -------------------------------&echo Port coded by Josiah Winslow.&echo Tweaked and tested by the&echo DosTips.com community. 2048&echo created by Gabriele Cirulli.&echo Based on 1024 by Veewo Studio&echo and conceptually similar to&echo Threes by Asher Vollmer.&exit /b
  78. :savegame
  79. echo(>2048JWinslow.dat&for %%g in (score bestscore winstate)do echo set %%g=!%%g!>>2048JWinslow.dat
  80. for /l %%g in (0,1,15)do set tmpboard[%%g]=!board[%%g]!&echo set board[%%g]=!board[%%g]!>>2048JWinslow.dat
  81. exit /b
  82. :end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement