-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (76 loc) · 2.01 KB
/
index.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
let firstCard;
let secondCard;
let cards = [];
let hasBlackJack = false;
let isAlive;
let messageMain = document.getElementById('message-el');
let sumEl = document.querySelector("#sum-el");
let cardsEl = document.querySelector("#cards-el");
let sum = 0;
let newcard = document.querySelector("#new-card");
let player = {
Name: "Perez",
Chips: 145
}
let playerEl = document.querySelector('#amount');
playerEl.textContent = player.Name + ": $" + player.Chips;
function getRandomIntInclusive() {
/* min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min); //The maximum is inclusive and the minimum is inclusive
*/
r = Math.floor(Math.random()*13)+ 1;
console.log(r)
if( r === 1){
return 11;
}else if(r > 10 ){
return 10;
}
else{
return r;
}
}
function newCard(){
if(sum < 21){
let x = getRandomIntInclusive();
cards.push(x);
sum += x;
checkSum();
cardShow();
sumEl.textContent = 'Sum: ' + (sum);
console.log("x: " + x);
console.log('cards: ' + cards)
console.log("New Sum: " + sum)
}
}
function playGame(){
isAlive = true;
cards = [];
cards[0] = getRandomIntInclusive();
cards[1] = getRandomIntInclusive();
sum = cards[0] + cards[1];
console.log('sum = ' + sum)
checkSum();
sumEl.textContent = 'Sum: ' + sum;
cardShow();
newcard.addEventListener('click',newCard);
}
function checkSum(){
if (sum < 21) {
messageMain.textContent = "Draw a new card? ";
}
else if (sum === 21) {
messageMain.textContent = "You got blackjack! ";
hasBlackJack = true;
}
else {
messageMain.textContent = "You are out of the game! ";
isAlive = false;
}
}
function cardShow(){
cardsEl.textContent = "Cards: ";
for(let count = 0; count < cards.length; count++){
cardsEl.textContent += cards[count] + " ";
}
}