How to create a memory puzzle game with Python and Pygame (#002)

 



In this tutorial, we are gonna create the data structure for the board. Let me explain the data struture graphically.

As you can see the graph above, our game board structure is a two dimentional structure. We could abstruct this 2D structure into a list of lists in Python. So lets define our board fucntion:

def board(patterns):

      # create the data structure for the game board

      board = []

      c = 0

      for i in range(6):

            board.append([])

            for j in range(6):

                  board[i].append(patterns[c])

                  c += 1

      return board

Explaination:

def board(patterns):

Our board function will take a list of patterns as the parameter, the patterns' element will be attached to the board structure. For this tutorial, we are gonna use a list of english letters from A to R (18 letters) to represent our patterns. Later we could use pictures to replace them.

Here is how our patterns will be generated (later we are gonna use this):

import random

patterns = [chr(i) for i in range(65,65+18)]*2

random.shuffle(patterns)

We use a list comprehension to generate a list of uppercase letters from A to R.  We use chr() to convert integers to its coresponding unicode letters. And then we duplicate all the elements in the list and give it a name patterns. Next we use handy random.shuffle() method to destroy its orders randomly.

board = []

c = 0

We create a empty board list and a variable c to keep track of the number of iterations of the nested for loop.

for i in range(6):

    board.append([])

        for j in range(6):

            board[i].append(patterns[c])

            c += 1

The nested for loop is the core of this game structure or other board games. It creates a list of lists with each pattern appended to its ideal place. It first creates the first column, then the 2nd, 3rd ...

So let's test this:

# test01.py only for test

import random

def board(patterns):

      # create the data structure for the game board

      board = []

      c = 0

      for i in range(6):

            board.append([])

            for j in range(6):

                  board[i].append(patterns[c])

                  c += 1

      return board


patterns = [chr(i) for i in range(65,65+18)]*2

random.shuffle(patterns)

board = board(patterns)

print(board)

The output should look like this:

[['R', 'L', 'A', 'O', 'G', 'D'], ['Q', 'K', 'M', 'A', 'G', 'B'], ['N', 'P', 'E', 'Q', 'I', 'C'], ['O', 'D', 'N', 'J', 'I', 'R'], ['K', 'E', 'M', 'F', 'J', 'P'], ['B', 'L', 'H', 'C', 'H', 'F']]

Or we could format the board structure to the same patterns as illustrated in the above picture. Just add the following code to the test01.py script above.

for i in range(6):

      for j in range(6):

            print(board[i][j], end=' ')

      print()

The output should look like this:
C Q D H B I 
J R A K N R 
K L A M E I 
Q F H B N P 
F C O M E G 
L G J O P D 

For this pattern printed above, each letter will occupy each place in the puzzle game board. Let's test it:

>>> board[0][0]

'C'

>>> board[3][3]

'B'

>>>

Well, we have created the data structure for the memory puzzle game board. We are on the right track. Next tutorial , we are gonna create a data structure for storing real position of each green square drawon on the main screen surface.


Comments

Popular posts from this blog

How to write a slide puzzle game with Python and Pygame (2020 tutorial)

How to create a memory puzzle game with Python and Pygame (#005)

Introduction to multitasking with Python #001 multithreading (2020 tutorial)