forked from omkrishna/ColorGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
150 lines (132 loc) · 4.7 KB
/
logic.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
var t = new Array();
var count = 0;
var score = 0;
for(var i = 1; i < 7; i++){
t[i] = document.getElementById(i);
}
var x;
var correctColor;
//function to get random colours
function getRandomColor() {
var letters = "0123456789ABCDEF";
var color = "#";
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
//function to reset game
function newGame() {
count = 0;
score = 0;
document.getElementById("trial").textContent = "No. of tries: " + count;
document.getElementById("score").textContent = "Score: " + score;
for(var i = 1; i < 7; i++){
t[i].style.background = getRandomColor();
t[i].textContent = "";
}
x = Math.floor(Math.random() * 6 + 1);
correctColor = t[x].style.background;
document.getElementById("header-clue").innerHTML = correctColor;
}
//function to move to next round without losing score
function next(){
for(var i = 1; i < 7; i++){
t[i].style.background = getRandomColor();
t[i].textContent = "";
}
x = Math.floor(Math.random() * 6 + 1);
correctColor = t[x].style.background;
document.getElementById("header-clue").innerHTML = correctColor;
}
//event listeneres for tiles
t[1].addEventListener("click", function(){
if(t[1].style.background == correctColor){
for(var j = 1; j < 7; j++){
t[j].style.background = correctColor;
t[j].textContent = "Click on next to continue!";
}
score += 1;
document.getElementById("header").style.background = correctColor;
document.getElementById("score").textContent = "Score: " + score;
} else {
t[1].style.background = "black";
count += 1;
document.getElementById("trial").textContent = "No. of tries: " + count;
}
});
t[2].addEventListener("click", function(){
if(t[2].style.background == correctColor){
for(var j = 1; j < 7; j++){
t[j].style.background = correctColor;
t[j].textContent = "Click on next to continue!";
}
score += 1;
document.getElementById("header").style.background = correctColor;
document.getElementById("score").textContent = "Score: " + score;
} else {
t[2].style.background = "black";
count += 1;
document.getElementById("trial").textContent = "No. of tries: " + count;
}
});
t[3].addEventListener("click", function(){
if(t[3].style.background == correctColor){
for(var j = 1; j < 7; j++){
t[j].style.background = correctColor;
t[j].textContent = "Click on next to continue!";
}
score += 1;
document.getElementById("header").style.background = correctColor;
document.getElementById("score").textContent = "Score: " + score;
} else {
t[3].style.background = "black";
count += 1;
document.getElementById("trial").textContent = "No. of tries: " + count;
}
});
t[4].addEventListener("click", function(){
if(t[4].style.background == correctColor){
for(var j = 1; j < 7; j++){
t[j].style.background = correctColor;
t[j].textContent = "Click on next to continue!";
}
score += 1;
document.getElementById("header").style.background = correctColor;
document.getElementById("score").textContent = "Score: " + score;
} else {
t[4].style.background = "black";
count += 1;
document.getElementById("trial").textContent = "No. of tries: " + count;
}
});
t[5].addEventListener("click", function(){
if(t[5].style.background == correctColor){
for(var j = 1; j < 7; j++){
t[j].style.background = correctColor;
t[j].textContent = "Click on next to continue!";
}
score += 1;
document.getElementById("header").style.background = correctColor;
document.getElementById("score").textContent = "Score: " + score;
} else {
t[5].style.background = "black";
count += 1;
document.getElementById("trial").textContent = "No. of tries: " + count;
}
});
t[6].addEventListener("click", function(){
if(t[6].style.background == correctColor){
for(var j = 1; j < 7; j++){
t[j].style.background = correctColor;
t[j].textContent = "Click on next to continue!";
}
score += 1;
document.getElementById("header").style.background = correctColor;
document.getElementById("score").textContent = "Score: " + score;
} else {
t[6].style.background = "black";
count += 1;
document.getElementById("trial").textContent = "No. of tries: " + count;
}
});