Author Topic: PENtrIS  (Read 360 times)

PENtrIS
« on: October 11, 2018, 05:42:37 PM »
An extension of tetris to pentominos as described here https://en.wikipedia.org/wiki/Pentomino

I've never actually played this variant before other then this very program so not sure how playable and balanced it is, I am able to get several lines with a bit of luck.  Suggestions such as which pieces to add/remove, board size, etc are welcome.  Edit: fixed lines not being cleared issue

Code: QB64 [Select]
  1. DEFLNG a-z
  2.  
  3. DIM SHARED piece(17, 2, 4)
  4. DIM SHARED piece_color(17)
  5. DIM SHARED size, sw, sh
  6.  
  7. size = 35
  8. sw = 11
  9. sh = 25
  10.  
  11. REDIM SHARED board(sw - 1, sh - 1)
  12.  
  13. piece(0,0,0)=0: piece(0,1,0)=1: piece(0,2,0)=0
  14. piece(0,0,1)=0: piece(0,1,1)=1: piece(0,2,1)=0
  15. piece(0,0,2)=0: piece(0,1,2)=1: piece(0,2,2)=0
  16. piece(0,0,3)=0: piece(0,1,3)=1: piece(0,2,3)=0
  17. piece(0,0,4)=0: piece(0,1,4)=1: piece(0,2,4)=0
  18.  
  19. piece(1,0,0)=0: piece(1,1,0)=0: piece(1,2,0)=0
  20. piece(1,0,1)=0: piece(1,1,1)=0: piece(1,2,1)=0
  21. piece(1,0,2)=0: piece(1,1,2)=1: piece(1,2,2)=1
  22. piece(1,0,3)=1: piece(1,1,3)=1: piece(1,2,3)=0
  23. piece(1,0,4)=0: piece(1,1,4)=1: piece(1,2,4)=0
  24.  
  25. piece(2,0,0)=0: piece(2,1,0)=0: piece(2,2,0)=0
  26. piece(2,0,1)=0: piece(2,1,1)=0: piece(2,2,1)=0
  27. piece(2,0,2)=1: piece(2,1,2)=1: piece(2,2,2)=0
  28. piece(2,0,3)=0: piece(2,1,3)=1: piece(2,2,3)=1
  29. piece(2,0,4)=0: piece(2,1,4)=1: piece(2,2,4)=0
  30.  
  31. piece(3,0,0)=0: piece(3,1,0)=0: piece(3,2,0)=0
  32. piece(3,0,1)=0: piece(3,1,1)=1: piece(3,2,1)=0
  33. piece(3,0,2)=0: piece(3,1,2)=1: piece(3,2,2)=0
  34. piece(3,0,3)=0: piece(3,1,3)=1: piece(3,2,3)=0
  35. piece(3,0,4)=1: piece(3,1,4)=1: piece(3,2,4)=0
  36.  
  37. piece(4,0,0)=0: piece(4,1,0)=0: piece(4,2,0)=0
  38. piece(4,0,1)=0: piece(4,1,1)=1: piece(4,2,1)=0
  39. piece(4,0,2)=0: piece(4,1,2)=1: piece(4,2,2)=0
  40. piece(4,0,3)=0: piece(4,1,3)=1: piece(4,2,3)=0
  41. piece(4,0,4)=0: piece(4,1,4)=1: piece(4,2,4)=1
  42.  
  43. piece(5,0,0)=0: piece(5,1,0)=0: piece(5,2,0)=0
  44. piece(5,0,1)=0: piece(5,1,1)=0: piece(5,2,1)=0
  45. piece(5,0,2)=1: piece(5,1,2)=1: piece(5,2,2)=0
  46. piece(5,0,3)=1: piece(5,1,3)=1: piece(5,2,3)=0
  47. piece(5,0,4)=0: piece(5,1,4)=1: piece(5,2,4)=0
  48.  
  49. piece(6,0,0)=0: piece(6,1,0)=0: piece(6,2,0)=0
  50. piece(6,0,1)=0: piece(6,1,1)=0: piece(6,2,1)=0
  51. piece(6,0,2)=0: piece(6,1,2)=1: piece(6,2,2)=1
  52. piece(6,0,3)=0: piece(6,1,3)=1: piece(6,2,3)=1
  53. piece(6,0,4)=0: piece(6,1,4)=1: piece(6,2,4)=0
  54.  
  55. piece(7,0,0)=0: piece(7,1,0)=0: piece(7,2,0)=0
  56. piece(7,0,1)=0: piece(7,1,1)=1: piece(7,2,1)=0
  57. piece(7,0,2)=0: piece(7,1,2)=1: piece(7,2,2)=0
  58. piece(7,0,3)=1: piece(7,1,3)=1: piece(7,2,3)=0
  59. piece(7,0,4)=1: piece(7,1,4)=0: piece(7,2,4)=0
  60.  
  61. piece(8,0,0)=0: piece(8,1,0)=0: piece(8,2,0)=0
  62. piece(8,0,1)=0: piece(8,1,1)=1: piece(8,2,1)=0
  63. piece(8,0,2)=0: piece(8,1,2)=1: piece(8,2,2)=0
  64. piece(8,0,3)=0: piece(8,1,3)=1: piece(8,2,3)=1
  65. piece(8,0,4)=0: piece(8,1,4)=0: piece(8,2,4)=1
  66.  
  67. piece(9,0,0)=0: piece(9,1,0)=0: piece(9,2,0)=0
  68. piece(9,0,1)=0: piece(9,1,1)=0: piece(9,2,1)=0
  69. piece(9,0,2)=1: piece(9,1,2)=1: piece(9,2,2)=1
  70. piece(9,0,3)=0: piece(9,1,3)=1: piece(9,2,3)=0
  71. piece(9,0,4)=0: piece(9,1,4)=1: piece(9,2,4)=0
  72.  
  73. piece(10,0,0)=0: piece(10,1,0)=0: piece(10,2,0)=0
  74. piece(10,0,1)=0: piece(10,1,1)=0: piece(10,2,1)=0
  75. piece(10,0,2)=0: piece(10,1,2)=0: piece(10,2,2)=0
  76. piece(10,0,3)=1: piece(10,1,3)=0: piece(10,2,3)=1
  77. piece(10,0,4)=1: piece(10,1,4)=1: piece(10,2,4)=1
  78.  
  79. piece(11,0,0)=0: piece(11,1,0)=0: piece(11,2,0)=0
  80. piece(11,0,1)=0: piece(11,1,1)=0: piece(11,2,1)=0
  81. piece(11,0,2)=0: piece(11,1,2)=0: piece(11,2,2)=1
  82. piece(11,0,3)=0: piece(11,1,3)=0: piece(11,2,3)=1
  83. piece(11,0,4)=1: piece(11,1,4)=1: piece(11,2,4)=1
  84.  
  85. piece(12,0,0)=0: piece(12,1,0)=0: piece(12,2,0)=0
  86. piece(12,0,1)=0: piece(12,1,1)=0: piece(12,2,1)=0
  87. piece(12,0,2)=0: piece(12,1,2)=0: piece(12,2,2)=1
  88. piece(12,0,3)=0: piece(12,1,3)=1: piece(12,2,3)=1
  89. piece(12,0,4)=1: piece(12,1,4)=1: piece(12,2,4)=0
  90.  
  91. piece(13,0,0)=0: piece(13,1,0)=0: piece(13,2,0)=0
  92. piece(13,0,1)=0: piece(13,1,1)=0: piece(13,2,1)=0
  93. piece(13,0,2)=0: piece(13,1,2)=1: piece(13,2,2)=0
  94. piece(13,0,3)=1: piece(13,1,3)=1: piece(13,2,3)=1
  95. piece(13,0,4)=0: piece(13,1,4)=1: piece(13,2,4)=0
  96.  
  97. piece(14,0,0)=0: piece(14,1,0)=0: piece(14,2,0)=0
  98. piece(14,0,1)=0: piece(14,1,1)=1: piece(14,2,1)=0
  99. piece(14,0,2)=1: piece(14,1,2)=1: piece(14,2,2)=0
  100. piece(14,0,3)=0: piece(14,1,3)=1: piece(14,2,3)=0
  101. piece(14,0,4)=0: piece(14,1,4)=1: piece(14,2,4)=0
  102.  
  103. piece(15,0,0)=0: piece(15,1,0)=0: piece(15,2,0)=0
  104. piece(15,0,1)=0: piece(15,1,1)=1: piece(15,2,1)=0
  105. piece(15,0,2)=0: piece(15,1,2)=1: piece(15,2,2)=1
  106. piece(15,0,3)=0: piece(15,1,3)=1: piece(15,2,3)=0
  107. piece(15,0,4)=0: piece(15,1,4)=1: piece(15,2,4)=0
  108.  
  109. piece(16,0,0)=0: piece(16,1,0)=0: piece(16,2,0)=0
  110. piece(16,0,1)=0: piece(16,1,1)=0: piece(16,2,1)=0
  111. piece(16,0,2)=0: piece(16,1,2)=1: piece(16,2,2)=1
  112. piece(16,0,3)=0: piece(16,1,3)=1: piece(16,2,3)=0
  113. piece(16,0,4)=1: piece(16,1,4)=1: piece(16,2,4)=0
  114.  
  115. piece(17,0,0)=0: piece(17,1,0)=0: piece(17,2,0)=0
  116. piece(17,0,1)=0: piece(17,1,1)=0: piece(17,2,1)=0
  117. piece(17,0,2)=1: piece(17,1,2)=1: piece(17,2,2)=0
  118. piece(17,0,3)=0: piece(17,1,3)=1: piece(17,2,3)=0
  119. piece(17,0,4)=0: piece(17,1,4)=1: piece(17,2,4)=1
  120.  
  121. SCREEN _NEWIMAGE(sw*size, sh*size, 32)
  122.  
  123. piece_color(0) = _RGB(255,0,0)
  124. piece_color(1) = _RGB(255,145,0)
  125. piece_color(2) = _RGB(255,200,211)
  126. piece_color(3) = _RGB(0,255,220)
  127. piece_color(4) = _RGB(0,230,255)
  128. piece_color(5) = _RGB(0,170,10)
  129. piece_color(6) = _RGB(0,250,20)
  130. piece_color(7) = _RGB(128,230,0)
  131. piece_color(8) = _RGB(80,150,0)
  132. piece_color(9) = _RGB(0,200,0)
  133. piece_color(10) = _RGB(50,160,170)
  134. piece_color(11) = _RGB(50,110,175)
  135. piece_color(12) = _RGB(50,50,175)
  136. piece_color(13) = _RGB(110,50,175)
  137. piece_color(14) = _RGB(210,0,255)
  138. piece_color(15) = _RGB(110,0,130)
  139. piece_color(16) = _RGB(255,0,140)
  140. piece_color(17) = _RGB(170,0,100)
  141.  
  142.  
  143. redraw = -1
  144.  
  145. speed = 2
  146. lines = 0
  147. pause = 0
  148. putpiece = 0
  149. startx = (sw - 4)/2
  150.  
  151. pn = INT(RND*18)
  152. px = startx
  153. py = -2
  154. rot = 0
  155.  
  156. title$ = "lines="+LTRIM$(STR$(lines))+",speed="++LTRIM$(STR$(speed))
  157. _TITLE title$
  158.  
  159. t = TIMER
  160.  
  161.         IF (TIMER - t) > (1/speed) AND NOT pause THEN
  162.                 IF valid(pn, px, py + 1, rot) THEN py = py + 1 ELSE putpiece = -1
  163.  
  164.                 t = TIMER
  165.                 redraw = -1
  166.         END IF
  167.  
  168.         IF putpiece THEN
  169.                 IF valid(pn, px, py, rot) THEN
  170.                         n = place(pn, px, py, rot)
  171.                         IF n THEN
  172.                                 lines = lines + n
  173.                                 title$ = "lines="+LTRIM$(STR$(lines))+",speed="++LTRIM$(STR$(speed))
  174.                                 _TITLE title$
  175.                         END IF
  176.                 END IF
  177.  
  178.                 pn = INT(RND*18)
  179.                 px = startx
  180.                 py = -2
  181.                 rot = 0
  182.  
  183.                 putpiece = 0
  184.                 redraw = -1
  185.  
  186.                 IF NOT valid(pn, px, py, rot) THEN
  187.                         FOR y=0 TO sh-1
  188.                                 FOR x=0 TO sw-1
  189.                                         board(x, y) = 0
  190.                                 NEXT
  191.                         NEXT
  192.                         lines = 0
  193.                         title$ = "lines="+LTRIM$(STR$(lines))+",speed="++LTRIM$(STR$(speed))
  194.                         _TITLE title$
  195.                 END IF
  196.         END IF
  197.  
  198.         IF redraw THEN
  199.                 LINE (0,0)-(sw*size, sh*size),_RGB(0,0,0),bf
  200.                 FOR y=0 TO sh - 1
  201.                         FOR x=0 TO sw - 1
  202.                                 IF board(x, y) <> 0 THEN
  203.                                         LINE (x*size, y*size)-STEP(size-2, size-2), piece_color(board(x, y)-1), bf
  204.                                 ELSE
  205.                                         LINE (x*size, y*size)-STEP(size-2, size-2), _RGB(50,50,50), b
  206.                                 END IF
  207.                         NEXT
  208.                 NEXT
  209.  
  210.                 FOR y=0 TO 4
  211.                         FOR x=0 TO 2
  212.                                 rotate xx, yy, x, y, pn, rot
  213.                                 IF piece(pn, x, y) THEN LINE ((px + xx)*size, (py + yy)*size)-STEP(size-2, size-2), piece_color(pn), bf
  214.                         NEXT
  215.                 NEXT
  216.  
  217.                 _DISPLAY
  218.                 redraw = 0
  219.         END IF
  220.  
  221.         k = _KEYHIT
  222.         IF k THEN
  223.                 shift = _KEYDOWN(100304) OR _KEYDOWN(100303)
  224.                 SELECT CASE k
  225.                 CASE 18432 'up
  226.                         IF valid(pn, px, py, (rot + 1) MOD 4) THEN rot = (rot + 1) MOD 4
  227.                         pause = 0
  228.                 CASE 19200 'left
  229.                         IF shift THEN
  230.                                 FOR xx=0 TO sw-1
  231.                                         IF NOT valid(pn, px - xx, py, rot) THEN EXIT FOR
  232.                                 NEXT
  233.                                 px = px - xx + 1
  234.                         ELSE
  235.                                 IF valid(pn, px - 1, py, rot) THEN px = px - 1
  236.                         END IF
  237.                         pause = 0
  238.                 CASE 19712 'right
  239.                         IF shift THEN
  240.                                 FOR xx=px TO sw-1
  241.                                         IF NOT valid(pn, xx, py, rot) THEN EXIT FOR
  242.                                 NEXT
  243.                                 px = xx - 1
  244.                         ELSE
  245.                                 IF valid(pn, px + 1, py, rot) THEN px = px + 1
  246.                         END IF
  247.                         pause = 0
  248.                 CASE 20480, 32 'down
  249.                         IF shift OR k = 32 THEN
  250.                                 FOR yy=py TO sh-1
  251.                                         IF NOT valid(pn, px, yy, rot) THEN EXIT FOR
  252.                                 NEXT
  253.                                 py = yy - 1
  254.                                 putpiece = -1
  255.                         ELSE
  256.                                 IF valid(pn, px, py + 1, rot) THEN py = py + 1
  257.                         END IF
  258.                         pause = 0
  259.                 CASE 112 'p
  260.                         pause = NOT pause
  261.                 CASE 13 'enter
  262.                         FOR y=0 TO sh-1
  263.                                 FOR x=0 TO sw-1
  264.                                         board(x, y) = 0
  265.                                 NEXT
  266.                         NEXT
  267.                         pn = INT(RND*17)
  268.                         px = startx
  269.                         py = -2
  270.                         rot = 0
  271.                         putpiece = 0
  272.                         lines = 0
  273.                         title$ = "lines="+LTRIM$(STR$(lines))+",speed="++LTRIM$(STR$(speed))
  274.                         _TITLE title$
  275.                 CASE 43, 61 'plus
  276.                         IF speed < 100 THEN
  277.                                 speed = speed + 1
  278.                                 title$ = "lines="+LTRIM$(STR$(lines))+",speed="++LTRIM$(STR$(speed))
  279.                                 _TITLE title$
  280.                         END IF
  281.                 CASE 95, 45
  282.                         IF speed > 1 THEN
  283.                                 speed = speed - 1
  284.                                 title$ = "lines="+LTRIM$(STR$(lines))+",speed="++LTRIM$(STR$(speed))
  285.                                 _TITLE title$
  286.                         END IF
  287.                 CASE 27
  288.                         EXIT DO
  289.                 END SELECT
  290.  
  291.                 redraw = -1
  292.         END IF
  293.  
  294.  
  295. SUB rotate(xx, yy, x, y, pn, rot)
  296.         SELECT CASE pn
  297.         CASE 0
  298.                 rot_new = rot MOD 2
  299.         CASE ELSE
  300.                 rot_new = rot
  301.         END SELECT
  302.  
  303.         SELECT CASE rot_new
  304.         CASE 0
  305.                 xx = x
  306.                 yy = y
  307.         CASE 1
  308.                 IF pn = 0 THEN
  309.                         xx = y - 1
  310.                         yy = 3 - x
  311.                 ELSEIF pn = 14 OR pn = 15 THEN
  312.                         xx = y - 1
  313.                         yy = 3 - x
  314.                 ELSE
  315.                         xx = y - 2
  316.                         yy = 4 - x
  317.                 END IF
  318.         CASE 2
  319.                 IF pn = 14 OR pn = 15 THEN
  320.                         xx = 2 - x
  321.                         yy = 4 - y
  322.                 ELSE
  323.                         xx = 2 - x
  324.                         yy = 6 - y
  325.                 END IF
  326.         CASE 3
  327.                 IF pn = 14 OR pn = 15 THEN
  328.                         xx = 3 - y
  329.                         yy = x + 1
  330.                 ELSE
  331.                         xx = 4 - y
  332.                         yy = x + 2
  333.                 END IF
  334.         END SELECT
  335.  
  336. FUNCTION valid(pn, px, py, rot)
  337.         FOR y=0 TO 4
  338.                 FOR x=0 TO 2
  339.                         rotate xx, yy, x, y, pn, rot
  340.                         IF py + yy >= 0 THEN
  341.                                 IF piece(pn, x, y) THEN
  342.                                         IF (px + xx >= sw) OR (px + xx < 0) THEN
  343.                                                 valid = 0
  344.                                                 EXIT FUNCTION
  345.                                         END IF
  346.                                         IF (py + yy >= sh) THEN
  347.                                                 valid = 0
  348.                                                 EXIT FUNCTION
  349.                                         END IF
  350.                                         'if (py >= 0) then
  351.                                         IF board(px + xx, py + yy) THEN
  352.                                                 valid = 0
  353.                                                 EXIT FUNCTION
  354.                                         END IF
  355.                                         'end if
  356.                                 END IF
  357.                         END IF
  358.                 NEXT
  359.         NEXT
  360.  
  361.         valid = -1
  362.  
  363. FUNCTION place(pn, px, py, rot)
  364.         lines = 0
  365.  
  366.         FOR y=0 TO 4
  367.                 FOR x=0 TO 2
  368.                         rotate xx, yy, x, y, pn, rot
  369.                         IF py + yy >= 0 THEN IF piece(pn, x, y) THEN board(px + xx, py + yy) = pn + 1
  370.                 NEXT
  371.         NEXT
  372.  
  373.         'clear lines
  374.         FOR y=py-5 TO py+5
  375.                 IF y>=0 AND y<sh THEN
  376.                         clr = -1
  377.                         FOR x=0 TO sw - 1
  378.                                 IF board(x, y) = 0 THEN
  379.                                         clr = 0
  380.                                         EXIT FOR
  381.                                 END IF
  382.                         NEXT
  383.  
  384.                         IF clr THEN
  385.                                 lines = lines + 1
  386.                                 FOR yy=y TO 1 STEP -1
  387.                                         FOR x=0 TO sw-1
  388.                                                 board(x, yy) = board(x, yy-1)
  389.                                         NEXT
  390.                                 NEXT
  391.                         END IF
  392.                 END IF
  393.         NEXT
  394.  
  395.         place = lines
  396.  
« Last Edit: October 13, 2018, 04:59:56 AM by v »

Offline johnno56

  • Live long and prosper.
Re: PENtrIS
« Reply #1 on: October 12, 2018, 02:05:50 AM »
Cool version. So much harder than the original but still cool... I like it.
Logic is the beginning of wisdom.

Offline STxAxTIC

  • Library Staff
  • Your mom would love me.
Re: PENtrIS
« Reply #2 on: October 12, 2018, 11:54:08 AM »
Enjoyed the heck out of this generalization of Tetris. Not sure how refined the code is yet but I swear I formed a line that didn't clear - but lines above that one did clear properly. In the old game I got more lines than a cokehead - this one's way harder. Love it!

Re: PENtrIS
« Reply #3 on: October 13, 2018, 08:59:57 AM »
Great V
I like  this version too, also if it uses blocks that have a more unusual shape.