Class Card

java.lang.Object
  |
  +--Card

public class Card
extends Object

The Card class represents a card in our game. This class is modeled after the notion of a two-dimensional sequence, as discussed in class. The API for Card does not disclose its representation. While this is considered good design, you will have to become acquainted with treating a card's rank and suit abstractly---so that each card is positioned with respect to two orthogonal sequences. A Card also "knows" whether it is visible (face-up) or not (face-down).

The following conditions are required of any Card implementation. The card created by the default constructor must be a Two of Clubs. If genNextRank is called on a Two of Clubs, a Three of Clubs should be created. If a genNextRank should be called on a Three of Clubs, a Four of Clubs should be returned, and so on up through Ace. If genNextRank is called on an Ace, a Two should be returned. The suits are ordered as follows: Clubs, Diamonds, Hearts, Spades. Therefore, if genNextSuit is called on a Two of Clubs, a Two of Diamonds should be returned, and so forth. genPrevRank and genPrevSuit should behave in a similar way.


Constructor Summary
Card()
          Constructs a Card with base rank and suit.
 
Method Summary
 Card genNextRank()
          Generates a card with the next rank.
 Card genNextSuit()
          Generates a card with the next suit.
 Card genPrevRank()
          Generates a card with the previous rank.
 Card genPrevSuit()
          Generates a card with the previous suit.
 PictureComponent getPicture()
          Returns the PictureComponent holding the card's image.
 boolean isAlternateSuitOf(Card other)
          Returns true if other and this are cards of a different color.
 boolean isRankPrevOf(Card other)
          Returns whether or not this card is the previous rank of the other card.
 boolean isRankSuccOf(Card other)
          Returns whether or not this card is the successor rank of the other card.
 boolean isVisible()
          Returns whether the card is face up (true) or down (false)
 int rankDiff(Card other)
          Returns the difference in rank.
 String rankToString()
          Returns the rank as a string, for example "Ace"
 boolean sameRankAs(Card c)
          Returns true if the rank of the cards is the same
 boolean sameSuitAs(Card c)
          Returns true if the suit of the cards is the same
 void setVisible(boolean v)
          Sets the visibility of the Card according to the parameter.
 int suitDiff(Card other)
          Returns the difference in suit.
 String suitToString()
          Returns the suit as a string, for example "Spades"
 String toString()
          Returns a string such as "Ace of Spades" that reflects the rank and suit of a card.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Card

public Card()
Constructs a Card with base rank and suit. By default, the card is invisible.
Method Detail

getPicture

public PictureComponent getPicture()
Returns the PictureComponent holding the card's image.

isVisible

public boolean isVisible()
Returns whether the card is face up (true) or down (false)

setVisible

public void setVisible(boolean v)
Sets the visibility of the Card according to the parameter. If the Card's image changes because its visibility is altered, do not construct a new PictureComponent. Instead, use the PictureComponent's setImage(String filename) on the existing PictureComponent.

genNextRank

public Card genNextRank()
Generates a card with the next rank. If the maximum rank is reached and passed, a card of the lowest rank is returned.

genNextSuit

public Card genNextSuit()
Generates a card with the next suit. If the maximum suit has been reached and passed, a card of the lowest suit is returned.

genPrevRank

public Card genPrevRank()
Generates a card with the previous rank. If the minimum rank has been reached and passed, a card of the highest rank is returned.

genPrevSuit

public Card genPrevSuit()
Generates a card with the previous suit. If the minimum suit has been reached and passed, a card of the highest suit is returned.

rankDiff

public int rankDiff(Card other)
Returns the difference in rank. The subtraction happens as follows: other-this

suitDiff

public int suitDiff(Card other)
Returns the difference in suit. The subtraction happens as follows: other-this

sameSuitAs

public boolean sameSuitAs(Card c)
Returns true if the suit of the cards is the same

sameRankAs

public boolean sameRankAs(Card c)
Returns true if the rank of the cards is the same

isRankPrevOf

public boolean isRankPrevOf(Card other)
Returns whether or not this card is the previous rank of the other card. The Ace is the previous rank of Two.

isRankSuccOf

public boolean isRankSuccOf(Card other)
Returns whether or not this card is the successor rank of the other card. The Two is the successor rank of Ace.

isAlternateSuitOf

public boolean isAlternateSuitOf(Card other)
Returns true if other and this are cards of a different color.

suitToString

public String suitToString()
Returns the suit as a string, for example "Spades"

rankToString

public String rankToString()
Returns the rank as a string, for example "Ace"

toString

public String toString()
Returns a string such as "Ace of Spades" that reflects the rank and suit of a card.
Overrides:
toString in class Object

by Sergey Klibanov and Nate Bayless