-
Notifications
You must be signed in to change notification settings - Fork 0
/
testingJS.html
363 lines (304 loc) · 12.9 KB
/
testingJS.html
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<html>
<head><script>
function findTheOutlier(){
numberArray = document.getElementById("input").value.split(", ");
//function findTheOutlier(numberArray){
modulusCount = Math.abs((numberArray[0] % 2)) + Math.abs((numberArray[1] % 2)) + Math.abs((numberArray[2] % 2));
if (modulusCount < 2){
return returnOutlier(1, numberArray);
}
return returnOutlier(0, numberArray);
}
function returnOutlier(truthValue, numberArray){
for (i=0; i < numberArray.length; i++) {
modulusValue = Math.abs(numberArray[i]) % 2;
if(modulusValue == truthValue){
return numberArray[i];
}
}
}
function fizzbuzz(){
input = document.getElementById("input").value;
//function fizzbuzz(input){
var output = "";
var modThree, modFive, modSeven;
modThree = input % 3;
modFive = input % 5;
modSeven = input % 7;
if (modThree === 0){
output += "fizz";
}
if (modFive === 0){
output += "buzz";
}
if (modSeven === 0){
output += "wizz";
}
if(output.length === 0){
console.log(input);
return input;
}
console.log(output);
return output;
}
function hand(cardString){
this.cards = cardString.toUpperCase().split(" ").sort(function(a,b){return cardSort(a,b)});
this.straightBool = isStraight(this.cards);
this.flushBool = isFlush(this.cards);
this.firstMatchBool = false;
this.firstMatchCardCount = 1;
this.secondMatchBool = false;
this.secondMatchCardCount = 1;
this.OverallRank = 1;
this.getHighCardFaceValue = function(){return this.faceValueArray.slice(-1)[0];}
}
var faceValueArray = ['2','3','4','5','6','7','8','9','T','J','Q','K','A'];
//Scaffolding//
function handComparison(){
console.clear();
firstHand = document.getElementById("input").value;
secondHand = document.getElementById("secondHand").value;
//Scaffolding//
//Requirements do not specify that cards are all unique and drawn from the same poker deck
//This should be clarified! Typical poker rules are assumed, card suits have no ranking
//function handComparison(firstHand, secondHand){
var output = 1;
//Requirements do not specify if the chars are guaranteed to be uppercase,
//converting to uppercase as a safety precaution
var firstHand_array = firstHand.toUpperCase().split(" ");
var secondHand_array = secondHand.toUpperCase().split(" ");
//Sorting cards from lowest face value to highest, this will help when determining straights or matching sets
firstHand_array.sort(function(a,b){return cardSort(a,b)});
secondHand_array.sort(function(a,b){return cardSort(a,b)});
//returns the rank of each hand, along with any data about matching sets in the same hand (ie full house, 4 of a kind etc.)
//array structure [rank, face value of first matching set, number of cards in first set, face value of second set, number in second set]
var firstHand_rankAndMatchResult = determineRankAndMatchResult(firstHand_array);
var secondHand_rankAndMatchResult = determineRankAndMatchResult(secondHand_array);
//tie breaker for sets that include matching sets in the same hand
var firstHand_tieBreaker;
var secondHand_tieBreaker;
//rankingArray simplifies ordering the rank of the face value of the cards
var rankingArray = ['2','3','4','5','6','7','8','9','T','J','Q','K','A'];
//check to see if the ranks of cards are from one/two pair, three/four of a kind, or a full house
var ranksForMatchingSets = [2,3,4,7,8];
//Tie, will need to determine if a tie breaker is possible
if(firstHand_rankAndMatchResult[0] == secondHand_rankAndMatchResult[0]){
//Hand has a matching set of cards, check to see which set has the higher number of cards (if there is more then one set)
//Then assign the face value of the larger set to the tie breaker fields
if(ranksForMatchingSets.indexOf(firstHand_rankAndMatchResult[0]) >= 0){
firstHand_tieBreaker = firstHand_rankAndMatchResult[2] > firstHand_rankAndMatchResult[4] ? firstHand_rankAndMatchResult[1] : firstHand_rankAndMatchResult[3];
secondHand_tieBreaker = secondHand_rankAndMatchResult[2] > secondHand_rankAndMatchResult[4] ? secondHand_rankAndMatchResult[1] : secondHand_rankAndMatchResult[3];
firstHand_rankAndMatchResult[0] = rankingArray.indexOf(firstHand_tieBreaker);
secondHand_rankAndMatchResult[0] = rankingArray.indexOf(secondHand_tieBreaker);
//Even after a tie breaker between matching sets, the ranks are the same
//Compare the highest card of each hand
if (firstHand_rankAndMatchResult[0] == secondHand_rankAndMatchResult[0]){
firstHand_rankAndMatchResult[0] = rankingArray.indexOf(firstHand_array.slice(-1)[0].charAt(0));
secondHand_rankAndMatchResult[0] = rankingArray.indexOf(secondHand_array.slice(-1)[0].charAt(0));
}
} else {
//No matching sets in the hands, can use the highest card
//Compare the highest card of each hand
firstHand_rankAndMatchResult[0] = rankingArray.indexOf(firstHand_array.slice(-1)[0].charAt(0));
secondHand_rankAndMatchResult[0] = rankingArray.indexOf(secondHand_array.slice(-1)[0].charAt(0));
}
}
//Final outcome
if(firstHand_rankAndMatchResult[0] == secondHand_rankAndMatchResult[0]){
output = 0;
} else if (firstHand_rankAndMatchResult[0] > secondHand_rankAndMatchResult[0]){
output = 1;
} else {
output = -1;
}
console.log("Final Outcome (-1 loss, 0 tie, 1 win): " + output);
return output;
}
//Purpose: Custom sort function to order the cards by face value followed by suit
//Input:
// firstCard - two char string representation of a card from a typical 52 card poker deck
// secondCard - another (unique) two char string representation of a card from the same deck
//Output: Integer - positive int means first is larger then second, negative means second number is larger
function cardSort(firstCard, secondCard){
var firstCard_translatedValue = translateCardValue(firstCard);
var secondCard_translatedValue = translateCardValue(secondCard);
return firstCard_translatedValue - secondCard_translatedValue;
}
//Purpose: convert the two char string, representing a card, into a numerical value for the sorting comparisons
//Input: two char string representing a card from a typical 52 card deck - see requirements for details
//Output: a numeric representation for the priority of the card, can range from 20 (2 of clubs) to 143 (ace of spades)
// The ones digit of the output can only be 0-3
function translateCardValue(card, translateFaceValueOnly){
//Translation Legend
//First Char
//2-9 no change
//T = 10
//J = 11
//Q = 12
//K = 13
//A = 14
//Multiply translated value by 10
/* Please note: the suit priority is assumed, in this function only, and not specified in the requirements */
//Second Char
//S = 3
//H = 2
//D = 1
//C = 0
//Add to translated value
var firstChar = card.charAt(0);
if(firstChar >= '2' && firstChar <= '9'){
cardTranslatedValue = parseInt(firstChar);
} else if (firstChar === 'T') {
cardTranslatedValue = 10;
} else if (firstChar === 'J') {
cardTranslatedValue = 11;
} else if (firstChar === 'Q') {
cardTranslatedValue = 12;
} else if (firstChar === 'K') {
cardTranslatedValue = 13;
} else if (firstChar === 'A') {
cardTranslatedValue = 14;
}
cardTranslatedValue *= 10;
var secondChar = card.charAt(1);
if (secondChar === 'S'){
cardTranslatedValue += 3;
} else if (secondChar === 'H'){
cardTranslatedValue += 2;
} else if (secondChar === 'D'){
cardTranslatedValue += 1;
} //else if 'C' += 0 (no change)
return cardTranslatedValue;
}
//Purpose: Determine if a hand of cards is a flush
//Input: Array of 5, each index of array contains a two char string representing a poker card
//Output: boolean representing if the hand is a flush
function isFlush(handOfCards){
var output = false;//assume not a flush until proven otherwise
if(handOfCards[0].charAt(1) === handOfCards[1].charAt(1) &&
handOfCards[1].charAt(1) === handOfCards[2].charAt(1) &&
handOfCards[2].charAt(1) === handOfCards[3].charAt(1) &&
handOfCards[3].charAt(1) === handOfCards[4].charAt(1)){
output = true;//is a flush
}
return output;
}
//Purpose: Determine if a hand of cards is a straight
//Input: Array of 5, each index of array contains a two char string representing a poker card
//Output: boolean representing if the hand is a straight
function isStraight(handOfCards){
var output = true;//Assume a straight until proven otherwise
var sequenceArray = ['2','3','4','5','6','7','8','9','T','J','Q','K','A'];
var index = sequenceArray.indexOf(handOfCards[0].charAt(0));
for (i = 1; i < handOfCards.length; i++){
if(handOfCards[i].charAt(0) !== sequenceArray[index + i]){
output = false;
break;
}
}
return output;
}
//Purpose: Determine if a hand of cards has any matching sets
//Input: Array of 5, each index of array contains a two char string representing a poker card
//Output: integer representing rank of the hand of cards
// 1 means no match, so no rank
function checkForMatchingSets(handOfCards){
//Need to check for two matches in case there is two pair or full house
var matchA = false;
var matchB = false;
var sizeOfMatchA = 1;
var sizeOfMatchB = 1;
var index;
var output = [1, 0, 0, 0, 0];//rank, card face value of first match, # of matching cards, card face value of second match, # of matching cards for the second set
for(i = 1; i < handOfCards.length; i++){
if(handOfCards[i - 1].charAt(0) === handOfCards[i].charAt(0)){
matchA = true;
sizeOfMatchA++;
output[1] = handOfCards[i - 1].charAt(0);
} else if(matchA === true){
index = i;
break;
}
}
for(i = index + 1; i < handOfCards.length; i++){
if(handOfCards[i - 1].charAt(0) === handOfCards[i].charAt(0)){
matchB = true;
sizeOfMatchB++;
output[3] = handOfCards[i - 1].charAt(0);
} else if(matchB === true){
break;
}
}
if(matchA === true && matchB === true){
//Found two separate matching sets of cards
if(sizeOfMatchA > 2 || sizeOfMatchB > 2){
output[0] = 7;//full house
} else {
output[0] = 3;//two pair
}
} else if(matchA === true || matchB === true){
//Found only one matching set
if (sizeOfMatchA > 3 || sizeOfMatchB > 3){
output[0] = 8; //four of a kind
} else if(sizeOfMatchA > 2 || sizeOfMatchB > 2){
output[0] = 4;//three of a kind
} else {
output[0] = 2;//one pair
}
} else {
output[0] = 1;
}
output[2] = sizeOfMatchA;
output[4] = sizeOfMatchB;
return output;
}
function determineRankAndMatchResult(handOfCards){
var flush = isFlush(handOfCards);
var straight = isStraight(handOfCards);
var output = [1, 0, 0, 0, 0];//rank, matchA's card face value, sizeOfMatchA, matchB's card face value, sizeOfMatchB
if(flush === true && straight === true){
if (handOfCards[4].charAt(0) === 'A'){
//Ace card found as the highest card of the sequence
output[0] = 10;//royal flush
} else {
output[0] = 9;//straight flush
}
} else if (flush === true){
output[0] = 6;//flush
} else if (straight == true){
output[0] = 5;
} else {
output = checkForMatchingSets(handOfCards);
}
console.log(handOfCards.toString() + " Rank: " + output[0] + " Straight: " + straight + " Flush: " + flush);
return output;
}
function randomHand(){
var cardDeck = ["2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS"];
var firstHand = [];
var secondHand = [];
for(i = 0; i < 5; i++){
index = Math.floor(Math.random()* cardDeck.length);
firstHand.push(cardDeck[index]);
cardDeck.splice(index, 1);
}
for(j = 0; j < 5; j++){
index = Math.floor(Math.random()* cardDeck.length);
secondHand.push(cardDeck[index]);
cardDeck.splice(index, 1);
}
document.getElementById("input").value = firstHand.toString().split(",").join(" ");
document.getElementById("secondHand").value = secondHand.toString().split(",").join(" ");
}
</script></head>
<body>
<table>
<tr><td><input type="text" id="input" title="primary input"/></td></tr>
<tr><td><input type="text" id="secondHand" title="secondHand"/></td></tr>
<tr><td><button onClick="findTheOutlier()">findOutlier</button></td></tr>
<tr><td><button onClick="fizzbuzz()">FizzBuzz</button></td></tr>
<tr><td><button onClick="handComparison()">Poker</button></td></tr>
<tr><td><button onClick="randomHand()">shuffle</button></td></tr>
</table>
</body>
</html>