CS 2500 Spring 2011
Lab 2

## Memory Matching Game

Adapted from Problem Solving with C++, Seventh Edition by Walter Savitch.
The goal of this program is to work with two-dimensional arrays.

Description
A common memory matching game played by young children is to start with a deck of cards that contain identical pairs. For example, given six cards in the deck, two might be labeled "1," two might be labeled "2," two might be labeled "3." The cards are shuffled and placed face down on the table. The player then selects two cards that are face down, turns them face up, and if they match they are left face up. If the two cards do not match, they are returned to their original position face down. The game continues until all cards are face up.

Programming Exercise
Write a program that plays the memory matching game. Use 16 cards that are laid out in a 4 x 4 square and are labeled with pairs of numbers from 1 to 8. Your program should allow the player to specify the cards they would like to select through a coordinate system.

For example, suppose the cards are in the following layout:

1     2     3     4
=========================
1 ||  8     *     *     *
2 ||  *     *     *     *
3 ||  *     8     *     *
4 ||  *     *     *     *

All of the cards are face down except the pair 8, which has been located at the coordinates (1,1) and (2,3). To hide the cards that have been temporarily place face up (but do not match), output a large number of newlines to force the old board off the screen (you may want to wait to do this until after you have finished debugging).

Some requirements:
• Use a two-dimensional array for the arrangement of the cards.
• Use a two-dimensional array that indicates if a card is face up or face down.
• Write a function that  initializes and "shuffles" the cards in the array by repeatedly (say 10000 times) selecting two cards at random and swapping them.
• Write a function that generates the screen display.

Here are strongly suggested, but not required prototypes for the two required functions:
void InitializeCards(int cards[][LENGTH]);
void ShowCards(int cards[][LENGTH], bool faceup[][LENGTH]);

Here is the required comment format for at least the two functions required above and any tother significant functions (courtesy of Dr. Sarraille):

/****************************************
FUNCTION NAME:
INPUT:
OUTPUT:
PRECONDITIONS:
POSTCONDITIONS:
CALLERS:
CALLEES:
*****************************************/

Random Numbers
(Some notes on generating pseudo random numbers in C++)

These functions are found in the cstdlib, so you will need to include it in your program. You will also need: #include <time.h> to seed the random number generator. You should look these functions up in your favorite C++ reference to get a better understanding of their functionality.

// Initialize random number generator using
// the current value of the clock
srand(time(NULL));

// to generate a random number between 1 and 365:
(rand() % 365)+1
// or
random(364) + 1
// (random may not be available in all implementations, so rand is safer.

What to Submit