-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHand.java
More file actions
276 lines (238 loc) · 10.2 KB
/
Hand.java
File metadata and controls
276 lines (238 loc) · 10.2 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
This Hand class represent one hand (set of 5 cards).
Assumption : A hand cannot have duplicate cards.
i.e. A hand cannot have two "Club of 2's"
Variables:
cardList - contains the list of card objects each representing a card in a
hand.
handNumber - represents the player number.
handCode - identifies the handtype.
handDesc - used to store the corresponding hand Description from handType
enum property.
isRankSequence - used to represent whether the card ranks in the hand form a
sequence.
isSameSuit - used to represent whether the card suits are all of same type.
rankOrderArray - contains an array of RankCodes in a order of what rank has to
be compared first to last while comparing two hands of same
type.
Ex. for hand input "2D 2H 4C 6D 6D" -> the array will be [2,6]
for hand input "QS JD TS 9S KS" -> [13, 12, 11, 10, 9]
Constructor:
Constructor receives two input:
1. String array of cardString each representing a card.
2. handNumber to identify the player number.
The constructor creates card objects for each card and forms a List of cards as
cardList variable. Then required methods are called to analyse and determine the
hand types and rankOrderArray, and display the hand description.
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import java.util.Comparator;
public class Hand implements Comparable<Hand>{
private List<Card> cardList = new ArrayList<>();
private int handNumber;
private int handCode;
private String handDesc;
private boolean isRankSequence = true;
private boolean isSameSuit = true;
private int[] rankOrderArray;
public Hand(String[] inputHand, int handNumber){
this.setHandNumber(handNumber);
for (String cardString:inputHand) {
this.getCardList().add(new Card(cardString));
}
Collections.sort(this.getCardList());
HashMap<Integer,Integer> rankMap
= this.checkRanksAndSuits(this.getCardList());
this.setRankOrderArray(this.determineHandType(rankMap));
this.displayHandDescription(this.getRankOrderArray(),
this.getHandNumber(),this.getHandDesc());
}
/*
the checkRanksAndSuits method takes the cardList as input and forms rankMap
which contains unique Ranks in a hand as keys and values as the number of
types the corresponding ranks repeated in the hand. It also return the
HashMap - rankMap.
*/
private HashMap<Integer, Integer> checkRanksAndSuits(List<Card> cardList){
HashMap<Integer,Integer> rankMap = new HashMap<>();
int previousRankCode = 0;
int rankCount = 1;
int currentRankCode, currentSuitCode;
int previousSuitCode = 0;
for (Card currentCard: cardList) {
currentRankCode = currentCard.getRankCode();
currentSuitCode = currentCard.getSuitCode();
//logic to check if the current card and previous card are in
//sequence or not. Also identifies repeated ranks and evaluate
//counts of the repeated ranks.
if(previousRankCode != 0){
if(currentRankCode - previousRankCode != 1){
this.setRankSequence(false);
}
if(currentRankCode - previousRankCode == 0){
rankCount++;
}else{
rankCount = 1;
}
}
previousRankCode = currentRankCode;
rankMap.put(currentRankCode, rankCount);
//logic to check if all suits in the hand are same.
if(previousSuitCode == 0){
previousSuitCode = currentSuitCode;
}else if(previousSuitCode != currentSuitCode){
this.setSameSuit(false);
}
}
return rankMap;
}
/*
the determineHandType method takes rankMap as input and, forms and returns
the rankOrderArray.
*/
private int[] determineHandType(Map rankMap){
//creates a list of map entries from the Map so as to sort the Map
//entries based on Values first and then keys. This will give the order
//of ranks of what has to be sorted first to last.
List<Map.Entry<Integer,Integer>> rankMapEntryList
= new ArrayList<Map.Entry<Integer,Integer>>(rankMap.entrySet());
rankMapEntryList.sort(new Comparator<Map.Entry<Integer,Integer>>() {
@Override
public int compare(Map.Entry<Integer,Integer> rankMapPair1,
Map.Entry<Integer,Integer> rankMapPair2) {
if (rankMapPair1.getValue().equals(rankMapPair2.getValue())) {
return rankMapPair2.getKey() - rankMapPair1.getKey();
} else {
return rankMapPair2.getValue() - rankMapPair1.getValue();
}
}
});
int[] rankOrderArray = new int[rankMap.size()];
int[] numOfRankOrderArray = new int[rankMap.size()];
int rankOrderArrayIndex = 0;
for (Map.Entry<Integer,Integer> rankMapEntry: rankMapEntryList ) {
rankOrderArray[rankOrderArrayIndex] = rankMapEntry.getKey();
numOfRankOrderArray[rankOrderArrayIndex] = rankMapEntry.getValue();
rankOrderArrayIndex++;
}
int maxNumOfRank = numOfRankOrderArray[0];
//logic to evaluates the hand Type comparing the
//4 properties(rankMapLength, maxNumOfRank, isRankSequence and
//isSameSuit) of this hand object to all of the handtpe properties in
//the handType enum and assign the handType code and description if a
//match is found.
for(HandType handtype : HandType.values()){
if(handtype.getRankMapLength()==rankMap.size()
&& handtype.getMaxNumOfRank() == maxNumOfRank
&& handtype.isRankSequence() == this.isRankSequence()
&& handtype.isSameSuit() == this.isSameSuit()){
this.setHandCode(handtype.getHandCode());
this.setHandDesc(handtype.getHandDesc());
}
}
return rankOrderArray;
}
/*
displayHandDescription method outputs the required hand description by
taking first two ranks from rankOrderArray and the player Number.
*/
private void displayHandDescription(int[] rankOrderArray, int handNumber,
String handDescription){
String FirstRankDesc = "";
String SecondRankDesc = "";
for(Rank rank : Rank.values()){
if(rankOrderArray[0]==rank.getRankCode()){
if(rank.getRankCode() <=10){
FirstRankDesc = Integer.toString(rank.getRankCode());
}else{
FirstRankDesc = rank.name();
}
}
if(rankOrderArray[1]==rank.getRankCode()){
if(rank.getRankCode() <=10){
SecondRankDesc = Integer.toString(rank.getRankCode());
}else{
SecondRankDesc = rank.name();
}
}
}
System.out.printf("Player " + handNumber + ": " + handDescription,
FirstRankDesc,SecondRankDesc);
}
/*
provides a comparator method for Collection.Sort method to sort the hands
in a list of hand objects based on the handCode first and then based on the
Ranks in the rankOrderArray from index 0 to last index.
*/
@Override
public int compareTo(Hand hand){
int inputHandCode = hand.getHandCode();
int[] inputRankOrderArray = hand.getRankOrderArray();
if(inputHandCode != this.getHandCode()){
return this.getHandCode() - inputHandCode;
}else{
for(int rankOrderArrayIndex = 0 ; rankOrderArrayIndex
< this.getRankOrderArray().length; rankOrderArrayIndex++){
if(inputRankOrderArray[rankOrderArrayIndex]
!= this.getRankOrderArray()[rankOrderArrayIndex]){
return inputRankOrderArray[rankOrderArrayIndex]
- this.getRankOrderArray()[rankOrderArrayIndex];
}
}
return 0;
}
}
//equals method provides a way to compare two hand objects using the
//handCode and the values in rankOrderArray in the corresponding indexes.
public boolean equals(Hand hand){
return this.getHandCode() == hand.getHandCode() &&
Arrays.equals(this.getRankOrderArray(),hand.getRankOrderArray());
}
public int getHandNumber() {
return handNumber;
}
private void setHandNumber(int handNumber) {
this.handNumber = handNumber;
}
private List<Card> getCardList() {
return cardList;
}
private void setCardList(List<Card> cardList) {
this.cardList = cardList;
}
private boolean isRankSequence() {
return isRankSequence;
}
private void setRankSequence(boolean rankSequence) {
isRankSequence = rankSequence;
}
private boolean isSameSuit() {
return isSameSuit;
}
private void setSameSuit(boolean sameSuit) {
isSameSuit = sameSuit;
}
private int getHandCode() {
return handCode;
}
private void setHandCode(int handCode) {
this.handCode = handCode;
}
private String getHandDesc() {
return handDesc;
}
private void setHandDesc(String handDesc) {
this.handDesc = handDesc;
}
private int[] getRankOrderArray() {
return rankOrderArray;
}
private void setRankOrderArray(int[] rankOrderArray) {
this.rankOrderArray = rankOrderArray;
}
}