Card.
java quarta-feira, 21 de abril de 2021 15:52
1 package [Link];
2
3 // [Link] - John K. Estell - 8 May 2003
4 // last modified: 21 April 2021
5 // Implementation of a playing card. Uses classes Rank and Suit
for
6 // expressing the card value.
7
8
9 import [Link];
10 import [Link];
11 import [Link];
12 import [Link];
13
14 /**
15 * Representation of a single playing card. A card consists of a
suit value
16 * (e.g. hearts, spades), a {@link Rank} value (e.g. ace, 7,
king), and an image of
17 * the front of the card. A card object is immutable; once
instantiated, the
18 * values cannot change.
19 *
20 * @author John K. Estell
21 * @author Update Everton Menor
22 * @version 1.1
23 */
24
25 public class Card implements Comparable<Card>{
26
27 // instance variables for the card
28 private final Suit suitValue;
29 private final Rank rankValue;
30 private Player player;
31 private Image cardImage = null;
32 private static boolean sortRankMajorOrder = true;
33 private boolean faceDown;
34
35
36 /**
37 * Creates a new playing card.
38 * @param suit the suit value of this card.
Page 1
[Link] quarta-feira, 21 de abril de 2021 15:52
39 * @param rank the rank value of this card.
40 * @param cardFace the face image of this card.
41 */
42
43 public Card( Suit suit, Rank rank, Image cardFace ) {
44 cardImage = cardFace;
45 suitValue = suit;
46 rankValue = rank;
47 }
48
49 public Card( Suit suit, Rank rank) {
50 suitValue = suit;
51 rankValue = rank;
52
53 }
54
55
56
57 /**
58 * Generates the filename associated with the card.
<code>getFilename</code> assumes that all of the standard card
images
59 * are stored in individual files using filenames in the form
of:
60 * <b>[Link]</b> where <b>R</b> is a single character used to
represent
61 * the rank value of the card and <b>S</b> is a single
character used to represent
62 * the suit value of the card.
63 * <p>The characters used for <b>R</b> are:
64 * 'a' (ace), '2', '3', '4', '5', '6', '7', '8', '9',
65 * 't' (10), 'j' (jack), 'q' (queen), and 'k' (king).
66 * <p>The characters used for <b>S</b> are:
67 * 'c' (clubs), 'd' (diamonds), 'h' (hearts), and 's'
(spades).
68 * <p>Two other cards are also available: "[Link]" (back of
card) and "[Link]" (joker).
69 *
70 * @param suit the suit value of the card.
71 * @param rank the rank value of the card.
72 * @return a string containing the filename of the card.
73 */
Page 2
[Link] quarta-feira, 21 de abril de 2021 15:52
74 public static String getFilename( Suit suit, Rank rank ) {
75 return [Link]() + [Link]() + ".gif";
76 }
77
78
79 /**
80 * Returns the suit of the card.
81 * @return a Suit constant representing the suit value of the
card.
82 */
83 public Suit getSuit() {
84 return suitValue;
85 }
86
87
88 /**
89 * Returns the rank of the card.
90 * @return a Rank constant representing the rank value of the
card.
91 */
92 public Rank getRank() {
93 return rankValue;
94 }
95
96 public void setPlayer(Player p){
97 [Link] = p;
98 }
99
100 public Player getPlayer() {
101 return player;
102 }
103
104
105 /**
106 * Returns the graphic image of the card.
107 * @return an icon containing the graphic image of the card.
108 */
109 public Image getCardImage() {
110 return cardImage;
111 }
112
113
Page 3
[Link] quarta-feira, 21 de abril de 2021 15:52
114 /**
115 * Returns a description of this card.
116 * @return the name of the card.
117 */
118 public String toString() {
119 return [Link]() + " of " +
[Link]();
120 }
121
122
123 /**
124 * Returns a description of the rank of this card.
125 * @return the rank value of the card as a string.
126 */
127 public String rankToString() {
128 return [Link]();
129 }
130
131
132 /**
133 * Returns a description of the suit of this card.
134 * @return the suit value of the card as a string.
135 */
136 public String suitToString() {
137 return [Link]();
138 }
139
140
141 /**
142 * Specifies that cards are to be sorted in rank-major order.
Cards are ordered
143 * first by their rank value; cards of the same rank are then
ordered by their
144 * suit value.
145 */
146 public static void setRankMajorSort() {
147 sortRankMajorOrder = true;
148 }
149
150
151 /**
152 * Specifies that cards are to be sorted in suit-major order.
Page 4
[Link] quarta-feira, 21 de abril de 2021 15:52
Cards are ordered
153 * first by their suit value; cards of the same suit are then
ordered by their
154 * rank value.
155 */
156 public static void setSuitMajorSort() {
157 sortRankMajorOrder = false;
158 }
159
160
161 /**
162 * Compares two cards for the purposes of sorting.
163 * Cards are ordered first by their suit value, then by their
164 * rank value.
165 * @param otherCardObject the other card
166 * @return a negative integer, zero, or a positive integer is
this card is
167 * less than, equal to, or greater than the referenced card.
168 */
169 public int compareTo( Card otherCard ) {
170 int suitDiff = [Link]( [Link] );
171 int rankDiff = [Link]( [Link] );
172
173 if ( sortRankMajorOrder ) {
174 if ( rankDiff != 0 )
175 return rankDiff;
176 else
177 return suitDiff;
178 }
179 else {
180 if ( suitDiff != 0 )
181 return suitDiff;
182 else
183 return rankDiff;
184 }
185 }
186
187
188 /**
189 * Compares two cards to determine if they have the same
value.
190 * This is not the same as the use of <code>equals</code>
Page 5
[Link] quarta-feira, 21 de abril de 2021 15:52
which compares
191 * two objects for equality.
192 * @param card the other card
193 * @return <code>true</code> if the two cards have the same
rank and suit
194 * values, <code>false</code> if they do not.
195 */
196 public boolean isSameAs( Card card ) {
197 if ( ( rankValue != [Link] ) || ( suitValue !=
[Link] ) )
198 return false;
199 else
200 return true;
201 }
202
203
204
205 }
206
Page 6