-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathscript.js
126 lines (102 loc) · 3.38 KB
/
script.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
var dealerSum = 0;
var yourSum = 0;
// count the number of aces, to track the value of aces.
var dealerAceCount = 0;
var yourAceCount = 0;
var hidden; //track hidden card of the dealer
var deck;
var canHit = true; //allows you to draw while yourSum <= 21
window.onload = function() {
buildDeck();
shuffleDeck();
startGame();
}
function buildDeck(){
let values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
let types = ["C", "D", "H", "S"];
deck = [];
for (let i=0; i < types.length; i++){
for(let j=0; j< values.length; j++){
deck.push(values[j] + "-" + types[i]);
}
}
}
function shuffleDeck(){
for(let i=0; i<deck.length; i++){
let j = Math.floor(Math.random() * deck.length);
let temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
}
function startGame(){
hidden = deck.pop();
dealerSum += getValue(hidden);
dealerAceCount += checkAce(hidden);
while(dealerSum < 17){
let cardImg = document.createElement("img");
let card = deck.pop();
cardImg.src = "./assets/" + card + ".png";
dealerSum += getValue(card);
dealerAceCount += checkAce(card);
document.getElementById("dealer-cards").append(cardImg);
}
console.log(dealerSum);
for(let i=0; i<2; i++){
let cardImg = document.createElement("img");
let card = deck.pop();
cardImg.src = "./assets/" + card + ".png";
yourSum += getValue(card);
yourAceCount += checkAce(card);
document.getElementById("your-cards").append(cardImg);
}
document.getElementById("hit").addEventListener("click", hit);
document.getElementById("stay").addEventListener("click", stay);
}
function hit(){
if(!canHit) return;
let cardImg = document.createElement("img");
let card = deck.pop();
cardImg.src = "./assets/" + card + ".png";
yourSum += getValue(card);
yourAceCount += checkAce(card);
document.getElementById("your-cards").append(cardImg);
if(reduceAce(yourSum, yourAceCount) > 21){
canHit = false;
}
}
function stay(){
dealerSum = reduceAce(dealerSum, dealerAceCount);
yourSum = reduceAce(yourSum, yourAceCount);
canHit = false;
document.getElementById("hidden").src = "./assets/" + hidden + ".png";
let message = "";
if(yourSum > 21) message = "You Lose!";
else if(dealerSum > 21) message = "You Win!";
else if(yourSum == dealerSum) message = "Tie!";
else if(yourSum > dealerSum) message = "You Win!";
else if(yourSum < dealerSum) message = "You Lose!";
document.getElementById("results").innerText = message;
document.getElementById("dealer-sum").innerText = dealerSum;
document.getElementById("your-sum").innerText = yourSum;
}
function reduceAce(playerSum, playerAceCount){
while(playerSum > 21 && playerAceCount > 0){
playerSum -= 10;
playerAceCount -= 1;
}
return playerSum;
}
function getValue(card){
let data = card.split("-");
let value = data[0];
if(isNaN(value)){
if(value == "A") return 11;
else return 10;
}
return parseInt(value);
}
function checkAce(card){
if(card[0] == "A") return 1;
else return 0;
}