Guest User

Untitled

a guest
Aug 24th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.63 KB | Source Code | 0 0
  1. \ 4096th.fth
  2.  
  3. \ Utils
  4. variable random
  5. 2 random ! \ seed
  6. \ random time&date + + + + + + random ! \ Memmory address + time seed
  7. : rand ( max -- r ) random @ 6364136223846793005 * 1 + dup random ! swap mod ;
  8. \ TODO: Discover the bit-ness and parse the string to the correct value
  9. \ : cell-bits ( -- n ) 1 begin 2* dup 0< until ;
  10. \ Values for LCG: Xn+1 = a Xn + c mod 2^bit
  11. \ 8-bit mod 2^8 a 5 c 1
  12. \ 16-bit mod 2^16 a 21 c 1
  13. \ 32-bit mod 2^32 a 1103515245 c 12345
  14. \ 64-bit mod 2^64 a 6364136223846793005 c 1
  15.  
  16.  
  17. \ Grid
  18. variable grid 15 cells allot
  19. grid 16 cells erase
  20.  
  21. \ Colors:
  22. \ 2 K 64 M
  23. \ 4 Y 128 R
  24. \ 8 G 256 Y
  25. \ 16 C 512 K
  26. \ 32 B 1024 W
  27. : drawGrid
  28. page
  29. grid
  30. dup 0 cells + @ 7 .r
  31. dup 1 cells + @ 7 .r
  32. dup 2 cells + @ 7 .r
  33. dup 3 cells + @ 7 .r
  34. cr
  35. dup 4 cells + @ 7 .r
  36. dup 5 cells + @ 7 .r
  37. dup 6 cells + @ 7 .r
  38. dup 7 cells + @ 7 .r
  39. cr
  40. dup 8 cells + @ 7 .r
  41. dup 9 cells + @ 7 .r
  42. dup 10 cells + @ 7 .r
  43. dup 11 cells + @ 7 .r
  44. cr
  45. dup 12 cells + @ 7 .r
  46. dup 13 cells + @ 7 .r
  47. dup 14 cells + @ 7 .r
  48. dup 15 cells + @ 7 .r
  49. cr
  50. drop
  51. ;
  52.  
  53. : countEmpty ( -- c ) \ Can later be replacced by a variable to keep count?
  54. 0 ( 0 )
  55. 16 0 do
  56. grid i cells + @ ( c gridi )
  57. 0= ( c grid=0? )
  58. if 1+ then ( c++ )
  59. loop
  60. ;
  61.  
  62. : addRandomVal ( -- )
  63. countEmpty ( n )
  64. rand ( rn )
  65. 15 0 do
  66. grid ( rn grid )
  67. i cells + @ ( rn gridi )
  68. 0= ( rn gridi=0? )
  69. if ( rn )
  70. 1- ( rn-- )
  71. dup
  72. 0< ( rn rn<0 ) \ gridi==0 and rn<0 Here it is
  73. if ( rn )
  74. grid i ( grid i )
  75. cells + ( gridi )
  76. 2 swap ! ( )
  77. leave
  78. then then
  79. loop
  80. drop
  81. \ grid 0 to 15 if gridi 0= rn-- if rn<0 gridi=2 leave
  82. \ Randomize sequence of [0,15]
  83. \ Find first of those that is empty
  84. \ Randomize val either 2 or 4
  85. \ put val in position
  86. \ Later: check for no more empty (here? elsewhere?)
  87. \ Easy mode?
  88. ;
  89.  
  90. : initGrid
  91. grid 16 cells erase
  92. addRandomVal
  93. addRandomVal
  94. ;
  95.  
  96.  
  97. \ Fall
  98. : drop0 ( a 0 ... 0 -- a )
  99. begin dup 0= while drop repeat
  100. ;
  101.  
  102. -1 constant sentinel
  103.  
  104. : ?sentinel ( a -- a a=sentinel? )
  105. dup sentinel =
  106. ;
  107.  
  108. : findSentinel ( S ... -- S ... n )
  109. 5 0 do \ Furthest sentinel can be is SIZE + 1
  110. i pick ( S ... xn )
  111. ?sentinel ( S ... xn xn=S? )
  112. if ( S ... xn )
  113. drop ( S ... )
  114. i leave ( S ... n )
  115. then ( S ... xn )
  116. drop ( S ... )
  117. loop
  118. ;
  119.  
  120. : 0pad ( S ... n -- S 0 ... 0 ... )
  121. 4 swap - ( S x ... 4-n ) \ x is known to be non-zero
  122. 0 do 0 loop ( S x ... 0 ... 0 )
  123. begin ( S x ... 0 ... )
  124. 3 pick ( s x ... 0 ... x )
  125. 0<> ( S x ... 0 ... x<>0? ) \ in fact, no zeroes before S
  126. while ( S x ... 0 ... )
  127. 3 roll ( S ... 0 ... x )
  128. repeat ( S 0 ... 0 x ... x )
  129. ;
  130.  
  131. : fall ( sentinel a1 b1 c1 d1 -- sentinel a2 b2 c2 d2 )
  132. sentinel >r ( S a b c d ) ( r: S )
  133. begin ( S ... x y ) ( r: ... S )
  134. ?sentinel 0= ( S ... x y y!=S? ) ( r: ... S )
  135. while ( S ... x y ) ( r: ... S )
  136. drop0 ?sentinel 0= ( S ... x y y!=S? ) ( r: ... S )
  137. if \ Needed check for case of ( S 0 ... 0 ) so drop0 >r doesnt sent S.
  138. drop0 >r ( S ... x ) ( r: y ... S )
  139. drop0 r> ( S ... x y ( r: ... S )
  140. 2dup ( S ... x y x y ) ( r: ... S )
  141. = ( S ... x y x=y? ) ( r: ... S )
  142. if + then ( S ... Z ) ( r: ... S ) \ Z = y | x+y
  143. >r ( S ... ) ( r: Z ... S )
  144. then
  145. repeat
  146. begin ( S ) ( r: ... S )
  147. r> ?sentinel
  148. until ( S ... S ) ( r: )
  149. drop ( S ... )
  150. findSentinel ( S ... n )
  151. dup 4 - 0<> ( S ... n n<>4 )
  152. if 0pad else drop then ( S a2 b2 c2 d2 )
  153. ;
  154.  
  155.  
  156. \ Lines
  157. : getCol ( i -- a b c d )
  158. grid swap ( grid i )
  159. cells + ( gridi0 )
  160. dup ( gridi0 gridi0 )
  161. @ swap ( a gridi0 )
  162. 4 cells + ( a gridi1 )
  163. dup ( a gridi1 gridi1 )
  164. @ swap ( a b gridi1 )
  165. 4 cells + ( a b gridi2 )
  166. dup ( a b gridi2 gridi2 )
  167. @ swap ( a b c gridi2 )
  168. 4 cells + ( a b c gridi3 )
  169. @ ( a b c d )
  170. ;
  171.  
  172. : getRow ( i -- a b c d )
  173. grid swap ( grid i )
  174. 4 * ( grid 4i )
  175. cells + ( gridi0 )
  176. dup ( gridi0 gridi0 )
  177. @ swap ( a gridi0 )
  178. 1 cells + ( a gridi1 )
  179. dup ( a gridi1 gridi1 )
  180. @ swap ( a b gridi1 )
  181. 1 cells + ( a b gridi2 )
  182. dup ( a b gridi2 gridi2 )
  183. @ swap ( a b c gridi2 )
  184. 1 cells + ( a b c gridi3 )
  185. @ ( a b c d )
  186. ;
  187.  
  188. : putCol ( a b c d i -- )
  189. grid swap ( a b c d grid i )
  190. cells + ( a b c d gridi0 )
  191. >r ( a b c d ) ( R: gridi0 )
  192. 3 roll ( b c d a ) ( R: gridi0 )
  193. r@ ! ( b c d ) ( R: gridi0 )
  194. 2 roll ( c d b ) ( R: gridi0 )
  195. r@ 1 4 * cells + ! ( c d ) ( R: gridi0 )
  196. swap ( d c ) ( R: gridi0 )
  197. r@ 2 4 * cells + ! ( d ) ( R: gridi0 )
  198. r> 3 4 * cells + ! ( ) ( R: )
  199. ;
  200.  
  201. : putRow ( a b c d i -- )
  202. grid swap ( a b c d grid i )
  203. 4 * ( a b c d grid 4i )
  204. cells + ( a b c d gridi0 )
  205. >r ( a b c d ) ( R: gridi0 )
  206. 3 roll ( b c d a ) ( R: gridi0 )
  207. r@ ! ( b c d ) ( R: gridi0 )
  208. 2 roll ( c d b ) ( R: gridi0 )
  209. r@ 1 cells + ! ( c d ) ( R: gridi0 )
  210. swap ( d c ) ( R: gridi0 )
  211. r@ 2 cells + ! ( d ) ( R: gridi0 )
  212. r> 3 cells + ! ( ) ( R: )
  213. ;
  214.  
  215. : 4reverse ( a b c d -- d c b a )
  216. swap 2swap swap
  217. ;
  218.  
  219.  
  220. \ Gameplay
  221. : fallDown ( -- )
  222. 4 0 do
  223. sentinel ( S )
  224. i getCol ( S a1 b1 c1 d1 )
  225. fall ( S a2 b2 c2 d2 )
  226. i putCol ( S )
  227. drop ( )
  228. loop
  229. ;
  230.  
  231. : fallLeft ( -- )
  232. 4 0 do
  233. sentinel ( S )
  234. i getRow ( S a1 b1 c1 d1 )
  235. 4reverse ( S d1 c1 b1 a1 )
  236. fall ( S d2 c2 b2 a2 )
  237. 4reverse ( S a2 b2 c2 d2 )
  238. i putRow ( S )
  239. drop ( )
  240. loop
  241. ;
  242.  
  243. : fallRight ( -- )
  244. 4 0 do
  245. sentinel ( S )
  246. i getRow ( S a1 b1 c1 d1 )
  247. fall ( S a2 b2 c2 d2 )
  248. i putRow ( S )
  249. drop ( )
  250. loop
  251. ;
  252.  
  253. : fallUp ( -- )
  254. 4 0 do
  255. sentinel ( S )
  256. i getCol ( S a1 b1 c1 d1 )
  257. 4reverse ( S d1 c1 b1 a1 )
  258. fall ( S d2 c2 b2 a2 )
  259. 4reverse ( S a2 b2 c2 d2 )
  260. i putCol ( S )
  261. drop ( )
  262. loop
  263. ;
  264.  
  265.  
  266. \ Input
  267. : ?isDown ( key -- bool )
  268. dup ( key key )
  269. [char] s = ( key s? )
  270. swap dup ( s? key key )
  271. [char] S = ( s? key S? )
  272. swap dup ( s? S? key key )
  273. [char] j = ( s? S? key j? )
  274. swap ( s? S? j? key )
  275. [char] J = ( s? S? j? J? )
  276. or or or ( s|S|j|J? )
  277. ;
  278.  
  279. : ?isLeft ( key -- bool )
  280. dup ( key key )
  281. [char] a = ( key a? )
  282. swap dup ( a? key key )
  283. [char] A = ( a? key A? )
  284. swap dup ( a? A? key key )
  285. [char] h = ( a? A? key h? )
  286. swap ( a? A? h? key )
  287. [char] H = ( a? A? h? H? )
  288. or or or ( a|A|h|H? )
  289. ;
  290.  
  291. : ?isRight ( key -- bool )
  292. dup ( key key )
  293. [char] d = ( key d? )
  294. swap dup ( d? key key )
  295. [char] D = ( d? key D? )
  296. swap dup ( d? D? key key )
  297. [char] l = ( d? D? key l? )
  298. swap ( d? D? l? key )
  299. [char] L = ( d? D? l? L? )
  300. or or or ( d|D|l|L? )
  301. ;
  302.  
  303. : ?isUp ( key -- bool )
  304. dup ( key key )
  305. [char] w = ( key w? )
  306. swap dup ( w? key key )
  307. [char] W = ( w? key W? )
  308. swap dup ( w? W? key key )
  309. [char] k = ( w? W? key k? )
  310. swap ( w? W? k? key )
  311. [char] K = ( w? W? k? K? )
  312. or or or ( w|W|k|K? )
  313. ;
  314.  
  315. : ?isQuit ( key -- bool )
  316. dup ( key key )
  317. [char] q = ( key q? )
  318. swap ( q? key )
  319. [char] Q = ( q? Q? )
  320. or ( q|Q? )
  321. ;
  322.  
  323. : getInput ( -- quit? )
  324. key ( k )
  325. dup ?isDown if fallDown else
  326. dup ?isLeft if fallLeft else
  327. dup ?isRight if fallRight else
  328. dup ?isUp if fallUp else
  329. then then then then ?isQuit
  330. ;
  331.  
  332.  
  333. \ Game
  334. : mainLoop ( -- )
  335. begin
  336. addRandomVal
  337. cr drawGrid cr
  338. .s
  339. getInput ( quit? )
  340. until
  341. ;
  342.  
  343. initGrid
  344. mainLoop
  345.  
  346. \ sentinel
  347. \ 0 0 0 0
  348. \ .s
  349. \ fall
  350. \ .s
  351. \ cr cr
  352. \ drop drop drop drop
  353. \
  354. \ sentinel
  355. \ 1 2 3 4
  356. \ .s
  357. \ fall
  358. \ .s
  359. \ cr cr
  360. \ drop drop drop drop
  361. \
  362. \ 2 2 0 0
  363. \ .s fall .s cr cr
  364. \ drop drop drop drop
  365. \
  366. \ 2 4 0 2
  367. \ .s fall .s cr cr
  368. \ drop drop drop drop
  369. \
  370. \ 4 4 0 2
  371. \ .s fall .s cr cr
  372. \ drop drop drop drop
  373. \
  374. \ 4 4 4 2
  375. \ .s fall .s cr cr
  376. \ drop drop drop drop
  377. \
  378. \ 4 0 4 2
  379. \ .s fall .s cr cr
  380. \ drop drop drop drop
  381. \
  382. \ 2 2 2 2
  383. \ .s fall .s cr cr
  384. \ drop drop drop drop
  385. \
  386. \ 2 4 8 4
  387. \ .s fall .s cr cr
  388. \ drop drop drop drop
  389.  
  390. .s cr bye
Advertisement
Add Comment
Please, Sign In to add comment