forked from omkrishna/ColorGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
108 lines (104 loc) · 3.33 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Color Game</title>
<link rel="stylesheet" href="./index.css" />
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Londrina+Solid&family=Permanent+Marker&display=swap" rel="stylesheet">
</head>
<body onload="newGame()">
<div id="header">
<h1>The Great</h1>
<h1 id="header-clue">rgb(xxx,xxx,xxx)</h1>
<h1>Guessing Game</h1>
</div>
<div id='info'>
<div id='tries'>
<h2 id='tries-clue'>Tries Left:</h2>
</div>
<div id='new'>
<h2>New Game</h2>
</div>
</div>
<div id="container">
<div class="tile" id="1"></div>
<div class="tile" id="2"></div>
<div class="tile" id="3"></div>
<div class="tile" id="4"></div>
<div class="tile" id="5"></div>
<div class="tile" id="6"></div>
</div>
<script>
var tries
var x;
var correctColor;
var t=[];
var n=6;
for (var i=0;i<n;i++) {
t[i]= document.getElementById(String(i+1));
}
var newg=document.getElementById('new');
var n_tries=document.getElementById('tries');
function getRandomColor() {
var letters = "0123456789ABCDEF";
var color = "#";
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function tries_inp(){
var m = prompt('Enter Number Of Tries(should be between 1 and 5)', '4');
if (m>0 && m<6 && m%1==0) {
tries=m;
document.getElementById("tries-clue").innerHTML = 'Tries Left:'+tries;
}
}
function newGame() {
for (var i=0;i<n;i++) {
t[i].style.background=getRandomColor();
t[i].style.border = '1px solid white';
}
x = Math.floor(Math.random() * n + 1);
tries=4
correctColor = t[x-1].style.background;
document.getElementById("header-clue").innerHTML = correctColor;
document.getElementById("tries-clue").innerHTML = 'Tries Left:'+tries;
}
for (var i=0;i<n;i++) {
t[i].addEventListener("click", function (event) {
if (event.currentTarget.style.background == correctColor) {
for (var j=0;j<6;j++) {
if (j!=event.currentTarget) {
t[j].style.background = correctColor;
}
}
document.getElementById("header").style.background = correctColor;
} else {
if (event.currentTarget.style.background != "black") {
tries-=1;
}
document.getElementById("tries-clue").innerHTML = 'Tries Left:'+tries;
if (tries== 0) {
document.getElementById("header").style.background = correctColor;
t.forEach(e => {
if (e.style.background != correctColor) {
e.style.background = "black";
e.style.border = "black";
}
});
}
event.currentTarget.style.background = "black";
event.currentTarget.style.border = "black";
}
})
};
newg.addEventListener("click", function () {
newGame()
});
n_tries.addEventListener("click", function () {
tries_inp()
});
</script>
</body>
</html>