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:
Here is my Deck Class:


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


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
public Deck(){ reset(); }
public void reset(){ cards = new ArrayList
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
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
