import java.util.*;
public class Card implements Comparable
{
//Data
private int rank;
private char suit;
//default constructor
public Card()
{
suit = '\u0003';
rank = 0;
}
//Constructor
public Card(char theSuit, int theRank)
{
this.suit = theSuit;
this.rank = theRank;
}
//get Rank of the card
public int getRank()
{
return rank;
}
//get Suite of the card
public char getSuit()
{
return suit;
}
//toString - returns its representation as a String
public String toString()
{
//Checks the rank of the cards and Changes them to the correct character
if(rank == 11)
{
return suit + "" + 'J';
}
else if(rank == 12)
{
return suit + "" + 'Q';
}
else if(rank == 13)
{
return suit + "" + 'K';
}
else if(rank == 14)
{
return suit + "" + 'A';
}
else
return suit + "" + rank;
}
//Compares 2 cards Rank and Suite
public int compareTo(Object anotherCard)
{
Card another = (Card)anotherCard;
return this.rank-another.rank;
}
}
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
}
}
}
}
// import needed classes and packages
import java.util.Scanner;
import java.text.NumberFormat;
import java.io.IOException;
import java.util.Locale;
import java.text.DecimalFormat;
public class Program2
{public static void main(String[] args) throws IOException
{//Declare Variables
Card heartCard;
Card diamondCard;
Card spadeCard;
Card clubCard;
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
for(int i = 2; i < 15; i++)
{
char heart = '\u0003';
char diamond ='\u0004';
char spade = '\u0005';
char club = '\u0006';
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));
}
//prints out the deck of Cards
System.out.println("Deck Of Cards: " + deckOfCards);
//shuffles the cards
deckOfCards.shuffle();
//Prints out the deck of cards after they are shuffled
System.out.println("Deck of Cards after shuffled: " + deckOfCards);
//Checking the size of the Deck
System.out.println("" + deckOfCards.size());
//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("Player 1's Cards: " + player1Pile);
System.out.println("Player 2's Cards: " + player2Pile);
//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 Player 2");
System.out.println("\n\t-------- --------");
//Starts the game of War
do
{
if(player1Pile.getTopCard().compareTo(player2Pile.getTopCard()) < player2Pile.getTopCard().compareTo(player1Pile.getTopCard()));
{
System.out.println("Player 1: " + player1Pile.getTopCard() + " Player 2: " + player2Pile.getTopCard());
System.out.println("Player 2 is the Winner");
player2Pile.add(player1Pile.getTopCard());
player2Pile.add(player2Pile.getTopCard());
System.out.println(player1Pile.size());
System.out.println(player2Pile.size());
}
if(player1Pile.getTopCard().compareTo(player2Pile.getTopCard()) > player2Pile.getTopCard().compareTo(player1Pile.getTopCard()));
{
System.out.println("Player 1: " + player1Pile.getTopCard() + " Player 2: " + player2Pile.getTopCard());
System.out.println("Player 1 is the Winner");
player1Pile.add(player2Pile.getTopCard());
player1Pile.add(player1Pile.getTopCard());
System.out.println(player1Pile.size());
System.out.println(player2Pile.size());
}
if(player1Pile.getTopCard().compareTo(player2Pile.getTopCard()) == player2Pile.getTopCard().compareTo(player1Pile.getTopCard()));
{
System.out.println("Player 1: " + player1Pile.getTopCard() + " Player 2: " + player2Pile.getTopCard());
System.out.println("WAR!!!!!!!");
do
{
player1Pile.getTopCard();
player1Pile.getTopCard();
player1Pile.getTopCard();
player2Pile.getTopCard();
player2Pile.getTopCard();
player2Pile.getTopCard();
if(player1Pile.getTopCard().compareTo(player2Pile.getTopCard()) < player2Pile.getTopCard().compareTo(player1Pile.getTopCard()));
{
System.out.println("Player 1: " + player1Pile.getTopCard() + " Player 2: " + player2Pile.getTopCard());
System.out.println("Player 2 is the Winner");
player2Pile.add(player1Pile.getTopCard());
player2Pile.add(player1Pile.getTopCard());
player2Pile.add(player1Pile.getTopCard());
player2Pile.add(player1Pile.getTopCard());
System.out.println(player1Pile.size());
System.out.println(player2Pile.size());
}
if(player1Pile.getTopCard().compareTo(player2Pile.getTopCard()) < player2Pile.getTopCard().compareTo(player1Pile.getTopCard()));
{
System.out.println("Player 1: " + player1Pile.getTopCard() + " Player 2: " + player2Pile.getTopCard());
System.out.println("Player 1 is the Winner");
player1Pile.add(player2Pile.getTopCard());
player1Pile.add(player2Pile.getTopCard());
player1Pile.add(player2Pile.getTopCard());
player1Pile.add(player2Pile.getTopCard());
System.out.println(player1Pile.size());
System.out.println(player2Pile.size());
}
}while(player1Pile.getTopCard().compareTo(player2Pile.getTopCard()) == player2Pile.getTopCard().compareTo(player1Pile.getTopCard()));
}
}while(player1Pile.size() > 0 || player2Pile.size() > 0);
} //end of main
}//end of class
This post has been edited by xxwolfsrainxx: 03 March 2009 - 11:47 AM

New Topic/Question
Reply



MultiQuote



|