tetrominos.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. """
  2. All tetrominos and their rotations
  3. """
  4. import collections
  5. import numpy as np
  6. def convert_to_array(tetromino):
  7. rows = len(tetromino)
  8. cols = len(tetromino[0])
  9. array_tetromino = np.zeros(shape=(rows,cols),
  10. dtype=np.int32, order="C")
  11. for row_index, row in enumerate(tetromino):
  12. for col_index, col in enumerate(row):
  13. array_tetromino[row_index,col_index] = 0 if col == '.' else 1
  14. return array_tetromino
  15. def crop_col_right_once(tetromino):
  16. if not np.any(tetromino[:,-1]):
  17. return tetromino[:,0:-1]
  18. return None
  19. def crop_col_right(tetromino):
  20. while True:
  21. new_tetromino = crop_col_right_once(tetromino)
  22. if new_tetromino is None:
  23. return tetromino
  24. tetromino = new_tetromino
  25. def crop_row_bottom_once(tetromino):
  26. if not np.any(tetromino[-1,:]):
  27. return tetromino[0:-1,:]
  28. return None
  29. def crop_row_bottom(tetromino):
  30. while True:
  31. new_tetromino = crop_row_bottom_once(tetromino)
  32. if new_tetromino is None:
  33. return tetromino
  34. tetromino = new_tetromino
  35. def crop_col_left_once(tetromino):
  36. if not np.any(tetromino[:,0]):
  37. return tetromino[:,1:]
  38. return None
  39. def crop_col_left(tetromino):
  40. col_offset = 0
  41. while True:
  42. new_tetromino = crop_col_left_once(tetromino)
  43. if new_tetromino is None:
  44. return (tetromino, col_offset)
  45. tetromino = new_tetromino
  46. col_offset += 1
  47. def crop_row_top_once(tetromino):
  48. if not np.any(tetromino[0,:]):
  49. return tetromino[1:,:]
  50. return None
  51. def crop_row_top(tetromino):
  52. row_offset = 0
  53. while True:
  54. new_tetromino = crop_row_top_once(tetromino)
  55. if new_tetromino is None:
  56. return (tetromino, row_offset)
  57. tetromino = new_tetromino
  58. row_offset += 1
  59. Tetromino = collections.namedtuple('Tetromino',
  60. ['data', 'col_offset', 'row_offset'])
  61. def crop(tetromino):
  62. new_tetromino = crop_col_right(crop_row_bottom(tetromino))
  63. new_tetromino, c_off = crop_col_left(new_tetromino)
  64. new_tetromino, r_off = crop_row_top(new_tetromino)
  65. return Tetromino(data=new_tetromino, col_offset=c_off, row_offset=r_off)
  66. def convert_to_tuples(tetrominos):
  67. return list(map(lambda x: crop(convert_to_array(x)), tetrominos))
  68. I = convert_to_tuples([['..........',
  69. '...0000...',
  70. '..........',
  71. '..........'],
  72. ['.....0....',
  73. '.....0....',
  74. '.....0....',
  75. '.....0....'],
  76. ['..........',
  77. '..........',
  78. '...0000...',
  79. '..........'],
  80. ['....0.....',
  81. '....0.....',
  82. '....0.....',
  83. '....0.....']])
  84. O = convert_to_tuples([['..........',
  85. '....00....',
  86. '....00....',
  87. '..........'],
  88. ['..........',
  89. '....00....',
  90. '....00....',
  91. '..........'],
  92. ['..........',
  93. '....00....',
  94. '....00....',
  95. '..........'],
  96. ['..........',
  97. '....00....',
  98. '....00....',
  99. '..........']])
  100. T = convert_to_tuples([['....0.....',
  101. '...000....',
  102. '..........',
  103. '..........'],
  104. ['....0.....',
  105. '....00....',
  106. '....0.....',
  107. '..........'],
  108. ['..........',
  109. '...000....',
  110. '....0.....',
  111. '..........'],
  112. ['....0.....',
  113. '...00.....',
  114. '....0.....',
  115. '..........']])
  116. S = convert_to_tuples([['....00....',
  117. '...00.....',
  118. '..........',
  119. '..........'],
  120. ['....0.....',
  121. '....00....',
  122. '.....0....',
  123. '..........'],
  124. ['..........',
  125. '....00....',
  126. '...00.....',
  127. '..........'],
  128. ['...0......',
  129. '...00.....',
  130. '....0.....',
  131. '..........']])
  132. Z = convert_to_tuples([['...00.....',
  133. '....00....',
  134. '..........',
  135. '..........'],
  136. ['.....0....',
  137. '....00....',
  138. '....0.....',
  139. '..........'],
  140. ['..........',
  141. '...00.....',
  142. '....00....',
  143. '..........'],
  144. ['....0.....',
  145. '...00.....',
  146. '...0......',
  147. '..........']])
  148. J = convert_to_tuples([['...0......',
  149. '...000....',
  150. '..........',
  151. '..........'],
  152. ['....00....',
  153. '....0.....',
  154. '....0.....',
  155. '..........'],
  156. ['..........',
  157. '...000....',
  158. '.....0....',
  159. '..........'],
  160. ['....0.....',
  161. '....0.....',
  162. '...00.....',
  163. '..........']])
  164. L = convert_to_tuples([['.....0....',
  165. '...000....',
  166. '..........',
  167. '..........'],
  168. ['....0.....',
  169. '....0.....',
  170. '....00....',
  171. '..........'],
  172. ['..........',
  173. '...000....',
  174. '...0......',
  175. '..........'],
  176. ['...00.....',
  177. '....0.....',
  178. '....0.....',
  179. '..........']])
  180. SHAPES = [S, Z, I, O, J, L, T]
  181. SHAPE_COLORS = [(0, 255, 0), (255, 0, 0), (0, 255, 255), (255, 255, 0),
  182. (255, 165, 0), (0, 0, 255), (128, 0, 128)]