Homework Assignment 7 Dealing with Arrays and Functions Solution



Overall Assignment

For this assignment, you are to write a simple computer program which will use arrays and functions to create a deck of cards, shuffle the deck, deal some hands, and print out the dealt hands. Possible optional enhancements include classifying the hands in a manner similar to a recent lab exercise.

Representing Playing Cards

Standard playing cards can be easily represented using two integers: One ranging from 0 to 12 for the ranks ( numbers ) of the cards ranging from deuce (2) to ace, and one ranging from 0 to 3 representing the 4 suits, as shown in the following tables:

Rank value

0 to 8
rank + 2





Suit Value





Printing a Single Playing Card

The first function that is needed is a function to print a single playing card, using words such as “three of clubs” or “ace of diamonds”. The prototype for this function is:

int printCard( int rank, int suit );

On input, rank should be an integer in the range 0 to 12 inclusive, and suit should be in the range 0 to 3.

If the input is NOT in this range, then the function should print “ERROR” and return -1.

If the input is in the valid range, the function should print the card rank and suit as described above, ( e.g. “ace of diamonds” ), and return 0.

To print out the ranks of the cards, create an array of strings called rankNames[ ], initialized to { “deuce”, “three”, “four”, . . . “ace” }. Then simply print rankNames[ rank ] where the rank name is needed in the output.

Print the card suits similarly, using suitNames[ ] initialized to { “clubs”, “diamonds”, “hearts”, “spades” } and printing suitNames[ suit ] as needed.
Creating an Ordered Deck of Cards

The best way to create a shuffled deck of cards is to first create an ordered deck of cards, and then shuffle it. This approach guarantees that every card is represented exactly once, with no missing cards or duplicates, and it does so in a reasonable time.

Key to storing the deck of cards will be two arrays, named “suits” and “ranks”, each containing room for 52 integers. Each card in the deck will be represented by matching entries in the two arrays. For example, the 10th card in the deck will have a suit according to the 10th element in the suits[ ] array, and a rank ( number ) according to the 10th element in the ranks[ ] array.

The function to create the initial ordered deck is:

void create52CardDeck( int ranks[ ], int suits[ ] );

o The ranks[ ] and suits[ ] arrays must be created by main( ) before calling this function, and must have space for at least 52 integers in each array.

To fill the arrays, create a loop with an integer, ( say “i” ), that varies from 0 to 51 inclusive.

o In the suits[ ] array for position i, store i%4. Convince yourself that this will generate
the series 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, . . ., I.e. 13 sets of the sequence 0, 1, 2, 3.

o In the ranks[ ] array for position i, store i / 4. Convince yourself that this will generate the series 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, …, 12, 12, 12, 12, I.e. 13 sets of 4 matching integers.

The resulting deck should look like this:

. . .
. . .
2 C
2 D
2 H
2 S
3 C
3 D
3 H
3 S
. . .

Shuffling the Deck

Shuffling the deck will be accomplished by swapping each card in the deck one-by-one with a randomly chosen different card from the deck. Note that some cards will be swapped more than once, and may even end up back where they started originally, but overall the order of cards will be randomized. The function to perform this task is:

int shuffle( int ranks[ ], int suits[ ], int nCards );

ranks[ ] and suits[ ] should have been previously filled, e.g. by create52CardDeck, and passed in.

nCards indicates how many cards in the deck are to be shuffled, e.g. 52 for a full deck. The return value is 0 so long as nCards is not negative, or -1 if it is negative.

Create a loop that will take “index” from 0 to nCards – 1 inclusive.

o For each value of index, select a random value from 0 to nCards – 1 inclusive. (Use %) o Swap the card at position “index” with the card at the randomly chosen position.

To swap two values, copy one value into a temporary variable, then copy the second value where the first value was, and then copy the temporary variable into the second value.

Note that this will require making the same swaps in both the ranks[ ] and suits[ ] arrays for each card being swapped.
Printing a Hand of Cards

Printing a hand of cards is as simple as calling printCard( ) for each of the cards in the hand. The only question is how to specify which cards in the deck correspond to which hand of cards. One way would be to copy the relevant cards from the deck into another pair of arrays, but a simpler approach is to create an array of indices into the deck indicating which cards correspond to a hand. The function to accomplish this is:

int printHand( int ranks[ ], int suits[ ],

int hand[ ], int nCardsInHand, int nCardsInDeck );

hand[ ] is the array of indices corresponding to the current hand. For example if there were 5 cards in each hand and each player is “dealt” 5 cards off the top of the deck, then the first players hand[ ] would equal { 0, 1, 2, 3, 4 }, and the second player would have { 5, 6, 7, 8, 9 }, etc.

The function should return -1 if nCardsInHand or nCardsInDeck is negative.

The function should also return -1 if it encounters any value in hand[ ] that is either negative or greater than or equal to nCardsInDeck.

Otherwise the function loops i from 0 to nCardsInHand – 1 and calls printCard( ranks[ hand[ i ], suits[ hand[ i ] ] ) for each i.

Dealing Hands

First ask the user how many hands to deal ( i.e. how many players ), and how many cards to deal in each hand. Your program should only accept reasonable values. ( I.e. the product of nHands * nCardsPerHand must be less than or equal to 52, and both must be positive. )

For the basic assignment, the easiest approach is to simply assign the first nCardsPerHand to the first player, the second nCardsPerHand to the second player, etc. This requires creating a hand[ ] array of size nCardsPerHand, filling it with integers 0 to nCardsPerHand – 1, and calling printHand( ). Then filling hand[ ] with nCardsPerHand to 2 * nCardsPerHand – 1, and calling printHand( ) again, etc. For the basic assignment only one hand[ ] array is needed, which can be reused for each player.

Dealing of the hands is not required to be done in a function, though it could be if you wish. In this case you will need to determine a reasonable function name, input and output parameters, and any necessary error checking.

Program Details

For this assignment, you are to write a simple computer program which will use arrays and functions to create a deck of cards, shuffle the deck, deal some hands, and print out the dealt hands, as described above.

Your program should first print out your name and ACCC netID ( e.g. jbell ), and explain to the user what the program does.

Your program should then implement the algorithm shown above. It is recommended that you develop your program one step at a time, verifying that each step works before working on the next step.

In order to use the random number generator, you must #include <cstdlib> and <ctime>. srand( time( NULL ) ) must be called one time only (before any looping) to initialize the random number generator. ( See http://www.cplusplus.com/reference/cstdlib/rand/ for more info. )
What to Hand In:

Your code, including a user documentation file, should be handed in using Blackboard. All of your source code should be in one file, with a .cpp extension:

o At the top of the file, before main( ), you should list prototypes for all your functions, in the order in which they will eventually appear.

o main( ) should be the first real function provided, just after the function prototypes.

o The remainder of the functions should follow main( ), in the order in which the prototypes are listed. ( Preferably in the order listed in this assignment, unless there is some reason to do otherwise. )

All files should be zipped together into a single file, whose name is comprised of your ACCC netID followed by the course number followed by the letters “HW”, followed by the assignment number. ( E.g. jbell109HW6.zip ) The zip file should be handed in via Blackboard. ( Your TA may provide alternate instructions, which override these. )

The intended audience for the documentation file is a general end user, who might want to use this program to perform some work. They do not get to see the inner workings of the code, and have not read the homework assignment. You can assume, however, that they are familiar with the problem domain ( e.g. poker hands. )

A secondary purpose of the documentation file is to make it as easy as possible for the grader to understand your program. If there is anything special the grader should know about your program, be sure to document it in the documentation file. In particular, if you do any of the optional enhancements, then you need to document what they are and anything special the TA needs to do to run your program and understand the results.

If there are problems that you know your program cannot handle, it is best to document them as well, rather than have the TA wonder what is wrong with your program.

Make sure that your name appears at the beginning of each of your files. Your program should also print this information when it runs.

Optional Enhancements:

It is course policy that students may go above and beyond what is called for in the base assignment if they wish. These optional enhancements will not raise any student’s score above 100 for any given assignment, but they may make up for points lost due to other reasons.

Allow players to exchange some cards from their hands for new cards from the deck. ( In a typical game of 5-card draw poker, players are allowed to exchange up to 3 of their cards in order to try and improve their hands, or 4 cards if the last remaining card in their hand is an ace. If you develop this enhancement you can decide for yourself what if any rules to apply to card exchanges. ) There are two possible approaches to handling the swaps:

1. Actually swap cards, keeping a pointer ( index ) to the next available card in the deck. In this way the first player still has the first nCardsPerHand cards, and the hand printing does not change.

2. Use a separate array with card indices for each player. So initially the first player may hold the cards in deck positions 0, 1, 2, 3, and 4, but after swapping his third and fifth cards for new cards, then his or her cards might be in positions 0, 1, 21, 3, and 22.

Categorize each of the hands as a pair, two pairs, straight, flush, etc. You can base your classification on the recent lab exercise in this area or do some research on poker hands.
Identify which player has the “best” hand, again according to the rules of poker.

Introduce betting and bluffing. Allow wild cards.

Develop a computer character which can play against the humans, knowing when to bet, how much to bet, and when to bluff, etc. This will require some research into Artificial Intelligence, AI.

Implement your shuffled deck into some other game of your choice other than poker, such as solitaire, spades, or hearts. ( Depending on the game, you may need to include jokers. )

Implement a game that uses a different sized deck, such as Euchre or Canasta, or even a radically different deck such as Tarot.

You can make the game repeatable if you call srand( ) with a specific value before starting each game. Call this the “gameID”. You can calculate a randomly chosen gameID by calling rand( ) and modding the result with a reasonable range, such as 100,000 to yield 5-digit gameIDs. If you allow the user to enter their own gameID, then they can play the same game again, or two different players could play the same game. ( You still need to call srand( time( NULL ) ) exactly once at the beginning of the program. )

Instead of reusing the hand[ ] array or keeping separate hand[ ] arrays for each player, use a two-dimensional array to keep track of all players hands. The big problem is that if the dealHands( ) function is written to take a two-dimensional array, the number of columns must be specified at the time the code is written, before asking the user how many players to deal hands for or how many cards in each hand. This can be handled by specifying an upper limit, writing the code and sizing the array to match the upper limit, and then partially filling the array.

Other enhancements that you think of – Check with TA for acceptability.

error: Content is protected !!