War the Card Game

Posted 03 March 2009 - 11:38 AM

Basically we are supposed to simulate the game of war. Here is my Card class and card Pile class. Card Pile class is the one that makes the deck of cards. There are two players. The problem I have is when the game starts, the sizes of the arrays after the first match are 23 23 instead of it supposed to be 27 25. For example Player one and Player two both have 26 cards at the beginning of the game (52 cards split evenly is 26). Then they each draw a card and say Player 1 gets a 6 and player 2 gets a 3. Player 1 gets his card back and player 2's card. The new size of their pile should be 27 cards in player 1's pile and 25 cards in player 2's pile. except this is not happening =/.

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
}
}
}
}

Re: War the Card Game

Posted 03 March 2009 - 03:55 PM

Okay so I fixed most these problems except the compareTo issue.
For example, some times it will work fine like for example Player1's card is a 5 and player2's card is a 7 and it will say player 2 win. Then other times it won't work.

//CardPile - holds a "pile" of Cards and has methods to access them
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
}
}
}
}