Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3 . In the card game War, a standard 5 2 - card deck is dealt to two players randomly so that each player ends

3. In the card game War, a standard 52-card deck is dealt to two players randomly so that each player ends up with a pile of 26 cards. Let us call these players 1 and 2. One wins the game by taking all of one's opponent's cards. Neither player looks at his or her cards. Instead, player 1 removes the top card of his or her pile and places it face-up in the center of the table. Player 2 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) : 2,3,4,5,6,7,8,9, 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 face-up 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 face-down. Finally, they each add one more card, face-up. These last two cards added face-up are again compared, and the player with the higher card takes all of the cards on the table (10 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 1's first card, player 2's first card, player 1's second card, etc. Should this second pair compared result again in a tie, three more face-down cards are played, and then one more face-up, with the player whose last card played is higher taking the entire stack of now 18 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 5000. Otherwise, declare the game a draw.
Sample run:
War
Enter Player 1's cards (from top to bottom):
6h 3h Jc Ts 3c 7h 2h 9c 3s 7s 8c 5d Js Jh 6d Ah Qh 9d Ks 6c Kc Td Jc Th 9s Kd
Enter Player 2's cards (from top to bottom):
3d Qs Jd 8d Ad Tc Qc 9h 2d 5h 5c 5s 6s 7c 4c 7d Kh 8s Ac 4h 2c Qd As 4d 4s 2s
Turn: 1
player1:
6h 3h Jc Ts 3c 7h 2h 9c 3s 7s
8c 5d Js Jh 6d Ah Qh 9d Ks 6c
Kc Td Jc Th 9s Kd
player2:
3d Qs Jd 8d Ad Tc Qc 9h 2d 5h
5c 5s 6s 7c 4c 7d Kh 8s Ac 4h
2c Qd As 4d 4s 2s
--------------------------------------
Turn: 2
player1:
3h Jc Ts 3c 7h 2h 9c 3s 7s 8c
5d Js Jh 6d Ah Qh 9d Ks 6c Kc
Td Jc Th 9s Kd 6h 3d
player2:
Qs Jd 8d Ad Tc Qc 9h 2d 5h 5c
5s 6s 7c 4c 7d Kh 8s Ac 4h 2c
Qd As 4d 4s 2s
--------------------------------------
Turn: 3
player1:
Jc Ts 3c 7h 2h 9c 3s 7s 8c 5d
Js Jh 6d Ah Qh 9d Ks 6c Kc Td
Jc Th 9s Kd 6h 3d
player2:
Jd 8d Ad Tc Qc 9h 2d 5h 5c 5s
6s 7c 4c 7d Kh 8s Ac 4h 2c Qd
As 4d 4s 2s 3h Qs
--------------------------------------
*** WAR! ***
Turn: 4
player1:
9c 3s 7s 8c 5d Js Jh 6d Ah Qh
9d Ks 6c Kc Td Jc Th 9s Kd 6h
3d
player2:
9h 2d 5h 5c 5s 6s 7c 4c 7d Kh
8s Ac 4h 2c Qd As 4d 4s 2s 3h
Qs Jc Jd Ts 8d 3c Ad 7h Tc 2h
Qc
--------------------------------------
*** WAR! ***
*** WAR! ***
Turn: 5
player1:
Qh 9d Ks 6c Kc Td Jc Th 9s Kd
6h 3d 9c 9h 3s 2d 7s 5h 8c 5c
5d 5s Js 6s Jh 7c 6d 4c Ah 7d
player2:
Kh 8s Ac 4h 2c Qd As 4d 4s 2s
3h Qs Jc Jd Ts 8d 3c Ad 7h Tc
2h Qc
--------------------------------------
Turn: 6
player1:
9d Ks 6c Kc Td Jc Th Th 9s Kd 6h
3d 9c 9h 3s 2d 7s 5h 8c 5c 5d
5s Js 6s Jh 7c 6d 4c Ah

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

6th International Edition

061921323X, 978-0619213237

More Books

Students also viewed these Databases questions