War Card Game - Java Pt. 4

Chris Chen
/**
* class Card - a class used to represent a playing card
*/

public class Card {

public static enum RANK {JOKER, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE}; //defines the range of values a card can have
public static enum SUIT {NO_SUIT, CLUB, DIAMOND, HEART, SPADE}; //defines a range of suits the card can have

private RANK myRank; //stores the rank of the card
private SUIT mySuit; //stores the suit of the card
private String backFile; //pathway to back image of card
private String frontFile; //pathway to front image of card
private Boolean faceUp; //stores whether or not the card is face up or not

/**
* Constructor Card - a default constructor setting the card to a joker without a suit
*/

public Card(){

this(RANK.JOKER, SUIT.NO_SUIT);

}

/**
* Constructor Card - overloaded constructor setting the card to the specified parameters
* @param r - rank of card
* @param s - suit of card
*/

public Card(RANK r, SUIT s){

this(r, s, null, null);

}

/**
* Constructor Card - overloaded constructor setting the card and the file pathways to the specified parameters
* @param r - rank of card
* @param s - suit of card
* @param back - pathway to file of the back of the card
* @param front - pathway to file of the front of the card
*/

public Card(RANK r, SUIT s, String back, String front){

this.faceUp = false;
this.myRank = r;
this.mySuit = s;
this.backFile = back;
this.frontFile = front;

}

/**
* Constructor Card - copy constructor for the object passed in
* @param card - object of the Card class to be copied
*/

public Card(Card card){

this(card.myRank, card.mySuit, card.backFile, card.frontFile);
if(card.isFaceUp())
this.flip();

}

/**
* summary: provides an accessor method for a cards rank.
* @return myRank - the rank of the card
*/

public RANK getRank(){

return(this.myRank);

}

/**
* summary: provides an accessor method for a cards suit.
* @return mySuit - the suit of the card
*/

public SUIT getSuit(){

return(this.mySuit);

}

/**
* summary: provides an accessor method for a cards file pathway to the image of the back of the card
* @return backFile - the cards file pathway to the image of the back of the card of the card
*/

public String getBackFile(){

return(this.backFile);

}

/**
* summary: provides an accessor method for a cards file pathway to the image of the front of the card
* @return frontFile - the cards file pathway to the image of the front of the card of the card
*/

public String getFrontFile(){

return(this.frontFile);

}

/**
* summary: determines whether or not a card is face up
* @return whether or not a card is face up or not
*/

public Boolean isFaceUp(){

return(this.faceUp);

}

/**
* summary: flips a card over
*/

public void flip(){

//determines if the card is face up
if(this.faceUp)
this.faceUp = false;

else
this.faceUp = true;

}

/**
* summary: displays a meaningful representation of the object
* @return - meaningful representation of the object
*/

public String toString(){

//determines if the card is face up or not
if(this.isFaceUp())
return(this.myRank + " of " + this.mySuit);

else
return("The card is face down");

}

/**
* summary: compares the calling object with the object passed in
* @param other - the object being passed in
* @return - a number representing which object is higher in rank and suit
*/

public int compareTo(Card other) throws IllegalArgumentException {

//determines if the card is face up or not
if(other.isFaceUp()){

int thisVal = this.cardOrdinal(), otherVal = other.cardOrdinal(); //stores the hierarchy value of the calling card, stores the hierarchy value of the passed in card

//determines if the calling card has a lower rank than the parameter
if(thisVal < otherVal)
return(-1);

//determines if the calling card has a higher rank than the parameter
else if(thisVal > otherVal)
return(1);

else
return(0);
}

else
throw new IllegalArgumentException("You can't compair a card to another card that you can't see");

}

/**
* summary: helper method that assigns a rank to a card
*/

public int cardOrdinal(){

return(this.mySuit.ordinal() * 13 + this.myRank.ordinal());

}
}

Published by Chris Chen

Chris is currently attending the University of California, Berkeley seeking an undergraduate's degree in Electrical Engineering Computer Science. He enjoys playing basketball, practicing kendo, hanging out w...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.