Project Description

PyMatch is a memory matching game where you try to find all of the 'cards' with the same symbol on them. In many versions of this game there are only two cards of any type, but in our version of the game there can be one card, two cards, or more.

If the player picks a card with only one copy, for instance if there were only one card with grapes on them, then that card stays turned over. If they pick a card that has other copies still to be turned over, then they have to find the rest of those cards and turn them over in order to keep that card turned over. The game should tell them how many of the cards are left. If they guess incorrectly, their guesses all turn back over (but previously correct guesses remain turned up).

Figure: see image.

Implementation Details

The game follows the following cycle:

1) Get the number of rows, columns, and the random seed.

2) Get the name of the symbol file to open.

  • Open that file and read in all the symbols, which are space separated. These are the symbols which you will use to populate the board.

3) Fill the board with random symbols from the file that you have opened and read.

  • Fill each row first, then the columns. That is, fill the board[0][x] row before moving on to board[1][x].
  • We do this to ensure that our rows and columns will match for testing purposes.

4) Display the board.

5) Get the player's selection of position to look at.

  • If the player has already selected this position or it's already been revealed in a previous answer, reprompt until they give a new position.
  • Once they have selected a new position, display the position whether or not it's part of the answer, so that they can remember what was there before.

6) If the new symbol matches the original symbol selected, then continue to uncover symbols.

7) If there are no more symbols of that type, set them all to fully revealed.

8) If there are more symbols of that type, then tell the user how many more are left of that type.

9) If they guess a symbol that doesn't match, recover all the positions they've guessed in this round.

10) Go back to step 4.

Use of the Random Library

At the top of your code you should import random by doing this:

import random

When you want to select a random element out of a list, use:

an_element_of_the_list = random.choice(the_list_to_select_from)

Obviously you'll want to change the variable names.

In order that our random outputs will match, you should set the seed to agree, by at the start of the program, calling the function:

random.seed(the_seed_we_input)

This function doesn't return anything, but what it does do is sets the (pseudo)-random number generator that python uses inside of its random library to output the specific sequence generated by the seed.

Cast the seed to an integer, since seed will also accept strings, but we want to ensure that the seed we pass in is exactly the same.

List Unpacking

For this project, I'm going to allow list unpacking, which works in the following way:

If you have a list of three elements [1, 2, 3] then you can set:

a, b, c = [1, 2, 3]

This will set three variables at once.

Mostly I find this useful for unpacking the positions on the board for instance if you store your position as a list [y, x] then you can unpack it like:

my_position = [y, x]
pos_y, pos_x = my_position

This is what people tend to call "syntactic sugar" meaning that it's not really necessary to use to complete the project but it does make life better than having a lot of code like:

pos_y = my_position[0]
pos_x = my_position[1]

This is especially true if unpacking more than 2 elements at a time.

However, it's a dangerous feature. If you try to unpack the wrong number of elements, it'll fail. If you ask for help with this feature, you will be directed to write it all out like in the last bit of code.

Sample Run

linux3[25]% python3 py_match.py
Enter Row, Col, Seed: 3, 3, 1234
What is the symbol file name? abcd.sym
. . .
. . .
. . .
Enter position to guess: 1 1
You have found all of the D
D . .
. . .
. . .
Enter position to guess: 1 2
D A .
. . .
. . .
Enter position to guess that matches A, there are 5 remaining:
1 3
D A A
. . .
. . .
Enter position to guess that matches A, there are 4 remaining:
2 1
D A A
A . .
. . .
Enter position to guess that matches A, there are 3 remaining:
2 2
D A A
A A .
. . .
Enter position to guess that matches A, there are 2 remaining:
2 3
D A A
A A A
. . .
Enter position to guess that matches A, there are 1 remaining:
3 2
No match this time:
D A A
A A A
. C .
Try again!
D . .
. . .
. . .
Enter position to guess: 3 2
You have found all of the C
D . .
. . .
. C .
Enter position to guess: 3 1
D . .
. . .
A C .
Enter position to guess that matches A, there are 5 remaining:
1 2
D A .
. . .
A C .
Enter position to guess that matches A, there are 4 remaining:
1 3
D A A
. . .
A C .
Enter position to guess that matches A, there are 3 remaining:
2 1
D A A
A . .
A C .
Enter position to guess that matches A, there are 2 remaining:
2 2
D A A
A A .
A C .
Enter position to guess that matches A, there are 1 remaining:
3 3
No match this time:
D A A
A A .
A C B
Try again!
D . .
. . .
. C .
Enter position to guess: 3 3
You have found all of the B
D . .
. . .
. C B
Enter position to guess: 1 2
D A .
. . .
. C B
Enter position to guess that matches A, there are 5 remaining:
1 3
D A A
. . .
. C B
Enter position to guess that matches A, there are 4 remaining:
2 1
D A A
A . .
. C B
Enter position to guess that matches A, there are 3 remaining:
2 2
D A A
A A .
. C B
Enter position to guess that matches A, there are 2 remaining:
2 3
D A A
A A A
. C B
Enter position to guess that matches A, there are 1 remaining:
3 1
You have found all of the A
D A A
A A A
A C B
Congratulations, you win!
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.