any opinions on how to simplify or improve a basic Card class, which will be instantiated for use in a Deck class.
Code:
/**
* PokerCard class represents a playing card of 52 values differing by suits and numeric ranks (1-13).
* Use int[2] to store the values of each card, with [0] for its rank and [1] for its suit.
* Suits consist of spades, hearts, clubs, diamonds coded by 0, 1, 2, 3, respectively.
* Values are from 1 to 13, with Ace, Jack, Queen, King coded by 1, 11, 12, 13, respectively.
*/
public class PokerCard implements Comparable<PokerCard>{
@Override
public boolean equals(PokerCard that){ //overriding equals for comparing cards
if (this == that) //if this and obj are the same
return true;
else if (that == null)
throw new NullPointerException("The card is null");
else if (this.getClass() != that.getClass())
return false;
return ( (this.value[rank] == that.value[rank]) && (this.value[suit] == that.value[suit]) ); //when both cards have the same numeric rank and suit
}
@Override
public int compareTo(PokerCard that){ //overriding Comparable for comparing cards
if (this.equals(that))
return 0;
else if (this.value[rank] == Ace && that.value[rank] != Ace) //special case when this card is an Ace and that isn't
return 1;
else if (this.value[rank] != Ace && that.value[rank] == Ace) //special case when that card is an Ace and this isn't
return -1;
else if (this.value[rank] > that.value[rank]) //both non-Ace and this card's numeric value is higher
return 1;
else if (this.value[rank] < that.value[rank]) //both non-Ace and this card's numeric value is lower
return -1;
else if (this.value[suit] < that.value[suit]) //both cards have the same numeric values and this card's suit is of a higher suit
return 1;
else
return -1; //both cards have the same numeric values and this card's suit is of a lower suit
}
protected final static int Spade = 0; //initializing constant values
protected final static int Heart = 1;
protected final static int Club = 2;
protected final static int Diamond = 3;
protected final static int Ace = 1;
protected final static int Jack = 11;
protected final static int Queen = 12;
protected final static int King = 13;
protected final static int rank = 0; //int[] array index
protected final static int suit = 1;
private final int[] value = new int[2]; //int[0] for storing numeric value, int[1] for suit; private to preventing tempering
PokerCard(int thisrank, int thissuit){ //sole constructor taking in rank and suit as args
if(thissuit>Diamond || thissuit<Spade) //invalid suit value
throw new IllegalArgumentException("invalid suit input");
if(thisrank>13 || thisrank<1)
throw new IllegalArgumentException("card numeric rank inputted invalid, must be 1-13"); //invalid card value
this.value[rank] = thisrank;
this.value[suit] = thissuit;
}
public int[] getValue(){ //getter for value
return value;
}
public String[] getValueAsString(){ //getting for value but returned as string[2]
String suitString;
String rankString;
switch(value[suit]){
case Spade: suitString = "Spade";
break;
case Heart: suitString = "Heart";
break;
case Club: suitString = "Club";
break;
default: suitString = "Diamond";
}
switch(value[rank]){
case Ace: rankString = "Ace";
break;
case Jack: rankString = "Jack";
break;
case Queen: rankString = "Queen";
break;
case King: rankString = "King";
break;
default: rankString = String.valueOf(value[rank]); //all numeric ranks returned as numeric strings
}
return new String[]{rankString, suitString};
}
}
