Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Exercise 13.5 Find and open the le War.java in the repository. The main method contains all the code from the last section of this

JAVA

Exercise 13.5 Find and open the le War.java in the repository. The main method contains all the code from the last section of this chapter. Check that you can compile and run this code before proceeding.

The program is incomplete; it does not handle the case when two cards have the same rank. Finish implementing the main method, beginning at the line that says: // it's a tie.

When there's a tie, draw three cards from each pile and store them in a collection, along with the original two. Then draw one more card from each pile and compare them. Whoever wins the tie takes all ten of these cards. If one pile does not have at least four cards, the game ends immediately. If a tie ends with a tie, draw three more cards, and so on.

Notice that this program depends on Deck.shuffle so you might have to do Exercise 13.2 rst.

Code follows:

/** * Simulates a simple card game. */ public class War {

public static void main(String[] args) {

int gameTurns = 0; // variable for tracking game interactions. // tie breaker variables // create the arraylist Pile for tie breaker // // create and shuffle the deck Deck deck = new Deck(); deck.shuffle(); System.out.println (deck.toString());

// divide the deck into piles Pile p1 = new Pile(); p1.addDeck(deck.subdeck(0, 25)); Pile p2 = new Pile(); p2.addDeck(deck.subdeck(26, 51)); System.out.println ("Pile 1 >> " + p1.toString()); System.out.println ("Pile 2 >> " + p2.toString());

// while both piles are not empty while ((p1.size() > 0 && p2.size() > 0) && gameTurns < 5000) { gameTurns++; // increment the number of 'hands' Card c1 = p1.popCard(); Card c2 = p2.popCard();

// compare the cards int diff = c1.getRank() - c2.getRank(); if (diff > 0) { p1.addCard(c1); p1.addCard(c2); } else if (diff < 0) { p2.addCard(c1); p2.addCard(c2); } else { // Player One always wins tie by default // this must be replaced by tie breaker algorithm p1.addCard(c1); p1.addCard(c2); if (p2.size() > 0) { p1.addCard(p2.popCard()); } // cards tie, following is 'new' tie breaker logic // Replace default with 13.5 tie breaker algorithm /* * Following are psedudocode statements suggesting actions required by the exercise * After the comment/statement suggest placement of Java statements * you develop to satisfy the requirement. * * If either hand is less than four cards, other player wins. * * * move current cards to tieBreak Pile * * * draw three cards from each hand for tieBreak pile, now contains eight cards * * * pop the two cards from player hands and compare for tie breake * * * if a second tie, simulate a coin flip with random number * * * determine winner of the tie breaker, then * winner (hand) gets the cards in tieBreak pile * * */ } } // end game while loop System.out.printf ("Number of turns = %d%n", gameTurns); // Draw game after 5000 turns, no winner. Output current game status //

// display the winner if (p1.size() > 0) { System.out.println("Player 1 wins!"); } else { System.out.println("Player 2 wins!"); } }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Databases On The Web Learn To Create Web Pages That Interface With Database Engines

Authors: Robert Papaj, Donald Burleson

11th Edition

1576100995, 978-1576100998

More Books

Students also viewed these Databases questions

Question

=+such as the dirigenti in Italy?

Answered: 1 week ago

Question

=+ Are there closed-shop requirements or practices?

Answered: 1 week ago