-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guessing-Game.js
130 lines (93 loc) · 3.62 KB
/
Guessing-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
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
class Colour{
constructor(){
let colText = document.querySelector(".colTextDisplay");
//To generate a random number between 0 to 255
function generateRandomColNumber(max){
return Math.floor(Math.random() * max) + 1;
}
let val1 = generateRandomColNumber(255);
let val2 = generateRandomColNumber(255);
let val3 = generateRandomColNumber(255);
//To display the random number
colText.innerHTML = `<h1>
RGB(${val1}, ${val2}, ${val3})
</h1>`
this.mainCol = `rgb(${val1}, ${val2}, ${val3})`;
}
//method to call up new colours
newCol(){
const colList = document.querySelector(".colBoxDisplay");
const colBox = document.querySelectorAll(".colBox");
const retakeButton = document.querySelector(".retake");
retakeButton.innerHTML = `<h1>
</h1>`;
//To generate a random number between 1 and 5
function generateRandomColNumber(min){
return Math.floor(Math.random() * min) + 1;
}
let theCol = generateRandomColNumber(5);
for(let i = 0; i < 6; i++){
colBox[theCol].style.backgroundColor = this.mainCol;
if(i != theCol){
let otherVal1 = generateRandomColNumber(255);
let otherVal2 = generateRandomColNumber(255);
let otherVal3 = generateRandomColNumber(255);
colBox[i].style.backgroundColor = `RGB(${otherVal1}, ${otherVal2}, ${otherVal3})`;
}
}//end of for loop
}
vaild(activeBox, total){
const retakeButton = document.querySelector(".retake");
let num = 3 - total;
let livesText = document.querySelector(".lives");
if (activeBox.style.backgroundColor == this.mainCol){
livesText.innerHTML = `<h1>
${3 - total } LIVES LEFT!
</h1>`;
}
else{
livesText.innerHTML = `<h1>
${num} LIVES LEFT!
</h1>`;
}
//if the colour clicked matches the color in the constructor -> that I will make, duh, then:
if(activeBox.style.backgroundColor == this.mainCol){
let headerCol = document.querySelector(".intro");
const colBox = document.querySelectorAll(".colBox");
headerCol.style.backgroundColor = this.mainCol;
for(let i= 0; i < 6; i++){
colBox[i].style.backgroundColor = this.mainCol;
}
console.log("It is correct");
// retakeButton.style.background = `inline-block`;
retakeButton.innerHTML = `<h1>
CORRECT!
</h1>`;
return true;
}
else{
activeBox.style.backgroundColor = `rgb(248, 248, 255)`;
retakeButton.innerHTML = `<h1>
TRY AGAIN!
</h1>`;
return false;
}
}
}
const initnewCol = new Colour();
initnewCol.newCol();
let buttonnewColour = document.querySelector(".newColor");
buttonnewColour.addEventListener('click', function(event){
location.reload(); //makes the whole page reload.
})
const colBox = document.querySelectorAll(".colBox");
let sum = 0;
//if any of the colour boxes are clicked.
for(let i = 0; i <6; i++){
colBox[i].addEventListener('click', function(event){
sum++
if(sum <=3){
let con = initnewCol.vaild(colBox[i], sum);
}
})
}//end of for loop