-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
80 lines (65 loc) · 2.39 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
// let secondCard = getRandomcard() //7 before random card;
let cards = []
let sum = 0
console.log(cards)
let message = "";
let player = {
name: "prabin",
chips: 124
}
let hasBlackJack = false
// let sum = firstCard + secondCard;
// let cards = [firstCard, secondCard]
let isAlive = false;
let messageEl = document.getElementById("message-el")
let sumEl = document.getElementById("sum-el")
let cardsEl = document.getElementById("card")
let playerEl = document.getElementById("player-el")
playerEl.textContent = player.name + ": $" + player.chips
// function startGame() {
// let firstCard = getRandomcard()
// let SecondCard = getRandomNumber()
// cards = [firstCard, SecondCard]
// sum = firstCard + SecondCard
// renderGame()
// isAlive = true
// }
function renderGame() {
cardsEl.textContent = "cards: ";
for (let i = 0; i < cards.length; i++) {
cardsEl.textContent += cards[i] + " ";
}
sumEl.textContent = "sum: " + sum;
if (sum < 21) {
message = "Do you want to draw a new card ?"
} else if (sum === 21) {
message = "Wohoo! you got a blackjack!"
hasBlackJack = true
} else if (sum > 21) {
message = "Oh! you are out from game"
}
messageEl.textContent = message;
}
function runCard() {
// console.log("Drawing a new card from the deck !")
if (isAlive === false && hasBlackJack === false) {
var card = getRandomcard()
sum += card
cards.push(card)
console.log(cards)
renderGame()
}
}
function getRandomcard() {
let randomNumber = Math.floor(Math.random() * 13) + 1
if (randomNumber > 10) {
return 10;
} else if (randomNumber === 1) {
return 11;
} else {
return randomNumber;
}
}
function ClosePage(){
window.close()
}