Quote
How is Card defined? Does it implement Comparable<Card>?
Damn! They went and changed Comparable too!! I didn't see that at first.
My class Card did implement Comparable but it was based on the old java 1.4.
I changed it and this seemed to compile fine. Do you see anything else I
should be aware of?
Is the signature for the compareTo() method correct?
Alan
package games;
import java.util.*;
import javax.swing.*;
/**
* <p>Title: Card</p>
* <p>Description: You can create 52 instances of this class, each
instance<br>
* containing a CardValue and a Suit</p>
* <p>You can use any container to hold the Cards, even a Hashtable if so
desired.
* @author Alan Shiers
* @version 1.0
*/
public class Card extends JLabel implements Comparable<Card>
{
private Suit suit;
private CardValue value;
private volatile int hash = 0;
private String alias = "";
public Card(CardValue value, Suit s )
{
suit = s;
this.value = value;
}
/**
* Set an alias name for this Card for identification
* @param st String
*/
public void setAlias(String st)
{
alias = st;
}
/**
* Get this Card(s) alias name
* @return String
*/
public String getAlias()
{
return alias;
}
/**
* Get the value of this Card
* @return CardValue
*/
public CardValue getValue()
{
return value;
}
/**
* Get the suit of this Card
* @return Suit
*/
public Suit getSuit()
{
return suit;
}
/**
* This compareTo method will only return either -1 for less than,
* or 1 for greater than results. No two cards are equal in a
* standard deck of cards. Therefore, this method does not return zero.
* This method will throw a ClassCastException if a Card type object
* is not passed as a parameter.
* @return int Either -1 or 1
* @param o Object expected to be a Card type
*/
public int compareTo(Card o)
{
//To compare cards, we first need to determine the ranking
//of the suits, then their values. The Deck class contains
//a default ranking of the Suits and a mutator method
//to change the default ranking.
Suit[] rank = Deck.getSuitRanking();
//Match the location of the card suit with that
//of the rank array.
int otherCardSuit = 0;
int thisCardSuit = 0;
for(; otherCardSuit < rank.length; otherCardSuit++)
{
if(o.suit == rank[otherCardSuit])
break;
}
for(; thisCardSuit < rank.length; thisCardSuit++)
{
if(o.suit == rank[thisCardSuit])
break;
}
//Now, we check the values of each card
List list = CardValue.VALUES;
if(thisCardSuit == otherCardSuit)
{
//In the same suit you can't have two identicle values.
//Therefore, we only need to test if this cards value
//is less than or greater than the other cards value.
if(list.indexOf(this.value) < list.indexOf(o.value))
return -1;
else
return 1;
}
else if(thisCardSuit < otherCardSuit)
{
return -1;
}
return 1;
}
/**
* Find out if this Card is equal to the one being passed as a parameter.
* @param o Object expected to be of type Card
* @return boolean True if equal, False otherwise
*/
public boolean equals(Object o)
{
if(!(o instanceof Card))
return false;
if(this.suit == ((Card)o).suit)
{
if(this.value == ((Card)o).value)
return true;
else
return false;
}
return false;
}
/**
* For Hashtables, obtain a unique hashcode.
* @return int The hashcode.
*/
public int hashCode()
{
if(hash == 0)
{
int result = 17;
result = 37 * result + calcHash(suit.toString());
result = 37 * result + calcHash(value.toString());
hash = result;
}
return hash;
}
private int calcHash(String st)
{
int i = 0;
int j = 0;
char ac[] = st.toCharArray();
for(int l = 0; l < ac.length; l++)
i = 31 * i + ac[j++];
return i;
}
/**
* Obtain a string representation of this Card.
* @return String
*/
public String toString()
{
return value + " of " + suit;
}
}