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;

}

}

[4069 byte] By [student85a] at [2007-11-27 2:38:05]
# 1
> 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:> I'm sure there is someone here that could help you. You only need to ask a question.
prometheuzza at 2007-7-12 2:58:54 > top of Java-index,Other Topics,Java Game Development...
# 2
haha ya dude go ahead and tell us what you need help on doing.
stephensk8sa at 2007-7-12 2:58:54 > top of Java-index,Other Topics,Java Game Development...
# 3

>// Calculate a random number between 0 and 51

>int randomCard = generator.nextInt(51);

if im not mistaken, this will should be

int randomCard = generator.nextInt(51) + 1;

i think that if u dont hav the plus one then it will only find the values of 0 through 50 so you will never find the king or ace of diamonds. not sure which one is ur last card.

stephensk8sa at 2007-7-12 2:58:54 > top of Java-index,Other Topics,Java Game Development...
# 4
and put ur code in theseso it will space the code out and make it easier to read.
stephensk8sa at 2007-7-12 2:58:54 > top of Java-index,Other Topics,Java Game Development...
# 5
dammit. k put them in these**** without the stars
stephensk8sa at 2007-7-12 2:58:54 > top of Java-index,Other Topics,Java Game Development...
# 6
dammmit. [ c o d e ][ / c o d e ]without the spaces
stephensk8sa at 2007-7-12 2:58:54 > top of Java-index,Other Topics,Java Game Development...
# 7
Put your code between [code] and [/code].;-)
CaptainMorgan08a at 2007-7-12 2:58:54 > top of Java-index,Other Topics,Java Game Development...
# 8
Cross post: http://forum.java.sun.com/thread.jspa?threadID=5166293Look here for examples of card game code: http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html If you look really close, you will see methods used there that shuffle the deck, deal hands,
robtafta at 2007-7-12 2:58:54 > top of Java-index,Other Topics,Java Game Development...