-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-2.js
138 lines (107 loc) · 5.1 KB
/
app-2.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
var scores, roundScore, activePlayer, gamePlaying, checkScore;
var previousDice;
var inputScoreNumber;
init();
// Hide the dice at the Start of the Game
document.querySelector('.dice-1').style.display = 'none';
document.querySelector('.dice-2').style.display = 'none';
// Add event listener to 'Roll Dice' Button : Roll Dice Button
document.querySelector('.btn-roll').addEventListener('click', function () {
if (gamePlaying) {
// Roll the Dice to Get a Random Number
var diceOne = Math.floor(Math.random() * 6) + 1;
console.log('Current Value of Dice One = ' + diceOne);
var diceTwo = Math.floor(Math.random() * 6) + 1;
console.log('Current Value of Dice Two = ' + diceTwo);
//console.log('Current Dice Value = ' + dice);
// Display the Dice Image and the Result
var diceObjectOne = document.querySelector('.dice-1');
diceObjectOne.style.display = 'block';
diceObjectOne.src = 'dice-' + diceOne + '.png';
var diceObjectTwo = document.querySelector('.dice-2');
diceObjectTwo.style.display = 'block';
diceObjectTwo.src = 'dice-' + diceTwo + '.png';
// Update the round Score (if the rolled number is not 1)
if (diceOne === 6 && diceTwo === 6) {
scores[activePlayer] = 0;
document.getElementById('score-' + activePlayer).textContent = scores[activePlayer];
switctPlayers();
} else if (diceOne > 1 && diceTwo > 1) {
// Add the Scores
roundScore = roundScore + diceOne + diceTwo;
document.querySelector('#current-' + activePlayer).textContent = roundScore;
} else {
// The Other Player Gets the Chance and your round Scroe is Set to Zero. Next Player
switctPlayers();
}
//previousDice = dice;
//console.log('Previous Dice Value = ' + previousDice);
}
});
// New Game Button
document.querySelector('.btn-new').addEventListener('click', init);
// Hold-Button
document.querySelector('.btn-hold').addEventListener('click', function () {
if (gamePlaying) {
// Add the Current Score to the Global Score.
scores[activePlayer] += roundScore;
// Update the UI With the Global Score. Next Player
document.getElementById('score-' + activePlayer).textContent = scores[activePlayer];
// Test
//console.log('Inside Hold ' + inputScoreNumber);
if (inputScoreNumber === undefined) {
checkScore = 50;
} else {
checkScore = inputScoreNumber;
}
// Check If a Player Has Won
if (scores[activePlayer] >= checkScore) {
document.querySelector('#name-' + activePlayer).textContent = 'Winner!!!';
document.querySelector('.dice-1').style.display = 'none';
document.querySelector('.dice-2').style.display = 'none';
document.querySelector('.player-' + activePlayer + '-panel').classList.add('winner');
document.querySelector('.player-' + activePlayer + '-panel').classList.remove('active');
gamePlaying = false;
} else {
switctPlayers();
}
}
});
// Next Player Re-Usable Function
function switctPlayers() {
// Hand over the dice to the other player
activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
roundScore = 0;
// Display the Reset roundScrore to zero
document.getElementById('current-0').textContent = '0';
document.getElementById('current-1').textContent = '0';
// Toogle the style for the Current Player
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';
}
// Initilize Function
function init() {
scores = [0, 0];
roundScore = 0;
activePlayer = 0;
gamePlaying = true;
// Initilize all the Values to zero at the Start of the game.
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.getElementById('name-0').textContent = 'Player 1';
document.getElementById('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');
// Get the Final Score from the user
document.querySelector('.btn-outline-success').addEventListener('click', function () {
inputScoreNumber = document.querySelector('.final-score').value;
console.log(inputScoreNumber);
});
}