To Home Page

CSC 212 -- Project Proj6 -- 250 Points

Goal

Implement a War card game simulation. The RunWar class will run five complete simulations.

Rules of War

  1. The number of players is two.

  2. Deal cards to each player from a standard 52 card deck to form two 26 card decks, each deck face down on the table in front of each player.

  3. Each player draws the top card from his or her deck. (Use the deal method of Deck to obtain the card.) The card with the higher rank wins. Both cards are placed at the bottom of the winner's deck along with any cards in the "in limbo" deck, described next. (Use the add method of Deck to place a card at the bottom of the deck.)

  4. In case there is a tie for the rank, each player places the card with tied rank, along with three additional cards into the "in limbo" deck. Then they play again.

  5. A player loses if he or she runs out of cards.

Details

The specifications for each method are provided in one of three ways:

  1. a short description.

  2. detailed pseudocode.

  3. complete source code.

Your job is to finish each method definition using the information provided.

Implement and test the following four classes in this order: Deck, ShuffleableDeck, War, RunWar. Use this Card class source code instead of the Card class you wrote for Project Proj3 for compatibility. Here are their UML Diagrams.

Note: ctor means constructor.

  1. The Card class represents a single playing card in a standard deck.

    Here is the source code for the Card class. Use this source code instead of the code you wrote for Project Proj3 for compatibility.

  2. The Deck class that represents a collection of cards sitting on the table. A player can draw a card from the top of the deck, or add a card to the bottom of the deck. A Deck object need not contain all 52 cards. Furthermore there will be more than one Deck object in use during the game.

    Deck has these methods for manipulating the cards during the game:

    public Deck( )

    public int getCount( )

      Return the number of cards in the deck, count. This number could be zero.

    public void add(Card theCard)

      Add theCard to the bottom of the deck. Move up the existing cards to make room for the new card. Update count.

    public void createNew( )

      Populate a Deck object with the standard 52 playing cards.

    public void deal(Deck theOtherDeck)

      Move the top card from the current deck to bottom of the theOtherDeck. Use the draw and add methods.

    public Card draw( )

      Draw a card off of the top of the deck. Return a reference to the card. This card is no longer in the deck after draw is called. Update count.

    public static String toString( )

    public static void main(String[ ] args)

  3. The ShuffleableDeck class is a derived class of Deck. In addition to its inherited Deck, it contains two auxillary decks named aux1 and aux2 to use for cutting and shuffling.

    The ShuffleableDeck methods:

    public ShuffleableDeck( )

    public void cut( )

      Move half of the cards from the inherited deck to aux1. Move the other half of the cards to aux2. Use for loops calling the deal method repeatedly.

      ShuffleableDeck.cut suggested pseudocode.

    public void shuffle( )

      Randomly choose, with probability 0.5, a card from aux1 or aux2. Deal a card from that deck to the inherited deck. deck. Consider the possibility that either the inherited deck or otherDeck will run out of cards before the other.

      ShuffleableDeck.shuffle suggested pseudocode.

    public String toString( )

      Use the toString method of the base class to show the contents of the inherited array, aux1 and aux2.

      ShuffleableDeck.toString source code.

    public static void main(String[ ] args )

  4. A War class that contains the Deck objects necessary for simulating the play during the children's game of War (rules listed above).

    The War methods:

    public War( )

    public void play( )

      Execute one play of the War game.

      War.play suggested pseudocode.

    public boolean gameOver( )

      Return true if either playerA or playerB has no cards.

    public String toString( )

      Return the count for each player and whether the game is over.

      War.toString source code.

    public static void main(String[ ] args)

      Test the War class.

      War.main source code.

  5. The RunWar class runs the War simulation.

    public static void main(String[ ] args)

 

Grading