/**
 * Card.java
 * Jim Glenn 10/25/2002
 *
 * A playing card from a 52-card deck.  Code based on the Card class from
 * the War example.
 */

public class Card
{
    // instance varaibles

    private int rank;  // "private" means only other Cards can access this
    private int suit;  // info directly


    // codes for the four suits so we can store the suit as an int
    // (this is like Label.RIGHT, Font.ITALIC, or BorderLayout.SOUTH)
    //
    // "final" means that the codes do not change
    // "static" means that all Cards use the same codes (otherwise it
    // would be possible that one Card thought 0 meant Clubs when other
    // cards thought 0 meant Spades)

    public static final int SPADES = 0;
    public static final int HEARTS = 1;
    public static final int DIAMONDS = 2;
    public static final int CLUBS = 3;
    

    // codes for other ranks (so we can play with a full deck instead of 2..10)

    public static final int ACE = 1;
    public static final int JACK = 11;
    public static final int QUEEN = 12;
    public static final int KING = 13;


    // the constructor: makes a card of the given rank and suit

    public Card(int initRank, int initSuit)
    {
	rank = initRank;  // when a card is created, it is told two things;
	suit = initSuit;  // those things must be recorded in the instance vars
    }


    // getRank: returns the rank of this card

    public int getRank()
    {
	return rank;
    }


    // getSuit: returns the suit of this card

    public int getSuit()
    {
	return suit;
    }


    // compareTo: returns > 0 if this card is higher than the other,
    // = 0 if same rank, and < 0 if this card is lower

    public int compareTo(Card other)
    {
	int result = rank - other.rank;  // other.rank tells this card to
	return result;                   // look at other card's instance var
    }


    // a couple of methods to demonstrate if statements

    // isFaceCard: returns true exactly when this card is J, Q, or K
    // (note: aces are low in this example)
    
    public boolean isFaceCard()
    {
	boolean result;

	if (rank >= Card.JACK)
	    result = true;
	else
	    result = false;

	return result;
    }

    // isRed: returns true exacly when this card's suit is hearts or diamonds

    public boolean isRed()
    {
	boolean result;

	if (suit == Card.HEARTS || suit == Card.DIAMONDS)
	    result = true;
	else
	    result = false;

	return result;
    }

    // a method to demonstrate switch statements

    // toString: returns a String describing this card

    public String toString()
    {
	String result;

	switch (rank)
	    {
	    case JACK:
		result = "Jack";
		break;

	    case QUEEN:
		result = "Queen";
		break;

	    case KING:
		result = "King";
		break;

	    case ACE:
		result = "Ace";
		break;

	    default:
		result = String.valueOf(rank);
	    }

	result = result + " of ";

	switch (suit)
	    {
	    case SPADES:
		result = result + "Spades";
		break;

	    case HEARTS:
		result = result + "Hearts";
		break;

	    case DIAMONDS:
		result = result + "Diamonds";
		break;

	    case CLUBS:
		result = result + "Clubs";
		break;
	    }

	return result;
    }

    // a method to test this class without making a whole applet
    
    public static void main(String[] args)
    {
	// make two cards

	Card threeSpades = new Card(3, Card.SPADES);
	Card jackHearts = new Card(Card.JACK, Card.HEARTS);


	// show some info about those cards (results appear in jGRASP's
	// Run I/O window)

	System.out.println(threeSpades.getRank());
	System.out.println(jackHearts.compareTo(threeSpades));
	System.out.println(jackHearts.isFaceCard());
	System.out.println(threeSpades.isRed());
	System.out.println(threeSpades.toString());
    }
}

