-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.app.js
136 lines (128 loc) · 3.46 KB
/
index.app.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
var rankCards;
var TripleCards, DoubleCards, QuadrupleCards;
var typingTimer;
var doneTypingInterval = 500;
$('#En').keyup(function () {
clearTimeout(typingTimer);
if ($('#En').val()) {
typingTimer = setTimeout(handlePoker, doneTypingInterval);
}
});
/**
* Count the number of triple cards, double cards and Quadruple cards.
* @param {array} rankCards
* Change the values of global variables: TripleCards, DoubleCards, QuadrupleCards
*/
function countCards(rankCards) {
TripleCards = 0;
DoubleCards = 0;
QuadrupleCards = 0;
error = false;
var temp = rankCards[0]
var count = 1;
for (var i = 1; i < 5; i++) {
if (rankCards[i] == temp) {
count++;
}
if (rankCards[i] != temp || i == 4) {
switch (count) {
case 2:
DoubleCards++;
break;
case 3:
TripleCards++;
break;
case 4:
QuadrupleCards++;
break;
case 5:
error = true;
break;
}
;
temp = rankCards[i];
count = 1;
}
}
}
/**
* Sort an array with Selection sort method
* @param {array} rankCards
*
*/
function sortArray(arr, n) {
for (var i = 0; i < n; i++) {
for (var j = 0; j < i; j++) {
if (arr[i] < arr[j]) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
/**
* Validate the poker string.
* @param {string} Poker
* @returns {string}
*/
function validatePoker(Poker) {
var Pattern = /((C|H|D|S)(10|[2-9]|(J|Q|K|A))){5}$/i;
var Cards = new Array;
if (!Pattern.test(Poker)) return "Poker không hợp lệ!";
var countTen =0;
for (var i = 0; i < 5; i++) {
if (Poker.substring(i * 2 + 1 + countTen, i * 2 + 2 + countTen) == "1") {
Cards.push(Poker.substring(i * 2 + countTen, i * 2 + 3 + countTen));
countTen++;
}
else {
Cards.push(Poker.substring(i * 2 + countTen, i * 2 + 2 + countTen));
console.log(i * 2 + 1 + countTen);
}
}
for(var i = 0; i<5;i++)
{
for (var j=i+1;j<5;j++)
{
if(Cards[i]==Cards[j]) return "Có cặp lá Poker bị lặp lại!"
}
}
}
/**
* Check the poker hand and return
* @param Poker
* @returns {string}
*/
function checkPoker(Poker) {
rankCards = new Array();
var countTen = 0;
for (var i = 0; i < 5; i++) {
if (Poker.substring(i * 2 + 1 + countTen, i * 2 + 2 + countTen) == "1") {
rankCards.push(Poker.substring(i * 2 + 1 + countTen, i * 2 + 3 + countTen));
countTen++;
}
else {
rankCards.push(Poker.substring(i * 2 + 1 + countTen, i * 2 + 2 + countTen));
console.log(i * 2 + 1 + countTen);
}
}
sortArray(rankCards, 5);
console.log(rankCards);
countCards(rankCards);
if (QuadrupleCards == 1) return "4C";
if (DoubleCards == 1 && TripleCards == 1) return "FH";
if (TripleCards == 1) return "3C";
if (DoubleCards == 2) return "2P";
if (DoubleCards == 1) return "1P";
return "--";
}
function handlePoker() {
var Poker = $('#En').val();
var Error = validatePoker(Poker);
if (Error) {
$('#Vi').val(Error);
return;
}
$('#Vi').val(checkPoker(Poker));
}