Here is the full code thus far:

public class Cardmain
{
    public static void main (String[] args)
    {
        War wargame = new War();
	wargame.play();
    }
}

class Deck
{
    static final int numberOfCards = 52;
    Card[] cards = new Card[numberOfCards];
    int index;
    
    public Deck()
    {
        // Constructs all 52 Card objects

	for (int i = 0;i < numberOfCards; i++)
	    cards[i] = new Card(i);
    }
    
    public void shuffle()
    {
        // Shuffles the deck.
	Card temp;
	int a;
	for (int x = 0; x < numberOfCards; x++)
	{
	    a = (int)(Math.random() * numberOfCards);
	    temp = cards[x];
	    cards[x] = cards[a];
	    cards[a] = temp;
	}
    }
    
    public Card getNextCard()
    {
        // Gets the next card in the deck.
	
	if (index >= numberOfCards)
	   index = 0;
	int temp = index;
	index++;
	return cards[temp];
    }
}

class Card 
{    
    // The values 0-12 represent clubs, 13-25 diamonds, 
    // 26-38 hearts, and 39-51 spades. Within each suit,
    // the lowest value is the ace and highest is the king.
    int suit = 0; // 0 through 3
    int rank = 0; // 0 through 12
    
    public Card(int s, int r) 
    {
        // Sets suit and rank
	if (s <= 12)
	{
	    suit = 0;
	    rank = r;
	}
	else if (s >= 13 && s <= 25)
	{
	    suit = 1;
	    rank = r % 13;
	}
	else if (s >= 26 && s <= 38)
	{
	    suit = 2;
	    rank = r % 26;
	}
	else if (s >= 39 && s <= 51)
	{
	    suit = 3;
	    rank = r % 39;
	}
    }
    
    public Card(int number) 
    {
        this(number, number);
    }
    
    public String toString()
    {
        // Returns a string representation of a Card in
        // a format like so: The Ace of Clubs
        String theCard;
	String[] cardRank = {"Ace","Two","Three","Four","Five","Six","Seven",
	                     "Eight","Nine","Ten","Jack","Queen","King"};
	String[] cardSuit = {"Clubs","Diamonds","Hearts","Spades"};
	
	// cardSuit is not needed for War:
	theCard = (cardRank[rank]);
	
	return theCard;
    }
    
    public int compareWar(Card other)
    {
        if ((rank == 0) && (other.rank != 0))
	   return 1;
	else if ((other.rank == 0) && (rank != 0))
	   return -1;
	else if (rank > other.rank)
	   return 1;
	else if (other.rank > rank)
	   return -1;
	else
	   return 0;
    }
}

public class War
{
    public final int END_LIMIT = 8;
    private Deck warDeck;
    private Vector pileAbe = new Vector(30,22); //set capacity at 30
    private Vector pileBob = new Vector(30,22); //increment by 22 if needed
    
    public War(Deck gameDeck)
    {
        warDeck = gameDeck;
	warDeck.shuffle(); //Shuffles deck
    }
    
    public void play()
    {
        for(int i = 0;i < Deck.numberOfCards; i+=2)
	    pileAbe.addElement(warDeck.getNextCard());
	for(int i = 0;i < Deck.numberOfCards; i+=2)
	    pileBob.addElement(warDeck.getNextCard());
	
	System.out.println("Abe = " + (Card)pileAbe.elementAt(0));
	System.out.println("Bob = " + (Card)pileBob.elementAt(0));
	
	int test = ((Card)pileAbe.firstElement()).compareWar((Card)pileBob.firstElement());
	if (test < 0)
	   System.out.println("Bob wins!");
	else if (test > 0)
	   System.out.println("Abe wins!");
	else
	   System.out.println("Battle!"); //The 2 cards match
	System.out.println("==========");
    }
}

If you were to run this, it would display one turn of war (e.g. both players put down one card).
What I need help on is this:
1.) Play a full game, not just one turn.
2.) If Abe wins the turn, then the won cards go to the bottom of pileAbe. If Bob wins the turn, then the won cards go to the bottom of pileBob. If a battle occures, certain things will happen (see #3 below)
3.) If a Battle:

a.) get 4 cards from each pile (including the first card...so a total of 5 from each pile)
b.) compare the 4th:
- if Abe wins, all 10 cards go to bottom of pileAbe.
- if Bob wins, all 10 go to bottom of pileBob.
- if they draw the same card again, then Battle again.
c.) A triple Battle halts the game (System.exit(0))

4.) Play ends (System.exit(0)) when one pile falls below an easily changed number (e.g. END_LIMIT = 8) or when a file has too few cards to engage in a Battle (e.g. a double battle).

Thanks to anyone who can help me get this working as a real game!

Recommended Answers

All 6 Replies

Well to start with number one do you know how to use loops thats how I'd do it if you don't ask.

Sure, I know how to use all kinds of loops....however, when I used a "for" loop to test out 5 turns, the winner was displayed incorrectly for some reason after the first turn.

Do you still have that code?

The code was just adding a for loop in the following fashion. It seems if I add a for loop inside play() to test...oh say 5 times, it messes up after the first turn....it gets "stuck" on Abe for some reason (test=1). To do this, add: for(int x = 0; x < 5; x++){ on top of: System.out.println("Abe = " + (Card)pileAbe.elementAt(0)). Close the for loop after: System.out.println("==========") with a }.

I have a way to simplify your deck class. In the shuffle method instead of the random generator in the math class use the random class by importing the java util package. You might also try recursion.

Little pointless to post in the thread which is over 2 years old and author and some members are not active for same time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.