Old Maid-Card Game

This program will use arrays to represent a simple card game; it will also use functions to organize the code into a structure easy to write and debug.

A Better description at : http://bayfiles.net/file/1bL70/lOSkoq/HW_5.pdf
For more info about Old Maid : http://www.pagat.com/passing/oldmaid.html


This program will simulate the card game Old Maid. It must support any number of players from two to six (since a game of only two players is rather uninteresting). One player will be a user at the keyboard; the rest will be simulated by the program on the computer.

One card is removed from the deck (the link removes a Queen), and the remaining cards are dealt out to all players. Each player then, in turn, draws a random card from the previous player.

Any time two matching cards (of the same rank) appear in the same hand, both cards are removed from the deck. Any time this causes a player to have zero cards, that player is out of the game (winning). Play proceeds until only one player remains (the sole loser).


These lines were copied from part of the instructor’s program’s output. They were heavily trimmed to highlight some game features.
You currently have these cards in your hand:
Three Queen Two Eight Four
You get to draw a card from a hand containing 4
Choose a number from 1 to 4: 3
You draw a(n) Eight
A computer player takes your Queen

You currently have these cards in your hand:
Three Two Four
You get to draw a card from a hand containing 4
Choose a number from 1 to 4: 1
You draw a Two
A computer player takes your Four

You currently have these cards in your hand:
You get to draw a card from a hand containing 2
Choose a number from 1 to 2: 2
You draw a Three
You have no cards! A winner!
Game over!
Play another game (y/n)?

A good functional plan is required for this assignment (as was mentioned above). There should be very clear and manageable interfaces between these functions, with NO global variables.
This will necessitate keeping track of which data is truly needed by which functions. Not all variables are relevant to the main function, so should not be declared there. Code that manipulates an individual handful of cards should not care about how many players are in the game, or which players are still active.

If you find yourself with a heavy reliance on global variables or if your parameter lists contain more than a half dozen parameters, then take a more serious look at your functional design before you get too deep in the code.

And, of course, a good functional design would make it easy to write each function individually on its own terms, without worrying about how the other functions are implemented.


Arrays will be required in this assignment – it would be nearly impossible to make a readable program without them.

Arrays may serve in various different ways:

Before dealing any cards, the deck can be represented as an array containing all the cards. This would help to assure that all cards are represented, and can also accomodate shuffling (see below).
Each player has a hand, which would best be represented by an array.
Passing a card around can easily be simulated just by having operations that represent it removing from one hand and inserting it into hand.
The names of cards may be stored in an array, subscripted by the rank of the card. An implementation of the game would be much simpler comparing numbers than strings, so postpone all text names for cards until output is needed.
One way to handle players exiting the game is to use a permutation array to take them out of turn rotation. Otherwise, you might have to make some major changes to your data to remember who is taking cards from whom.
It should be noted that large segments of duplicated code that only differ in variable names strongly suggest a need for arrays.

The order of cards in the hand does not matter in this game, so you have two reasonable choices to deal with this:

The hand could be a collection of cards, just as the deck itself. You would need to know how many cards are currently in the hand. It would be far simplest to use the earliest entries in the array (with no ‘holes’ in your data).
Drawing a card would simply add a card to the end of the array. But removing cards (either by matching a pair, or handing them to the opponent) would also want to preserve the values consecutively at the front of the array. Since order does not matter, any time a card is withdrawn ‘from the middle’, just replace it with the card at the bottommost part of the data, recording that there is one fewer card in the hand.

Alternatively, you could describe a hand by counting the number of occurrences of each rank. A relatively small array could accommodate a very large handful of cards, and it is very easy to count how many 3’s someone has (since that is exactly what is stored). Drawing a card just increments how many one has – and losing a card simply decrements.
This way to organize the data is very easy to manipulate, but unfortunately interferes somewhat with an extra credit option below.


Shuffling a deck is simply an application of a random number generator. Initialize an array to a series of known values (i.e. known to have distinct values for each card); then visit each subscript in turn and exchange that array element with some element randomly chosen from anywhere in the array. Every card will have been moved somewhere – and no one should have any idea where.

Shuffling would be a necessary first step to this program. (If you don’t believe me, see how your program runs if you don’t shuffle.)


Include card descriptions in the user interface to make it feel more like a card game, instead of just a bunch of numbers.

So instead of just ‘Seven’ the user would see “Seven of Hearts”, and “You receive the Five of Clubs”.

Use constant arrays of strings for full credit (not If or Switch statements).

Of course, you have to make sure that all the cards are actually different, so you don’t have two cards of the same rank and suit.

One way to do this is to keep parallel arrays of card ranks and suits (taking care to keep the data consistent). Another way is to somehow encode both the rank and suit of card in a single integer value, using a scheme that is both easy to encode and to decode.

It is still sufficient to let any card match any other card of the same rank. If you wish a longer game, you might only match black cards (clubs and spades) and red cards (hearts and diamonds) of the same rank. Either approach is about the same difficulty in coding, so neither will be worth more credit than the other.

For more info about Old Maid : http://www.pagat.com/passing/oldmaid.html