Here is my code:
import java.util.*; public class Card implements Comparable { private int rank; private int suit; public Card() { suit = 0; rank = 0; } // public Card(int theSuit, int theRank) { this.suit = theSuit; this.rank = theRank; } public int getRank() { return rank; } public int getSuit() { return suit; } public String toString() { if(rank == 14 && suit == 4) { return "Ace" + " of " + "Hearts"; } else if(rank == 14 && suit == 3) { return "Ace" + " of " + "Diamonds"; } else if(rank == 14 && suit == 2) { return "Ace" + " of " + "Clubs"; } else if(rank == 14 && suit == 1) { return "Ace" + " of " + "Spades"; } else if(rank == 13 && suit == 4) { return "King" + " of " + "Hearts"; } else if(rank == 13 && suit == 3) { return "King" + " of " + "Diamonds"; } else if(rank == 13 && suit == 2) { return "King" + " of " + "Clubs"; } else if(rank == 13 && suit == 1) { return "King" + " of " + "Spades"; } else if(rank == 12 && suit == 4) { return "Queen" + " of " + "Hearts"; } else if(rank == 12 && suit == 3) { return "Queen" + " of " + "Diamonds"; } else if(rank == 12 && suit == 2) { return "Queen" + " of " + "Clubs"; } else if(rank == 11 && suit == 1) { return "Queen" + " of " + "Spades"; } else if(rank == 12 && suit == 4) { return "Jack" + " of " + "Hearts"; } else if(rank == 12 && suit == 3) { return "Jack" + " of " + "Diamonds"; } else if(rank == 11 && suit == 2) { return "Jack" + " of " + "Clubs"; } else if(rank == 11 && suit == 1) { return "Jack" + " of " + "Spades"; } else if(suit == 4) { return rank + " of " + "Hearts"; } else if(suit == 3) { return rank + " of " + "Diamonds"; } else if(suit == 2) { return rank + " of " + "Clubs"; } else if(suit == 1) { return rank + " of " + "Spades"; } else { return rank + " of " + suit; } } public int compareTo(Object anotherCard) { Card another = (Card)anotherCard; return this.rank-another.rank; } }
import java.util.Scanner; import java.text.NumberFormat; import java.io.IOException; import java.util.Locale; import java.text.DecimalFormat; public class War {public static void main(String[] args) throws IOException { //Declare Variables //Creates the cards based on their suite Card heartCard; Card diamondCard; Card spadeCard; Card clubCard; int countingPlays = 0; Scanner keyboard = new Scanner(System.in); //Allows Input //creates the cardPile array called DeckOfCards CardPile deckOfCards = new CardPile(); //Creates Player1's Card Pile CardPile player1Pile = new CardPile(); //Creates Player2's Card Pile CardPile player2Pile = new CardPile(); //Creates the cards to fill the array (1-14 of hearts/diamonds/spades/clubs). for(int i = 2; i < 15; i++) { int heart = 4; int diamond= 3; int spade = 2; int club = 1; deckOfCards.add(heartCard = new Card(heart, i)); deckOfCards.add(diamondCard = new Card(diamond, i)); deckOfCards.add(spadeCard = new Card(spade, i)); deckOfCards.add(clubCard = new Card(club, i)); } //shuffles the cards deckOfCards.shuffle(); //Takes the deckOfCards and splits them up into 2 piles for Player1 and Player2 for(int i = 0; i < 26; i++) { player1Pile.add(deckOfCards.getTopCard()); player2Pile.add(deckOfCards.getTopCard()); } //Prints out the deck of Cards and then the player 1's pile and player 2's pile System.out.println("Deck of Cards after removing cards into two piles: " + deckOfCards); System.out.println(); System.out.println("Player 1's Cards: " + player1Pile); System.out.println(); System.out.println("Dealer's Cards: " + player2Pile); System.out.println(); //checking the size of each players Pile System.out.println("" + player1Pile.size()); System.out.println("" + player2Pile.size()); //Prints the header for the game System.out.println("Lets have a war!!!"); System.out.println("\n\tPlayer 1 Dealer"); System.out.println(); do { //gets the top cards of each players Pile Card player1RemovedTopCard = player1Pile.getTopCard(); Card player2RemovedTopCard = player2Pile.getTopCard(); //Compares the 2 cards to test which is bigger. If player 1's card is smaller than player 2 is the winner if(player1RemovedTopCard.compareTo(player2RemovedTopCard) < player2RemovedTopCard.compareTo(player1RemovedTopCard)) { System.out.println("Player 1: " + player1RemovedTopCard + " Dealer: " + player2RemovedTopCard); System.out.println("Dealer is the Winner"); player2Pile.add(player1RemovedTopCard); player2Pile.add(player2RemovedTopCard); System.out.println("Player 1 has: " + player1Pile.size() + " cards left."); System.out.println("Dealer has:" + player2Pile.size() + " cards left."); System.out.println("\n"); keyboard.nextLine(); } //Compares the 2 cards to test which is bigger. If player 2's card is smaller than player 1 is the winner. else if(player1RemovedTopCard.compareTo(player2RemovedTopCard) > player2RemovedTopCard.compareTo(player1RemovedTopCard)) { System.out.println("Player 1: " + player1RemovedTopCard + " Dealer: " + player2RemovedTopCard); System.out.println("Player 1 is the Winner"); player1Pile.add(player1RemovedTopCard); player1Pile.add(player2RemovedTopCard); System.out.println("Player 1 has: " + player1Pile.size() + " cards left."); System.out.println("Dealer has: " + player2Pile.size() + " cards left."); System.out.println("\n"); keyboard.nextLine(); } //Else it is a war else { System.out.println("Player 1: " + player1RemovedTopCard + " Dealer: " + player2RemovedTopCard); System.out.println("WAR!!!!!!!"); if(player1RemovedTopCard.compareTo(player2RemovedTopCard) > player2RemovedTopCard.compareTo(player1RemovedTopCard)) { System.out.println("Player 1 is the winner of the War! "); System.out.println("Player 1 has: " + player1Pile.size() + " cards left."); System.out.println("Dealer has: " + player2Pile.size() + " cards left."); System.out.println("\n"); keyboard.nextLine(); } } }while(player1Pile.size() > 0 || player2Pile.size() > 0); if(countingPlays >= 10) { player1Pile.shuffle(); player2Pile.shuffle(); System.out.println("Cards Shuffled"); countingPlays = 0; } { if(player1Pile.size() == 0) { System.out.println("Winner is Dealer" ); } else System.out.println("Winner is Player 1" ); } } //end of main }//end of class
import java.util.*; public class CardPile { //data - uses an ArrayList of Cards to actually store them private ArrayList<Card> pile; //constructor - creates the ArrayList that will be used public CardPile() { pile = new ArrayList<Card>(); } //methods //add - puts a Card at the end ("bottom") of the pile. It just uses the ArrayList method public void add(Card aCard) { pile.add(aCard); } //getTopCard - removes and returns the "top" card of the pile. It just uses the ArrayList method public Card getTopCard() { return pile.remove(0); } //toString - returns a String representation of the pile. It just uses the ArrayList method public String toString() { return pile.toString(); } //size - returns the size of the pile. It just uses the ArrayList method public int size() { return pile.size(); } //clear - empties the pile. It just uses the ArrayList method public void clear() { pile.clear(); } //shuffle - randomly reorders the pile. public void shuffle() { Random rand = new Random(); for (int i=0; i<2000; i++) //repeat 2000 times { if (size() > 0) //if there is anything here to shuffle, then... { Card topCard = pile.remove(0); //take off the top card... int newPosition = rand.nextInt(pile.size()); //...find a new spot for it in a random position pile.add(newPosition, topCard); //...and put it back there } } } }
If you could show/tell me how to do the War by suits that would be awesome. Thank you!