VanGans

Tetris.bat

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