-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
183 lines (124 loc) · 3.33 KB
/
Copy pathCard.java
File metadata and controls
183 lines (124 loc) · 3.33 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
/* Alua Zhanuzakova
* az2879
* Card.java implements a Card object to
* simulate any card found in a 52-card deck.
*/
public class Card implements Comparable<Card>{
private int suit; // integers 1-4 encode the suit
private int rank; // integers 1-13 encode the rank
/**
* Creates a Card object with suit s and rank r
*/
public Card(int s, int r){
suit = s;
rank = r;
}
/**
* Compares two cards first on the basis of their
* ranks, and then based on their suits if the ranks
* are the same.
*
* @param c The card object to be compared to the
* implicit parameter (IP)
* @return A positive integer if the IP is larger
* A negative integer if the IP is smaller
* 0 if the IP is equal to card c
*/
public int compareTo(Card c){
if ((this.rank - c.rank) != 0) {
return this.rank - c.rank;
} else {
return this.suit - c.suit;
}
}
/**
* Overrides Java's equals() method by determining
* whether two Card objects are equal based on the
* output of the compareTo() method.
*
* @param obj Will be upcasted to a Card object and
* compared to the implicit parameter
* @return False if the cards are not equal
* True if the cards are equal
*/
public boolean equals(Object obj) {
Card other = (Card) obj;
return (compareTo(other) == 0);
}
/**
* Overrides Java's toString() method by encoding
* suit values 1-4 to the suits c, d, h, and s,
* respectively to represent Card objects as strings.
*
* @return A string that represents the Card object's
* suit and rank.
*/
public String toString() {
// use this method to easily print a Card object
String cardSuit;
if (suit==1) {
cardSuit = "c";
} else if (suit==2) {
cardSuit = "d";
} else if (suit==3) {
cardSuit = "h";
} else {
cardSuit = "s";
}
return cardSuit + rank;
}
/**
* Returns the int representation of a card's suit.
*
* @return The card's suit
*/
public int getSuit() {
return suit;
}
/**
* Returns the int representation of a card's rank.
*
* @return The card's rank
*/
public int getRank() {
return rank;
}
/**
* Creates a new Card object based on its string
* representation.
*
* @param s The string representation of the card
* @return A Card object based on the suit and rank
* specified in string s.
* @throws IllegalArgumentException if the suit or
* rank input is not valid.
*/
public static Card toCard(String s) {
int intCardSuit;
int intCardRank;
String cardSuit;
String cardRank;
try {
cardSuit = s.substring(0, 1);
cardRank = s.substring(1);
if (cardSuit.equals("c")) {
intCardSuit = 1;
} else if (cardSuit.equals("d")) {
intCardSuit = 2;
} else if (cardSuit.equals("h")) {
intCardSuit = 3;
} else if (cardSuit.equals("s")) {
intCardSuit = 4;
} else {
throw new IllegalArgumentException("Suit must be c, d, h, or s");
}
intCardRank = Integer.parseInt(cardRank);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Rank must be an integer between 1 and 13");
}
if (intCardRank < 1 || intCardRank > 13) {
throw new IllegalArgumentException("Rank must be an integer between 1 and 13");
}
return new Card(intCardSuit, intCardRank);
}
}