#include
#include
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 << "\n";
return card;
}
int main(int argc, char *argv[])
{
bool verbose = false;
int seedvalue = 0;
for (int i=1; 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\n";
}
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
UNIX> ./prog2b
Clubs : 10 6 Queen King 9 Jack **
Diamonds : Jack 9 6 Ace 3 8
Hearts : 9 3 King 7 Jack
Spades : Queen Ace Jack
UNIX>
UNIX> ./prog2b -seed=140
Clubs : 10 Jack King 4 2 Ace 7 6 3 8
Diamonds : King 3 Ace 9 Queen 5 10 8 7 2 Jack **
Hearts : Jack 2 10 7 6 3 Ace 5 4
Spades : 6 5 Jack 4 9 Queen 2