-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossword.js
197 lines (154 loc) · 5.56 KB
/
crossword.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
let icon;
let board;
let stageCount = 0;
let inputList = []; // board 테이블의 입력란 리스트입니다.
let checkboxList = []; // hints 섹션의 체크박스 리스트입니다.
/**
* 게임 초기화 및 구성
*/
function initBoard() {
for (const tdEl of board.getElementsByTagName('td')) {
const textEl = document.createElement('input');
textEl.setAttribute('type', 'text');
textEl.setAttribute('tabindex', '-1');
textEl.setAttribute('maxlength', '1');
inputList.push(tdEl.appendChild(textEl));
}
}
function setupBoard(wordList) {
const tdElements = board.getElementsByTagName('td');
const usingGrids = new Set();
for (const word of wordList) {
word.gridList.forEach(grid => usingGrids.add(grid));
}
// 십자말 칸 나타내기
for (const grid of usingGrids) {
const index = 20 * (grid[0] - 1) + grid[1] - 1;
tdElements.item(index).setAttribute('class', `on bd-${(stageCount - 1) % 2}`);
inputList[index].removeAttribute('tabindex');
inputList[index].style.setProperty('pointer-events', 'auto');
}
}
function clearBoard() {
inputList = [];
for (const tdEl of board.getElementsByTagName('td')) {
while (tdEl.firstChild)
{
tdEl.removeChild(tdEl.firstChild);
}
tdEl.removeAttribute('class');
}
}
let hintList;
function buildHintItemHtmlString(number, hint) {
return `<button type="button" id="hint_item_${number}` +
'" class="list-group-item list-group-item-action"><div class="hint_wrap"><div class="hint_checkbox_wrap"><input type="checkbox" class="form-check-input me-1" /></div><div>' +
`<span class="form-check-label">${hint}</span></div></div></button>`;
}
function initHints(wordList) {
for (const [idx, word] of wordList.entries()) {
hintList.innerHTML += buildHintItemHtmlString(idx + 1, word.hint);
}
}
function setupHints(wordList) {
for (const [idx, buttonEl] of Array.from(hintList.getElementsByClassName('list-group-item')).entries()) {
buttonEl.addEventListener('mouseenter', () => {
wordList[idx].gridList.forEach(grid => {
inputList[20 * (grid[0] - 1) + grid[1] - 1].style.setProperty('background-color', '#ffeed9');
});
});
buttonEl.addEventListener('mouseleave', () => {
wordList[idx].gridList.forEach(grid => {
inputList[20 * (grid[0] - 1) + grid[1] - 1].style.setProperty('background-color', 'transparent');
});
});
checkboxList.push(buttonEl.querySelector('input[type="checkbox"]'));
}
}
function clearHints() {
checkboxList = [];
document.getElementById('hint_list').innerHTML = '';
}
/**
* 게임 시작 및 작동
*/
let puzzleList;
let isPuzzleCompleted;
function verifyPuzzle(wordList) {
let result = true;
for (const [idx, word] of wordList.entries()) {
let text = '';
for (const grid of word.gridList) {
text += inputList[20 * (grid[0] - 1) + grid[1] - 1].value[0] ?? '';
}
checkboxList[idx].checked = text == word.word;
result &&= checkboxList[idx].checked;
}
isPuzzleCompleted[stageCount - 1] ||= result;
return result;
}
function setupNextPuzzle() {
icon.src = '../images/ic_fridge_empty.png';
icon.setAttribute('alt', 'This is an empty fridge icon.');
icon.style.removeProperty('animation');
stageCount++;
const puzzle = puzzleList[stageCount - 1];
const wordList = puzzle.wordList;
// 십자말풀이 board 테이블 구성하기
initBoard();
setupBoard(wordList);
// 십자말풀이 hints 섹션 구성하기
hintList ??= document.getElementById('hint_list');
initHints(wordList);
setupHints(wordList);
return puzzle;
}
function activatePuzzle(wordList) {
inputList.forEach(input => {
input.addEventListener('input', event => {
event.stopPropagation();
if (!isPuzzleCompleted[stageCount - 1] && verifyPuzzle(wordList)) {
icon.src = '../images/ic_fridge_full.png';
icon.setAttribute('alt', 'This is a full fridge icon.');
icon.style.setProperty('animation', 'shake 0.5s');
if (stageCount == puzzleList.length) {
new bootstrap.Modal('#completion_modal').show();
} else {
clearBoard();
clearHints();
activatePuzzle(setupNextPuzzle().wordList);
}
}
});
});
}
function getPuzzleList() {
let result = null;
jQuery.ajax({
async: false,
url: 'https://gist.githubusercontent.com/koreandroid/5766402dc38b48c27d135cf3959b369d/raw/cfa01748c917b66bcdd8db576e05844696a9feb7/crosswordPuzzleList.json',
dataType: 'json',
success: function(response) {
result = response.puzzleList;
},
error: function(xhr, status, error) {
console.log('Error:', error);
}
});
return result;
}
function startGame() {
isPuzzleCompleted = new Array(puzzleList.length).fill(false);
setupNextPuzzle();
activatePuzzle(puzzleList[stageCount - 1].wordList);
}
{
const $ = jQuery;
$(document).ready(function() {
if ((puzzleList = getPuzzleList())?.length > 0) {
icon = document.getElementById('ic_fridge');
board = document.getElementById('board');
startGame();
} else {} // TODO: 홈 화면으로 되돌아갈 수 있도록 아이콘 토글하기
});
}