Question: Using C++: + Writing the Rank and Suit types : Your first decision is how to represent the Suit (U, T, or A) and Rank
Using C++:
+ Writing the Rank and Suit types :
Your first decision is how to represent the Suit (U, T, or A) and Rank (0 through 9) for a card. For this assignment, use an enum for Suit and an int (with optional typedef from Lecture 05) for Rank. You will also need to determine how to represent the number of suits (since enums are not classes and thus have no .size() method) and number of ranks (or min and max ranks) for your program. Finally, you will (eventually) need a way to convert the suit back into a char (U, T, or A) - this may be via a function, a method in Card, a vector of strings subscripted by the enumerated suit, a substring, or any other approach you prefer. You may need to switch between variables of type Suit and variables of type int. You may use C casting for now, e.g., Suit s = A; int i = (int)s; These type(s) may be in their own file(s), e.g., rank.cpp and suit.cpp, or in cards.cpp, as you prefer.
+Writing the Card class:
Write the card class and store it in a file named card.cpp. You are not required to use the underscore in front of the private variables. Youll need a constructor where the suit and rank are passed as parameters and simply stored in the private variables. If the suit or rank is not valid, throw a runtime error with a text message as a parameter, e.g., Suit: Out of range. Do NOT print anything libraries throw exceptions, they dont print error messages! Youll need a second constructor where the suit and rank are selected at random. You can include the cstdlib library (which is the same as the stdlib.h library from C), in which case the rand() function will return a random positive integer. Thus, e.g., rand()%10 will give you a random integer from 1 to 9. Its not required that you seed the random number generator, though you may if you like. Youll need two getters, one for suit (suit()) and one for rank (rank()). These just return the respective private variables. Thus, suit and rank are effectively read-only due to our encapsulation.
Youll need a card_to_string() method eventually for the main application. The standard to_string function should work with the rank, but youll need some way to convert a suit to a U, T, or A (see above). A card should print as A3, T0, U9, or similar.
+Writing the test_card regression test:
Youll write a main() function in a file named test_card.cpp that tests the following 4 cases. Remember to generate no output when the test succeeds (except for case #1 we dont have the string manipulation tools yet to detect failure there, so youll have to rely on your eyes).
1. Normative case: Create a deck with 20 cards and stream them to cout. Verify that your card_to_string is working, and that all of the generated cards are valid. (In the future, well learn how to automate this test!) By the end, you will have moved this test to be your main application, so that your tests will generate no output.
2. Normative case with explicit constructor case: Create card U3, and verify the suit is U and the rank is
3. Stream an error to cerr if not, or if an exception is generated. 3. Invalid suit case: Verify that creating a card with an invalid suit throws a runtime error. You may need to use a simple cast to pass an invalid suit, e.g., (Suit)3.
4. Invalid rank case: Verify that creating a card with an invalid rank throws a runtime error.
+Writing the Deck class:
Write the Deck class and store it in a file named deck.cpp. You have only one private variable, a vector of Card objects called _deck or deck as you prefer. Youll need one constructor, which accepts an optional parameter of type int name cards with a default value of 10. As part of the constructor body, push the number of cards requested onto your private deck vector.
Youll need an exception named Deck_empty, either inside or outside of class Deck. Remember the isa notation we use for exceptions, e.g., class my_exception : public exception {... Youre allowed to declare a class in the public area of Deck this is called a nested class. So if you nest Deck_empty inside of Deck, which is a Good Idea, it can be accessed from elsewhere using the membership operator as Deck::Deck_empty.
Youll need a deal() method that returns the card from the deck vector with the highest index, and deletes it from the vector. But if the vector is empty, throw a Deck_empty exception instead. Again, print nothing!
+Writing the test_deck regression test:
Youll write a main() function in a file named test_deck.cpp that tests 3 cases:
1. Normative case: Create a deck with the default number of cards, and deal each one into a variable of type Card. If it works, print nothing. If it fails, an exception will be thrown, and you may either catch it and print an error to cerr or allow the test to abort as you prefer.
2. Parameterized Constructor case: Create another deck with MORE than the default number of cards. e.g., 20, and extract them as before. This verifies that the constructor parameter is correct.
3. Failure case: Create a default deck, and deal one more card than the default number of cards that you actually added to the vector. Verify that a Deck_empty exception is thrown; if not, stream an error message to cerr.
+Writing the main application :
The main application just creates 20 random cards and prints them to the screen. This is exactly the same as our test_card.cpp first case, so just move the code from there. Thus, youll be back to having no output at all if your regression tests pass! + Out put (example) : prints out a deck of 20 random cards : T6 A5 U5 T3.........
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
