Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in c programming language 3 . In the card game War, a standard 5 2 - card deck is dealt to two players randomly so
in c programming language
In the card game War, a standard card deck is dealt to two players randomly so that each player ends up with a pile of cards. Let us call these players and One wins the game by taking all of one's opponent's cards. Neither player looks at his or her cards. Instead, player removes the top card of his or her pile and places it faceup in the center of the table. Player does the same with the top card from his or her pile. Whoever flipped over the higher ranked card takes both cards in the center and adds them to the bottom of their packet in the reverse order they were revealed. For the purposes of deciding which card is the higher card, cards are ranked in the normal way with the Ace being the card of highest rank : T J Q K A Suits are ignored. Both players then flip over the next card in their respective piles, again moving them to the center of the table and the process is repeated. This continues until one player wins by taking all of the cards.
When the faceup cards on any given turn are of the same rank, there is a war. When this happens, the initial cards played stay on the table to form a small pile to which the players alternatively add their next three cards facedown. Finally, they each add one more card, faceup These last two cards added faceup are again compared, and the player with the higher card takes all of the cards on the table cards in total now and adds them to the bottom of their pile. Whenever cards are added to the bottom of a player's pile, they are always added in the reverse order as the cards were played specifically, player s first card, player s first card, player s second card, etc. Should this second pair compared result again in a tie, three more facedown cards are played, and then one more faceup with the player whose last card played is higher taking the entire stack of now cards. In the unlikely event that there should still be a tie, this process is repeated until there is no tie or until someone doesn't have enough cards to continue. Should a player not have enough cards to complete their turn, they lose.
You must simulate this game to predict on what turn the game ends and who wins given the initial states of the two players' piles.
Note:
Your program; for the cards that players hold should use queues, for the cards presented on the table should use a stack.
Your program should show the state of each player's pile on each turn, as shown in the sample run below. This will help debugging!
Every time compared cards have the same rank, your program should print WAR! as shown in the sample run below. Note, this means that occasionally when multiple ties occur in the same turn that this message may be printed more than once on the same turn.
Some games might be infinite. As such, your program needs only to identify on which turn the game ends and who wins if the number of turns required to do so is less than Otherwise, declare the game a draw.
Sample run:
War
Enter Player s cards from top to bottom:
h h Jc Ts c h h c s s c d Js Jh d Ah Qh d Ks c Kc Td Jc Th s Kd
Enter Player s cards from top to bottom:
d Qs Jd d Ad Tc Qc h d h c s s c c d Kh s Ac h c Qd As d s s
Turn:
player:
h h Jc Ts c h h c s s
c d Js Jh d Ah Qh d Ks c
Kc Td Jc Th s Kd
player:
d Qs Jd d Ad Tc Qc h d h
c s s c c d Kh s Ac h
c Qd As d s s
Turn:
player:
h Jc Ts c h h c s s c
d Js Jh d Ah Qh d Ks c Kc
Td Jc Th s Kd h d
player:
Qs Jd d Ad Tc Qc h d h c
s s c c d Kh s Ac h c
Qd As d s s
Turn:
player:
Jc Ts c h h c s s c d
Js Jh d Ah Qh d Ks c Kc Td
Jc Th s Kd h d
player:
Jd d Ad Tc Qc h d h c s
s c c d Kh s Ac h c Qd
As d s s h Qs
WAR!
Turn:
player:
c s s c d Js Jh d Ah Qh
d Ks c Kc Td Jc Th s Kd h
d
player:
h d h c s s c c d Kh
s Ac h c Qd As d s s h
Qs Jc Jd Ts d c Ad h Tc h
Qc
WAR!
WAR!
Turn:
player:
Qh d Ks c Kc Td Jc Th s Kd
h d c h s d s h c c
d s Js s Jh c d c Ah d
player:
Kh s Ac h c Qd As d s s
h Qs Jc Jd Ts d c Ad h Tc
h Qc
Turn:
player:
d Ks c Kc Td Jc Th Th s Kd h
d c h s d s h c c d
s Js s Jh c d c Ah
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