-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
174 lines (146 loc) · 4.36 KB
/
game.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
170
171
172
173
174
const computerPoints = document.querySelector(".computer-counter");
const computerCards = document.querySelector("#computer-cards");
const userCards = document.querySelector("#user-cards");
const userPoints = document.querySelector(".user-counter");
const btnHit = document.querySelector(".hit");
const btnStand = document.querySelector(".stand");
const btnRestart = document.querySelector(".restart");
const messageDisplay = document.querySelector(".message");
let countComputerPoints;
let countUserPoints;
let deck;
let userHand = [];
let computerHand = [];
btnHit.disabled = false;
btnStand.disabled = false;
const startGame = () => {
resetDefaultValues();
//Give computer 2 cards
giveCard(computerCards, computerHand, 2);
countComputerPoints = countPoints(computerHand);
computerPoints.textContent = countComputerPoints;
// Give user 2 cards
giveCard(userCards, userHand, 2);
countUserPoints += countPoints(userHand);
userPoints.textContent = countUserPoints;
checkRules();
};
const giveCard = (player, hand, n) => {
for (let i = 0; i < n; i++) {
let cardImg = document.createElement("img");
let card = deck.pop();
hand.push(card);
cardImg.src = "./cards/" + card + ".png";
player.append(cardImg);
}
};
const createDeck = () => {
//prettier-ignore
let values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"];
let suits = ["C", "D", "H", "S"];
deck = [];
for (let i = 0; i < values.length; i++) {
for (let j = 0; j < suits.length; j++) {
deck.push(values[i] + "-" + suits[j]);
}
}
};
const suffleDeck = () => {
for (let i = 0; i < deck.length; i++) {
let random = Math.floor(Math.random() * deck.length);
let temp = deck[i];
deck[i] = deck[random];
deck[random] = temp;
}
};
const resetDefaultValues = () => {
countComputerPoints = 0;
countUserPoints = 0;
createDeck();
suffleDeck();
};
const checkRules = () => {
if (countUserPoints > 21 && countComputerPoints > 21) {
messageDisplay.textContent = `Both lost the game, try again!`;
disableBtn();
} else if (countUserPoints > 21) {
messageDisplay.textContent = `You lost the game, try again!`;
disableBtn();
} else if (countComputerPoints > 21) {
messageDisplay.textContent = `You win! Computer > 21 points!`;
disableBtn();
} else if (countComputerPoints === 21 && countUserPoints === 21) {
messageDisplay.textContent = `It's a draw!`;
} else if (countComputerPoints === 21) {
messageDisplay.textContent = `BLACKJACK! Computer win!!`;
disableBtn();
} else if (countUserPoints === 21) {
messageDisplay.textContent = `BLACKJACK! You win!!`;
disableBtn();
} else {
messageDisplay.textContent = `Do you want HIT or STAND?`;
}
};
const disableBtn = () => {
btnHit.disabled = true;
btnStand.disabled = true;
};
const countPoints = (cards) => {
let acesNumber = 0;
let result = 0;
for (let i = 0; i < cards.length; i++) {
let data = cards[i].split("-");
let value = data[0];
switch (value) {
case "Q":
case "K":
case "J":
case "T":
result += 10;
break;
case "A":
result += 11;
acesNumber++;
break;
default:
result += parseInt(value);
break;
}
}
while (result > 21 && acesNumber > 0) {
result = result - 10;
acesNumber--;
}
return result;
};
const resetGame = () => {
document.location.reload(true);
};
const hitOption = () => {
giveCard(userCards, userHand, 1);
countUserPoints = countPoints(userHand);
userPoints.textContent = countUserPoints;
checkRules();
};
const standOption = () => {
computerCheck();
if (countUserPoints > countComputerPoints || countComputerPoints > 21) {
messageDisplay.textContent = `😍😍 You win!!`;
} else if (countComputerPoints > countUserPoints) {
messageDisplay.textContent = `Oh noo.. 😭😭 Computer win!`;
} else if (countComputerPoints === countUserPoints) {
messageDisplay.textContent = `It's a draw!`;
}
disableBtn();
};
const computerCheck = () => {
if (countComputerPoints < 17) {
giveCard(computerCards, computerHand, 1);
countComputerPoints = countPoints(computerHand);
computerPoints.textContent = countComputerPoints;
}
};
startGame();
btnHit.addEventListener("click", hitOption);
btnRestart.addEventListener("click", resetGame);
btnStand.addEventListener("click", standOption);