-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
127 lines (117 loc) · 3.8 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html lang="ko-KR">
<head>
<title>Merlin-Game</title>
<meta charset="utf-8">
<style>
input {
width: 30px;
}
div {
width: 100px;
height: 100px;
border: 1px solid black;
}
hr {
width: 80%;
float: left;
}
label {
clear: left;
display: block;
}
.light {
background-color: #ffea73;
}
</style>
</head>
<body>
<label>
세로 크기
<input id="N" type="number" min="1" max="10" value="3">
가로 크기
<input id="M" type="number" min="1" max="10" value="3">
<button onclick="set()">시작</button>
</label>
<h2 id="stopwatch">00분 00초</h2>
<hr>
<br><br><br>
<script>
let board = [[]], N, M, dx = [1, 0, -1, 0], dy = [0, 1, 0, -1], fin = false, congrats, timerId, time = 0, min, sec,
stopwatch = document.getElementById("stopwatch");
function set() {
clearTimeout(timerId);
stopwatch.innerText = "00:00";
time = -1;
startClock();
if (fin) {
fin = false;
congrats.remove();
}
for (let i = 0; i < board.length; i++) for (let j = 0; j < board[i].length; j++) board[i][j].remove();
N = document.getElementById("N").value;
M = document.getElementById("M").value;
board = new Array(N);
for (let i = 0; i < N; i++) board[i] = new Array(M);
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
board[i][j] = document.createElement('div');
board[i][j].className = 'light';
board[i][j].style.float = 'left';
board[i][j].setAttribute('onClick', `touch(${i}, ${j})`);
if (j === 0) board[i][j].style.clear = 'left';
document.body.appendChild(board[i][j]);
}
}
for (let i = 0; i < N; i++) for (let j = 0; j < M; j++) if (Math.floor(Math.random() * 2) === 1) touch(i, j);
}
function touch(i, j) {
if (fin) return;
if (board[i][j].classList.contains('light')) {
board[i][j].classList.remove('light');
board[i][j].classList.add('off');
} else {
board[i][j].classList.remove('off');
board[i][j].classList.add('light');
}
for (let dir = 0; dir < 4; dir++) {
let nx = i + dx[dir];
let ny = j + dy[dir];
if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue;
if (board[nx][ny].classList.contains('light')) {
board[nx][ny].classList.remove('light');
board[nx][ny].classList.add('off');
} else {
board[nx][ny].classList.remove('off');
board[nx][ny].classList.add('light');
}
}
let done = true;
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
if (board[i][j].classList.contains('off')) {
done = false;
break
}
}
}
if (done) {
fin = true;
clearTimeout(timerId);
congrats = document.createElement('div');
congrats.classList.add('congrats');
congrats.textContent = '축하합니다!';
document.body.appendChild(congrats);
}
}
function startClock() {
time++;
min = parseInt(String((time / 60)));
sec = time % 60;
stopwatch.innerText = String(min).padStart(2, '0') + "분 " + String(sec).padStart(2, '0') + "초";
clearTimeout(timerId);
timerId = setTimeout(startClock, 1000);
}
</script>
</body>
</html>