#include <cstdlib> #include <iostream> using namespace std; const string face[]
ID: 3889714 • Letter: #
Question
#include <cstdlib>
#include <iostream>
using namespace std;
const string face[] = { "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King" };
const string suit[] = { "Clubs", "Diamonds", "Hearts", "Spades" };
string random_card(bool verbose=false) {
string card;
card = face[ rand()%13 ];
card += " of ";
card += suit[ rand()%4 ];
if (verbose)
cout << card << " ";
return card;
}
int main(int argc, char *argv[])
{
bool verbose = false;
int seedvalue = 0;
for (int i=1; i<argc; i++) {
string option = argv[i];
if (option.compare(0,6,"-seed=") == 0) {
seedvalue = atoi(&argv[i][6]);
} else if (option.compare("-verbose") == 0) {
verbose = true;
} else
cout << "option " << argv[i] << " ignored ";
}
srand(seedvalue);
// declare a table called deck that keeps track of
// card faces dealt for each suit -- initialize to 0
while (1) {string card = random_card(verbose);
// reverse engineer card suit and face
// break out of loop if stopping criteria met
}
// print formatted table contents to stdout
}
write the non-existent Prog2b.cpp code and have it keep track of the order in which a card rank is observed for each suit. You do this by inserting the cards at the back of linked lists, using one list for each suit. The exception applies: each time a rank is seen again, the card gets moved to the front of the list. When the stopping criterion from Prog2a is encountered, break the infinite-loop and print the contents of each linked list to stdout as shown below.
Add an array of linked lists: the length of the array is fixed at 4 like before, while the length of each linked list varies from suit to suit. Each new card is added to the appropriate list thru use of an insert() function. Declare a list class based on a single linked list. The list class needs to define a private node datatype, and it must include a constructor for properly initializing an empty list, a destructor for deallocating all the nodes in a list as well as the list itself, and the mentioned insert function which works as described next; no other list class functions are needed. Overload operator<<() used by ostream and have it print the contents of the list. Since access is needed to private data members, make the overloaded output operator a friend of the list class. See the code handouts for how to set this up.
The list::insert() function is where the fun work takes place. The function takes a single integer argument, namely, the rank index of a card where index refers to the position of the rank in the global rank string array. If the rank index is not held by any node in the linked list, a new node is created and added to the end of the list that stores the rank index value. However, if a node is found to already hold the present rank index argument, that node is moved to the front the linked list. That is, the node in question is unlinked from where it is and inserted after the head node.
Hint: You are using a single linked list which means you cannot go back once you have a match on the rank index. One option is to look ahead instead of advancing and then taking a look a the rank index. Another option is to maintain two pointers, namely, one pointing to previous node and one pointing to current node.
Hint: Write generic code that works under all circumstances rather than have several codes for special cases. For example, write the insert() function so that it can handle a rank match regardless of where the matching node resides in the list. Draw a sketch of the different scenarios that need to be handled and infer from it how to do it generically.
Prog2b example output
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1
#define TRUE 1
#define FALSE 0
#define NUM_RANKS 13
//external variables
int straight, flush, four, three;
int pairs = 0;
void dealACard(int suitsInHand[], int facesInHand[],char *suits[], char *faces[], int deck[][FACES]);
void dealAHand(int suitsInHand[], int facesInHand[], char *suits[], char *faces[], int deck[][FACES]);
void analyzeHand(int suitsInHand[], int facesInHand[]);
void printResults(int suitsInHand[], int facesInHand[], int *pointValue);
main(){
char *suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };
char *faces[13] = { "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
int deck[4][13] = { { AVAILABLE } };
int suitsInHand[4] = {0} ;
int facesInHand[13] = {0};
int suitsInHand2[4] = {0};
int facesInHand2[13] = {0};
int player1Points = 0;
int player2Points = 0;
srand( time( NULL ) );
printf("Player 1 was dealt: ");
dealAHand(suitsInHand, facesInHand, suits, faces, deck);
analyzeHand(suitsInHand, facesInHand);
printResults(suitsInHand, facesInHand, &player1Points);
printf("Player 2 was dealt: ");
dealAHand(suitsInHand2, facesInHand2, suits, faces, deck);
analyzeHand(suitsInHand2, facesInHand2);
printResults(suitsInHand2, facesInHand2, &player2Points);
if(player1Points > player2Points) {
printf( "Congrats, you won this hand " );
}
else if(player1Points < player2Points) {
printf( "Ahh, too bad. This Round goes to me " );
}
else
printf( "A Tie " );
system("pause");
}
void dealAHand(int suitsInHand[], int facesInHand[], char *suits[], char *faces[], int deck[][FACES]){
int i;
for(i = 0; i < 5; i++)
dealACard(suitsInHand, facesInHand, suits, faces, deck);
printf(" ");
}
void dealACard(int suitsInHand[], int facesInHand[], char *suits[], char *faces[], int deck[][FACES]){
int suitIndex, faceIndex;
suitIndex = rand() % 4;
faceIndex = rand() %13;
while( deck[suitIndex][faceIndex] == TAKEN ){
suitIndex = rand() % 4;
faceIndex = rand() %13;
}
deck[suitIndex][faceIndex] = TAKEN;
facesInHand[faceIndex]++;
suitsInHand[suitIndex]++;
printf("%s of %s ", faces[faceIndex], suits[suitIndex]);
}
void analyzeHand(int suitsInHand[], int facesInHand[])
{
int num_consec = 0;
int rank, suit;
straight = FALSE;
flush = FALSE;
four = FALSE;
three = FALSE;
pairs = 0;
for (suit = 0; suit < SUITS; suit++)
if (suitsInHand[suit] == 5)
flush = TRUE;
rank = 0;
while (facesInHand[rank] == 0)
rank++;
for (; rank < FACES && facesInHand[rank]; rank++)
num_consec++;
if (num_consec == 5) {
straight = TRUE;
return;
}
for (rank = 0; rank < NUM_RANKS; rank++) {
if (facesInHand[rank] == 4)
four = TRUE;
if (facesInHand[rank] == 3)
three = TRUE;
if (facesInHand[rank] == 2)
pairs++;
}
}
/* assign value to hand and print results */
void printResults(int suitsInHand[], int facesInHand[], int *pointValue)
{
analyzeHand(suitsInHand, facesInHand);
if (straight && flush) {
printf("Straight flush ");
*pointValue = 9;
}
else if (four) {
printf("Four of a kind ");
*pointValue = 8;
}
else if (three && pairs == 1) {
printf("Full house ");
*pointValue = 7;
}
else if (flush) {
printf("Flush ");
*pointValue = 6;
}
else if (straight) {
printf("Straight ");
*pointValue = 5;
}
else if (three) {
printf("Three of a kind ");
*pointValue = 4;
}
else if (pairs == 2) {
printf("Two pairs ");
*pointValue = 3;
}
else(pairs == 1) {
printf("Pair ");
*pointValue = 2;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.