PA 4 Due

advertisement
Computer Science 145.002
Introduction to Computing for Engineers
Fall 2008
Programming Assignment 4
“Functions”
Due: 7:30 AM, Thursday, October 9, 2008
In this programming assignment, you will break a large programming
problem into smaller self-contained functions, each of which will solve a
particular task as part of the overall programming effort. Your program
will repeatedly simulate the dealing of five cards from a standard 52-card
deck, as long as the resulting five-card hand isn't a full house (i.e., three
cards of one rank and two of another). When that happens, the program
terminates, telling the user how many hands were dealt.
For each hand dealt, a random number generator will be used to
generate five distinct values between 1 and 52. Values from 1 to 13
represent the "Hearts" cards from the Ace through the King; values from
14 to 26 represent the "Diamonds" cards from the Ace through the King;
values from 27 to 39 represent the "Spades" cards from the Ace through
the King; and values from 40 to 52 represent the "Clubs" cards from the
Ace through the King.
As each hand is "dealt", the previous cards are examined to ensure that
no duplicates occur. Once all five cards for a hand are dealt, the entire
hand is analyzed to see if it constitutes a full house (e.g., three Aces and
two Fours, etc.). A count is kept of how many hands are dealt, and this
count is output when a full house hand is finally dealt. A sample
execution session for this program is illustrated at right.
There are twelve functions in this program, and the prototypes and
function headers have already been written. In addition, the code and
comment sections for five of the functions (including main) are included.
A soft copy of the code as it currently exists is available on the course
Web page at http://www.cs.siue.edu/wwhite/CS145/Syllabus.htm. The twelve functions are described below:
 The main function (already written) coordinates the repeated dealing of the cards, until a full house hand
is dealt.
 The dealCard function produces a random number between 1 and 52, representing a card from the 52card deck.
 The generateRandomNumber function (already written) uses library functions in stdlib.h and
time.h to generate a random integer value that is between two specified integer values.
 The four overloaded checkForDuplicate functions determine whether the integer value representing
the newly dealt card is equal to the value associated with one of the previously dealt cards.
 The displayHand function outputs the contents of the five-card hand that was dealt, specifying the rank
and suit of each card in the hand.
 The displayOneCard function outputs two characters, representing the rank and suit of the card
corresponding to a parameterized integer value (e.g., K♥ represents the King of Hearts, corresponding to
integer value 13).
 The rank function (already written) calculates the single character corresponding to the rank of the card
being evaluated. It uses 'K' for King, 'Q' for Queen, 'J' for Jack, 'T' for 10, '9' for 9, and so on, down to 'A' for
Ace.
 The suit function (already written) calculates the single character corresponding to the suit of the card
being evaluated. It uses '♥' for Hearts, '♦' for Diamonds, '♣' for Spades, and '♠' for Clubs.
 The containsFullHouse function determines whether three of the five cards in the dealt hand have
the same rank, while the other two cards have a different (but equal) rank (e.g., three Aces and two 4's,
three 7's and two Jacks, etc.).
Your assignment is to write the remaining seven functions so the program will yield the kind of output
illustrated above. You also must write descriptive comments of the program as a whole (including your
name), and of the individual functions that you write.Name your project PA4_Lastname where Lastname is
your last name (e.g., Mary Miller’s project would be PA4_Miller). Don’t forget to include adequate explanatory
comments in your code.
Zip-compress the entire project folder and place the zipped folder on your Moodle dropbox by 7:30 AM on
Thursday, October 9, 2008.
//////////////////////////////////////////////////////////////////////
// YOUR NAME GOES HERE!
//
// CS 145 - Section 002
//
// Programming Assignment #4 - Functions
//
//
//
// YOUR DESCRIPTIVE COMMENT (AT LEAST ONE PARAGRAPH) GOES HERE!
//
//////////////////////////////////////////////////////////////////////
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int dealCard();
int generateRandomNumber(int lowerBound, int upperBound);
bool checkForDuplicate(int newCard, int oldCard);// YOUR DESCRIPTIVE COMMENT GOES HERE! //
bool checkForDuplicate(int newCard, int oldCard1,int
intdealCard()
oldCard2);
{ int oldCard2, int oldCard3);
bool checkForDuplicate(int newCard, int oldCard1,
//YOUR
CODE GOES
// int oldCard4);
bool checkForDuplicate(int newCard, int oldCard1, int
oldCard2,
int HERE!
oldCard3,
void displayHand(int card1, int card2, int card3,} int card4, int card5);
void displayOneCard(int card);
char rank(int card);
// This function generates a pseudorandom integer //
char suit(int card);
between
values
the two parameters.
//
bool containsFullHouse(int card1, int card2, int //
card3,
int the
card4,
int of
card5);
int generateRandomNumber(int lowerBound, int upperBound)
{
bool firstTime = true;
// YOUR DESCRIPTIVE COMMENT GOES HERE! //
// The main function deals poker hands repeatedly, static
//
long
void displayHand(int card1, int card2, int card3, int card4, int card5)
// until a hand containing a full house is dealt. At
// int randomNumberSeed;
{
// that point, the number of dealt hands is output.
//
if
(firstTime)
// YOUR CODE GOES HERE! //
void main()
{
}
{
time(&randomNumberSeed);
int card1;
srand(randomNumberSeed);
// YOUR DESCRIPTIVE COMMENT GOES HERE!
//
int card2;
firstTime = false;
void displayOneCard(int card)
int card3;
}
{
int card4;
// YOUR CODE GOES HERE! //
int card5;
return (lowerBound + int((upperBound - lowerBound)
* (float(rand()) / RAND_MAX)));
}
int count = 0;
}
bool foundFullHouse = false;
// This function determines the rank of a card by converting //
// YOUR DESCRIPTIVE COMMENT GOES HERE! //
// the number representing the card into a number from 1 to //
while (!foundFullHouse)
bool checkForDuplicate(int newCard, int oldCard)// 13, and using 13 to represent a "King", 12 to represent a //
{
{
// "Queen", and so on down to 1 to represent an "Ace".
//
count++;
// YOUR CODE GOES HERE! //
char rank(int card)
}
{
card1 = dealCard();
int value;
// YOUR DESCRIPTIVE COMMENT GOES HERE! //
value = 1 + (card - 1) % 13;
card2 = dealCard();
bool
checkForDuplicate(int
newCard,
int
oldCard1,
int
oldCard2)
if (value
== 1)
while (checkForDuplicate(card2, card1))
{
return 'A';
card2 = dealCard();
// YOUR CODE GOES HERE! //
else if (value == 10)
}
return 'T';
card3 = dealCard();
else if (value == 11)
while (checkForDuplicate(card3, card1, card2))
//
YOUR
DESCRIPTIVE
COMMENT
GOES
HERE!
//
return 'J';
card3 = dealCard();
bool checkForDuplicate(int newCard, int oldCard1, else
int oldCard2,
oldCard3)
if (value int
== 12)
{
return 'Q';
card4 = dealCard();
YOUR CODE GOES HERE! //
else if (value == 13)
while (checkForDuplicate(card4, card1, card2, //
card3))
}
return 'K';
card4 = dealCard();
else
// YOUR DESCRIPTIVE COMMENT GOES HERE! //
return char(value - 2 + int('2'));
card5 = dealCard();
boolcard3,
checkForDuplicate(int
newCard, int oldCard1,
int oldCard2, int oldCard3, int oldCard4)
}
while (checkForDuplicate(card5, card1, card2,
card4))
{
card5 = dealCard();
// YOUR CODE GOES HERE! //
// This function determines the suit of a card by converting //
}
// the number representing the card into a character that
//
displayHand(card1, card2, card3, card4, card5);
// corresponds to "Hearts", "Diamonds", "Spades", or "Clubs". //
char
suit(int
card)
foundFullHouse = containsFullHouse(card1, card2, card3, card4, card5);
{
}
int value;
value = 3 + (card - 1) / 13;
cout << "A full house was dealt" << endl
return char(value);
<< "after " << count << " attempts!" << endl << endl;
}
return;
}
// YOUR DESCRIPTIVE COMMENT GOES HERE! //
bool containsFullHouse(int card1, int card2, int card3, int card4, int card5)
{
// YOUR CODE GOES HERE! //
}
Download