You are tasked to make a game of WAR! War is a card game played between two people. Each person gets half of a shuffled deck of cards. They both place the top card of their deck onto the field and the winner is the one whose card has the highest rank (Ace is counted as the highest card).

If both players place two cards down of the same rank the players go to war. The players again both draw a card and place it on the field. The winner of this bout wins all the cards on the field. If they both play the same rank again, you go to war again and keep drawing cards and comparing until one is clearly a winner.  (In your program, you can make it so they can only go to war once. If they draw a second time you can return the players cards back to their original decks. )

As a player wins cards, he adds these cards to the bottom of his deck. You do not shuffle these cards that were won. A player wins when the other player has less than 10 cards in their deck.

This program needs to be coded from a grade 12 perspective. In other words, your program needs to include classes, objects and data structures.


You program should have three classes:

Deck Class

  • This class should create a data structure instance variable that supports a deck of 52 cards.
  • It should also have a method that populates this data structure with all 52 unique cards.
    • You should call this method on the creation of the deck (in the constructor!)
  • It should have a draw and remove card method. You can use Java’s built in add(), remove(), pop(), push() etc.. methods inside the code of your methods.
  • You can use the Collections class shuffle method to shuffle this deck

playerDeck Class

  • This class should create a very specific type of data structure instance variable for the potential 26 cards the player’s deck can have.
  • It should have methods that handle drawing cards, inserting won cards, removing lost cards, detecting if the deck is empty, detecting if the player is below the minimum deck size (10 cards!).
    • For your insertion and removal methods, you can use Java’s built in add(), remove(), pop(), push() etc.. methods inside the code of your methods.

Card Class

  • A typical card class similar to the ones we have made before in class
  • ach card has to have a rank and suit.
  • Have the rank eb an integer to simplify the code
  • Have a separate method that converts the rank number to strings for the jack, queen, king and ace
  • There should be a custom toString method

All classes should have mutators, accessors and at least one constructor.

Main Program:

The main program should utilize the previous classes and run the general flow of the game. You will need several methods to run your game: You will need a method that initializes the two 26 card playerDeck objects with the cards from the 52 card deck object. You will also need a method that runs the general flow of the game but also a method to handle when the players go to war during a draw.

The main program can be done many different ways so you will be assessed on how efficient and neat the main program is.

Post-Coding UML diagram

You need to include a UML diagram for your program that includes all three classes (you do not need to include the Main class). It should show all the instance variables, instance methods and the correct types of associations between the classes.

jwenting commented: good luck, sounds like an interesting project for you. Better than the usual homework assignment that gets posted here -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.