-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·151 lines (118 loc) · 5.15 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
document.addEventListener('DOMContentLoaded', function () {
let scores, roundScore, activePlayer, diceValue, diceValueB, diceImg, diceBImg, gamePlaying, globalScore, twoRolls, winningValue;
initializeGame();
// roll dice btn listener
const diceRoll = document.querySelector('.btn-roll');
diceRoll.addEventListener('click', function () {
if (gamePlaying) {
let dice = Math.floor(Math.random() * 6) + 1;
let diceB = Math.floor(Math.random() * 6) + 1;
diceValue = document.querySelector('#current-' + activePlayer);
diceImg.style = 'block';
diceImg.src = `img/dice-${dice}.png`;
diceValueB = document.querySelector('#current-' + activePlayer);
diceBImg.style = 'block';
diceBImg.src = `img/dice-${diceB}.png`;
if(dice !== 1 || diceB !== 1) {
//add score
roundScore+= (dice + diceB);
diceValue.textContent = roundScore;
twoRolls.push(dice);
console.log(dice, diceB);
// sets score to 0 when two six in a row are rolled
if (twoRolls.length === 2) {
if (twoRolls[0] === 6 && twoRolls[1] === 6) {
scores[activePlayer] = 0;
roundScore = 0;
globalScore.textContent = `${scores[activePlayer]}`;
twoRolls = [];
nextPlayer();
} else {
twoRolls = [];
}
}
}
if (dice === 1 || diceB === 1) {
nextPlayer();
}
}
});
const holdPoints = document.querySelector('.btn-hold');
holdPoints.addEventListener('click', function () {
if(gamePlaying) {
scores[activePlayer] += roundScore;
globalScore = document.getElementById(`score-${activePlayer}`);
globalScore.textContent=`${scores[activePlayer]}`;
let winner = document.getElementById(`name-${activePlayer}`);
if (scores[activePlayer] >= winningValue) {
diceImg.style.display = "none";
diceBImg.style.display = "none";
winner.parentElement.classList.remove('active');
winner.parentElement.classList.add('winner');
winner.textContent = "Winner!";
gamePlaying = false;
} else {
nextPlayer();
}
}
});
// submitting the winning value to the game
document.querySelector('form').addEventListener('submit', function(event) {
event.preventDefault();
winningValue = document.querySelector('input').value;
//input type validation
if (isNaN(winningValue)) {
alert('Value must be a number');
} else {
document.querySelector('form').style.display="none";
document.querySelector('.winning-header').textContent = `You need to score ${winningValue} points to win.`;
document.querySelector('.winning-header').style.display = "inline-block";
}
});
//function switching players
function nextPlayer() {
twoRolls = [];
roundScore = 0;
diceImg.style.display = "none";
diceValue.textContent = roundScore;
diceBImg.style.display = "none";
diceValueB.textContent = roundScore;
activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
document.querySelector('.player-0-panel').classList.toggle('active');
document.querySelector('.player-1-panel').classList.toggle('active');
}
// sets all scores to 0
function initializeGame() {
gamePlaying = true;
scores = [0,0];
roundScore = 0;
activePlayer = 0;
document.querySelector('.winning-header').style.display = "none";
// set initial values to 0
document.getElementById('score-0').textContent = '0';
document.getElementById('score-1').textContent = '0';
document.getElementById('current-0').textContent = '0';
document.getElementById('current-1').textContent = '0';
// reset of the input filed (form)
document.querySelector('form').reset();
//hiding the dice initially
diceImg = document.querySelector('.dice');
diceImg.style.display = "none";
diceBImg = document.querySelector('.diceB');
diceBImg.style.display = "none";
// display the winning value input
document.querySelector('form').style.display = "flex";
//revert player name
document.getElementById(`name-0`).parentElement.classList.remove('winner');
document.getElementById(`name-1`).parentElement.classList.remove('winner');
document.getElementById(`name-0`).parentElement.classList.remove('active');
document.getElementById(`name-1`).parentElement.classList.remove('active');
document.getElementById(`name-0`).parentElement.classList.add('active');
document.getElementById('name-0').textContent = 'Player 1';
document.getElementById('name-1').textContent = 'Player 2';
//rolls array
twoRolls = [];
}
const newGame = document.querySelector('.btn-new');
newGame.addEventListener('click', initializeGame);
});