Im having a brain block and I have no clue what to do next. Im not asking for someone to write code for me, im asking for someone to give me an idea or suggestion so I know what to write next. Im pretty confused. Below are my classes related to the card game war. The rules of war are below.
1) Each player is dealt 26 cards to unplayed pile.
2) Repeat steps 3 through 5 until one or both unplayed piles become empty
3)each player plays the topmost card from his unplayed pile by placing it face up on his war pile.
4. If cards have same rank, repeat step 2.
5.Otherwise, move both war piles to the winning pile of the player who has the card of a higher rank at the top of war pile.
6.The player with the largest winning pile wins.
There are 3 piles unplayed pile ,war pile, winning pile
Deck
import java.util.*; public class Deck { public static final int MAX_SIZE = 52; private static ArrayList<Card> cards; public Deck(){ reset(); } private void reset() { cards = new ArrayList<Card>(); addSuit(Suit.spade); addSuit(Suit.heart); addSuit(Suit.diamond); addSuit(Suit.club); } private void addSuit(Suit suit){ for (int i = 1; i <= 13; i++) cards.add(new Card(suit, i)); } public boolean isEmpty(){ return cards.isEmpty(); } //methods //add - puts a Card at the end ("bottom") of the pile. It just uses the ArrayList method public void add(Card aCard) { cards.add(aCard); } public void clear() { cards.clear(); } //getTopCard - removes and returns the "top" card of the pile. It just uses the ArrayList method public static Card getTopCard() { return cards.remove(0); } public int size(){ return cards.size(); } public Card deal(){ if( isEmpty()) return null; else return cards.remove(cards.size() -1); } public Card[] deal(int number){ if (number > cards.size()) return null; else{ Card[] hand = new Card[number]; for(int i = 0; i < hand.length; i++) hand[i] = deal(); return hand; } } public void shuffle(){ if (cards.size() < MAX_SIZE) return; Random gen = new Random(); Card[] array = new Card[MAX_SIZE]; while (cards.size() > 0){ Card card = cards.remove(cards.size() - 1); int i = gen.nextInt(MAX_SIZE); while (array[i] != null) i = gen.nextInt(MAX_SIZE); array[i] = card; } for (Card card : array) cards.add(card); } public String toString(){ String result = ""; for(Card card : cards) result += card + "\n"; return result; } }
Card
public class Card implements Comparable{ private Suit suit; private int rank; private boolean faceUp; public Card(Suit suit, int rank){ this.suit = suit; this.rank = rank; faceUp = false; } public Card(int i, char rank2) { // TODO Auto-generated constructor stub } public boolean equals(Object other){ if ( this == other) return true; else if (! (other instanceof Card )) return false; else{ Card otherCard = (Card)other; return rank == otherCard.rank; } } public int compareTo(Object other){ if (! (other instanceof Card)) throw new IllegalArgumentException("Parameter must be a card "); Card otherCard = (Card)other; return rank - otherCard.rank; } public int getRank(){ return rank; } public Suit getSuit(){ return suit; } public boolean isFaceUp(){ return faceUp; } public boolean isRed(){ return suit == Suit.heart || suit == Suit.diamond; } public void turn(){ faceUp = ! faceUp; } public String toString(){ return rankToString() + " of " + suit; } private String rankToString(){ if ( rank == 1) return "Ace"; else if (rank == 11) return "Jack"; else if (rank == 12) return "queen"; else if (rank == 13) return "king"; else return "" + rank; }}
Suit
public class Suit implements Comparable{ static public final Suit spade = new Suit(4, "spades"); static public final Suit heart = new Suit(3, "hearts"); static public final Suit diamond = new Suit(2, "diamonds"); static public final Suit club = new Suit(1, "clubs"); private int order; private String name; private Suit(int ord, String nm){ name = nm; order = ord; } public int compareTo(Object other){ if(! (other instanceof Suit)) throw new IllegalArgumentException("Parameter must be a Suit"); Suit otherSuit = (Suit)other; return order - otherSuit.order; } public String toString(){ return name; } }
And heres what i have so far
import java.util.*; public class War{ public static void main (String args[]){ { int halfDeck; Scanner scanner = new Scanner (System.in); Deck unplayedpile = new Deck(); Deck deck1 = new Deck(); Deck deck2 = new Deck(); unplayedpile.shuffle(); unplayedpile.add(Deck.getTopCard()); Card card1 = unplayedpile.getTopCard(); Card card2 = unplayedpile.getTopCard(); System.out.println(card1); System.out.println(card2); }}