Author Topic: PENtrIS  (Read 5184 times)

0 Members and 1 Guest are viewing this topic.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
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

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
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
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
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!
You're not done when it works, you're done when it's right.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
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.
Programming isn't difficult, only it's  consuming time and coffee