Question: Help! I cant need help on my war class Heres my Card class: Here is my Deck Class: And Heres the class I need help

Help! I cant need help on my war class

Heres my Card class:

Help! I cant need help on my war class Heres my Card

Here is my Deck Class:

class: Here is my Deck Class: And Heres the class I need

help on(I commented where): public class Card { private String suit; private

And Heres the class I need help on(I commented where):

int rank; public Card(String suit, int rank){ this.suit = suit; this.rank =

rank; } public boolean equals(Card otherCard){ return rank == otherCard.rank; } public

public class Card { private String suit; private int rank;

public Card(String suit, int rank){ this.suit = suit; this.rank = rank; }

public boolean equals(Card otherCard){ return rank == otherCard.rank; }

public int compareTo(Card otherCard){ return rank - otherCard.rank; }

public int getRank(){ return rank; }

public String getSuit(){ return suit; }

public String toString(){ return rankToString() + " of " + suit; }

public String rankToString(){ if (rank == 14) return "Ace"; else if (rank == 11) return "Jack"; else if (rank == 12) return "Queen"; else if (rank == 13) return "King"; else return "" + rank; } }

import java.util.ArrayList; import java.util.Random;

public class Deck{

public static final int MAX_SIZE = 52;

private ArrayList cards;

public Deck(){ reset(); }

public void reset(){ cards = new ArrayList(); addSuit("spades"); addSuit("hearts"); addSuit("diamonds"); addSuit("clubs"); }

private void addSuit(String suit){ for (int i = 2; i

public boolean isEmpty(){ return cards.isEmpty(); }

public int size(){ return cards.size(); }

public Card deal(){ if (isEmpty()) return null; else return cards.remove(cards.size() - 1); }

public void shuffle() { Random generator = new Random(1); if (cards.size() 0){ Card card = cards.remove(cards.size() - 1); int i = (int) (generator.nextDouble() * MAX_SIZE); while (array[i] != null) i = (int) (generator.nextDouble() * MAX_SIZE); array[i] = card; } for (Card card : array) cards.add(card); }

public String toString(){ String result = ""; for (Card card : cards) result += card + " "; return result; } }

import java.util.ArrayList;

public class War { // FIXME Create a static Deck object called deck (This is your deck of cards) public static Deck deck = new Deck(); // FIXME Create 2 static ArrayLists of Card objects. // Name them player1 and player 2 (These are the player's hands) ArrayList player1; ArrayList player2; static int warCard = 4; public static void main(String[] args) { // FIXME Shuffle the deck by calling the shuffle method shuffle(); while (isEmpty() = false) { // FIXME Deal the cards to the players. Add a card dealt // from the Deck to each player's hand. Use the deal method. } while (!player1.isEmpty() && !player2.isEmpty()) { System.out.println("Player 1 plays: " + /* FIXME get the first card from player1's hand */); System.out.println("Player 2 plays: " + /* FIXME get the first card from player2's hand */);

if (/* FIXME Compare to see if the first card in player 1's hand is bigger than the first card in player 2's hand */) { System.out.println("Player 1 wins"); player1.add(player1.remove(0)); if (player2.size() > 0) // FIXME Remove the first card from player2's hand and add it to player1's. } else if (/* FIXME Compare to see if the first card in player 1's hand is less than the first card in player 2's hand */) { System.out.println("Player 2 wins"); player2.add(player2.remove(0)); if (player1.size() > 0) // FIXME Remove the first card from player1's hand and add it to player2's. } else { System.out.println("WAR!"); goToWar(); } } System.out.println(); System.out.println("Player 1's Cards, " + /* FIXME Get the number of cards in player1's hand */ + " Cards"); // for (Card i : player1) // System.out.println(i); System.out.println(); System.out.println("Player 2's Cards, " + /* FIXME Get the number of cards in player2's hand */ + " Cards"); // for (Card i : player2) // System.out.println(i); if (player1.size() != 0) System.out.println("Player 1 wins the game!"); else System.out.println("Player 2 wins the game!"); } private static void goToWar() { while ((player1.size() 1) warCard--; if (player1.get(warCard - 1).compareTo(player2.get(warCard - 1)) > 0) { System.out.println("Player 1 wins"); for (int i =0; i 0) player1.add(player2.remove(0)); } } else if (player1.get(warCard - 1).compareTo(player2.get(warCard - 1)) 0) player2.add(player1.remove(0)); } } else { System.out.println("WAR!"); warCard +=4; goToWar(); } }

}

Current file: Card.java 1 public class card 2 ( 3 private string suit; 4 private int rank: 5 6 public card(string suit, int rank) 7 this.suit - suit; 8 this.rankrank; } 10 11 public boolean equals(card othercard) 12 return rank - othercard.rank; 13 ) 14 15 public int compareTo(card othercard){ 16 return rank - othercard.rank; 17 } 18 19 public int get Rank 20 return rank; 21 } 22 23 public String getsuit 24 return suit; 25 ) 26 27 public string tostring() 28 return rankToString() + " of + suit; 29 } 30 31 public String rankToString 32 if (rank = 14) 33 return "Ace": 34 else if (rank 11) 35 return "Jack": 36 else if (rank -12) 37 return "Queen"; 38 else if (rank = 13) 39 return "King": 40 else 41 return + rank; 42 ) 43 44 E o Current file: Deck.java 1 import java.util.ArrayList; 2 import java.util. Random; 3 4 public class Deck 5 6 public static final int MAX_SIZE = 52; 7 8 private ArrayList cards; 9 10 public Deck 11 reset(); 12 } 13 14 public void reset() { 15 cards - new ArrayList(); 16 addsuit("spades"); 17 addSuit("hearts"); 18 addsuit("diamonds"); 19 addsuit("clubs"); 20 } 21 22 private void addsuit(string suit) 23 for (int i = 2; i 0){ Card card cards.remove(cards.size() 1); int i (int) (generator.nextDouble() * MAX_SIZE); while (array[i] != null) (int) (generator.nextDouble() MAX_SIZE); array[i] card; } for (Card card : array) cards.add(card); } public String tostring String result = ""; for (Card card : cards) result += card + " "; return result; } Current file: War.java 21 Loud default tem 1 2 import java.util.ArrayList; 3 4 public class War 5 6 // FIXME Create a static Deck object called deck (This is your deck of cards) 2 public static Deck deck - new Deck(); 8 9 // FIXE Create 2 static ArrayLists of card objects. LO Name them player and player 2 (These are the player's hands) 11 ArrayList player2; La 14 static int warCard -4; 15 16 public static void main(string] args) { 17 1/ FIXME Shuffle the deck by calling the shuffle method 18 shuffle(); 19 20 while (isempty() false) // FIXME Deal the cards to the players. Add a card dealt 22 // from the Deck to each player's hand. Use the deal // method 2 25 26 while (!player1.isEmpty() && player2.isEmpty) 27 28 System.out.println("Player 1 plays! / FIXHE get the first card from player's hand ) 29 System.out.println("Player 2 plays: " / FX get the first card from player's hand); 30 if (* FIXME Compare to see if the first card in player i's hand is bigger than the first card in player's hand) 32 33 System.out.printin("Player 1 wins): 34 playeri.add(playeri.remove(o) if (player2.size() > 0) 36 // FIXM Remove the first card from player hand and add it to playeri's. 37 38 else if (/* FIXE Compare to see if the first card or player is hand is less than the first card in player's hand >> 39 System.out.println("Player 2 wins): 41 player 2.add(player 2.remove()); 42 if (player.size(0) W Fixe temove the first card from player's hand and add it to player's ) 31 4a 44 47 System.out.println("WARI"); ROToWar(): Current file: War.java 3 1 // 57 5 7 8 9 // - // 51 52 53 54 55 56 67 68 69 70 71 72 73 74 25 System.out.println(); System.out.println("Player i's Cards, + / FIXME Get the number of cards in playeri's har for (Card i : playeri) System.out.println(i); System.out.println(); System.out.println("player 2's Cards, +/" FIXME Get the number of cards in player2's han for (Card i : player2) system.out.println(i); if (player1.size() - 0) System.out.println("Player 1 wins the game!"); else System.out.println("Player 2 wins the game!"); } private static void go Towar() while ((playeri.size() 1) warCard-- 76 27 78 79 80 81 82 83 84 85 86 87 88 89 00 91 if (playeri.get (warCard - 1).compareTo(player2.get(warCard - 1)) > 0) { System.out.println("Player 1 wins"); for (int i =0; i 0) playeri.add(player 2.remove()); 3 else if (playeri.get(warCard - 1).compareto(player2.get(warcard - 1)) 0) player2.add(playeri.remove()); 3 } else { System.out.println("WARI"); warCard ; go Towar(); 93 94 95 96

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!