War Game
Hi,
I was wondering if anyone could help me to make the java game War. I have these two classes, the Deck class and Card class:
import java.util.*;
public class Deck {
// Instance Variables
private Card[] theDeck = new Card[52];
// Set up random number generator (an instance variable because we use it in multiple methods)
private Random generator = new Random();
/** Creates a new instance of Deck */
public Deck() {
// Initialize the deck by filling it with 52 cards in order
for (int currCard = 0; currCard < 13; currCard++)
{
// Spades first
theDeck[currCard] = new Card(currCard, 0);
// Then Hearts
theDeck[currCard+13] = new Card(currCard, 1);
// Then Clubs
theDeck[currCard+26] = new Card(currCard, 2);
// And finally Diamonds
theDeck[currCard+39] = new Card(currCard, 3);
}
}
public Card getTopCard()
{
// The 0th card is the one on the "top" of the deck
return theDeck[0];
}
public Card getRandomCard()
{
// Calculate a random number between 0 and 51
int randomCard = generator.nextInt(51);
// Return the card at that array index of the deck
return theDeck[randomCard];
}
public void shuffle()
{
final int NUM_EXCHANGES = 500;
// My shuffle method (admittedly not very interesting) works by exchanging
// pairs of cards at random locations in the deck. The more exchanges you
// make (by changing the constant in the for-loop) the more "shuffled" your deck is.
// We need to declare a temporary Card variable to hold one Card as it is
// being exchanged.
Card tempCard;
for (int currExchange = 0; currExchange < NUM_EXCHANGES; currExchange++)
{
// Get two random indices in the deck
int firstIndex = generator.nextInt(51);
int secondIndex = generator.nextInt(51);
// Exchange the cards at those locations
tempCard = new Card(theDeck[firstIndex].getRank(), theDeck[firstIndex].getSuit());
theDeck[firstIndex] = new Card(theDeck[secondIndex].getRank(), theDeck[secondIndex].getSuit());
theDeck[secondIndex] = new Card(tempCard.getRank(), tempCard.getSuit());
}
}
public String toString()
{
String toReturn = "";
// Print the card at each index of the deck using the toString method in Card
for (int currCard = 0; currCard < 52; currCard++)
toReturn += theDeck[currCard].toString() + "\n";
return toReturn;
}
}
public class Card {
// Instance Variables
private int rank;
private int suit;
/** Creates a new instance of Card */
public Card(int pRank, int pSuit) {
rank = pRank;
suit = pSuit;
}
// Accessors
public int getRank()
{
return rank;
}
public int getSuit()
{
return suit;
}
// Mutators
public void setRank(int pRank)
{
rank = pRank;
}
public void setSuit(int pSuit)
{
suit = pSuit;
}
// toString method
public String toString()
{
// String values of suit and rank
String sSuit = "", sRank = "";
// Set String value of suit
if (suit == 0)
sSuit = "Spades";
else if (suit == 1)
sSuit = "Hearts";
else if (suit == 2)
sSuit = "Clubs";
else if (suit == 3)
sSuit = "Diamonds";
// Set String value of rank
if (rank == 0)
sRank = "Ace";
else if (rank == 10)
sRank = "Jack";
else if (rank == 11)
sRank = "Queen";
else if (rank == 12)
sRank = "King";
else
sRank = "" + (rank + 1); // if the rank is numerical, turn it into a String by
// concatenating it with the empty string. Remember that we
// have to add one to compensate for starting at 0
// Return String value of suit and rank
return sRank + " of " + sSuit;
}
}

