This repository has been archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rank.js
154 lines (138 loc) · 4.02 KB
/
rank.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
// rank.js
module.exports = {
getWinner: function(pack){
return getWinner(pack);
},
};
function getNumber(card){
card = card.replace("A","14");
card = card.replace("J","11");
card = card.replace("Q","12");
card = card.replace("K","13");
return Number(card.substring(0,card.length-1));
}
function getSymbol(card){
return card[card.length - 1];
}
function getNthHighCard(cards,n){
var count = [0,0,0];
for(var i=0;i<2;i++){
for(var j=i+1;j<3;j++){
if(getNumber(cards[i]) > getNumber(cards[j]))
count[i]++;
else
count[j]++;
}
}
var index = count.indexOf(3 - n);
return getNumber(cards[index]);
}
function checkTriplet(cards){
if((getNumber(cards[0]) == getNumber(cards[1])) && (getNumber(cards[1]) == getNumber(cards[2])))
return true;
return false;
}
function checkSequence(cards){
var first = getNumber(cards[0]);
var second = getNumber(cards[1]);
var third = getNumber(cards[2]);
if((Math.abs(first - second) == 1) && (Math.abs(second - third) == 1) && (first!=third))
return true;
if((Math.abs(second - first) == 1) && (Math.abs(first - third) == 1) && (second!=third))
return true;
if((Math.abs(first - third) == 1) && (Math.abs(third - second) == 1) && (first!=second))
return true;
return false;
}
function checkColour(cards){
if((getSymbol(cards[0]) == getSymbol(cards[1])) &&(getSymbol(cards[1]) == getSymbol(cards[2])))
return true;
return false;
}
function checkPair(cards){
var first = getNumber(cards[0]);
var second = getNumber(cards[1]);
var third = getNumber(cards[2]);
if(first == second)
return true;
if(first == third)
return true;
if(third == second)
return true;
return false;
}
function getCount(ranks,num){
var count = 0;
for(var i=0;i<ranks.length;i++){
if(ranks[i] == num)
count++;
}
return count;
}
function getSum(cards){
return getNumber(cards[0])+getNumber(cards[1])+getNumber(cards[2]);
}
function getMaxSumIndex(pack,maxRank,ranks){
var maxSum = 0;
var currSum = 0;
var maxIndex = 0;
for(var i=0;i<ranks.length;i++){
if(ranks[i] == maxRank){
currSum = getSum(pack[i]);
if(currSum > maxSum){
maxSum = currSum;
maxIndex = i;
}
}
}
return maxIndex;
}
function getHighValue(pack,maxRank,ranks){
console.log('High value');
console.log('max rank : '+maxRank);
var bigIndex = -1;
for(var i=0;i<ranks.length;i++){
if(ranks[i] == maxRank){
if(bigIndex == -1)
bigIndex = i;
else{
for(var j=1;j<=3;j++){
if(getNthHighCard(pack[i],j) > getNthHighCard(pack[bigIndex],j))
bigIndex = i;
else if(getNthHighCard(pack[i],j) < getNthHighCard(pack[bigIndex],j))
break;
}
}
}
}
return bigIndex;
}
function getWinner(pack){
winner_index = 0
var ranks = [];
for(var i=0;i<pack.length;i++){
if(checkTriplet(pack[i]))
ranks.push(1);
else if(checkSequence(pack[i]) && checkColour(pack[i]))
ranks.push(2);
else if(checkSequence(pack[i]))
ranks.push(3);
else if(checkColour(pack[i]))
ranks.push(4);
else if(checkPair(pack[i]))
ranks.push(5);
else
ranks.push(6);
}
var maxRank = Math.min.apply(null,ranks);
var count = getCount(ranks,maxRank);
if(count == 1){
winner_index = ranks.indexOf(maxRank);
}else{
if(maxRank == 5)
winner_index = getHighValue(pack,maxRank,ranks);
else
winner_index = getMaxSumIndex(pack,maxRank,ranks)
}
return winner_index;
}