-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
235 lines (209 loc) · 6.74 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const colors = ["blue", "green", "pink", "yellow", "purple", "orange"];
let codeToGuess = [];
let currentColor = "";
let rowIndex = 1;
let code;
let roundIndex = 1;
let playerNumber;
let span1 = document.getElementById("points-1");
let span2 = document.getElementById("points-2");
let penalty = 0;
let currentScore;
let currentCodeRow;
//check which player
const whichPlayer = function () {
if (roundIndex % 2 == 0) {
playerNumber = 2;
currentScore = span2;
} else {
playerNumber = 1;
currentScore = span1;
}
}
//draw color code by CPU
const draw = function () {
code = $("#end-game-popup .code .pin");
let index = 0;
while (codeToGuess.length < 4) {
const randomNum = Math.floor(Math.random() * colors.length);
codeToGuess.push(colors[randomNum]);
code[index].classList.add(codeToGuess[[index]]);
index += 1;
}
console.log(codeToGuess);
};
//set code by player
const setCode = function () {
let currentCodeRow = $("#two-players-popup .active div");
let index = 0;
code = $("#two-players-popup .code .pin")
if (currentCodeRow[0].classList.length == 2 &&
currentCodeRow[1].classList.length == 2 &&
currentCodeRow[2].classList.length == 2 &&
currentCodeRow[3].classList.length == 2) {
codeToGuess = [];
while (codeToGuess.length < 4) {
codeToGuess.push(currentCodeRow[index].classList[1]);
code[index].classList.add(codeToGuess[[index]]);
index += 1;
}
console.log(codeToGuess);
$(document).on("click", "#play-area .active div", setColor);
$("#two-players-popup").fadeOut();
}
}
//pick color
const pickColor = function () {
currentColor = this.classList[0];
$(".color-pin").removeClass("currentColor");
this.classList.add("currentColor");
$(".prompt").fadeOut();
// console.log(currentColor);
// console.log(this.classList)
}
//set the selected color
const setColor = function () {
$(this).removeClass();
$(this).addClass("pin");
$(this).addClass(currentColor);
// console.log(this.classList);
}
//check result
const checkCode = function () {
let currentRow = $("#play-area .active div");
let currentPoints = $("#punctation .active div");
if (currentRow[0].classList.length === 2 &&
currentRow[1].classList.length === 2 &&
currentRow[2].classList.length === 2 &&
currentRow[3].classList.length === 2) {
for (let i = 0; i < currentRow.length; i++) {
if (currentRow[i].classList[1] == codeToGuess[i]) {
for (let j = 0; j < currentPoints.length; j++) {
if (currentPoints[j].classList.length < 2) {
currentPoints[j].classList.add("red");
currentRow[i].classList.add("checked");
code[i].classList.add("checked");
break;
}
}
}
}
for (let i = 0; i < currentRow.length; i++) {
if (!currentRow[i].classList.contains("checked")) {
for (let j = 0; j < code.length; j++) {
// duplicated condition, without it some bug appear, don't know why yet
if (!code[j].classList.contains("checked") &&
!currentRow[i].classList.contains("checked") &&
currentRow[i].classList[1] == code[j].classList[1]) {
currentRow[i].classList.add("checked");
code[j].classList.add("checked");
for (let k = 0; k < currentPoints.length; k++) {
if (currentPoints[k].classList.length < 2) {
currentPoints[k].classList.add("white");
break;
}
}
} else {
continue;
}
}
} else {
continue;
}
}
rowIndex += 1;
whichPlayer();
if (currentPoints[0].classList.contains("red") &&
currentPoints[1].classList.contains("red") &&
currentPoints[2].classList.contains("red") &&
currentPoints[3].classList.contains("red")) {
if ($("#two-players-score").is(':hidden')) {
$("#end-game-popup p").html(`Gratulations<br>You won in: ${rowIndex -1}. attempt!`);
$("#end-game-popup").fadeIn();
} else if ($("#two-players-score").is(':visible')) {
roundIndex += 1;
$(document).off('click', "#play-area .active div");
endRound();
}
} else if (rowIndex > 10) {
if ($("#two-players-score").is(':hidden')) {
$("#end-game-popup p").html(`Game over<br>Code was:`);
$("#end-game-popup").fadeIn();
} else if ($("#two-players-score").is(':visible')) {
roundIndex += 1;
penalty = 11;
$(document).off('click', "#play-area .active div");
endRound();
}
}
$(".active").removeClass("active");
$(`.row-${rowIndex}`).addClass("active");
$(".code .pin").removeClass("checked");
}
}
//end round in player vs player variant
const endRound = function () {
$("#end-round-popup .pin").removeClass();
$("#end-round-popup .code div").addClass("pin");
$("#end-round-popup .pin")[0].classList.add(codeToGuess[0]);
$("#end-round-popup .pin")[1].classList.add(codeToGuess[1]);
$("#end-round-popup .pin")[2].classList.add(codeToGuess[2]);
$("#end-round-popup .pin")[3].classList.add(codeToGuess[3]);
if (penalty == 11) {
currentScore.textContent = parseInt(currentScore.textContent) + penalty;
$("#end-round-popup p").html("Sorry, you missed a chance<br>Code was:");
$("#end-round-popup").fadeIn();
} else {
currentScore.textContent = parseInt(currentScore.textContent) + (rowIndex - 1);
$("#end-round-popup p").html(`Gratulations<br>You guessed in: ${rowIndex -1}. attempt!`);
$("#end-round-popup").fadeIn();
}
penalty = 0;
$("#end-round-popup button").click(newRound);
}
//new round in player vs player variant
const newRound = function () {
whichPlayer();
$("#end-round-popup").fadeOut();
rowIndex = 1;
$("#play-area .pin").removeClass();
$("#play-area .pins-row div").addClass("pin");
$(".point").removeClass();
$(".round-punctation div").addClass("point");
console.log($(".active"));
$(".active").removeClass("active");
$(`.row-${rowIndex}`).addClass("active");
$("#two-players-popup h2").text(`Player ${playerNumber} set the code:`);
$("#two-players-popup .pin").removeClass();
$("#two-players-popup .code div").addClass("pin");
$("#two-players-popup .code").addClass("active");
$(document).on("click", "#two-players-popup .active div", setColor);
$("#two-players-popup").fadeIn();
}
//<---play with computer--->
$("#1vCPU").click(function () {
$("#welcome-popup").fadeOut();
draw();
$(".prompt").fadeIn();
// pick color
$(".color-pin").click(pickColor);
// set the selected color
$(document).on("click", "#play-area .active div", setColor);
// check code by Try button
$("#color-pins button").click(checkCode);
});
//reload game
$("#end-game-popup button").click(function () {
window.location.reload();
})
//<---play with friend--->
$("#1v1").click(function () {
$("#welcome-popup").fadeOut();
$("#two-players-popup").fadeIn();
$("#two-players-score").fadeIn();
$(".prompt").fadeIn();
$(".color-pin").click(pickColor);
$(document).on("click", "#two-players-popup .active div", setColor);
$("#two-players-popup button").click(setCode);
$("#color-pins button").click(checkCode);
})