-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
84 lines (70 loc) · 1.97 KB
/
script.js
File metadata and controls
84 lines (70 loc) · 1.97 KB
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
// BlackJack
//card variables
let suits = ['Hearts', 'Clubs', 'Spades', 'Diamonds'];
let values = ['Ace', 'King', 'Queen', 'Jack', '10', '9', '8',
'7', '6', '5', '4', '3', '2'];
//DOM variable
let textArea = document.getElementById('text-area');
let newGameButton = document.getElementById('new-game-button');
let hitButton = document.getElementById('hit-button');
let stayButton = document.getElementById('stay-button');
//game variable
let gameStarted = false,
gameOver = false,
playerWon = false,
dealerCards = [],
playerCards = [],
dealerScore = 0,
playerScore = 0,
deck =[];
hitButton.style.display = 'none';
stayButton.style.display = 'none';
showStatus();
newGameButton.addEventListener('click', function(){
gameStarted = true;
gameOver = false;
playerWon = false;
deck = createDeck();
shuffleDeck(deck);
dealerCards = [getNextCard(), getNextCard()];
playerCards = [getNextCard(), getNextCard()];
newGameButton.style.display = 'none';
hitButton.style.display = 'inline';
stayButton.style.display = 'inline';
showStatus();
});
function createDeck() {
let deck = [];
for (let suitIdx = 0; suitIdx<suits.length; suitIdx++) {
for (let valueIdx = 0; valueIdx<values.length; valueIdx++){
let card = {
suit: suits[suitIdx],
value: values[valueIdx]
};
deck.push(card);
}
}
return deck;
}
function shuffleDeck(deck){
for (let i=0; i<deck.length; i++){
let swapIdx = Math.trunc(Math.random()*deck.length);
let tmp = deck[swapIdx];
deck[swapIdx] = deck[i];
deck[i] = tmp;
}
}
function getCardString(card) {
return card.value + ' of ' + card.suit;
}
function getNextCard() {
return deck.shift();
}
function showStatus(){
if(!gameStarted){
textArea.innerText = 'Welcome to Blackjack'
}
}
for (var i=0; i<deck.length; i++) {
console.log (getCardString(deck[i]));
}