-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
85 lines (45 loc) · 1.99 KB
/
game.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
var roundScore, activePlayer, gamePlaying;
int();
document.querySelector('.btn-roll').addEventListener('click', function() {
// 1. Random number
if(gamePlaying){
var dice1 = Math.floor(Math.random() * 6) + 1;
var dice2 = Math.floor(Math.random() * 6) + 1;
//2. Display the result
var diceDOM1 = document.querySelector('.dice-1');
diceDOM1.style.display = 'block';
diceDOM1.src = 'dice-' + dice1 + '.png';
var diceDOM2 = document.querySelector('.dice-2');
diceDOM2.style.display = 'block';
diceDOM2.src = 'dice-' + dice2 + '.png';
//display the score
document.querySelector('#score-' + activePlayer).textContent = dice1 + dice2;
if (dice1 === 6 && dice2 === 6){
document.querySelector('#name-' + activePlayer).textContent = 'Winner!';
gamePlaying = false;
}
else if (dice1 !=6 && dice2 != 6){
activePlayer === 1 ? activePlayer = 2 : activePlayer = 1;
document.querySelector('#score-' + activePlayer).textContent = 0;
}
}
});
// function next(){
// activePlayer === 1 ? activePlayer = 2 : activePlayer = 1;
// document.getElementById('score-1').textContent = '0';
// document.getElementById('score-2').textContent = '0';
// document.querySelector('.dice-1').style.display = 'none';
// document.querySelector('.dice-2').style.display = 'none';
// }
document.querySelector('.btn-new').addEventListener('click', int);
function int(){
activePlayer = 1;
roundScore = 0;
gamePlaying = true;
document.getElementById('name-1').textContent = 'Player 1';
document.getElementById('name-2').textContent = 'Player 2';
document.querySelector('.dice-1').style.display = 'none';
document.querySelector('.dice-2').style.display = 'none';
document.getElementById('score-1').textContent = '0';
document.getElementById('score-2').textContent = '0';
}