Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you help solve this? I have provided both exercises. Find and open the file War.java in the repository. The main method contains all the

Can you help solve this? I have provided both exercises.

Find and open the file 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 first.

/** * 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!"); } }

}

Exercise 13.2

import java.util.Arrays; import java.util.Random;

public class Deck { private Card[] cards; // Random Number Generator private static Random rand = new Random();

public Deck() { this.cards = new Card[52]; int index = 0; for (int suit = 0; suit <= 3; suit++) { for (int rank = 1; rank <= 13; rank++) { this.cards[index] = new Card(rank, suit); index++; } }

}

public Deck(int n) { this.cards = new Card[n]; }

public Card[] getCards() { return this.cards; }

public void print() { for (Card card : this.cards) { System.out.println(card); } }

public String toString() { return Arrays.toString(this.cards); }

public void shuffle() { // shuffle the decks // loop over n-1 elements for (int i = 0; i < cards.length - 1; i++) { // get a random j such than i <= j < n and swap int j = randomInt(i, cards.length); // swap cards swapCards(i, j); } }

private static int randomInt(int low, int high) { // return a random number p where low <= p < high // by adding low and getting a number between high - low return low + rand.nextInt(high - low); }

private void swapCards(int i, int j) {

// swap Card temp = cards[i]; cards[i] = cards[j]; cards[j] = temp; }

public void selectionSort() { }

private int indexLowest(int low, int high) { return 0; }

public Deck subdeck(int low, int high) { Deck sub = new Deck(high - low + 1); for (int i = 0; i < sub.cards.length; i++) { sub.cards[i] = this.cards[low + i]; } return sub; }

private static Deck merge(Deck d1, Deck d2) { return null; }

public Deck almostMergeSort() { return this; }

public Deck mergeSort() { return this; }

public void insertionSort() { } }

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

Contemporary Issues In Database Design And Information Systems Development

Authors: Keng Siau

1st Edition

1599042894, 978-1599042893

More Books

Students also viewed these Databases questions