source: pages/api/blackjack/checkCardCombinations.js@ e903234

main
Last change on this file since e903234 was ebf5e04, checked in by anastasovv <simon@…>, 2 years ago

Code cleanings

  • Property mode set to 100644
File size: 2.1 KB
Line 
1export function checkIfSameValue(cards) {
2 for (let i = 1; i < cards.length; i++) {
3 if (cards[i][1] !== cards[i-1][1]) {
4 return false;
5 }
6 }
7
8 return true;
9}
10
11function checkIf2CardsAreSameColour(card1, card2) {
12 if (card1[0] === card2[0]) return true;
13 if (card1[0] === 'H' && card2[0] === 'D') return true;
14 if (card1[0] === 'D' && card2[0] === 'H') return true;
15 if (card1[0] === 'S' && card2[0] === 'C') return true;
16 if (card1[0] === 'C' && card2[0] === 'S') return true;
17 return false;
18}
19
20export function checkIfSameColour(cards) {
21 for (let i = 1; i < cards.length; i++) {
22 if (!checkIf2CardsAreSameColour(cards[i], cards[i-1])) {
23 return false;
24 }
25 }
26
27 return true;
28}
29
30export function checkIfSameSuit(cards) {
31 for (let i = 1; i < cards.length; i++) {
32 if (cards[i][0] !== cards[i-1][0]) {
33 return false;
34 }
35 }
36
37 return true;
38}
39
40export function checkIfStraight(cards) {
41 let values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K'];
42
43 let valuesInCards = [];
44 for (let i = 0; i < cards.length; i++) {
45 valuesInCards.push(cards[i][1]);
46 }
47
48 let temp = values.reduce((c, v, i) => Object.assign(c, {[v]: i}), {});
49
50 valuesInCards = valuesInCards.sort((a, b) => temp[a] - temp[b]);
51
52 let idx = values.indexOf(valuesInCards[0]);
53
54 let straight = true;
55
56 for (let i = 0; i < valuesInCards.length; i++) {
57 if (valuesInCards[i] !== values[idx]) {
58 straight = false;
59 break;
60 }
61
62 idx++;
63 if (idx >= temp.length) {
64 straight = false;
65 break;
66 }
67 }
68
69 if (straight) {
70 return true;
71 }
72
73 values = ['2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K', 'A'];
74 temp = values.reduce((c, v, i) => Object.assign(c, {[v]: i}), {});
75
76 valuesInCards = valuesInCards.sort((a, b) => temp[a] - temp[b]);
77
78 idx = values.indexOf(valuesInCards[0]);
79
80 for (let i = 0; i < valuesInCards.length; i++) {
81 if (valuesInCards[i] !== values[idx]) return false;
82
83 idx++;
84 if (idx >= temp.length) return false;
85 }
86
87 return true;
88}
Note: See TracBrowser for help on using the repository browser.