public enum Suit
{
CLUBS,
HEARTS,
SPADES,
DIAMONDS
}
public enum Value
{
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
ACE
}
Card.java
public class Card {
private Suit suit;
private Value value;
public Card(Suit theSuit, Value theValue)
{
suit = theSuit;
value = theValue;
}
public String toString()
{
return value + " of " + suit;
}
public Value getValue()
{
return value;
}
public Suit getSuit()
{
return suit;
}
public boolean equals(Card other)
{
if (value.ordinal() == other.value.ordinal()
|| suit.ordinal() == other.suit.ordinal())
{
return true;
}
else {
return false;
}
}
}
CardPile.java
public class CardPile
{
public Card[] cards;
private int numCards;
public CardPile()
{
this.cards = new Card[52];
this.numCards = 0;
// The problem is here, when I try to iterate through the enums and the
// array to populate my cards[] of 52 objects Card it populates it with
// 52 Card which are all ACE of DIAMONDS, it looks like the triple loops
// populates 52 times the two last elements of my enum, but I can't
// figure out how to fix that! Thanks in advance!
for (Suit s : Suit.values())
{
for (Value v : Value.values())
{
for (int π = 0; π < cards.length; π++)
{
cards[π] = new Card(s, v);
}
}
}
}
public boolean isEmpty()
{
for (int i = 0; i < cards.length; i++)
{
if (cards[i] != null)
{
return false;
}
}
return true;
}
public int getNumCards()
{
return numCards;
}
}