The content on this page is the work of Professor Glenn Blank.
Be advised that Professor Blank is no longer on the active faculty at Lehigh.
This content continues to be available as a courtesy, but it may not be maintained or current.
Use case analysis and partial class and methods pseudocode for Game of
War
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)