COMP 141: Project 9
Memory
You may work with a partner for this project. If you choose to do so, only turn in one copy of the project with
both of your names in the comments at the beginning of the code.
If Memory doesn't particularly interest you, you may do one of the challenge problems at the end of this description,
or propose a different project to me of comparable difficulty.
You must get my approval before starting work on your project if you are not writing the Memory program.
The game Memory is a matching game commonly played by children. The game starts with all the cards turned face-down
(so you can't see the face-up side), and a player will select two cards to turn over.
If the two cards match, they remain face-up, otherwise, they are turned back over.
The object of the game is to find all pairs of cards using one's memory of which cards they've seen before and where they are located.
Memory is normally a 2-player game where that the player that finds the most pairs at the end of the game wins,
but we will implement a single-player version, in which case the player will always "win" assuming they don't give up.
This program will most likely take longer than others this semester - be sure to START EARLY!
Program Specifications
This program requires you to use:
2-D lists
SimpleGraphics: open a canvas, use mouse clicks as the user input for moves, close the canvas
Random number generation: to initialize the board
Nested loops
Description
In Memory, we will begin with a 4-by-4 grid of cards arranged in a square. This grid will be
represented internally (in Python) as a 2d list. Similarly, each card in the grid
will be represented internally (in your Python program) as a number from 1-8. Note that there
are 16 squares in a 4x4 grid, so each number from 1-8 will appear twice (which makes sense, since
the player must find pairs of numbers).
However, when you draw the cards using simplegraphics, you will use colors instead of numbers (you can decide
on which color each number 1-8 should go with).
The program will work similarly to our tic-tac-toe with graphics program, in that you're using
a numeric representation internally in Python (numbers 1-8 for Memory, and 1/0/-1 for tic-tac-toe), but when you
display the game board, you're using a different representation visually (colors for Memory, and X/O for
tic-tac-toe).
Requirements
Your program should do the following:
- Use simplegraphics to:
- Open a canvas
- Draw grid lines to display a 4 x 4 grid
- Use mouse clicks to determine which grid location a user has selected
- Correctly uncover the appropriate "card" (i.e. display the correct color for the clicked on grid square)
- Allow the user to select 2 cards for each turn and if the cards match, leave them turned up, otherwise, turn them back over
- You must correctly display the board after every click.
- You will compare 2 cards and then on the next click, turn the cards back over if they are not matching.
- You must use at least a 4 x 4 board, but you are welcome to make it bigger.
- You must draw something in each grid when it is clicked, but it does not have to be a filled in circle.
Your program should keep running until there are no more unturned "cards".
Example Run
Since this program is using graphics, there is no example run. However, here are some screenshots of a game while it is being played.
Initial Board |
Board After 1st turn |
End Board |
|
|
|
Your board does not need to look exactly like this, but all the mentioned functionality should work as shown.
Suggested steps that your program should take
You can follow the same basic idea as the graphical tic-tac-toe game.
- Initialize a 2d list (4-by-4) with two copies each of 1-8 in a random fashion.
Here's an easy way to do this: initialize the 2d list with two copies of 1-8 in a non-random fashion, e.g.,
[[1, 1, 2, 2]... etc]. Then, pick two random locations within the 2d list (pick two random rows
and two random columns) and swap the values at those locations. Do this swapping operation, say, 1000 times.
This effectively "shuffles" the 2d-list.
- Use positive values of 1-8 to designate cards that are NOT showing (currently face-down),
and negative values to designate cards that ARE showing (cards that are face-up).
- Write a make_move function that takes in board, row, and column as arguments and switches
the sign of the value at that location in the board. (i.e. if it is -5 it becomes 5, if it is 5 it becomes -5.)
This essentially turns a card face-up to face-down or vice-versa.
- Write a function that converts a number to a color so you can easily lookup the color associated with
a particular value.
This will be a simple if-elif-else type function to take in a number 1-8 and return a color.
- Include a draw_board function like tic-tac-toe that draws the 2d list representing the board
using simplegraphics. You may also want to write a print_board function (again, like tic-tac-toe) that
prints the board using text to help with debugging.
- Include a game_over function that tests whether the game is over (this happens when all the cards are face-up).
- Have your main function operate similarly to tic-tac-toe with graphics main function:
- While the game is not over,
- draw the board,
- get a click from the user for the first card to turn over, show the card (by switching the value to negative and calling draw_board),
- get a click from the user for the second card to turn over, show the card (by switching the value to negative and calling draw_board),
- compare the cards to see if they match, and update the values in the board as necessary (switch the values back to positive
if they didn't match).
What to Do
Create a Python program named yourlastname_yourfirstname_memory.py. If you are working
with a partner, name it yourlastname_partnerslastname_memory.py
Include the standard program header at the top of your Python file and
follow the comment guide to correctly comment your code.
Submit your Python file on Moodle under Program 9.
Note: If you include any images in your code, be sure to create a zip file with your .py file and the image files and submit the .zip file on Moodle.
Requirements
When I examine your program, an A program must satisfy the following requirements.
Your program file is named correctly and it includes the standard program header.
You must open the canvas.
You must draw grid lines on the canvas so the user can tell where each "card" is.
You must randomly initialize the board so that the game is different every time.
The user must be able to clearly tell when a pair is matching.
You must close the canvas on the last click.
You must turn over a card when the user clicks.
You must compare the 2 uncovered cards and turn them back over if they are not the same.
You must display a final message when the user "wins".
You must use appropriate functions (at least 3 functions besides main).
Creativity points - have fun with this and make it your own!
You must comment your code appropriately (particularly your functions), use good variable names, and your code must be neatly and clearly formatted and include proper use of 'white space'.
Note: If your program doesn't run (i.e, has a syntax error), you may receive partial credit for the items listed above in bold.
Alternate Challenge Questions
Here are some alternate games/projects with graphics that involve a 2-D list. You are welcome to work on one of these problems instead of Memory, but you MUST let me know as it is best to agree upon final functionality before you being implementing. As mentioned, you may also select a different game with graphics if none of these are to your liking either. Any ideas beyond this list MUST be approved before you begin working on them.
Minesweeper
Connect 4 (requires the normal board size of 6 x 7)
2048
Grading
Your program will be graded on correctness, as well as on coding style, which refers to choices you make when writing your code, such as good use of variable names, appropriate indentation,
and comments (this is not an exhaustive list). See the syllabus for further grading details.