-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPile.js
169 lines (157 loc) · 4.15 KB
/
Pile.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
const {cardCompare} = require('./index.js');
module.exports = {
replacePile,
}
/**
* @param {Array} oldPileArr
* @param {String} newPileStr
* @return
* @throws
*/
function replacePile(oldPileArr, newPileStr) {
var parts = newPileStr.split(' ');
if(parts.length > 4) throw "ERROR: There cannot be more than 4 cards in a pile."
var newPileArr = [];
parts.forEach(p => {
var text = "";
var rankVal = -1;
var suitVal = -1;
switch(p.charAt(0)) {
case '2':
rankVal = 2;
text = "2_of_"
break;
case '3':
rankVal = 3;
text = "3_of_"
break;
case '4':
rankVal = 4;
text = "4_of_"
break;
case '5':
rankVal = 5;
text = "5_of_"
break;
case '6':
rankVal = 6;
text = "6_of_"
break;
case '7':
rankVal = 7;
text = "7_of_"
break;
case '8':
rankVal = 8;
text = "8_of_"
break;
case '9':
rankVal = 9;
text = "9_of_"
break;
case 'T':
rankVal = 10;
text = "10_of_"
break;
case 'J':
rankVal = 11;
text = "jack_of_"
break;
case 'Q':
rankVal = 12;
text = "queen_of_"
break;
case 'K':
rankVal = 13;
text = "king_of_"
break;
case 'A':
rankVal = 14;
text = "ace_of_"
break;
default:
throw "ERROR: Invalid pile request.";
}
switch(p.charAt(1)) {
case '?':
suitVal = 0;
text += "null";
break;
case 'c':
suitVal = 1;
text += "clubs";
break;
case 'd':
suitVal = 2;
text += "diamonds";
break;
case 'h':
suitVal = 3;
text += "hearts";
break;
case 's':
suitVal = 4;
text += "spades";
break;
default:
throw "ERROR: Invalid pile request.";
}
newPileArr.push({
"text": text,
"rank": p.charAt(0),
"suit": p.charAt(1),
"rankVal": rankVal,
"suitVal": suitVal,
// "imgURL": '/img/' + p.charAt(0) + p.charAt(1) + '.png'
"imgURL": '/img/' + text + '.png'
});
});
if(!check(oldPileArr, newPileArr)) {
throw "ERROR: Invalid pile request.";
}
return newPileArr;
}
/**
* Returns a string representation of an array of cards (no limit to array size)
* @param {Array.<text: String, rank: String, suit: String, rankVal: Number, suitVal: Number>} hand
* - Represents the array of cards in question.
* @return a string representation of the array of cards, such as "Kd 5s Jc Ah Qc"
*/
function pileAsString(hand) {
var str = "";
hand.forEach(c => {
str += c.text + " ";
});
return str;
}
function pileAsURL(hand) {
var imgs = [];
hand.forEach(c => {
imgs.push(c.imgURL);
});
return imgs;
}
/**
*
* @param
* @return
*/
function check(oldPileArr, newPileArr) {
newPileArr.sort((a, b) => cardCompare(b, a));
const checkVal = newPileArr[0].rankVal;
for(var i = 1; i < newPileArr.length; i++) {
if(newPileArr[i].rankVal !== checkVal) {
return false;
}
}
if(oldPileArr.length === 0) {
return true;
}
if(oldPileArr[0].rankVal <= checkVal && oldPileArr.length === newPileArr.length) {
return true;
}
else if(newPileArr[0].rankVal === 2 && newPileArr.length === 1) {
return true;
}
return false;
}