Advertisement
Guest User

2048 in Batch script - Color Edition

a guest
Mar 29th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 9.03 KB | None | 0 0
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. title 2048 Colorized
  4. mode con: cols=35 lines=13
  5. for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do set "DEL=%%a"
  6. <nul >"%temp%\x" set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%.%DEL%"
  7. :startgame
  8. set score=0
  9. set winstate=0
  10. for /l %%g in (0,1,15) do set board[%%g]=0
  11. call :tilespawn
  12. call :tilespawn
  13. :startloop
  14. for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!& if !board[%%g]!==0 set board2[%%g]=      
  15. set h=-1
  16. :0
  17. set /a h+=1
  18. set board2[%h%]=!board2[%h%]:~-6!
  19. if %h% neq 15 goto 0
  20. set h=-1
  21. :00
  22. set /a h+=1
  23. if !board[%h%]!==0 set board3[%h%]=07
  24. if !board[%h%]!==2 set board3[%h%]=f0
  25. if !board[%h%]!==4 set board3[%h%]=70
  26. if !board[%h%]!==8 set board3[%h%]=c7
  27. if !board[%h%]!==16 set board3[%h%]=cf
  28. if !board[%h%]!==32 set board3[%h%]=47
  29. if !board[%h%]!==64 set board3[%h%]=4f
  30. if !board[%h%]!==128 set board3[%h%]=67
  31. if !board[%h%]!==256 set board3[%h%]=6f
  32. if !board[%h%]!==512 set board3[%h%]=e8
  33. if !board[%h%]!==1024 set board3[%h%]=e8
  34. if !board[%h%]!==2048 set board3[%h%]=e0
  35. if %h% neq 15 goto 00
  36. cls
  37. echo Score: %score%
  38. echo(
  39. echo ÚÄÄÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÄÄ¿
  40. <nul set /p=^|
  41. call :colorPrintVar %board3[0]% board2[0]
  42. <nul set /p=^|
  43. call :colorPrintVar %board3[1]% board2[1]
  44. <nul set /p=^|
  45. call :colorPrintVar %board3[2]% board2[2]
  46. <nul set /p=^|
  47. call :colorPrintVar %board3[3]% board2[3]
  48. echo ^|
  49. echo ÃÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄ´
  50. <nul set /p=^|
  51. call :colorPrintVar %board3[4]% board2[4]
  52. <nul set /p=^|
  53. call :colorPrintVar %board3[5]% board2[5]
  54. <nul set /p=^|
  55. call :colorPrintVar %board3[6]% board2[6]
  56. <nul set /p=^|
  57. call :colorPrintVar %board3[7]% board2[7]
  58. echo ^|
  59. echo ÃÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄ´
  60. <nul set /p=^|
  61. call :colorPrintVar %board3[8]% board2[8]
  62. <nul set /p=^|
  63. call :colorPrintVar %board3[9]% board2[9]
  64. <nul set /p=^|
  65. call :colorPrintVar %board3[10]% board2[10]
  66. <nul set /p=^|
  67. call :colorPrintVar %board3[11]% board2[11]
  68. echo ^|
  69. echo ÃÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄ´
  70. <nul set /p=^|
  71. call :colorPrintVar %board3[12]% board2[12]
  72. <nul set /p=^|
  73. call :colorPrintVar %board3[13]% board2[13]
  74. <nul set /p=^|
  75. call :colorPrintVar %board3[14]% board2[14]
  76. <nul set /p=^|
  77. call :colorPrintVar %board3[15]% board2[15]
  78. echo ^|
  79. echo ÀÄÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÙ
  80. for /l %%g in (0,1,15) do set tmpboard[%%g]=!board[%%g]!
  81. choice /c wasdxn /n /m ""
  82. if %errorlevel%==1 call :rotateclockwise&call :rotateclockwise&call :rotateclockwise&call :compress&call :merge&call :compress&call :compress&call :rotateclockwise
  83. if %errorlevel%==2 call :compress&call :merge&call :compress&call :compress
  84. if %errorlevel%==3 call :rotateclockwise&call :compress&call :merge&call :compress&call :compress&call :rotateclockwise&call :rotateclockwise&call :rotateclockwise
  85. if %errorlevel%==4 call :rotateclockwise&call :rotateclockwise&call :compress&call :merge&call :compress&call :compress&call :rotateclockwise&call :rotateclockwise
  86. if %errorlevel%==5 goto end
  87. if %errorlevel%==6 goto startgame
  88. set boardchanged=0
  89. set tiles=0
  90. for /l %%g in (0,1,15) do (
  91. if !board[%%g]! neq !tmpboard[%%g]! set boardchanged=1
  92. if !board[%%g]! neq 0 set /a tiles+=1
  93. if !board[%%g]!==2048 if %winstate%==0 set winstate=1
  94. )
  95. if %boardchanged%==1 call :tilespawn
  96. if %winstate%==1 goto youwin
  97. set youlost=0
  98. set v=0
  99. :1
  100. set /a v+=1
  101. set /a w=%v%%%4
  102. set /a x=%v%/4
  103. set /a y=%v%-1
  104. set /a z=%v%-4
  105. if %tiles%==15 if %w% neq 0 if !board[%v%]!==!board[%y%]! set youlost=1
  106. if %tiles%==15 if %x% neq 0 if !board[%v%]!==!board[%z%]! set youlost=1
  107. if %tiles%==15 if %v% neq 15 goto 1
  108. if %tiles%==15 if %youlost%==0 goto youlost
  109. goto startloop
  110. :youlost
  111. for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!& if !board[%%g]!==0 set board2[%%g]=      
  112. set h=-1
  113. :0
  114. set /a h+=1
  115. set board2[%h%]=!board2[%h%]:~-6!
  116. if %h% neq 15 goto 0
  117. set h=-1
  118. :00
  119. set /a h+=1
  120. if !board[%h%]!==0 set board3[%h%]=07
  121. if !board[%h%]!==2 set board3[%h%]=f0
  122. if !board[%h%]!==4 set board3[%h%]=70
  123. if !board[%h%]!==8 set board3[%h%]=c7
  124. if !board[%h%]!==16 set board3[%h%]=cf
  125. if !board[%h%]!==32 set board3[%h%]=47
  126. if !board[%h%]!==64 set board3[%h%]=4f
  127. if !board[%h%]!==128 set board3[%h%]=67
  128. if !board[%h%]!==256 set board3[%h%]=6f
  129. if !board[%h%]!==512 set board3[%h%]=e8
  130. if !board[%h%]!==1024 set board3[%h%]=e8
  131. if !board[%h%]!==2048 set board3[%h%]=e0
  132. if %h% neq 15 goto 00
  133. cls
  134. echo Score: %score%
  135. echo(
  136. call :colorPrintVar %board3[0]% board2[0]
  137. call :colorPrint 07 "|"
  138. call :colorPrintVar %board3[1]% board2[1]
  139. call :colorPrint 07 "|"
  140. call :colorPrintVar %board3[2]% board2[2]
  141. call :colorPrint 07 "|"
  142. call :colorPrintVar %board3[3]% board2[3] /n
  143. echo ------+------+------+------
  144. call :colorPrintVar %board3[4]% board2[4]
  145. call :colorPrint 07 "|"
  146. call :colorPrintVar %board3[5]% board2[5]
  147. call :colorPrint 07 "|"
  148. call :colorPrintVar %board3[6]% board2[6]
  149. call :colorPrint 07 "|"
  150. call :colorPrintVar %board3[7]% board2[7] /n
  151. echo ------+------+------+------
  152. call :colorPrintVar %board3[8]% board2[8]
  153. call :colorPrint 07 "|"
  154. call :colorPrintVar %board3[9]% board2[9]
  155. call :colorPrint 07 "|"
  156. call :colorPrintVar %board3[10]% board2[10]
  157. call :colorPrint 07 "|"
  158. call :colorPrintVar %board3[11]% board2[11] /n
  159. echo ------+------+------+------
  160. call :colorPrintVar %board3[12]% board2[12]
  161. call :colorPrint 07 "|"
  162. call :colorPrintVar %board3[13]% board2[13]
  163. call :colorPrint 07 "|"
  164. call :colorPrintVar %board3[14]% board2[14]
  165. call :colorPrint 07 "|"
  166. call :colorPrintVar %board3[15]% board2[15] /n
  167. echo GAME OVER!
  168. choice /c nx /n /m "Press N to play again..."
  169. if %errorlevel%==1 goto startgame
  170. if %errorlevel%==2 goto end
  171. :youwin
  172. set winstate=2
  173. for /l %%g in (0,1,15) do set board2[%%g]=      !board[%%g]!& if !board[%%g]!==0 set board2[%%g]=      
  174. set h=-1
  175. :0
  176. set /a h+=1
  177. set board2[%h%]=!board2[%h%]:~-6!
  178. if %h% neq 15 goto 0
  179. set h=-1
  180. :00
  181. set /a h+=1
  182. if !board[%h%]!==0 set board3[%h%]=07
  183. if !board[%h%]!==2 set board3[%h%]=f0
  184. if !board[%h%]!==4 set board3[%h%]=70
  185. if !board[%h%]!==8 set board3[%h%]=c7
  186. if !board[%h%]!==16 set board3[%h%]=cf
  187. if !board[%h%]!==32 set board3[%h%]=47
  188. if !board[%h%]!==64 set board3[%h%]=4f
  189. if !board[%h%]!==128 set board3[%h%]=67
  190. if !board[%h%]!==256 set board3[%h%]=6f
  191. if !board[%h%]!==512 set board3[%h%]=e8
  192. if !board[%h%]!==1024 set board3[%h%]=e8
  193. if !board[%h%]!==2048 set board3[%h%]=e0
  194. if %h% neq 15 goto 00
  195. cls
  196. echo Score: %score%
  197. echo(
  198. call :colorPrintVar %board3[0]% board2[0]
  199. call :colorPrint 07 "|"
  200. call :colorPrintVar %board3[1]% board2[1]
  201. call :colorPrint 07 "|"
  202. call :colorPrintVar %board3[2]% board2[2]
  203. call :colorPrint 07 "|"
  204. call :colorPrintVar %board3[3]% board2[3] /n
  205. echo ------+------+------+------
  206. call :colorPrintVar %board3[4]% board2[4]
  207. call :colorPrint 07 "|"
  208. call :colorPrintVar %board3[5]% board2[5]
  209. call :colorPrint 07 "|"
  210. call :colorPrintVar %board3[6]% board2[6]
  211. call :colorPrint 07 "|"
  212. call :colorPrintVar %board3[7]% board2[7] /n
  213. echo ------+------+------+------
  214. call :colorPrintVar %board3[8]% board2[8]
  215. call :colorPrint 07 "|"
  216. call :colorPrintVar %board3[9]% board2[9]
  217. call :colorPrint 07 "|"
  218. call :colorPrintVar %board3[10]% board2[10]
  219. call :colorPrint 07 "|"
  220. call :colorPrintVar %board3[11]% board2[11] /n
  221. echo ------+------+------+------
  222. call :colorPrintVar %board3[12]% board2[12]
  223. call :colorPrint 07 "|"
  224. call :colorPrintVar %board3[13]% board2[13]
  225. call :colorPrint 07 "|"
  226. call :colorPrintVar %board3[14]% board2[14]
  227. call :colorPrint 07 "|"
  228. call :colorPrintVar %board3[15]% board2[15] /n
  229. echo YOU WIN!
  230. echo Press C to continue
  231. choice /c cnx /n /m "Press N to play again"
  232. if %errorlevel%==1 goto startloop
  233. if %errorlevel%==2 goto startgame
  234. if %errorlevel%==3 goto end
  235.  
  236. :tilespawn
  237. set /a randtile=%random%%%16
  238. if !board[%randtile%]! gtr 0 goto tilespawn
  239. set /a board[%randtile%]=%random%%%10/9*2+2
  240. exit /b
  241.  
  242. :compress
  243. set x=-1
  244. :2
  245. set /a x+=1
  246. for /l %%g in (0,1,3) do set tmparray[%%g]=0
  247. set w=0
  248. set y=-1
  249. :3
  250. set /a y+=1
  251. set /a z=%x%*4+%y%
  252. if !board[%z%]! neq 0 set /a tmparray[%w%]=!board[%z%]! & set /a w+=1
  253. if %y% neq 3 goto 3
  254. for /l %%g in (0,1,3) do set /a z=%x%*4+%%g & set /a board[!z!]=!tmparray[%%g]!
  255. if %x% neq 3 goto 2
  256. exit /b
  257.  
  258. :merge
  259. set x=-1
  260. :4
  261. set /a x+=1
  262. set y=-1
  263. :5
  264. set /a y+=1
  265. set /a w=%x%*4+%y%
  266. set /a z=%w%+1
  267. if !board[%w%]!==!board[%z%]! set /a board[%w%]*=2 & set board[%z%]=0 & set /a score+=!board[%w%]!
  268. if %y% neq 2 goto 5
  269. if %x% neq 3 goto 4
  270. exit /b
  271.  
  272. :rotateclockwise
  273. set i=0
  274. for %%j in (12 8 4 0 13 9 5 1 14 10 6 2 15 11 7 3) do (
  275.    set /A tmparray[!i!]=board[%%j], i+=1
  276. )
  277. for /l %%g in (0,1,15) do set /a board[%%g]=!tmparray[%%g]!
  278. exit /b
  279.  
  280. :colorPrint Color  Str  [/n]
  281. setlocal
  282. set "str=%~2"
  283. call :colorPrintVar %1 str %3
  284. exit /b
  285.  
  286. :colorPrintVar  Color  StrVar  [/n]
  287. if not defined %~2 exit /b
  288. set "str=a%DEL%!%~2:\=a%DEL%\..\%DEL%%DEL%%DEL%!"
  289. set "str=!str:/=a%DEL%/..\%DEL%%DEL%%DEL%!"
  290. set "str=!str:"=\"!"
  291. pushd "%temp%"
  292. findstr /p /A:%1 "." "!str!\..\x" nul
  293. if /i "%~3"=="/n" echo(
  294. exit /b
  295.  
  296. :end
  297. del "%temp%\x"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement