The card game “War” is played by the following rules (you may have learned different rules. Use these): - Each player gets half of the deck of cards (in our case, a set of random integers with value 1-13) - The game itself is played in a series of “rounds”. In each round: o Both players draw a card, putting it into the “reward pile” (and removing it from their hand). o If one player’s card is higher than another, they gain all cards in the reward pile (and add it to their hand) o If there is a tie, both players add an additional card to the reward pile (which is not compared - I call this a “penalty card” in my sample run below), and then we begin a new round with a larger reward pile. The next round’s winner will win the entire reward pile (though it is possible to have multiple consecutive ties, leading to a much larger reward pile) - The game ends when a player needs to draw a card and cannot. That player loses. Your task is to implement this game with the player’s hands and the reward pile as bags of integers. You should allow the user to specify the size of the initial hands (I used 5 card hands to test my program, since half of an official card deck is 26 cards, and simulating 26 card decks runs for a long time). Each round, you should display: - The current round - The values of each player’s hands (by outputting the values of the bag’s to_vector function) - The cads drawn in the current round, and who won. Of course, you will have to finish the Bag lab to be able to do this. Your main program should only use Bags to manage the cards. (So no vectors, strings, or anything like that). You should implement the game using the Bag member functions. My only exception was that I used the “to_vector” Bag function to get the cards in a hand so I could write a Print_Vector function. But generally: - Cards go into the hand and reward pile using insert - Specific cards leave the hand and reward pile using remove - The card that gets played from the hand gets selected using (the random version of) choose
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
The card game “War” is played by the following rules (you may have learned different rules. Use these):
- Each player gets half of the deck of cards (in our case, a set of random integers with value 1-13)
- The game itself is played in a series of “rounds”. In each round: o Both players draw a card, putting it into the “reward pile” (and removing it from their hand). o If one player’s card is higher than another, they gain all cards in the reward pile (and add it to their hand) o If there is a tie, both players add an additional card to the reward pile (which is not compared
- I call this a “penalty card” in my sample run below), and then we begin a new round with a larger reward pile. The next round’s winner will win the entire reward pile (though it is possible to have multiple consecutive ties, leading to a much larger reward pile)
- The game ends when a player needs to draw a card and cannot. That player loses.
Your task is to implement this game with the player’s hands and the reward pile as bags of integers. You should allow the user to specify the size of the initial hands (I used 5 card hands to test my program, since half of an official card deck is 26 cards, and simulating 26 card decks runs for a long time).
Each round, you should display: - The current round - The values of each player’s hands (by outputting the values of the bag’s to_vector function)
- The cads drawn in the current round, and who won. Of course, you will have to finish the Bag lab to be able to do this. Your main program should only use Bags to manage the cards. (So no
- Cards go into the hand and reward pile using insert
- Specific cards leave the hand and reward pile using remove
- The card that gets played from the hand gets selected using (the random version of) choose
Trending now
This is a popular solution!
Step by step
Solved in 2 steps