program will shuffle a deck of cards, deal hands to two players, accept requests from the two players to exchange cards, and finally report the winner.
Define a 52-card deck of playing cards by the numbers 0 to 51. Cards numbered 0 to 12, are from the suit of Spades, 13-25 from Clubs, 26-38 are Diamonds and 39-51 are Hearts. In each suit, the 13 cards are the 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King and Ace. For example, card number 29 is the 5 of Diamonds while card 25 is the Ace of Clubs.
Write a program which, given the number of the card, creates a string consisting of the actual card. Thus,
input of 29 would generate the string "Five of Diamonds".
Modify your program by moving the code for generating the string into a function, within the file
main.cpp, with prototype
which takes the card number in the first argument and returns the card name in the second argument. The
value of the function is true if the card is a valid card, and 0 otherwise.
Move your function to a new file called subs.cpp. This new file should also have includes for libraries
used in the function. Place the prototype at the top of
this file as well.
Create a new file called subs.h containing the prototype for the above function. Replace the prototype line
in main.cpp and subs.cpp with an include line for this new header file.
The deck of cards is stored in a 52-long integer array. To shuffle a deck of cards, a random ordering of the
digits 0-51 is placed in the array. This involves initialising the array to the digits 0-51, and then randomly
swapping pairs of digits. Thus, two random numbers are needed. These numbers will be on the range 0-
51. But how can a computer generate a random number? Obviously, computers are deterministic. That is,
any process will produce a repeatable result.
Most computer languages provide a library function for generating pseudo-random numbers. Such a
function, when called, returns a sequence of numbers in some range (in C++ from 0 to RAND_MAX) with
properties such as equal probabilities of all possible numbers, and no correlation between one number
generated and the next.
One of the common methods used is an additive-multiplicative congruence where a sequence of unsigned
long integers is produced by multiplying the last number by a constant and adding another constant. The
short integer random number produced is then only some of the bits of the long integer. Symantec C++
next = current * 1103515245 + 12345;
and then takes the least significant 15 bits to produce random numbers on the range 0 to 32767 inclusive.
On most C++ systems, the function for generating a (pseudo-)random number is rand() which is part of the
stdlib library with associated header file cstdlib. So that the generator is not dependent on hard coded
upper limits, the header also defines the value RAND_MAX (as 32767), which is the maximum value
generated. You should use this value as the maximum.
To generate a random number in the range 0 to n-1, we could use the expression
which is then truncated. (The +1. accounts for generated values of RAND_MAX and makes the expression
real.) An alternative method is just to evaluate
rand() % n
This expression, with some methods of pseudo-random number generation, may cause problems but
Symantec's approach means this method is acceptable.
How many swaps should produce a reasonable shuffle? Who knows? But there is a better way.
Start with the first card in the deck (card 0). Swap it with one of the 52 cards (including the possibility of
no swap). Now move to the next card, and swap it within the 51 remaining cards. And so on. Now only
51 swaps are needed (the last card cannot be moved in the end).
Write the function (in file subs.cpp with the prototype added to subs.h)
to fill the deck with the 52 cards and shuffle it. Rewrite main.cpp as a driver program to test this new
Note that repeated runs of the program will produce the same sequence of cards. This is because
the sequence of numbers generated by the function rand() starts with the same long integer. To overcome
this, another function called srand() with an unsigned integer argument starts the sequence. Add a request
(in main.cpp) for an initial unsigned integer seed called, say, seed, and then add the line
A poker hand consists of 5 cards dealt from a shuffled deck. We are simulating a two-player game, so that
two hands are needed. Write a function
which takes the deck in the first argument and deals two hands into the two arrays in the second and third
arguments. Cards from the deck should alternate to each hand. Modify the driver program to test this
The next step is to be able to determine what the quality of a poker hand is. There are 9 different levels (in
order of decreasing quality):
1. Straight Flush
The five cards are in sequence in the same suit. Ace can be low (namely A,2,3,4,5) or high
2. Four of a Kind
Four of the cards are of the same rank.
3. Full House
Three of the cards are of one rank while two are of another rank.
The five cards are in the same suit.
The five cards are in sequence, as for the Straight Flush, but not of the same suit.
6. Three of a Kind
Three cards are of the same rank, and two unmatched cards.
7. Two Pair
Two pairs of cards are of the same rank.
8. One Pair
Two cards are the same rank, while the other three are all different.
All cards are of different rank, and are not of the same suit.
Write a function
int HandValue(int, char);
which returns the number above for the given hand, plus puts the name of the hand type in the second
argument. (Hint: Count the number of each rank that appears in the hand, storing the result in an array.
Determining fours, threes and twos of a kind is then trivial.) Test your function on the two hands dealt in
the previous section.
The only problem when deciding whether one hand beats another is when both hands are of the same
quality. In each case there is a tie-breaker. Sort the cards into decreasing rank, with the proviso that
multiples (fours/threes/twos of a kind) rank higher than single cards (and straights with Ace at the bottom,
should have 5 as the highest card). Then compare the two hands card by card to find the higher hand. For
example, if both players have two pair with Player 1 (A,A,5,5,4) and Player 2 (A,A,5,5,6), then Player 2
Write a function
which returns 1 if the first hand (cards in argument 1, quality in argument 2) is better, 2 if the second hand
(cards in argument 3, quality in argument 4) is better. In the event of a complete tie return 0.
The final step is to allow the two players to exchange a number of cards from their hands. At this time the
final layout of the communications with the players can be programmed. The program should
(a) request a random seed
(b) shuffle the decks
(c) deal two hands without displaying them
(d) for each player in turn:
show their hand
request which cards to change
change the cards
show the final hand including what kind of hand
(after player 1, output 24 blank lines)
(e) report the final result.
DescriptionIn this final assignment, the students will demonstrate their ability to apply two majorconstructs of the C programming language – Fu
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of
1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of