-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (53 loc) · 2.05 KB
/
script.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
'use strict ';
const player0EL = document.querySelector('.player--0')
const player1EL = document.querySelector('.player--1')
const score0 = document.getElementById('score--0');
const score1 = document.getElementById('score--1');
const diceEL = document.querySelector('.dice');
const btnnew = document.querySelector('.btn--new');
const btnroll = document.querySelector('.btn--roll');
const btnhold = document.querySelector('.btn--hold');
const current0EL = document.getElementById('current--0');
const current1EL = document.getElementById('current--1');
const scores = [0 , 0 ];
let currentscore = 0 ;
let acplayer = 0 ;
const switchplayer = function () {
document.getElementById(`current--${acplayer}`).textContent = 0 ;
acplayer = acplayer == 0 ? 1 : 0;
currentscore = 0 ;
player0EL.classList.toggle('player--active') ;
player1EL.classList.toggle('player--active') ;
}
let playing = true ;
score0.textContent= 0;
score1.textContent= 0;
diceEL.classList.add('hidden');
btnroll.addEventListener('click', function(){
if(playing)
{const dice = Math.trunc(Math.random() * 6) + 1 ;
diceEL.classList.remove('hidden');
diceEL.src = `dice-${dice}.png`;
if (dice != 1) {
currentscore = currentscore + dice
document.getElementById(`current--${acplayer}`).textContent = currentscore ;
} else {
switchplayer();
}}
});
btnhold.addEventListener('click', function(){
if(playing)
{scores[acplayer] += currentscore ;
document.getElementById(`score--${acplayer}`).textContent = scores[acplayer];
if (scores[acplayer] >= 9 ) {
playing = false ;
document.querySelector(`.player--${acplayer}`).classList.add('player--winner') ;
document.querySelector(`.player--${acplayer}`).classList.remove('player--active');
}else{
switchplayer();
}
}
})
document.querySelector('.btn--new').onclick = function (){
location.href = 'http://piggame.tk';
}