Question
C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data type for a single playing card. A playing card
C++
Purpose: To practice arrays of records, and stepwise program development.
1. Define a record data type for a single playing card. A playing card has a suit ('H','D','S',or 'C'), and a value (1 through 13). Your record data type should have two fields: a suit field of type char, and a value field of type int. 2. In main(), declare an array with space for 52 records, representing a deck of playing cards. 3. Define a function called initialize() to initialize a deck of cards. It will take an array as a parameter, but will return nothing. You need to store the 13 cards of each of the four suits ('H','D','S',or 'C') in this array, but the order of the cards is not too important. In main(), call your function, giving it your array as input. 4. Stop here, to build and run your program. You may not know whether it is working yet, but it should build and run before going on. 5. Define a function called displaySingleCard() to display a single playing card. Your function should take a record, and display the value and the suit, as follows: The values 2-10 are displayed as numbers. The value 1 should be displayed as A (the "ace"), and the values 11,12,13 should be displayed as J, Q, K ("jack", "queen", "king"), respectively. Display the value first, and then the suit. Here are some examples: 5D AC 10H JH QS. Don't use endl in your function here. 6. Define a function called displayCards() to display an array of playing cards (it should call the previous function, displaySingleCard() repeatedly). Your function should take an array and its size as parameters, and it should display all the cards in the array on a single line in the console. Note that this function can be used to display a full deck (52 cards), or the hand of a player (see part 6 below). You should display the value and the suit as in the following example of a 5 card hand: 5D AC 10H JH QS 7. Demonstrate and test your program so far by calling your function displayCards() in main() on the entire deck. Make sure to check the console to see that you have every card of every suit! 8. Define a function to randomly shuffle the cards in the deck. One way to randomly shuffle the elements of an array is to repeatedly generate two random offsets for the array, and swap the two elements at those offsets (you can use the expression (rand() % 52) to create a random offset into a full deck of cards). If this random swapping is done a lot of times (a few hundred for a deck of 52 cards), then you will have a fairly random ordering of the cards at the end. 9. Demonstrate and test your program by calling your shuffle function in main() on the entire deck, and using your display function in main() to show the shuffled deck. Make sure to check the console to be sure that you still have every card (and that nothing got lost in the shuffle). 10. Declare arrays to store a "hand" of cards for 4 players, assuming each player receives 5 cards. To store each player's hand, you could have a single one-dimensional array for each player that contains the cards in their hand; or you could use a two-dimensional array that has an array of cards for each player. 11. In main(), "deal out" (i.e., copy) the cards from your shuffled deck to each player's hand, and display the 4 players' hands to the console as if at the start of a game. You should use your function from part 4 above to do most of the display work. For example, you might display this: Player 1: 5D 10H QS 8C JH Player 2: JC 2H 8D AS 9C Player 3: 7H 6H 9H 10S 8H Player 4: QD JD QC KH JC 12. Write a function that will find the best card in a player's hand. The best card is the card with the highest value. For this question, assume that aces are "low", and assume that the suit of a card is irrelevant. You just want to find the highest value. Use your function in main() so that you can display the best card from each player, e.g., Player 1's best card: QS Player 2's best card: JC Player 3's best card: 10S Player 4's best card: KH If there is a tie, it doesn't matter which of the equal-valued cards you display.
What to hand in: 1. Your program in a text file called a8q1.cpp. 2. A text file called a8q1_testing.tex (.rtf or .doc are okay too) demonstrating that your program works, showing the result of tasks 11 and 12 above.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started