Guest User

Tetris in BATCH

a guest
Dec 9th, 2015
5,529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 KB | None | 0 0
  1. @echo off
  2. setlocal EnableDelayedExpansion
  3.  
  4. if "%~1" neq "" goto %1
  5.  
  6. title Tetris.BAT by Aacini
  7. rem Written by Antonio Perez Ayala
  8. rem http://www.dostips.com/forum/viewtopic.php?f=3&t=6812
  9. rem Reference: http://colinfahey.com/tetris/tetris.html
  10. rem 2015/11/27 - version 1.0
  11.  
  12. rem Delete the next line when Bitmap font 16x8 is used
  13. set "F16x8=REM"
  14.  
  15. cls
  16. echo/
  17. echo === Pure .BATch-file Tetris game by Aacini ===
  18. echo/
  19. echo/
  20. echo Tetris pieces are controlled with these keys:
  21. echo/
  22. echo rot.right
  23. echo rot. rot. move I move
  24. echo left ^<- A S D -^> right left ^<- J K L -^> right
  25. echo ^| ^|
  26. echo v v
  27. echo soft drop hard drop
  28. echo/
  29. echo/
  30. echo Press P to pause the game; press N to end game
  31. echo/
  32. echo/
  33. pause
  34. cls
  35.  
  36. rem Field dimensions
  37. set /A cols=10, lines=20
  38.  
  39. set /A col=cols+6, lin=lines+8
  40. %F16x8% set /A lin+=lines+2
  41. mode CON: cols=%col% lines=%lin%
  42. chcp 850 > NUL
  43. cd . > pipeFile.txt
  44. "%~F0" Input >> pipeFile.txt | "%~F0" Main < pipeFile.txt
  45. ping localhost -n 2 > NUL
  46. del pipeFile.txt
  47. goto :EOF
  48.  
  49.  
  50.  
  51. :Input
  52. set "com[J]=Dx=-1"
  53. set "com[L]=Dx=1"
  54. set "com[K]=del=3"
  55. set "com[I]=R=-1"
  56. set "com[A]=R=1"
  57. set "com[D]=R=-1"
  58. set "com[S]=Dy=-1"
  59. set "com[Y]=Y"
  60. set "com[N]=N=1"
  61. set "com[P]=pause=1"
  62.  
  63. for /L %%# in () do (
  64. set "key="
  65. for /F "delims=" %%k in ('xcopy /W "%~F0" "%~F0" 2^>NUL') do if not defined key set "key=%%k"
  66. for /F %%k in ("!key:~-1!") do (
  67. echo(!com[%%k]!
  68. if /I "%%k" equ "N" exit
  69. )
  70. )
  71. rem exit
  72.  
  73.  
  74.  
  75. :Main
  76.  
  77. (
  78. for /F "delims==" %%v in ('set') do set "%%v="
  79. set /A cols=%cols%, lines=%lines%
  80. set "F16x8=%F16x8%"
  81. )
  82.  
  83. rem Initialize the Field
  84. for /L %%i in (1,1,%cols%) do set "spc=!spc! "
  85. for /L %%i in (1,1,%lines%) do set "F%%i= ³%spc%³"
  86. set /A top=lines+1
  87. set "F%top%= Ú" & set "F0= À"
  88. for /L %%i in (1,1,%cols%) do set "F%top%=!F%top%!Ä" & set "F0=!F0!Ä"
  89. set "F%top%=!F%top%!¿" & set "F0=%F0%Ù"
  90. set "F-1= Level: 1" & set "Level=1"
  91. set "F-2= Rows: 0" & set "Rows=0"
  92. set "F-3= Score: 0" & set "Score=0"
  93. for /L %%i in (1,1,%cols%) do set "blk=!blk!Û"
  94. set /A top=lines+3, delay=50
  95. %F16x8% set /A linesP2=lines+2
  96.  
  97. rem Define all ":orientations:" of the O I S Z L J T pieces via "triplets":
  98. rem (offset Y . offset X . length X); one "triplet" for each horizontal line
  99. for %%t in ( "O:0.-1.2 -1.-1.2"
  100. "I:0.-2.4:1.0.1 0.0.1 -1.0.1 -2.0.1"
  101. "S:0.0.2 -1.-1.2:1.0.1 0.0.2 -1.1.1"
  102. "Z:0.-1.2 -1.0.2:1.1.1 0.0.2 -1.0.1"
  103. "L:0.-1.3 -1.-1.1:1.0.1 0.0.1 -1.0.2:1.1.1 0.-1.3:1.-1.2 0.0.1 -1.0.1"
  104. "J:0.-1.3 -1.1.1:1.0.2 0.0.1 -1.0.1:1.-1.1 0.-1.3:1.0.1 0.0.1 -1.-1.2"
  105. "T:0.-1.3 -1.0.1:1.0.1 0.0.2 -1.0.1:1.0.1 0.-1.3:1.0.1 0.-1.2 -1.0.1" ) do (
  106. set "pc=%%~t"
  107. set "i=-1"
  108. for /F "delims=" %%p in (^"!pc::^=^
  109. % New line %
  110. !^") do (
  111. if !i! lss 0 (set "pc=%%p") else set "!pc!!i!=%%p"
  112. set /A i+=1
  113. )
  114. set "!pc!N=!i!"
  115. )
  116. set "pcs=OISZLJT"
  117.  
  118. set "init=1"
  119. for /L %%# in () do (
  120.  
  121. if defined init (
  122. setlocal EnableDelayedExpansion
  123. set "init="
  124.  
  125. rem Create the first "previous" piece
  126. for /L %%i in (0,1,!time:~-1!) do set /A p=!random!%%7
  127. for %%p in (!p!) do set "p2=!pcs:~%%p,1!"
  128. for %%p in (!p2!) do set "p3=!%%p0!" & set "p4=!%%pN!"
  129.  
  130. set "new=1"
  131. )
  132.  
  133. if defined new (
  134. set "new="
  135.  
  136. rem Take the "previous" piece as current one
  137. set "pc=!p2!" & set "p0=!p3!" & set "pN=!p4!"
  138.  
  139. rem Create a new "previous" piece
  140. for /L %%i in (1,1,2) do (
  141. set /A p=!random!*7/32768
  142. for %%p in (!p!) do (
  143. set "p=!pcs:~%%p,1!"
  144. if !p! neq !pc! set "p2=!p!"
  145. )
  146. )
  147. for %%p in (!p2!) do set "p3=!%%p0!" & set "p4=!%%pN!"
  148.  
  149. rem Insert the new "previous" piece in its place, above Field
  150. set /A x=3+cols/2, y=top, yp=top-1
  151. set "F!yp!= %spc%"
  152. for %%p in (!p3!) do (
  153. for /F "tokens=1-3 delims=." %%i in ("%%p") do (
  154. set /A yp=y+%%i, xp=x+%%j, xL=xp+%%k
  155. for /F "tokens=1-3" %%a in ("!yp! !xp! !xL!") do (
  156. set "F%%a=!spc:~0,%%b!!blk:~0,%%k!!spc:~%%c!"
  157. )
  158. )
  159. )
  160.  
  161. rem Try to insert the new current piece in the Field...
  162. set /A x=3+cols/2, y=lines, b=1
  163. for %%p in (!p0!) do (
  164. for /F "tokens=1-3 delims=." %%i in ("%%p") do (
  165. set /A yp=y+%%i, xp=x+%%j, xL=xp+%%k
  166. for /F "tokens=1-3" %%a in ("!yp! !xp! !xL!") do (
  167. if "!F%%a:~%%b,%%k!" neq "!spc:~0,%%k!" set "b="
  168. set "F%%a=!F%%a:~0,%%b!!blk:~0,%%k!!F%%a:~%%c!"
  169. )
  170. )
  171. )
  172. cls
  173. for /L %%i in (%top%,-1,-3) do (
  174. echo(!F%%i!
  175. %F16x8% if %%i geq %linesP2% echo(!F%%i!
  176. %F16x8% if %%i geq 1 if %%i leq %lines% echo(!F%%i!
  177. )
  178.  
  179. rem ... if that was not possible:
  180. if not defined b call :endGame & endlocal
  181.  
  182. set "p1=!p0!"
  183. set /A "pI=0, del=delay, b=1!time:~-2!"
  184.  
  185. )
  186.  
  187. rem Control module: move the piece as requested via a key, or down one row each %del% centiseconds
  188. set "move="
  189. set /A "Dy=Dx=0"
  190. set /P "com="
  191. if defined com (
  192. set /A "!com!, move=1"
  193. set "com="
  194. if defined N exit
  195. if defined pause call :Pause & set "move="
  196. set "b=1!time:~-2!"
  197. ) else (
  198. set /A "e=1!time:~-2!, elap=e-b, elap-=(elap>>31)*100"
  199. if !elap! geq !del! set /A b=e, Dy=move=-1
  200. )
  201.  
  202. if defined move (
  203.  
  204. rem Delete the piece from its current position, and store current coordinates
  205. set i=0
  206. for %%p in (!p0!) do for /F "tokens=1-3 delims=." %%i in ("%%p") do (
  207. set /A yp=y+%%i, xp=x+%%j, xL=xp+%%k
  208. for /F "tokens=1-3" %%a in ("!yp! !xp! !xL!") do (
  209. set "F%%a=!F%%a:~0,%%b!!spc:~0,%%k!!F%%a:~%%c!"
  210. set /A i+=1
  211. set "c!i!=%%a %%b %%c %%k"
  212. )
  213. )
  214.  
  215. rem If move is Rotate: get rotated piece
  216. if defined R (
  217. set /A "p=(pI+R+pN)%%pN"
  218. for /F "tokens=1,2" %%i in ("!pc! !p!") do set "p1=!%%i%%j!"
  219. )
  220.  
  221. rem Test if the piece can be placed at the new position, and store new coordinates
  222. set j=0
  223. for %%p in (!p1!) do if defined move (
  224. for /F "tokens=1-3 delims=." %%i in ("%%p") do (
  225. set /A yp=y+%%i+Dy, xp=x+%%j+Dx, xL=xp+%%k
  226. for /F "tokens=1-3" %%a in ("!yp! !xp! !xL!") do (
  227. if "!F%%a:~%%b,%%k!" equ "!spc:~0,%%k!" (
  228. set /A j+=1
  229. set "n!j!=%%a %%b %%c %%k"
  230. ) else (
  231. set "move="
  232. )
  233. )
  234. )
  235. )
  236.  
  237. if defined move (
  238.  
  239. rem Place the piece at the new position
  240. for /L %%j in (1,1,!j!) do (
  241. for /F "tokens=1-4" %%a in ("!n%%j!") do (
  242. set "F%%a=!F%%a:~0,%%b!!blk:~0,%%d!!F%%a:~%%c!"
  243. )
  244. )
  245.  
  246. rem Update the Field in screen
  247. cls
  248. for /L %%i in (%top%,-1,-3) do (
  249. echo(!F%%i!
  250. %F16x8% if %%i geq %linesP2% echo(!F%%i!
  251. %F16x8% if %%i geq 1 if %%i leq %lines% echo(!F%%i!
  252. )
  253.  
  254. rem Update any changes in the piece
  255. set /A y+=Dy, x+=Dx
  256. if defined R set "p0=!p1!" & set "pI=!p!" & set "R="
  257.  
  258. ) else ( rem The piece can not be moved
  259.  
  260. rem Recover the piece at its current position
  261. for /L %%i in (1,1,!i!) do (
  262. for /F "tokens=1-4" %%a in ("!c%%i!") do (
  263. set "F%%a=!F%%a:~0,%%b!!blk:~0,%%d!!F%%a:~%%c!"
  264. )
  265. )
  266. if defined R set "p1=!p0!" & set "R="
  267.  
  268. if !Dy! neq 0 ( rem The piece "lands"
  269.  
  270. rem Count completed lines
  271. set "j=0"
  272. for /L %%i in (1,1,!i!) do for /F %%a in ("!c%%i!") do (
  273. if "!F%%a:~3,%cols%!" equ "%blk%" (
  274. set "F%%a= ³%spc: ==%³"
  275. set /A j+=1
  276. )
  277. )
  278.  
  279. if !j! neq 0 (
  280. rem Update scores (See N-Blox at http://www.tetrisfriends.com/help/tips_appendix.php#rankingsystem)
  281. set /A "xp=Level*(40+((j-2>>31)+1)*60+((j-3>>31)+1)*200+((j-4>>31)+1)*900), Score+=xp, Rows+=j, xL=Level, Level=(Rows-1)/10+1"
  282. set "F-2=!F-2:~0,8!+!j! "
  283. set "xp=!xp! "
  284. set "F-3=!F-3:~0,8!+!xp:~0,6!"
  285. echo  BEL Ctrl-G Ascii-7
  286. cls
  287. for /L %%i in (%top%,-1,-3) do (
  288. echo(!F%%i!
  289. %F16x8% if %%i geq %linesP2% echo(!F%%i!
  290. %F16x8% if %%i geq 1 if %%i leq %lines% echo(!F%%i!
  291. )
  292. set "F-1=!F-1:~0,8! !Level!"
  293. set "F-2=!F-2:~0,8! !Rows!"
  294. set "F-3=!F-3:~0,8! !Score!"
  295. if !Level! neq !xL! if !delay! gtr 5 set /A delay-=5
  296.  
  297. rem Remove completed lines
  298. set "i=1"
  299. for /L %%i in (1,1,%lines%) do (
  300. set "F!i!=!F%%i!"
  301. if "!F%%i:~3,1!" neq "=" set /A i+=1
  302. )
  303. for /L %%i in (!i!,1,%lines%) do set "F%%i= ³%spc%³"
  304. call :Delay 95
  305. cls
  306. for /L %%i in (%top%,-1,-3) do (
  307. echo(!F%%i!
  308. %F16x8% if %%i geq %linesP2% echo(!F%%i!
  309. %F16x8% if %%i geq 1 if %%i leq %lines% echo(!F%%i!
  310. )
  311. )
  312.  
  313. rem Request to show a new piece
  314. set "new=1"
  315.  
  316. )
  317.  
  318. )
  319.  
  320. )
  321.  
  322. )
  323.  
  324. :endGame
  325. set /P "=Play again? " < NUL
  326. :choice
  327. set /P "com="
  328. if not defined com goto choice
  329. if /I "%com%" equ "Y" exit /B
  330. if /I "%com:~0,1%" neq "N" set "com=" & goto choice
  331. echo N
  332. exit
  333.  
  334.  
  335. :Pause
  336. set "pause=!F%lines%!"
  337. set "F%lines%= ³%spc: = PAUSED %³"
  338. cls & for /L %%i in (%top%,-1,-3) do (
  339. echo(!F%%i!
  340. %F16x8% if %%i geq %linesP2% echo(!F%%i!
  341. %F16x8% if %%i geq 1 if %%i leq %lines% echo(!F%%i!
  342. )
  343. :wait
  344. set /P "com="
  345. if not defined com goto wait
  346. set "com="
  347. set "F%lines%=%pause%"
  348. cls & for /L %%i in (%top%,-1,-3) do (
  349. echo(!F%%i!
  350. %F16x8% if %%i geq %linesP2% echo(!F%%i!
  351. %F16x8% if %%i geq 1 if %%i leq %lines% echo(!F%%i!
  352. )
  353. set "pause="
  354. exit /B
  355.  
  356.  
  357. :Delay centisecs
  358. set "b=1%time:~-2%"
  359. :wait2
  360. set /A "e=1%time:~-2%, elap=e-b, elap-=(elap>>31)*100"
  361. if %elap% lss %1 goto wait2
  362. set "b=1%time:~-2%"
  363. exit /B
Advertisement
Add Comment
Please, Sign In to add comment