-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
142 lines (116 loc) · 3.44 KB
/
main.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
139
140
141
142
const new_game = document.querySelector('.new_game')
const roll_dice = document.querySelector('.roll_dice')
const keep = document.querySelector('.keep')
const dice = document.querySelector('.dice')
const gs1 = document.querySelector('.global_score-1')
const gs2 = document.querySelector('.global_score-2')
const cs1 = document.querySelector('.current_score-1')
const cs2 = document.querySelector('.current_score-2')
const player1div = document.querySelector('.player1')
const player2div = document.querySelector('.player2')
const player1heading = document.querySelector('.player1_heading')
const player2heading = document.querySelector('.player2_heading')
var hasTurn = 'player1'
var player1CurrentScore = 0
var player2CurrentScore = 0
var player1GlobalScore = 0
var player2GlobalScore = 0
var gameFinished = false
roll_dice.addEventListener('click',roll)
keep.addEventListener('click',keepScore)
new_game.addEventListener('click',startNewGame)
function roll() {
if(!gameFinished){
let roll = Math.floor(Math.random() * 6)
dice.src = `dice-${roll}.png`
dice.style.display = 'inline'
if(checkRoll(roll)) {
changeTurn()
return
}
addToCurrent(roll)
}
}
function keepScore() {
if(!gameFinished){
if(hasTurn == 'player1') {
player1GlobalScore += player1CurrentScore
gs1.textContent = player1GlobalScore
if(checkWinner()) {
return
}
changeTurn()
} else {
player2GlobalScore += player2CurrentScore
gs2.textContent = player2GlobalScore
if(checkWinner()) {
return
}
changeTurn()
}
}
}
function checkRoll (roll) {
if(roll === 0) {
return true
} else {
return false
}
}
function addToCurrent(roll) {
if(hasTurn == 'player1') {
player1CurrentScore += roll
cs1.textContent = player1CurrentScore
} else {
player2CurrentScore += roll
cs2.textContent = player2CurrentScore
}
}
function changeTurn() {
if(hasTurn=='player1') {
player1CurrentScore = 0
cs1.textContent = 0
dice.style.display = 'none'
player1div.classList.remove('turn')
hasTurn = 'player2'
player2div.classList.add('turn')
} else {
player2CurrentScore = 0
cs2.textContent = 0
dice.style.display = 'none'
player2div.classList.remove('turn')
hasTurn = 'player1'
player1div.classList.add('turn')
}
}
function checkWinner() {
if(player1GlobalScore >= 100) {
player1heading.textContent = 'WINNER!'
dice.style.display = 'none'
gameFinished = true
return true
} else if(player2GlobalScore >= 100) {
player2heading.textContent = 'WINNER!'
dice.style.display = 'none'
gameFinished = true
return true
}
}
function startNewGame() {
player1CurrentScore = 0
player2CurrentScore = 0
player1GlobalScore = 0
player2GlobalScore = 0
cs1.textContent = 0
cs2.textContent = 0
gs1.textContent = 0
gs2.textContent = 0
hasTurn = 'player1'
gameFinished = false
dice.style.display = 'none'
player1heading.textContent = 'Player 1'
player2heading.textContent = 'Player 2'
player1div.classList.remove('turn')
player2div.classList.remove('turn')
player1div.classList.add('turn')
}