-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandChecker.js
206 lines (176 loc) · 5.31 KB
/
handChecker.js
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
// Here we define helper functions to determine if the given hand is a specific poker hand
const cardValues = {
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
T: 10,
J: 11,
Q: 12,
K: 13,
A: 14
};
const checkHand = hand => {
if (hasStraightFlush(hand)) return 9;
if (hasFourOfAKind(hand)) return 8;
if (hasFullHouse(hand)) return 7;
if (hasFlush(hand)) return 6;
if (hasStraight(hand)) return 5;
if (hasThreeOfAKind(hand)) return 4;
if (hasTwoPairs(hand)) return 3;
if (hasOnePair(hand)) return 2;
return 1;
};
const hasStraightFlush = hand => {
if (hasStraight(hand) && hasFlush(hand)) {
return true;
} else {
return false;
}
};
const hasFourOfAKind = hand => {
// Take only face-values of the given hand of cards
let values = hand.map(card => card.charAt(0));
// Counter object to count how many times a specific face-value appears in a hand
let counter = {};
// Calculate how many times face-values appear in a hand
for (let i = 0; i < values.length; i++) {
let num = values[i];
counter[num] = counter[num] ? counter[num] + 1 : 1;
}
// Find the most frequent appearing face-value by taking only the values from the counter object
let maxCount = Math.max(...Object.values(counter));
if (maxCount === 4) {
return true;
} else {
return false;
}
};
const hasFullHouse = hand => {
// Take only face-values of the given hand of cards
let values = hand.map(card => card.charAt(0));
// Counter object to count how many times a specific face-value appears in a hand
let counter = {};
// Calculate how many times face-values appear in a hand
for (let i = 0; i < values.length; i++) {
let num = values[i];
counter[num] = counter[num] ? counter[num] + 1 : 1;
}
// Check if counter's values are 2 and 3
if (
Object.values(counter)
.sort()
.toString() === [2, 3].toString()
) {
return true;
} else {
return false;
}
};
const hasFlush = hand => {
// Take only suits of the given hand of cards
let suits = hand.map(card => card.charAt(1));
// Check if all the values in suits array are the same
if (new Set(suits).size === 1) {
return true;
} else {
return false;
}
};
const hasStraight = hand => {
// Take only face-values of the given hand of cards
let values = hand.map(card => card.charAt(0));
// Counter object to count how many times a specific face-value appears in a hand
let counter = {};
// Calculate how many times face-values appear in a hand
for (let i = 0; i < values.length; i++) {
let num = values[i];
counter[num] = counter[num] ? counter[num] + 1 : 1;
}
// Create array of ranks of the given face-values (according to the cardValues object)
let rankedValues = values.map(value => cardValues[value]);
let rankedDifference = Math.max(...rankedValues) - Math.min(...rankedValues);
// Find the most frequent appearing face-value by taking only the values from the counter object
let maxCount = Math.max(...Object.values(counter));
// Check if
if (rankedDifference === 4 && maxCount === 1) {
return true;
} // Check if the hand has 2, 3, 4, 5 and an Ace
else if (values.sort().toString() === [2, 3, 4, 5, "A"].toString()) {
return true;
} else {
return false;
}
};
const hasThreeOfAKind = hand => {
// Take only face-values of the given hand of cards
let values = hand.map(card => card.charAt(0));
// Counter object to count how many times a specific face-value appears in a hand
let counter = {};
// Calculate how many times face-values appear in a hand
for (let i = 0; i < values.length; i++) {
let num = values[i];
counter[num] = counter[num] ? counter[num] + 1 : 1;
}
// Find the most frequent appearing face-value by taking only the values from the counter object
let maxCount = Math.max(...Object.values(counter));
if (maxCount === 3) {
return true;
} else {
return false;
}
};
const hasTwoPairs = hand => {
// Take only face-values of the given hand of cards
let values = hand.map(card => card.charAt(0));
// Counter object to count how many times a specific face-value appears in a hand
let counter = {};
// Calculate how many times face-values appear in a hand
for (let i = 0; i < values.length; i++) {
let num = values[i];
counter[num] = counter[num] ? counter[num] + 1 : 1;
}
// Check if counter's values are 1, 2, 2
if (
Object.values(counter)
.sort()
.toString() === [1, 2, 2].toString()
) {
return true;
} else {
return false;
}
};
const hasOnePair = hand => {
// Take only face-values of the given hand of cards
let values = hand.map(card => card.charAt(0));
// Counter object to count how many times a specific face-value appears in a hand
let counter = {};
// Calculate how many times face-values appear in a hand
for (let i = 0; i < values.length; i++) {
let num = values[i];
counter[num] = counter[num] ? counter[num] + 1 : 1;
}
// Find the most frequent appearing face-value by taking only the values from the counter object
let maxCount = Math.max(...Object.values(counter));
if (maxCount === 2) {
return true;
} else {
return false;
}
};
module.exports = {
checkHand,
hasStraightFlush,
hasFourOfAKind,
hasFullHouse,
hasFlush,
hasStraight,
hasThreeOfAKind,
hasTwoPairs,
hasOnePair
};