COMP 110 Summer 2012

Program 4

100 points

Assigned: Wenesday, June 06
Due: Friday, June 15

Description

The game: Memory

The game Memory involves a 2-dimensional array of game squares, each of which has an associated color (or image, number, etc.) which is initially hidden from the player. During each turn, the player chooses two of these squares (one at a time), and their hidden colors are revealed. If the hidden colors are the same (each color appears on only two squares), the player has successfully found a match, and the colors remain visible. If they do not match, the player has not found a match, so the colors are again hidden, and the turn ends. The player continues to take turns until all matches have been found.

Example implementation

The applet below is an example of the Memory implementation you will be implementing in this assignment.

Assignment overview

You will write a Java applet memory game equivalent to the example above. I have given you a starting program Memory.java, where you will have to fill in the missing pieces. The program I have given you has some functionality. Make sure you read all the comments in the code and understand which pieces you can/should change and which pieces you should not change.

You are only being asked to fill in the bodies of some of the methods in the Memory class. You will not need to define any additional methods, constants, or instance variables, though you certainly may if you find them useful (e.g., for debugging).

Every method which you are required to complete has pre- and post-conditions given in comments above it and contains the comment "// TODO". There are 14 such methods. Those incomplete methods that have a non-void return type also include a placeholder return statement which is otherwise meaningless and should be replaced with your code.

Some of the methods you need to implement are very simple, and others are more complex. You should try to work your way down from the top of the file to the bottom if possible, but you are free to work in any order you find works best for you.

I recommend starting immediately.

The game board

Think of the game board as a 2-dimensional array that holds matching pairs of integers. Each integer will represent a different color for the circles displayed on the board.

Your board must be random, and some of the methods you are writing will randomize it. To visualize your array, you may want to write a method to display the values in your array to make sure they are correct (see page 504, or MyArrayTest.java).

Example Board:
3 7 3 2
4 6 6 1
5 1 2 0
4 0 5 7

When randomizing the board, you will probably want to use Math.random() or a Random object.

Mouse Clicking

For methods that need to know which square the user clicked (or which to act on for some other reason), the preconditions will state which variables will contain the indices for that square. The "x" variable will be a column on the game board, and the "y" variable will be a row. So if the square in question is represented by xIndex and yIndex, then the corresponding square in the array gameBoard is gameBoard[yIndex][xIndex].

Pixels

The pixel values on the actual game board 0 to BOARD_SIZE_PIXELS-1, inclusive. The Memory will use the Graphics class to display the game board. The window is BOARD_SIZE_PIXELS x BOARD_SIZE_PIXELS with (0,0) in the upper left corner. You can think of the board as a visual array with board[0][0] as the top right corner and each board square is squareWidth x squareWidth. The current value of BOARD_SIZE_PIXELS is 400, and since there are 4 squares on each side of our game board (SQUARES == 4), squareWidth is 100. You should use these variables and constants whenever possible instead of hard-coding numbers in your code.

The indices for the squares correspond, then, to 100 pixels each (in our case):

index 0: 0 - 99
index 1: 100 - 199
index 2: 200 - 299
index 3: 300 - 399

Game Functionality

There is nothing tricky about the coding for these methods. They are designed to be very straightforward to implement. Some of them might need to call other methods in Memory (some that you are writing, some that you leave unchanged). You might want to read the comments for all the methods to get an idea of what's available.

Extra Credit

Change the end-of-game output to add a message that shows how much time the game took: e.g., "Game time: 32 seconds"

You will need to change methods that are labeled "DO NOT change" to complete the extra credit. In particular, you will have to make changes in paint and setUpGame. You will also need to create additional instance variables.

Grading

Credit will be given as follows:

How to hand in the assignment