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.
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.
3) Fill the board with random symbols from the file that you have opened and read.
4) Display the board.
5) Get the player's selection of position to look at.
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.
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.
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.
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!