-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
96 lines (78 loc) · 3.18 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
let scores, roundScore, activePlayer, gameOn;
const diceImg =document.querySelector('.diceImg');
const diceRollBtn = document.querySelector('.btn-roll');
const diceGrabBtn = document.querySelector('.btn-grab');
const newGameBtn = document.querySelector('.btn-new');
let score0 = document.getElementById('score-0');
let roundScore0= document.getElementById('roundScore-0');
let score1= document.getElementById('score-1');
let roundScore1= document.getElementById('roundScore-1');
let prevRoll;
init();
newGameBtn.addEventListener('click', init);
diceRollBtn.addEventListener('click', ()=> {
if (gameOn){
let diceRoll = Math.floor(Math.random()*6 + 1);
diceImg.src = `public/dice-${diceRoll}.png`;
diceImg.style.display = 'block';
if (diceGrabBtn === 6 && prevRoll === 6){
roundScore += (diceRoll*2);
document.getElementById(`roundScore-${activePlayer}`).textContent = roundScore;
} else if(diceRoll !== 1) {
roundScore += diceRoll;
document.getElementById(`roundScore-${activePlayer}`).textContent = roundScore;
} else {
playerSwitch();
}
prevRoll= diceRoll;
}
});
diceGrabBtn.addEventListener('click', ()=> {
if(gameOn){
scores[activePlayer] += roundScore;
document.getElementById(`score-${activePlayer}`).textContent = scores[activePlayer];
let inputScore = document.querySelector('.win-score').value;
let winScore;
if (inputScore && inputScore > 0) {
winScore = inputScore;
} else {
winScore = 100;
}
if (scores[activePlayer] >= winScore){
document.querySelector(`#player-${activePlayer}`).textContent = 'You win!';
document.querySelector(`.player-${activePlayer}-card`).classList.add('winner');
document.querySelector(`.player-${activePlayer}-card`).classList.remove('active');
diceImg.style.display = 'none';
gameOn = false;
} else {
playerSwitch();
}
}
});
function playerSwitch() {
activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
roundScore = 0;
roundScore0.textContent = '0';
roundScore1.textContent = '0';
document.querySelector('.player-0-card').classList.toggle('active');
document.querySelector('.player-1-card').classList.toggle('active');
diceImg.style.display = 'none';
};
function init() {
scores = [0,0];
activePlayer=0;
roundScore = 0;
gameOn =true;
diceImg.style.display = 'none';
score0.textContent = '0';
roundScore0.textContent = '0';
score1.textContent = '0';
roundScore1.textContent = '0';
document.querySelector(`#player-0`).textContent = 'Player 1';
document.querySelector(`#player-1`).textContent = 'Player 2';
document.querySelector('.player-0-card').classList.remove('winner');
document.querySelector('.player-1-card').classList.remove('winner');
document.querySelector('.player-0-card').classList.remove('active');
document.querySelector('.player-1-card').classList.remove('active');
document.querySelector('.player-0-card').classList.add('active');
};