-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
166 lines (142 loc) · 4.25 KB
/
script.js
File metadata and controls
166 lines (142 loc) · 4.25 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use strict";
let p1Score = 0;
let p2Score = 0;
let p1Total = 0;
let p2Total = 0;
let activePlayer = 1;
let newDice = 0;
let winFlag = false;
let player1_Card = document.querySelector(".player1");
let player2_Card = document.querySelector(".player2");
const p1_TotalLabel = document.querySelector(".player1-score");
const p2_TotalLabel = document.querySelector(".player2-score");
const p1_currentScoreLabel = document.querySelector(".player1-current-score");
const p2_currentScoreLabel = document.querySelector(".player2-current-score");
const p1Label = document.querySelector(".p1-label");
const p2Label = document.querySelector(".p2-label");
const dice = document.querySelectorAll(".dice");
function hold() {
if (winFlag) return;
if (newDice != 0 || newDice != 1) {
//add to totalScore
if (activePlayer == 1) {
p1Total += p1Score;
p1_TotalLabel.textContent = p1Total;
} else {
p2Total += p2Score;
p2_TotalLabel.textContent = p2Total;
}
}
changePlayer();
checkWin();
}
function changePlayer() {
if (activePlayer == 1) {
activePlayer = 2;
//
p1Score = 0;
p2Score = 0;
p1_currentScoreLabel.textContent = 0;
p2_currentScoreLabel.textContent = 0;
player2_Card.style.backgroundColor = "rgba(255,255,255,0.65)";
player1_Card.style.backgroundColor = "rgba(255,255,255,0.4)";
} else {
activePlayer = 1;
//
p1Score = 0;
p2Score = 0;
p1_currentScoreLabel.textContent = 0;
p2_currentScoreLabel.textContent = 0;
player2_Card.style.backgroundColor = "rgba(255,255,255,0.4)";
player1_Card.style.backgroundColor = "rgba(255,255,255,0.65)";
}
}
function newGame() {
p1_currentScoreLabel.textContent = 0;
p2_currentScoreLabel.textContent = 0;
p1_TotalLabel.textContent = 0;
p2_TotalLabel.textContent = 0;
if (activePlayer == 2) changePlayer();
p1Score = 0;
p2Score = 0;
p1Total = 0;
p2Total = 0;
winFlag = false;
for (let i = 0; i < dice.length; i++) {
dice[i].classList.add(`dice${i + 1}`);
}
p1Label.textContent = "PLAYER 1";
p2Label.textContent = "PLAYER 2";
p1Label.style.color = "#2f2f2f";
p2Label.style.color = "#2f2f2f";
p1_TotalLabel.style.color = "rgb(188,30,30)";
p2_TotalLabel.style.color = "rgb(188,30,30)";
player1_Card.style.backgroundColor = "rgba(255,255,255,0.65)";
player2_Card.style.backgroundColor = "rgba(255,255,255,0.4)";
}
function rollDice() {
if (winFlag) return;
//increased chance to roll 1. You actually roll a number between 1-8, (2,3,4,5,6) are good. (1,7,8) are bad.
newDice = Math.round(Math.random() * 7 + 1);
if (newDice > 6) newDice = 1;
for (let i = 0; i < dice.length; i++) {
if (i === newDice - 1) {
dice[i].classList.remove(`dice${i + 1}`);
} else {
dice[i].classList.add(`dice${i + 1}`);
}
}
if (newDice == 1) {
changePlayer();
} else {
addScore(newDice);
}
checkWin();
}
function addScore(score) {
if (activePlayer == 1) {
p1Score += score;
p1_currentScoreLabel.textContent = p1Score;
} else {
p2Score += score;
p2_currentScoreLabel.textContent = p2Score;
}
checkWin();
}
function checkWin() {
if (p1Total >= 36) {
// player 1 has won
//checking if cheated.
let p1TotalNow = Number(p1_TotalLabel.textContent);
if (p1TotalNow < 36) {
alert("Cheating from player 1 detected. Restarting game...");
newGame();
return;
}
player1_Card.style.backgroundColor = "#2f2f2f";
p1Label.style.color = "rgb(0,255,0)";
p1Label.textContent = "WINNER!";
p1_TotalLabel.style.color = "rgb(0,255,0)";
winFlag = true;
return;
}
if (p2Total >= 36) {
// player 2 has won
//checking if cheated.
let p2TotalNow = Number(p2_TotalLabel.textContent);
if (p2TotalNow < 36) {
alert("Cheating from player 2 detected. Restarting game...");
newGame();
return;
}
player2_Card.style.backgroundColor = "#2f2f2f";
p2Label.style.color = "rgb(0,255,0)";
p2Label.textContent = "WINNER!";
p2_TotalLabel.style.color = "rgb(0,255,0)";
winFlag = true;
return;
}
}
document.querySelector(".roll-dice").addEventListener("click", rollDice);
document.querySelector(".new-game").addEventListener("click", newGame);
document.querySelector(".hold").addEventListener("click", hold);