In this Python challenge we will complete an algorithm to generate the 9×9 grid of candies used in the game Candy Crush.

In the game of Candy Crush, 81 candies are displayed using 9 rows of 9 candies. There are 6 different candies of different shapes and colours.

To implement this game, we will be using a 2D array of integer values. Each value be stored as an integer between 1 and 6. We will also use 6 different pictures of sweets so that we can render the grid by replacing the numbers stored with their corresponding picture.

Here is our 2D array matching the above Candy Crush grid:

grid = [[1,2,3,4,6,2,5,1,3], [5,2,6,4,5,6,1,2,3], [4,1,1,3,3,6,2,4,5], [2,3,4,1,5,1,6,4,1], [5,5,3,2,1,4,3,2,2], [1,3,6,2,5,5,1,4,4], [6,1,6,3,4,6,6,3,6], [4,6,4,2,3,4,2,5,3], [4,5,1,1,3,5,2,4,6]]

#### Python Code so far…

#### Task 1: Generating a Random Grid

The code provided above will always generate the same starting grid. Your task is to add some code to randomise the grid so that each of the 81 cells of the grid contains a random integer value between 1 and 6. The following flowchart will help you implement this code.

#### Task 2: Identifying and Removing Blocks

In the game Candy Crush, every time a block of 3 or more adjacent candies of the same type in a row or in a column is detected, it is automatically removed from the grid. Your second task consists of detecting if the grid that was randomly generated (task 1) contains any blocks of 3 adjacent candies, and if so replace these candies.

In order to identify blocks of at least 3 adjacent candies, we can search for any candy on the grid which has two candies of the same type on either side (left and right) or above and below.

Let’s see how we can identify a block of three in a row:

Let’s see how we can identify a block of four in a column:

Once a block has been identified, we can break it by replacing either all three candies or by simply replacing the candy in the middle position. Not that in the game of Candy Crush, when a candy is removed, the candies above fall down to fill up the empty space and a new candy is generated at the top of the grid. However, we do not need to implement this at this stage, as we are only working on generating a valid start up grid that does not contain any block of 3 or more candies of the same type.

Let’s investigate the algorithm needed to detect and remove any block of three adjacent candies using the following flowchart:

Your task is to implement the above flowchart within the Python code provided above. Once done, all generated grids should not contain any blocks of 3 or more adjacent candies of the same type.

#### Extension Task

Now that thew grid is ready, we could ask the user to enter a row and column number of the candy they would like to remove. From there, we should implement the rules of the Candy Crush game to drop new candies from the top and see when a block has been created and should hence be removed from the grid.

#### Solution...

The solution for this challenge is available to full members!Find out how to become a member:

➤ Members' Area