Every game needs code to manage and control the gameplay. This code is what we c
ID: 3834265 • Letter: E
Question
Every game needs code to manage and control the gameplay. This code is what we call the game loop. In this discussion explain what you think should be the key elements and flow of the game loop for our Three Card War game. You may use a bulleted list or pseudo code to show the game loop flow.
-As a start for the game loop:
-Instantiate the two players
-Instantiate the deck Shuffle
-Determine who goes first (random pick)
-Deal three cards to each player
Additional information:
The card game Three Card War is a simple card game for two players. The game uses a 52 card conventional deck. To begin the deck is shuffled and each player
draws three cards. The players may look at their three cards. The remaining cards are placed in a pile facedown between the two players. The players decide who is going
first.
• The player going first selects one of his three cards and places it faces up.
• The opposing player selects one of her three cards and places it faces up.
• If the ranks of both cards are the same (a push), then both players retain their cards and set them aside. Otherwise, the player with the highest-ranking card keeps both cards (again, setting them aside).
• After playing the round, each player draws one card from the deck to replace the card just played with the first playing drawing first.
• The player that won the round is now the first player for the next round unless there was a push then the original first player goes first again.
• The game ends when the deck is exhausted and both players have played all of their three cards.
• The player with the most cards set aside wins. Card ranking begins with the 2 as the lowest card going up to the ten, then jack, queen, king, and ace
Explanation / Answer
Use case: Game of War
1. Shuffle a deck of 52 cards.
2. Deal pile of cards to two players, face down.
3. Both players show top card.
4. Player with higher card puts both cards at bottom of his/her pile.
5. Go to step 3.
Alternative: War
At step 4, both cards have same value.
6. Declare "war".
7. Both players draw three cards face down.
8. Go to step 3.
Alternative: not enough cards for War.
At step 7, a player does not have three cards.
9. Draw all available cards except one (for step 3).
9b. If this player has no cards left, put top card back (for step 3).
10. Go to step 3.
Alternative:
At step 5, player has no more cards.
Other plays wins the game.
Design:
class Card Represents a playing card
int suit 0..3, representing Clubs, Diamonds, Hearts, Spades
int face 0..12, representing 2..10, Jack, Queen, King, Ace
Card(int n) construct one of 52 cards, based in value of n
show() Display the value and suit as text on screen
e.g., value=13, suit=1 displays "Ace of Diamonds"
beats(Card other) true if this Card beats other Card's face value
ties(Card other) true if this Card ties other Card's face value
class Deck Represents a deck of playing cards
Deck() construct array of 52 Cards
shuffle() shuffle deck into random order
deal(Pile,Pile) arrange cards into two Piles
class Pile Arrange a player's cards in a queue
Pile() construct an empty queue
nextCard() get next card from top of Pile
putCard(Card card) add card to bottom of pile
noMoreCards() true if no more cards in pile
size() number of cards in pile
class Game Sets up and plays a game of war
Game() Construct Deck, shuffle deal two piles, and play
play() Play the game until a player wins
Pseudocode:
Card(int n) //construct a Card, given n from 1 to 52
face = n % 13 e.g., 12 is 12, an Ace; 13 is 1, a Deuce
suit = n / 13 e.g., 12 is 0, a Club; 13 is 1, a Diamond
show()
if face is 0 then display "Deuce"
else if face between 1 and 8 then display face+1
else if face is 9 then display "Jack"
else if face is 10 then display "Queen"
else if face is 11 then display "King"
else if face is 12 then display "Ace"
display " of "
if suit is 0 then display "Clubs"
else if suit is 1 then display "Diamonds"
else if suit is 2 then display "Hearts"
else if suit is 3 then display "Spades"
beats(Card other)
if face > other.face then true else false
ties(Card other)
if face == other.face then true else false
Pile() construct an empty pile of Cards
nextCard()
get Card from top of Pile
remove it from Pile
return Card
Card putCard(Card)
put card at the bottom of the pile
boolean noMoreCards() if pile is empty then true else false
Deck()
for each n, 1 to 52, construct Card(n) and insert in deck[n]
shuffle deck
shuffle()
display "Shuffling deck"
for each card in deck, pick another random card and swap them
deal(Pile pile1, Pile pile2)
display "Dealing deck into two piles"
for each card in deck, put every other card in one of two piles
Game()
construct a Deck
deal deck into two piles
start an empty war pile
play until there is a winner
play()
get card from top of each player's pile
show player 1's card " against " player 2's card
put each player's card into war pile
winner = 0
while winner is 0
if player 1's card beats player 2's card then
display player 1's card " wins"
winner = 1
else if player 2's card beats player 1's card then
display player 2's card " wins"
winner = 2
else
display "War!"
if size of player1's pile is at least 4 then
get three cards from player1's pile put them into war pile
else get size-1 card from player 1's pile
Ditto for player 2
if either player has no more cards then
put top card back in that player's pile (so that play can continue)
return true (keep playing)
if winner is 1 then
for each card in war pile
display "player 1 gets " show card
put each card in player's 1 pile
else ditto if winner is 2
if either's player pile is empty
then declare other player the winner
return false (stop playing)
else return true (keep playing)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.