-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignals.html
177 lines (148 loc) · 5.18 KB
/
signals.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
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
<!DOCTYPE html>
<html>
<head>
<title>Grid Game</title>
<style>
#container {
position: relative;
width: 500px;
height: 500px;
border: 1px solid #ccc;
display: grid;
grid-template-columns: repeat(10, 1fr);
grid-template-rows: repeat(10, 1fr);
gap: 1px;
}
.label {
position: absolute;
font-size: 12px;
color: #555;
pointer-events: none;
}
.top-left {
bottom: 0;
left: 0;
}
.top-right {
bottom: 0;
right: 0;
}
.bottom-left {
top: 0;
left: 0;
}
.bottom-right {
top: 0;
right: 0;
}
.dot {
position: absolute;
width: 50px;
height: 50px;
background-color: yellow;
border-radius: 50%;
pointer-events: none;
}
.green {
background-color: green !important;
}
</style>
</head>
<body>
<div id="container"></div>
<br>
<label>X Coordinate:</label>
<input type="number" id="x-input">
<label>Y Coordinate:</label>
<input type="number" id="y-input">
<br>
<button id="start-button" disabled>Start</button>
<div id="result-container"></div>
<script>
var container = document.getElementById('container');
var xInput = document.getElementById('x-input');
var yInput = document.getElementById('y-input');
var startButton = document.getElementById('start-button');
var resultContainer = document.getElementById('result-container');
var dot;
// Add corner labels to the grid
var topLeftLabel = document.createElement('div');
topLeftLabel.textContent = "(0, 0)";
topLeftLabel.classList.add('label', 'top-left');
container.appendChild(topLeftLabel);
var topRightLabel = document.createElement('div');
topRightLabel.textContent = "(9, 0)";
topRightLabel.classList.add('label', 'top-right');
container.appendChild(topRightLabel);
var bottomLeftLabel = document.createElement('div');
bottomLeftLabel.textContent = "(0, 9)";
bottomLeftLabel.classList.add('label', 'bottom-left');
container.appendChild(bottomLeftLabel);
var bottomRightLabel = document.createElement('div');
bottomRightLabel.textContent = "(9, 9)";
bottomRightLabel.classList.add('label', 'bottom-right');
container.appendChild(bottomRightLabel);
var dotX;
var dotY;
var userX;
var userY;
var isGameStarted = false;
function getRandomCoordinate() {
return Math.floor(Math.random() * 10);
}
function highlightWinningArea(userX, userY) {
var selectedGrid = document.querySelector('.grid[data-x="' + userX + '"][data-y="' + userY + '"]');
if (selectedGrid) {
selectedGrid.classList.add('green');
}
}
function startGame() {
if (!isGameStarted) {
userX = parseInt(xInput.value);
userY = parseInt(yInput.value);
if (isNaN(userX) || isNaN(userY)) {
alert("Please enter valid coordinates.");
return;
}
resultContainer.textContent = '';
highlightWinningArea(userX, userY);
if (dot) {
dot.remove();
}
dotX = getRandomCoordinate();
dotY = getRandomCoordinate();
dot = document.createElement('div');
dot.classList.add('dot');
dot.style.left = (dotX * 51 + 1) + 'px';
dot.style.top = (dotY * 51 + 1) + 'px';
container.appendChild(dot);
isGameStarted = true;
startButton.textContent = 'Guess';
setTimeout(function () {
if (userX === dotX && userY === dotY) {
resultContainer.textContent = 'Congratulations! You won. It was: ' + dotX + "px" + " " + dotY + "px";
} else {
resultContainer.textContent = 'Sorry, you lost. It was: ' + dotX + "px" + " " + dotY + "px";
}
isGameStarted = false;
startButton.textContent = 'Start';
dot.classList.add('green');
startButton.disabled = true;
xInput.disabled = true;
yInput.disabled = true;
}, 10000);
}
}
function enableStartButton() {
if (xInput.value.trim() !== '' && yInput.value.trim() !== '') {
startButton.disabled = false;
} else {
startButton.disabled = true;
}
}
xInput.addEventListener('input', enableStartButton);
yInput.addEventListener('input', enableStartButton);
startButton.addEventListener('click', startGame);
</script>
</body>
</html>