|
|
|
(I choose this code, because in most games, Spade ranks higher than Heart, Heart ranks higher than Club, and Club ranks higher than Diamond
|
public class Card { private byte cardSuit; private byte cardRank; } |
|
|
public class Card { private static final String[] Suit = { "*", "d", "c", "h", "s"}; private static final String[] Rank = { "*", "*", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; private byte cardSuit; private byte cardRank; } |
Suit[0] is not used ("*" means error) Suit[1] contains the string "d" (for Diamond) Suit[2] contains the string "c" (for Club) Suit[3] contains the string "h" (for Heart) Suit[4] contains the string "s" (for Spade) |
So:
Suit[ cardSuit ] gives us the suit information of the card Rank[ cardRank ] gives us the rank information of the card |
|
|
|
|
|
Information necessary to create a playing card:
|
A useful constructor:
public Card ( int suit, int rank ) { initial the instance variables "cardSuit" and "cardRank" to represent this play card. } |
|
|
public class Card { public static final int SPADE = 4; public static final int HEART = 3; public static final int CLUB = 2; public static final int DIAMOND = 1; private static final String[] Suit = { "*", "d", "c", "h", "s"}; private static final String[] Rank = { "*", "*", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; private byte cardSuit; private byte cardRank; /* ---------------------------------------------------------------- Constructor Example usage: Card x = new Card( Card.SPADE, 1 ); // Ace of Spade Card x = new Card( Card.HEART, 11 ); // Jack of Heart --------------------------------------------------------------- */ public Card( int suit, int rank ) { if ( rank == 1 (Ace) ) cardRank = 14; // Give Ace the rank 14 else cardRank = (byte) rank; cardSuit = (byte) suit; } } |
public class Card { public static final int SPADE = 4; public static final int HEART = 3; public static final int CLUB = 2; public static final int DIAMOND = 1; private static final String[] Suit = { "*", "d", "c", "h", "s"}; private static final String[] Rank = { "*", "*", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; private byte cardSuit; private byte cardRank; /* ---------------------------------------------------------------- Constructor Example usage: Card x = new Card( Card.SPADE, 1 ); // Ace of Spade Card x = new Card( Card.HEART, 11 ); // Jack of Heart --------------------------------------------------------------- */ public Card( int suit, int rank ) { if ( rank == 1 (Ace) ) cardRank = 14; // Give Ace the rank 14 else cardRank = (byte) rank; cardSuit = (byte) suit; } } |
So to your program, the Card class looks like THIS:
public class Card { public Card( int suit, int rank ) { if ( rank == 1 (Ace) ) cardRank = 14; // Give Ace the rank 14 else cardRank = (byte) rank; cardSuit = (byte) suit; } } |
|
|
You can only do these operations to them:
|
|
|
public class Card { public static final int SPADE = 4; public static final int HEART = 3; public static final int CLUB = 2; public static final int DIAMOND = 1; private static final String[] Suit = { "*", "d", "c", "h", "s"}; private static final String[] Rank = { "*", "*", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; private byte cardSuit; private byte cardRank; /* ---------------------------------------------------------------- Constructor Example usage: Card x = new Card( Card.SPADE, 1 ); // Ace of Spade Card x = new Card( Card.HEART, 11 ); // Jack of Heart --------------------------------------------------------------- */ public Card( int suit, int rank ) { if ( rank == 1 (Ace) ) cardRank = 14; // Give Ace the rank 14 else cardRank = (byte) rank; cardSuit = (byte) suit; } public int suit() { return ( cardSuit ); // This is a shorthand for: // this.cardSuit } public String suitStr() { return( Suit[ cardSuit ] ); // This is a shorthand for: // this.Suit[ this.cardSuit ] } public int rank() { return ( cardRank ); } public String rankStr() { return ( Rank[ cardRank ] ); } } |
|
|
public class Card { public static final int SPADE = 4; public static final int HEART = 3; public static final int CLUB = 2; public static final int DIAMOND = 1; private static final String[] Suit = { "*", "d", "c", "h", "s"}; private static final String[] Rank = { "*", "*", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; private byte cardSuit; private byte cardRank; public Card( int suit, int rank ) { if ( rank == 1 ) cardRank = 14; // Give Ace the rank 14 else cardRank = (byte) rank; cardSuit = (byte) suit; } ..... (some methods omitted for brevity)..... public String toString() { return ( Rank[ cardRank ] + Suit[ cardSuit ] ); } } |
public class Card { public static final int SPADE = 4; public static final int HEART = 3; public static final int CLUB = 2; public static final int DIAMOND = 1; private static final String[] Suit = { "*", "d", "c", "h", "s"}; private static final String[] Rank = { "*", "*", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; private byte cardSuit; private byte cardRank; public Card( int suit, int rank ) { if ( rank == 1 ) cardRank = 14; // Give Ace the rank 14 else cardRank = (byte) rank; cardSuit = (byte) suit; } public int suit() { return ( cardSuit ); // This is a shorthand for: // this.cardSuit } public String suitStr() { return( Suit[ cardSuit ] ); // This is a shorthand for: // this.Suit[ this.cardSuit ] } public int rank() { return ( cardRank ); } public String rankStr() { return ( Rank[ cardRank ] ); } public String toString() { return ( Rank[ cardRank ] + Suit[ cardSuit ] ); } } |