Until now in this series we’ve been working to retrieve the informations from the board: detecting the board, the tetrominoes and their position.
Now it’s the time to use these informations in order to process them.

If we consider that we want to use some sort of artificial intelligence, even if really basic, to solve the game, we need first to figure out how to store the informations and how to use them.

For example, how to convert into informations the image below where we have the board and the tetrominoes position?

Convert Virtual board into an Array

If we consider this board by its cells, we clearly know that it’s composed by 200 cells (10 columns x 20 rows).
Some cells are empty and some contains tetrominoes. The easiest way to work with this, will be to create an array with the same size and distinguish the empty cells from the one where there are the tetrominoes.

We can associate each empty cell to the number 0.

Let’s first of all create an array with the size of 20 rows and 10 columns which contains only zeros with the code below:

board_array = np.zeros((20, 10))

The output will be this one below:

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

And then we’re going to identify the locations where there are the tetrominoes (which row and which cell) and put the number 1.

We can do this inside the loop when we’re creating the cells for the virtual board, we also check if in the same location there is the tetrominoe.
If there is a tetrominoe, then on the corresponding cell on the array, we put the number one.

# Check if tetrominoe is inside the cell
if board_x + block_x <= x < board_x + block_x + block_width and board_y + block_y <= y < board_y + block_y + block_height:

	board_array[board_row, board_col] = 1


At the end we can check if it’s correct, by comparing the virtual board with the array.

I highlited in yellow the area where the tetrominoes are. You can see that in the array all these parts contain the number one instead of the 0.

Basically our goal will be to have as many number 1 as possible in a single row, starting from the bottom of the screen. And that’s how somehow we will find the way to tell python to solve the game.