-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
executable file
·209 lines (158 loc) · 3.65 KB
/
Card.java
File metadata and controls
executable file
·209 lines (158 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* Class to handle hands
*
* @author Herb Wolfe Jr
*
* TODO:
* Add validation to setCard()
*
*/
package maverick;
import static java.lang.System.*;
public class Card {
private short aCard;
private Card() {
this.setCard((short)0);
}
public Card(Card c) {
this.aCard = c.aCard;
}
public Card(short rank, short suit) {
this();
this.setCard((short)(rank + suit));
}
boolean isValidRank(short rank) {
boolean valid = false;
//return valid;
return true;
}
boolean isValidSuit(short suit) {
boolean valid = false;
//return valid;
return true;
}
/**
* Returns the internal representation of the current card
*/
short getCard() {
return this.aCard;
}
/**
* Returns a short representation of the rank
*/
short getRank() {
return (short)(this.aCard & Maverick.RANKMASK);
}
/**
* Returns a character representing the rank of a card, such as 'A' or '7'
*/
char getRankChar() {
short idx = this.getRankIdx();
return Maverick.RANKCHARS[idx];
}
/**
* Returns the index in the SUIT array of the card
*/
private short getRankIdx() {
short aRank = this.getRank();
short idx = -1;
for (byte i = 0; i < Maverick.RANKS; i++) {
if (aRank == Maverick.RANKLIST[i]) {
idx = i;
break;
}
}
if (idx == -1) {
// change to exception ?
out.println("Error! card's rank not found!");
out.println("Card: " + this.aCard);
out.println("Rank: " + aRank);
System.exit(-1);
}
return idx;
}
/**
* Returns a String representing the rank of the card, such as 'ACE'
*/
private String getRankStr() {
short idx = this.getRankIdx();
return Maverick.LONGRANKS[idx];
}
/**
* Returns a byte representation of the suit
*/
byte getSuitByte() {
return (byte)(this.aCard & Maverick.SUITMASK);
}
/**
* Returns a character representing the suit of a card, 'C','D','H', or 'S'
*/
char getSuitChar() {
short idx = this.getSuitIdx();
return Maverick.SUITCHARS[idx];
}
/**
* Returns the index in the SUIT array of the card
*/
private short getSuitIdx() {
byte aSuit = this.getSuitByte();
short idx = -1;
for (byte i = 0; i < Maverick.SUITS; i++) {
if (aSuit == Maverick.SUIT[i]) {
idx = i;
break;
}
}
if (idx == -1) {
// change to exception ?
out.println("Error! card's suit not found!");
out.println("Card: " + aCard);
out.println("Suit: " + aSuit);
System.exit(-1);
}
return idx;
}
/**
* Returns the name of the suit, such as "CLUBS"
*/
private String getSuitStr() {
short idx = this.getSuitIdx();
return Maverick.LONGSUITS[idx];
}
/**
* Set the card
* @param short - the internal representation of the card
*/
public void setCard(short aCard) {
short rank, suit;
rank = (short)(aCard & Maverick.RANKMASK);
suit = (short)(aCard & Maverick.SUITMASK);
if (this.isValidRank(rank) && this.isValidSuit(suit)) {
this.aCard = aCard;
} else {
// throw exception?
out.println("Error! Invalid card!");
}
}
/**
* Converts a card to a long string, such as "ACE of CLUBS"
*/
public String toLongString() {
return this.getRankStr() + " of " + this.getSuitStr();
}
/**
* Converts a card to a 2 character string, such as "AH" or "7C"
*/
public String toString() {
char rankCh, suitCh;
String str = "";
rankCh = this.getRankChar();
suitCh = this.getSuitChar();
str += rankCh;
str += suitCh;
/* test code
str += (" " + Short.toString(this.getCard()));
*/
return str;
}
}