Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
scipy.signal.correlate2d solution in 3rd party category for Identify Block by Rcp8jzd
import numpy as np
from scipy.signal import correlate2d
BLOCKS = {'I': [[1],
[1],
[1],
[1]],
'J': [[0, 1],
[0, 1],
[1, 1]],
'L': [[1, 0],
[1, 0],
[1, 1]],
'O': [[1, 1],
[1, 1]],
'S': [[0, 1, 1],
[1, 1, 0]],
'T': [[1, 1, 1],
[0, 1, 0]],
'Z': [[1, 1, 0],
[0, 1, 1]]}
def identify_block(numbers):
""" Identify a Tetris block in a 4x4 grid based on the tiles numbers.
Return None if no Tetris block is found """
grid = np.zeros(16)
for index in numbers:
grid[index - 1] = 1
grid.resize((4, 4))
for name, shape in BLOCKS.items():
# For each block, try every rotation until match is found
for rotation in range(4):
correlation = correlate2d(grid, shape, mode='valid')
if (correlation == 4).any():
return name
shape = np.rot90(shape)
return None
April 13, 2020
Comments: