War Card Game
Dynamic Data Structures 1
Note: There is absolutely no requirement to create a GUI for this assignment. If you are interested in doing so, however, come see me first so we can discuss whether or not you have time.
Come see me before beginning so you can see a sample run of the code and to discuss possible additions to the requirements.
Your goal is to create a War Card Game using a Queue. Recall that Queues use the first in, first out thinking. For this assignment, this means that cards come off the top of one hand and then are continuously placed back on the bottom of the winner’s hand.
Use the following code example to set up your Queues:
- just put everything in main
- add the type of data to the initialization line so that it looks more like this:
Queue <String> queue_name = new LinkedList().
String is the data type that your queue will hold. For this assignment, your queue will be holding instances of your card class so it should look like: Queue <Card> queue_name = new LinkedList()
Never played War? Here is a link to the rules. I would be happy to show you too.
You are going to create 2 classes:
- Card (instance class)
- WarGameMain (main class)
The Card class should:
- Hold only 2 things: a suit and a number (using 11 through 14 to represent jack through ace).
- Make these two variables PRIVATE
- You will need to make a simple constructor method that takes in these two things as parameters
- Write getSuit and getNumber methods
The WarGame class will take care of the following:
- Create two queues to store each hand.
- (using the ‘offer’ command) each queue 13 cards Initialize two full suits of Cards. Yes this means calling 26 ‘new’ statements. Feel free to copy and paste code AND you may want to combine this with the following instruction.
- Make sure you have a couple of pairs that will line up in the first run through the hands. This is necessary for marking and testing purposes to be sure your War code works.
- Create a War game such as the following:
- Between each hand, the game waits for you to press enter. (This can just be an empty/unused numscan.nextLine() if you wish.) OR you can pause the game for a half second between each flip of the cards. See the code example at the bottom of this document For testing, you may want to comment out this line so that it just loops automatically.
- Takes a card off the top of each stack (using the ‘remove’ command) and places each into temporary Card instances
- Prints each card (you could write a seperate printCard() method for this) and then compares the cards
- ‘Offers’ the two cards into the winner’s queue
- Print the number of cards in each player’s hand. This is for debugging purposes and you will find it VERY handy.
- If there is a tie, a war is declared. Temporary Card instances are needed to hold the tied cards and the next two cards in each hand. The fourth cards are then compared.
- What happens if they are still tied? You have two options:
- Simpler Option: Call it a draw and return all the cards to the original owners hands. It is optional if you want to worry about maintaining the original order.
- More difficult option: figure out how to handle another war.
- when someone runs out of cards, even if it is during a war, the other person wins. You may want to use an if statement to see if the queue is empty. Note that there is a boolean method for queues called isEmpty(). Note that this may prove harder than it sounds (especially if it happens during a war) and is hence only worth 10%. If you can not figure it out, the program will just die with an exception/error when someone runs out of cards.
Entirely Optional Challenge
- The above requirements ask you to arbitrarily set up the 2 hands. If you are up to the challenge, create a ShuffleandDeal method. This would likely start with one ArrayList of all the cards in the deck and then randomly split them up into two hands. If you do this, you are free to make the lists GLOBAL rather than have to worry about parameters and return values.
Evaluation
Setting up Card class and queues 40%
isHigher() method 10%
Game Play 30%
Determining the winner of the game 10%
Good Coding and Commenting 10%
Want to pause between flips of the cards?
I have had a few students ask how to make the program pause for a few seconds before moving on. Want to know how to make your program pause? Try this:
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
The 1000 is milliseconds so 2000 is a 2 second pause.