-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
293 lines (272 loc) · 9.21 KB
/
main.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'use strict';
var card_point = {'2':0, '3':1, '4':2, '5':3,
'6':4, '7':5, '8':6, '9':7,
'T':8, 'J':9, 'Q':10, 'K':11, 'A':12};
function get_point(x){
return card_point[x.substring(0,1)];
}
function quick_sort_card(x, left, right){
if (right <= left) return;
var i = left;
var j = right;
var key = x[left];
while(i<j){
while(i<j && get_point(key)<=get_point(x[j])){
j = j-1;
}
x[i] = x[j];
while(i<j && get_point(key)>=get_point(x[i])){
i = i+1;
}
x[j] = x[i];
}
x[i] = key;
quick_sort_card(x, left, i-1);
quick_sort_card(x, i+1, right);
}
function sort_poker(x){
quick_sort_card(x, 0, x.length-1);
}
function get_suit(x){
return x.substring(1);
}
function is_two_same_suit(x, y){
return get_suit(x)==get_suit(y);
}
function is_flush(x){
return is_two_same_suit(x[0], x[1]) && is_two_same_suit(x[1], x[2]) && is_two_same_suit(x[2], x[3]) && is_two_same_suit(x[3], x[4]);
}
function is_straight(x){
var result = true;
for(var i=1; i<5; i++){
result = result && (get_point(x[i])-get_point(x[i-1])==1);
}
return result;
}
function is_straight_flush(x){
return is_flush(x) && is_straight(x);
}
function get_sum_value(x){
var result = 0;
for(var i=0; i<5; i++){
result = result + get_point(x[i]);
}
return result;
}
function is_four_of_a_kind(x){
var count = 1;
for(var i=1; i<5; i++){
if (get_point(x[0])==get_point(x[i])) count = count + 1;
}
if (count == 4) return true;
else count = 1;
for(var i=2; i<5; i++){
if (get_point(x[1])==get_point(x[i])) count = count + 1;
}
if (count == 4) return true;
else return false;
}
function get_value_of_four_kind(x){
var count = 1;
for(var i=1; i<5; i++){
if (get_point(x[0])==get_point(x[i])) count = count + 1;
}
if (count == 4) return get_point(x[0]);
else return get_point(x[1]);
}
function is_three_of_a_kind(x){
var result = new Array()
var key = get_point(x[2]);
if (key==get_point(x[0]) || key==get_point(x[4])){
result.push(true);
result.push(key);
}
else if(get_point(x[1])==get_point(x[3])){
result.push(true);
result.push(get_point(x[1]));
}
return result;
}
function is_one_pair(x){
var result = new Array()
for (var i=1; i<5; i++){
if(get_point(x[i])==get_point(x[i-1])){
result.push(true);
result.push(get_point(x[i]));
break;
}
}
return result;
}
function are_two_pairs(x){
var result = new Array();
for(var i=1; i<5; i++){
if(get_point(x[i-1])==get_point(x[i])){
result.push(get_point(x[i]));
}
}
if (result.length>1) result.unshift(true);
return result;
}
function compare_poker(x){
// return 0;
var black_cards = x.split(' ').slice(1, 6);
var white_cards = x.split(' ').slice(7, 12);
console.log(black_cards, white_cards);
sort_poker(white_cards);
sort_poker(black_cards);
console.log(black_cards, white_cards);
// Straight flush
var is_black_straight_flush = is_straight_flush(black_cards);
var is_white_straight_flush = is_straight_flush(white_cards);
if (is_black_straight_flush && (!is_white_straight_flush)) return 'Black wins';
if (is_white_straight_flush && (!is_black_straight_flush)) return 'White wins';
var white_value = get_sum_value(white_cards);
var black_value = get_sum_value(black_cards);
console.log(black_value, white_value);
if (is_white_straight_flush && is_black_straight_flush){
if(white_value>black_value){
return 'White wins';
}
else if(white_value<black_value){
return 'Black wins';
}
else{
return 'Tie';
}
}
// Four of a kind
var is_black_four_of_a_kind = is_four_of_a_kind(black_cards);
var is_white_four_of_a_kind = is_four_of_a_kind(white_cards);
if (is_black_four_of_a_kind && (!is_white_four_of_a_kind)) return 'Black wins';
if (is_white_four_of_a_kind && (!is_black_four_of_a_kind)) return 'White wins';
if (is_white_four_of_a_kind && is_black_four_of_a_kind){
var white_value_of_four_kind = get_value_of_four_kind(white_cards);
var black_value_of_four_kind = get_value_of_four_kind(black_cards);
if(white_value_of_four_kind>black_value_of_four_kind){
return 'White wins';
}
else if(white_value_of_four_kind<black_value_of_four_kind){
return 'Black wins';
}
else{
return 'Tie';
}
}
// Full House
var black_has_three_of_a_kind = is_three_of_a_kind(black_cards);
var white_has_three_of_a_kind = is_three_of_a_kind(white_cards);
var black_has_one_pair = is_one_pair(black_cards);
var white_has_one_pair = is_one_pair(white_cards);
var is_black_full_house = black_has_three_of_a_kind[0] && black_has_one_pair[0];
var is_white_full_house = white_has_three_of_a_kind[0] && white_has_one_pair[0];
if (is_black_full_house && (!is_white_full_house)) return 'Black wins';
if (is_white_full_house && (!is_black_full_house)) return 'White wins';
if (is_white_full_house && is_black_full_house){
if(white_has_three_of_a_kind[1]>black_has_three_of_a_kind[1]){
return 'White wins';
}
else if(white_has_three_of_a_kind[1]<black_has_three_of_a_kind[1]){
return 'Black wins';
}
else{
return 'Tie';
}
}
// Flush
var is_black_flush = is_flush(black_cards);
var is_white_flush = is_flush(white_cards);
if (is_black_flush && (!is_white_flush)) return 'Black wins';
if (is_white_flush && (!is_black_flush)) return 'White wins';
if (is_white_flush && is_black_flush){
if(white_value>black_value){
return 'White wins';
}
else if(white_value<black_value){
return 'Black wins';
}
else{
return 'Tie';
}
}
// Straight
var is_black_straight = is_flush(black_cards);
var is_white_straight = is_flush(white_cards);
if (is_black_straight && (!is_white_straight)) return 'Black wins';
if (is_white_straight && (!is_black_straight)) return 'White wins';
if (is_white_straight && is_black_straight){
if(white_value>black_value){
return 'White wins';
}
else if(white_value<black_value){
return 'Black wins';
}
else{
return 'Tie';
}
}
// Three of a Kind
if (white_has_three_of_a_kind[0] && black_has_three_of_a_kind[0]){
if(white_has_three_of_a_kind[1]>black_has_three_of_a_kind[1]){
return 'White wins';
}
else if(white_has_three_of_a_kind[1]<black_has_three_of_a_kind[1]){
return 'Black wins';
}
else{
return 'Tie';
}
}
// two pairs
var is_black_two_pairs = are_two_pairs(black_cards);
var is_white_two_pairs = are_two_pairs(white_cards);
if (is_black_two_pairs[0] && (!is_white_two_pairs[0])) return 'Black wins';
if (is_white_two_pairs[0] && (!is_black_two_pairs[0])) return 'White wins';
if (is_white_two_pairs[0] && is_black_two_pairs[0]){
if(is_white_two_pairs[2]>is_black_two_pairs[2]){
return 'White wins';
}
else if(is_white_two_pairs[2]<is_black_two_pairs[2]){
return 'Black wins';
}
else if(is_white_two_pairs[1]<is_black_two_pairs[1]){
return 'Black wins';
}
if(is_white_two_pairs[1]>is_black_two_pairs[1]){
return 'White wins';
}
else if(white_value>black_value){
return 'White wins';
}
else if(white_value<black_value){
return 'Black wins';
}
else{
return 'Tie';
}
}
// one pair
if (black_has_one_pair[0] && (!white_has_one_pair)) return 'Black wins';
if (white_has_one_pair[0] && (!black_has_one_pair[0])) return 'White wins';
if (white_has_one_pair[0] && black_has_one_pair[0]){
if(white_has_one_pair[1]>black_has_one_pair[1]){
return 'White wins';
}
else if(white_has_one_pair[1]<black_has_one_pair[1]){
return 'Black wins';
}
}
// others
if(get_point(white_cards[4])>get_point(black_cards[4])) return 'White wins';
else if(get_point(white_cards[4])<get_point(black_cards[4])) return 'Black wins';
if(get_point(white_cards[3])>get_point(black_cards[3])) return 'White wins';
else if(get_point(white_cards[3])<get_point(black_cards[3])) return 'Black wins';
if(get_point(white_cards[2])>get_point(black_cards[2])) return 'White wins';
else if(get_point(white_cards[2])<get_point(black_cards[2])) return 'Black wins';
if(get_point(white_cards[1])>get_point(black_cards[1])) return 'White wins';
else if(get_point(white_cards[1])<get_point(black_cards[1])) return 'Black wins';
if(get_point(white_cards[0])>get_point(black_cards[0])) return 'White wins';
else if(get_point(white_cards[0])<get_point(black_cards[0])) return 'Black wins';
else return 'Tie';
}
module.exports = compare_poker;