Question
we will implement of the card game of War. Implement a Generic Queue using the Floating Front Design Approach Use the Stack class, imported from
we will implement of the card game of War.
Implement a Generic Queue using the Floating Front Design Approach
Use the Stack class, imported from java.util ( use peek() instead of top() )
Define a QueueUnderflow class which extends Exception,
Define a Card class
Define a Deck of Card class using a static array.
Use try/catch blocks to detect Exceptions
Use a recursive method, battle()
Please note the requirements of this lab at the end of this document. Before turning this assignment in, please verify you have fulfilled all of the requirements.
I assume you know how to play the game of war. We will cover the basic rules in class and you can reference the internet for additional information.
For this lab, please create a new Java Project called War. Then create a staticArray package where we will implement the Queue class and the QueueUnderflow Exception class.
In the staticArray package
QueueUnderflowException
This class should extend the Exception class. When you create the class in Eclipse, specify Exception as the super class and select Constructors from superclass under the Which method stubs would you like to create. Otherwise, create 2 constructors, one default (without any parameters) and another with 1 parameter (which would be a String). Both constructors will then call their corresponding super classs constructor ( super () ).
QueueInterface (with the following method headers defined:)
void enqueue ( T element );
T dequeue ( ) throws QueueUnderflowException;
boolean isEmpty ( );
String toString();
int size();
Queue class implements the QueueInterface
Implement the Queue with a static array and the Floating Front Design Approach described on page 225 in your text. Your class will have the following instance variables:
private T [ ] queue;
private int front = 0;
private int rear;
private int numElements = 0;
Implement the following methods (most specified in QueueInterface):
public Queue()
call the 1 argument constructor with the default size of 10
public Queue( int size )
create the queue with the specified size.
set rear to size 1
public void enqueue ( T element )
add the element on to the end of the queue. If the array is full, expand the array. Increment numElements. Note, enqueue pre-increments rear with a check for wrap around.
public T dequeue ( ) throws QueueUnderflowException
remove the element from the front of the queue. If the queue is empty, throw the QueueUnderflowException. Decrement numElements. Note, dequeue post-increments front with a check for wrap around.
public boolean isEmpty ( )
return true if numElements == 0.
public String toString()
build a string by traversing the queue, from front to rear. You will need this for debugging purposes.
public int size()
returns the numElements in the Queue.
I suggest you create a main() method in Queue to verify the operation of your Queue before developing your application.
In the default package
Card class Cards in a standard deck can be represented with an integer ranging from 0 to 51. There are 4 suites: Hearts, Spades, Clubs, and Diamonds and 13 different cards/ranks per suit: 2-10, J, Q, K, and Ace. Given an integer from 0-51, divide by 13 to get the suit and take the modulus (remainder) of 13 to get the rank (2-10, J, Q, K, and Ace), where 0 is a 2 and 12 is an Ace. You class will include: array of Strings for the suits: {"H", "S", "C", "D"} array of Strings for the ranks: {2, 3, .... 10, J, Q, K, A} 1 argument constructor which will take an integer within the range 0-51 inclusive. An accessor/observer to get the rank of the Card A toString() method to display the card. Use the following formatting method: String str = String.format("%1s-%2s", suits[suitIndex], ranks[rankIndex]); Deck class Use a static array and a next index to store 52 Card objects. Your constructor will initialize your deck by substantiating each Card object and storing the reference into the array. The next index will be used as the next card to be dealt from the Deck. Your class will include the following methods:
public void shuffle() traverse the array and swap each location with another random index. Note, we are only swapping cards already in the deck. When complete, there should never be duplicates of the same card.
public Card dealCard() throws Exception this method will return the next Card referenced by the next index. The next index should then be incremented.
public boolean hasNext() this method returns true if there exists another card in the deck.
public String toString() this method should traverse (whats left of) the deck, calling the Cards toString() method and concatenate the results. A new line, , should be added every 10 entries. Note, if you concatenate a reference to a String, it calls the Cards toString(). This method should be used for debugging purposes.
War class Define the following static variables: static Deck deck; static Queue player1; static Queue player2; static Stack player1stack; static Stack player2stack; static boolean gameOver; static int numIterations=0; static int maxIterations=0; static int totalIterations = 0; static int numTies=0; static int maxTies=0; static int totalTies = 0; Note, youll import the Queue class from the staticArray package and youll import the Stack class from java.util. In main(), create the Deck, the 2 Queues, and the 2 Stacks. Shuffle the Deck and display the contents before and after the shuffle so that you can verify we are starting with a valid deck. You will then deal out all the cards from the Deck, queuing them in an alternating manner to each players Queue (player1 and player2). Then use the following while loop: while (!gameOver) { battle(); if (debug) { System.out.println("pl: " + player1); System.out.println("p2: " + player2); System.out.println("hit return"); String str = input.nextLine(); } } The method, battle(), will set the boolean gameOver when one of the players runs out of cards. In the method battle(), dequeue a Card from each players queue and push them on to their corresponding stacks. Then compare the cards rank. If player 1 wins, pop all Cards off both stacks and enqueue them onto player 1s queue. If player 2 wins, pop all Cards off both stacks and enqueue them onto player 2s queue. If they tie, then push 2 additional cards onto each players stack and recursively call battle again. The method battle() should use a try/catch block to detect if a player runs out of cards and set the static boolean variable, gameOver, accordingly. As mentioned above, when a player wins in battle(), you need to move the Cards from both Stacks to the winning players Queue. I suggest you randomly select which Stack to take from first. When I ran this program 10,000 times, I ran into a situation where I went into an endless loop because the cards get into a certain order and it just repeated. When I randomized the order of how they are put back onto the winners Queue, I did not run into the problem. The variable numIterations tracks the number of calls to battle(). Once you get the program running, add a for loop at the beginning of main() and repeat 10000 times. Note, youll also need to reinitialize some variables. Determine the max number of iterations and the average number of iterations. You can also add an additional counter when you detect a tie in battle. Compare them with my results below. Note, comment out all print statements when you run this test. When I ran it 10,000 times, it took less than 20 seconds. Requirements of this lab: Card class Deck class Generic Queue using the Floating Point Design Approach described on page 225. A queue for each player, holding their Cards. A stack for each player, used in battle(). Output detailing the max number of iterations and the average number of iterations for 10,000 games played. In my implementation, I saw the following results with 10,000 games: Iterations: max=2664, avg=349 Ties: max=146, avg = 20 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started