### 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 9 10 11 12 2 3 4 5 6 7 8 9 10 J Q K A card # 13 14 15 16 17 18 19 20 21 22 23 24 25 2 3 4 5 6 7 8 9 10 J Q K A card # 26 27 28 29 30 31 32 33 34 35 36 37 38 2 3 4 5 6 7 8 9 10 J Q K A card # 39 40 41 42 43 44 45 46 47 48 49 50 51 2 3 4 5 6 7 8 9 10 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:

• the `class` of hand (royal flush, straight flush, full house, etc), as follows: royal flush = 900, straight flush = 800, four of a kind = 700 full house = 600, flush = 500, straight = 400, three of a kind = 300, two pairs = 200, pair = 100, high card = 0.

• the `rank` of the hand (with each class of hand, there is a rule for determining the rank of the hand, as described in the poker rules): '2' = 1, '3' = 2, '4' = 3, '5' = 4, '6' = 5, '7' = 6, '8' = 7, '9' = 8, '10' = 9, 'J' = 10, 'Q' = 11, 'K' = 12, 'A' = 13.

For example, K-J-7-6-5 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!