-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
101 lines (85 loc) · 3.39 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var score, roundScore, activePlayer, dice, gamePlaying;
//console.log(dice);
//document.querySelector("#current-" + activePlayer).textContent = dice;
//GETTER
//if we want to put some HTML content then we need to put the HTML method.
//document.querySelector("#current-0").innerHTML = "<em>" + dice + "</em>";
//if WE do this without innerHTML by using textContent then it will not display correctly..
//document.querySelector("#score-0").textContent = "<em>" + dice + "</em>";
//SETTER
//This SETS the value of the variable x equals to value of the element i.e.44
//x=document.querySelector('#score-0').textContent;
//If at the starting we want to set the dice not to display then we need to
init();
document.querySelector(".btn-roll").addEventListener("click", function() {
if (gamePlaying) {
//1. random Number
var dice = Math.floor(Math.random() * 6) + 1;
//2. Display the Results
var diceDOM = document.querySelector(".dice");
diceDOM.style.display = "block";
diceDOM.src = "dice-" + dice + ".png";
//3.update the round number if the score was not 1
if (dice !== 1) {
roundScore += dice;
document.getElementById(
"current-" + activePlayer
).textContent = roundScore;
} else {
//next player
nextPlayer();
}
}
});
//pressing the hold button
document.querySelector(".btn-hold").addEventListener("click", function() {
if (gamePlaying) {
score[activePlayer] += roundScore;
document.getElementById("score-" + activePlayer).textContent =
score[activePlayer];
//Check if the player wins the game
if (score[activePlayer] >= 20) {
document.querySelector("#name-" + activePlayer).textContent = "Winner!";
document.querySelector(".dice").style.display = "none";
document
.querySelector(".player-" + activePlayer + "-panel")
.classList.add("winner");
document
.querySelector(".player-" + activePlayer + "-panel")
.classList.remove("active");
gamePlaying = false;
} else {
//change the player
nextPlayer();
}
}
});
//pressing the new game button!
document.querySelector(".btn-new").addEventListener("click", init);
//new player function
function nextPlayer() {
activePlayer === 0 ? (activePlayer = 1) : (activePlayer = 0);
roundScore = 0;
document.getElementById("current-0").textContent = "0";
document.getElementById("current-1").textContent = "0";
document.querySelector(".player-0-panel").classList.toggle("active");
document.querySelector(".player-1-panel").classList.toggle("active");
}
function init() {
gamePlaying = true;
score = [0, 0];
roundScore = 0;
activePlayer = 0;
document.querySelector(".dice").style.display = "none";
document.getElementById("score-0").textContent = "0";
document.getElementById("score-1").textContent = "0";
document.getElementById("current-0").textContent = "0";
document.getElementById("current-1").textContent = "0";
document.querySelector("#name-0").textContent = "Player 1";
document.querySelector("#name-1").textContent = "Player 2";
document.querySelector(".player-0-panel").classList.remove("winner");
document.querySelector(".player-1-panel").classList.remove("winner");
document.querySelector(".player-0-panel").classList.remove("active");
document.querySelector(".player-1-panel").classList.remove("active");
document.querySelector(".player-0-panel").classList.add("active");
}