spacer.gif .spacer.gif

Purpose

In this homework, you will gain more experience using loops by implementing a simplified version of the card game, Peace. 

If you are unfamiliar with Peace you may want to read Wikipedia's Peace page.  We also provide a short introduction to the game below.

Note that this game is normally called "War".  But in an effort not to get into any trouble, we decided to rename it to "Peace". 

Background -- Cards and Peace

A standard deck of cards has 52 cards, the cards are split evenly into four suits: spades (♠), hearts (), diamonds () and clubs (♣).  Note that two of the suits are colored red (hearts and diamonds) and two are colored black (spades and clubs).  In this assignment, we will be abbreviating the suits by their first letter: "S", "H", "D", and "C", respectively.

Each suit has 13 "face cards", or rank values 1 through 13.  1 is called an Ace, 11 is called a Jack, 12 is a Queen, and 13 is a King.

Peace is a card game where there are two players (the user and the computer), and the computer is the dealer. The goal of the game is to win as many rounds of cards (or tricks) as possible. For the purposes of our implementation, there will be the following modifications to the rules of Peace:

  1. Cards will be ordered by their rank AND their suit. In other words, there is a strict value ordering of the cards and tied values between cards played are not possible. Suits are ordered as follows (highest to lowest): Spades, Hearts, Diamonds, Clubs
  2. A game consists of playing all the cards in each players' hand.
  3. The player with the most cards at the end of the game is the winner, or if both players have the same number of cards, the game ends in a tie.

Game Play

  1. At the beginning of each game, the dealer shuffles the deck.
  2. The dealer then deals one card from the top of the deck to each player in sequence, until each player has 26 cards.
  3. Tricks (or rounds) are played until there are no more cards in either player's hand.  Each player plays the top card from their hand.  The cards are compared.  The player who played the higher valued card wins the trick and adds the cards from the trick to their win pile, which is separate from their hand.
  4. The winner or a tie is announced.  The winner is the player with the win pile with the most cards.  If both win piles are equal in size, then the game is a tie.

Design

Not surprisingly, there will need to be a number of loops in this homework -- many of the steps below require one or more loops.  The game must allow the user to play multiple hands, so there is an outer loop as well. 

Within the program, a number of things must happen:

  • Create the deck.  The deck is just a Vector that contains 52 Card objects (see below for more details).
  • Shuffle the deck (also see below)
  • Deal cards to the computer and the player. 
  • Play the game
  • Figure out who won.
  • Ask to play again.  If the player is playing again, it's easiest to fully re-create the deck each time.

Your code must be in a public class named Peace, in a file named Peace.java. 

Note that you should only ask for input (via the YesNoExtractor class) for one thing: asking if s/he wants to play another round.

The good programming practices from HW 1 need to be in this homework as well.  However, you do not have to echo input that was read via the askUser() method in the YesNoExtractor class.

Class Card

To assist you a class Card has implemented.  Right click on that link, select "save as", and save it to the SAME directory that your Peace.java file is in.  The class has the following public methods.

  • Card(int i)
    • If i is in the inclusive interval 1 ... 52 then a card is configured in the following manner
      • If 1 <= i <= 13 then the card is a club
      • If 14 <= i <= 26 then the card is a diamond
      • If 27 <= i <= 39 then the card is a heart
      • If 40 <= i <= 52 then the card is a spade
      • If (i % 13) +1 is 1 then the card is an Ace
      • If (i % 13) +1 is 2, then the card is a 2
      • ...
      • If (i % 13) +1 is 10, then the card is a 10
      • If (i % 13) +1 is 11, then the card is a Jack
      • If (i % 13) +1 is 12, then the card is a Queen
      • If (i % 13) +1 is 13, then the card is a King
  • int compare(Card b)
    • Returns -1 if the calling Card is less than Card b
    • Returns 1 if the calling Card is greater than Card b.
    • This method compares the values of the cards (i.e. 1-13), and considers Aces as high
    • Note:  If cards are of equal face value, the tie will be broken by comparing the suit.  Thus, no cards may be of equal value for this Card class.
  • String getFace()
    • Returns the face of the card as a String.  For example, an Ace returns an "A", a 2 a "2", an King a "K", etc.
  • String getSuit()
    • Returns the suit of the card as a String ("S", "D", "C", or "H").
  • int getValue()
    • Returns the value of the card (an Ace is 1, Jack is 11, Queen is 12, and King is 13).
  • String toString()
    • Returns a String version of this card -- useful for printing.

Class YesNoExtractor

The YesNoExtractor class should be used to get user input, and supports the following methods.  Right click on that link, select "save as", and save it to the SAME directory that your Peace.java file is in.  This is the same class used in HW 4.

  • public YesNoExtractor()

    • The default constructor configures the new YesNoExtractor so that it displays messages to standard output and extracts information from standard input; that is the YesNoExtractor reads input from the keyboard (System.in) and writes to the screen (System.out).
       

  • public boolean askUser(String message)

    • Causes the YesNoExtractor to query the user for a yes or no response after printing the message. If the user indicates yes, then true is returned. If the user indicates no, then false is returned. If the user indicates something else, then the question is reissued until a proper reply is given.

    • Note that you can also enter "q" or "quit", which will immediately exit the program (this is new since HW 4).

Hints

There are a few parts of this homework that will be a bit challenging, and some of them are addressed here.

  1. The easiest way to keep your "deck" of cards is in a Vector (if you forgot about Vectors, see HW 2).  Given an int variable i, you can create a Card object via new Card(i) -- then add that to your Vector.  To shuffle your deck, you can use the shuffle() method in the Collections class.  Assuming your Vector is named deck, to shuffle it you would use the following statement.

    Collections.shuffle(deck);

    To verify that the deck is shuffled, you can use System.out.println (deck);.  Note that this and other similar debugging statements should NOT execute in your final version of the code (leaving them in and commenting them out might be a good approach here).

    Then you can pick cards right out of the top of the deck (meaning the front of the Vector).  Recall that when you remove something from a Vector, you need to cast it back to whatever class it is (in HW 2, it was a String; here, it's a Card).  Thus, to get the first card from the deck (and also remove it from the deck), you use (Card) deck.remove(0), assuming deck is your Vector.  Also recall that you will get some compiler Warnings ("uses unchecked or unsafe operations") -- you can safely ignore these.
     

  2. Dealer and player hands and win piles.  You will have to keep track of all the cards that were dealt to the player's and the dealer's hands as well as the player's and dealer's respective win piles.  These can be stored using Vectors just as for the deck.
     
  3. Where to start: Start by implementing each step in succession, and testing those steps out.  You can put in extra print statements to do such. (Remember to comment out the extra print statements before submitting your assignment.) For example, after you write the code to create and shuffle the deck, print out the deck and run it a few times to make sure it works.  The big loop that allows the player to play multiple hands can be added in at the end.

Sample Execution Run

The following is some sample output. Note that your output doesn't have to precisely match ours, but the general idea should still be there.

Let's play some Peace!

Trick number: 1
Player played: QC
Dealer played: JS
Player's card wins.

Trick number: 2
Player played: AD
Dealer played: 6D
Player's card wins.

Trick number: 3
Player played: 2H
Dealer played: AC
Dealer's card wins.

Trick number: 4
Player played: 5H
Dealer played: 6H
Dealer's card wins.

Trick number: 5
Player played: 3D
Dealer played: 4C
Dealer's card wins.

Trick number: 6
Player played: JH
Dealer played: 7H
Player's card wins.

Trick number: 7
Player played: KH
Dealer played: 3S
Player's card wins.

Trick number: 8
Player played: 2C
Dealer played: 8D
Dealer's card wins.

Trick number: 9
Player played: 8H
Dealer played: 5C
Player's card wins.

Trick number: 10
Player played: 10C
Dealer played: AS
Dealer's card wins.

Trick number: 11
Player played: JD
Dealer played: QD
Dealer's card wins.

Trick number: 12
Player played: KD
Dealer played: 9H
Player's card wins.

Trick number: 13
Player played: 9D
Dealer played: QS
Dealer's card wins.

Trick number: 14
Player played: 4D
Dealer played: 10H
Dealer's card wins.

Trick number: 15
Player played: KS
Dealer played: 2S
Player's card wins.

Trick number: 16
Player played: 8C
Dealer played: 3H
Player's card wins.

Trick number: 17
Player played: 6S
Dealer played: 9C
Dealer's card wins.

Trick number: 18
Player played: JC
Dealer played: 5S
Player's card wins.

Trick number: 19
Player played: 7D
Dealer played: 4S
Player's card wins.

Trick number: 20
Player played: 9S
Dealer played: 8S
Player's card wins.

Trick number: 21
Player played: AH
Dealer played: 3C
Player's card wins.

Trick number: 22
Player played: KC
Dealer played: 7S
Player's card wins.

Trick number: 23
Player played: 6C
Dealer played: 2D
Player's card wins.

Trick number: 24
Player played: 5D
Dealer played: 10S
Dealer's card wins.

Trick number: 25
Player played: 10D
Dealer played: 7C
Player's card wins.

Trick number: 26
Player played: 4H
Dealer played: QH
Dealer's card wins.

Game is over, let's tally who won.
Player's win pile has 30 cards.
Dealer's win pile has 22 cards.
You win!

Play another round? (y/n)
spacer.gif
spacer.gif footer-middle.gif spacer.gif