Computer Science I
MC140.01 / MC140.02
Fall 2000
Assignment #5
due Monday 6 November 2000

The goal of this assignment is to learn about random number generation and to practise manipulating arrays.

The assignment is worth 5% of your term mark. It will be marked out of 50 points. 45 points are allotted to each of the functions described below. 5 points is allotted to comments and general readability of your code. Make sure you have a header comment at the top of your program, as well as comments at the beginning of each function. Make sure your code is nicely indented and your variables have meaningful names.

As usual, submit your program electronically. So you should deposit your file in the professor's folder, named as follows: sklarel-ass5.c. Bring a hardcopy to class. Please staple loose sheets together.

In this assignment, you will write a program to generate two hands of poker from a deck of cards, evaluate the hands and declare a winner.

Use a global, one-dimensional array with 52 entries to keep track of the cards that have been dealt (to make sure you don't deal the same card twice). This is an array of flags, where a flag is a logical variable that can be either true or false (1 or 0) -- if an entry is true (1), it means that card has been dealt; if an entry is false (0), then that card has not been dealt. Declare this array as follows:

     int deck[52];

1. (5 points) Write a function to shuffle the deck by initializing all its entries to false:

     void shuffle();

2. (5 points) Write a function to deal a card from the deck. Use the random number generator to pick a number between 0 and 51, and check to see if that number card has already been dealt. If it has, then try again -- until a card is found that has not been dealt. The function should return that card number:

     int deal_one_card();

In standard poker, each player is dealt 5 cards. In our game here, we have two players. Use two global, one-dimensional arrays with five entries to keep track of each player's hand, declared as follows:

     int player1[5], player2[5];

3. (5 points) Write a function that deals a hand of five cards to each player.

     void deal();

By using our deck array, we are numbering the cards from 0 to 51, and you can think of each number as in index into a table like this:

card # 0 1 2 3 4 5 6 7 8 9101112
2 3 4 5 6 7 8 910 J Q K A
card # 13141516171819202122232425
2 3 4 5 6 7 8 910 J Q K A
card # 26272829303132333435363738
2 3 4 5 6 7 8 910 J Q K A
card # 39404142434445464748495051
2 3 4 5 6 7 8 910 J Q K A

4. (5 points) Write a function called find_suit() that returns the suit of a card, given its card number. The return value should be a single character: 'C' = clubs (), 'D' = diamonds (), 'H' = hearts (), 'S' = spades ().

5. (5 points) Write a function called find_rank() that returns the rank of a card, given its card number. The return value should be a single character: '2'..'9' = 2 through 9, '0' = 10, 'J' = jack, 'Q' = queen, 'K' = king, 'A' = ace.

6. (5 points) Write a function called print_hand() that prints out each player's hand, using find_suit() and find_rank() to determine the name of each card.

7. (10 points) Write a function called evaluate_hand() that classifies and evaluates each player's hand, using the rules defined here: http://www.cs.bc.edu/~sklar/mc140/poker.html. The value of the hand is the sum of two numbers:

     value = class + rank;

defined as follows:

For example, diamondK-diamondJ-diamond7-diamond6-diamond5 would have class = 500 (flush) and rank = 12, so value = 512.

8. (5 points) Finally, compare the value of the two hands and declare one of the players a winner!