-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·83 lines (77 loc) · 2.67 KB
/
app.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
// Create variable for score, round score, actice player
var score, roundScore, acticePlayer, gamePlaying, previousDice;
// reset score
gameInit();
// Add event for roll dice
document.querySelector(".btn-roll").addEventListener("click", function() {
if (gamePlaying) {
//Add random var
var dice = Math.floor(Math.random() * 6) + 1;
var selectDice = document.querySelector(".dice");
// Add UI for dice
selectDice.src = "dice-" + dice + ".png";
// Display dice
selectDice.style.display = "block";
// Update score (add UI score) and
if (dice !== 1) {
roundScore += dice;
document.querySelector(
"#current-" + acticePlayer
).textContent = roundScore;
} else {
// change player & reset score if dice = 1
changePlayer();
}
}
});
// Add event for hold
document.querySelector(".btn-hold").addEventListener("click", function() {
if (gamePlaying) {
//Add round score to score , show UI
score[acticePlayer] += roundScore;
document.querySelector("#score-" + acticePlayer).textContent =
score[acticePlayer];
//Reset round score
roundScore = 0;
// Check if some one win the game
if (score[acticePlayer] >= 20) {
document.querySelector(".dice").style.display = "none";
document.querySelector("#name-" + acticePlayer).textContent = "Winner";
document
.querySelector(".player-" + acticePlayer + "-panel")
.classList.add("winner");
gamePlaying = false;
} else {
//change player
changePlayer();
}
}
});
// Add event for new game
document.querySelector(".btn-new").addEventListener("click", function() {
// Reset game
gameInit();
});
function changePlayer() {
acticePlayer !== 0 ? (acticePlayer = 0) : (acticePlayer = 1);
roundScore = 0;
document.querySelector("#current-0").textContent = "0";
document.querySelector("#current-1").textContent = "0";
document.querySelector(".player-0-panel").classList.toggle("active");
document.querySelector(".player-1-panel").classList.toggle("active");
document.querySelector(".dice").style.display = "none";
}
function gameInit() {
score = [0, 0];
roundScore = 0;
acticePlayer = 0;
document.querySelector("#current-0").textContent = 0;
document.querySelector("#current-1").textContent = 0;
document.querySelector("#score-0").textContent = 0;
document.querySelector("#score-1").textContent = 0;
document.querySelector(".dice").style.display = "none";
document.querySelector(".player-0-panel").classList.remove("active");
document.querySelector(".player-1-panel").classList.remove("active");
document.querySelector(".player-0-panel").classList.add("active");
gamePlaying = true;
}