-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
311 lines (301 loc) · 9.15 KB
/
script.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const board = document.getElementById("board");
const wordInput = document.getElementById("wordInput");
const submitWord = document.getElementById("submitWord");
const wordLength = JSON.parse(localStorage.getItem("wordLength") ?? 5);
const maxAttempts = JSON.parse(localStorage.getItem("maxAttempts") ?? 6);
document.getElementById("wordLength").value = wordLength;
document.getElementById("maxAttempts").value = maxAttempts;
document.getElementById("wordInput").maxLength = wordLength;
let secretWordDict;
function getRandomWord() {
return new Promise((resolve, _reject) => {
// // 从 A-Z 挑一个出来
// const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// const index = Math.floor(Math.random() * 26);
// const letter = letters[index];
// // 插入位置
// const positions = [0, 1, 2, 3, 4];
// const positionIndex = Math.floor(Math.random() * 5);
// const position = positions[positionIndex];
// // 生成查询字符串
// let query = "?".repeat(wordLength);
// query = query.slice(0, position) + letter + query.slice(position + 1);
// // 查询 API
// const url = `https://api.datamuse.com/words?sp=${query}`;
// // XHR
// const xhr = new XMLHttpRequest();
// xhr.open("GET", url);
// xhr.onload = function () {
// if (xhr.status === 200) {
// const response = JSON.parse(xhr.responseText);
// if (response.length > 0) {
// resolve(response[0].word);
// }
// }
// };
// xhr.onerror = function (e) {
// console.error(e);
// Gmal.notice(
// "Failed to get word from API.\nYou may need to check your internet connection, or turn on the VPN.\nPlease try again later",
// {
// type: "error",
// timeout: 5000,
// }
// );
// };
// xhr.send();
// XHR
const xhr = new XMLHttpRequest();
xhr.open("GET", "dict.json");
xhr.onload = function () {
if (xhr.status === 200) {
const response = JSON.parse(xhr.responseText);
if (response.length > 0) {
secretWordDict = response;
resolve(
response[Math.floor(Math.random() * response.length)]
);
}
}
};
xhr.onerror = function (e) {
console.error(e);
Gmal.notice(
"Failed to get word from server.\nYou may need to check your internet connection, or turn on the VPN.\nPlease try again later",
{
type: "error",
timeout: 5000,
}
);
};
xhr.send();
});
}
let secretWord;
const secretWordMap = {
A: 0,
B: 0,
C: 0,
D: 0,
E: 0,
F: 0,
G: 0,
H: 0,
I: 0,
J: 0,
K: 0,
L: 0,
M: 0,
N: 0,
O: 0,
P: 0,
Q: 0,
R: 0,
S: 0,
T: 0,
U: 0,
V: 0,
W: 0,
X: 0,
Y: 0,
Z: 0,
};
let attempts = 0;
// 初始化棋盘
function initBoard() {
for (let i = 0; i < maxAttempts * wordLength; i++) {
const tile = document.createElement("div");
tile.classList.add("tile");
board.style.gridTemplateColumns = `repeat(${wordLength}, 50px)`;
board.appendChild(tile);
}
for (let i = 0; i < secretWord.length; i++) {
const letter = secretWord[i];
secretWordMap[letter.toUpperCase()]++;
}
}
function updateBoard(response, guess) {
for (let i = 0; i < response.length; i++) {
const secretWordGuessMap = {
A: 0,
B: 0,
C: 0,
D: 0,
E: 0,
F: 0,
G: 0,
H: 0,
I: 0,
J: 0,
K: 0,
L: 0,
M: 0,
N: 0,
O: 0,
P: 0,
Q: 0,
R: 0,
S: 0,
T: 0,
U: 0,
V: 0,
W: 0,
X: 0,
Y: 0,
Z: 0,
};
const rowStart = attempts * wordLength;
for (let i = 0; i < wordLength; i++) {
const tile = board.children[rowStart + i];
tile.textContent = guess[i];
secretWordGuessMap[guess[i].toUpperCase()]++;
if (guess[i] === secretWord[i]) {
tile.style.backgroundColor = "#00bd55";
} else if (
secretWord.includes(guess[i]) &&
secretWordGuessMap[guess[i].toUpperCase()] <=
secretWordMap[guess[i].toUpperCase()]
) {
tile.style.backgroundColor = "#e69900";
} else {
tile.style.backgroundColor = "#53626a";
}
tile.style.color = "#fff";
}
attempts++;
if (guess === secretWord) {
Gmal.notice("Congratulations! You guessed the word.", {
type: "success",
timeout: 2500,
});
submitWord.disabled = true;
} else if (attempts === maxAttempts) {
Gmal.notice("Game over! The word was: " + secretWord, {
type: "warn",
timeout: 5000,
});
submitWord.disabled = true;
}
wordInput.value = "";
return;
}
}
function crackUpdateBoard(target) {
for (let i = 0; i < 5; i++) {
const tile = board.children[i];
tile.textContent = "CMD"[i];
tile.style.backgroundColor = ["#d10700", "#d10700", "#d10700", "#53626a", "#53626a"][i];
tile.style.color = "#fff";
}
for (let i = 5; i < 10; i++) {
const tile = board.children[i];
tile.textContent = "Crack"[i - 5];
tile.style.backgroundColor = "#d10700";
tile.style.color = "#fff";
}
for (let i = 10; i < 15; i++) {
const tile = board.children[i];
tile.textContent = ""[i - 10];
tile.style.backgroundColor = "#53626a";
tile.style.color = "#fff";
}
for (let i = 15; i < 20; i++) {
const tile = board.children[i];
tile.textContent = ""[i - 15];
tile.style.backgroundColor = "#53626a";
tile.style.color = "#fff";
}
for (let i = 20; i < 25; i++) {
const tile = board.children[i];
tile.textContent = target[i - 20];
tile.style.backgroundColor = "#00bd55";
tile.style.color = "#fff";
}
for (let i = 25; i < 30; i++) {
const tile = board.children[i];
tile.textContent = ""[i - 25];
tile.style.backgroundColor = "#53626a";
tile.style.color = "#fff";
}
}
// 检查用户输入的单词
function checkWord() {
const guess = wordInput.value.toLowerCase();
if (guess.length !== wordLength) {
Gmal.notice(`Word must be ${wordLength} letters!`, {
type: "warn",
timeout: 2500,
});
return;
}
// 检查单词是否有效
// const url = `https://api.datamuse.com/words?sp=${guess}`;
// const xhr = new XMLHttpRequest();
// xhr.open("GET", url);
// xhr.onload = function () {
// if (xhr.status === 200) {
// const response = JSON.parse(xhr.responseText);
// updateBoard(response, guess);
// }
// };
// xhr.onerror = function (e) {
// console.error(e);
// Gmal.notice(
// "Failed to check word from API.\nYou may need to check your internet connection, or turn on the VPN.\nPlease try again later",
// {
// type: "error",
// timeout: 5000,
// }
// );
// };
// xhr.send();
if (secretWordDict.includes(guess)) {
updateBoard(secretWordDict, guess);
} else {
Gmal.notice("That is not a word . . .", {
type: "warn",
timeout: 2500,
});
wordInput.value = "";
}
}
// 添加事件监听器
submitWord.addEventListener("click", checkWord);
wordInput.addEventListener("keypress", function (e) {
if (e.key === "Enter") {
checkWord();
}
});
document.getElementById("wordLength").addEventListener("change", function () {
document.getElementById("maxAttempts").value =
JSON.parse(document.getElementById("wordLength").value) + 1;
});
document.getElementById("newGame").addEventListener("click", function () {
localStorage.setItem(
"wordLength",
document.getElementById("wordLength").value
);
localStorage.setItem(
"maxAttempts",
document.getElementById("maxAttempts").value
);
location.reload();
});
// 初始化游戏
function startGame() {
getRandomWord().then((word) => {
// 不包含除了英文以外的字符
if (/* word.match(/[^a-zA-Z]/) */ false) {
startGame();
} else {
secretWord = word;
initBoard();
}
});
}
startGame();
var Crack = () => {
crackUpdateBoard(secretWord);
console.log('Cracking! The word is: "' + secretWord + '"');
submitWord.disabled = true;
};