-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
102 lines (85 loc) · 3.76 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
var scores,activePlayer,currentScore, gamePlaying, winScore;
currentScore = 0;
scores = [0,0];
activePlayer = 0;
winScore = 20;
var lastDice;
newGame();
function newGame() {
currentScore = 0;
scores = [0,0];
activePlayer = 0;
gamePlaying = true;
document.querySelector('#dice-1').style.display = 'none';
document.querySelector('#dice-2').style.display = 'none';
document.querySelector('#score-0').textContent = '0';
document.querySelector('#score-1').textContent = '0';
document.querySelector('#current-0').textContent = '0';
document.querySelector('#current-1').textContent = '0';
document.querySelector('.player-0-panel').classList.add('active');
document.querySelector('.player-1-panel').classList.remove('active');
document.querySelector('.player-0-panel').classList.remove('winner');
document.querySelector('.player-1-panel').classList.remove('winner');
document.querySelector('#name-0').textContent = 'Player 1';
document.querySelector('#name-1').textContent = 'Player 2';
}
function rollDice() {
if(gamePlaying) {
var dice1 = Math.floor(Math.random() * 6) + 1;
var dice2 = Math.floor(Math.random() * 6) + 1;
if(dice1 === 6 && lastDice === 6){
// All Entire Score is gone
scores[activePlayer] = 0;
document.querySelector('#score-'+activePlayer).textContent = scores[activePlayer] ;
document.querySelector('.player-0-panel').classList.toggle('active');
document.querySelector('.player-1-panel').classList.toggle('active');
activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
} else if(dice1 !== 1 && dice2 !== 1) {
currentScore += dice1 + dice2;
document.querySelector('#dice-1').src = 'dice' + '-'+dice1+'.png';
document.querySelector('#dice-2').src = 'dice' + '-'+dice2+'.png';
document.querySelector('#dice-1').style.display = 'block';
document.querySelector('#dice-2').style.display = 'block';
document.querySelector('#current-'+activePlayer).textContent = currentScore ;
}else {
currentScore = 0;
document.querySelector('.player-0-panel').classList.toggle('active');
document.querySelector('.player-1-panel').classList.toggle('active');
document.querySelector('#dice-1').style.display = 'none';
document.querySelector('#dice-2').style.display = 'none';
document.querySelector('#current-'+activePlayer).textContent = '0' ;
activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
}
lastDice = dice1;
}
}
function Hold() {
if(gamePlaying) {
// put the score in the current score in the player score
scores[activePlayer] += currentScore;
document.querySelector('#score-'+activePlayer).textContent = scores[activePlayer];
document.querySelector('#current-'+activePlayer).textContent = '0';
// checking if player is won
if(scores[activePlayer] >= winScore) {
document.querySelector('#name-'+activePlayer).textContent = 'Winner!';
document.querySelector('.player-'+activePlayer+'-panel').classList.add('winner');
document.querySelector('.player-0-panel').classList.remove('active');
document.querySelector('.player-1-panel').classList.remove('active');
document.querySelector('#dice-1').style.display = 'none';
document.querySelector('#dice-2').style.display = 'none';
gamePlaying = false;
}else {
currentScore = 0;
activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
document.querySelector('.player-0-panel').classList.toggle('active');
document.querySelector('.player-1-panel').classList.toggle('active');
document.querySelector('#dice-1').style.display = 'none';
document.querySelector('#dice-2').style.display = 'none';
}
}
}
function addScore() {
var winning_score = parseInt(document.querySelector('.winning-score').value,10) ;
winScore = typeof winning_score === 'number' ? winning_score : 20;
document.querySelector('.winning-score').value = '';
}