-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwordle.html
More file actions
1057 lines (913 loc) · 38.4 KB
/
wordle.html
File metadata and controls
1057 lines (913 loc) · 38.4 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>WordlePro</title>
<link rel="icon" type="image/png" href="wordle_logo_32x32.png">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@7.2.0/css/all.min.css">
<!-- 引入官方替代字体 -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Libre+Franklin:wght@100..900&family=Roboto+Slab:wght@100..900&display=swap"
rel="stylesheet">
<link rel="stylesheet" href="style.css">
<script src="common.js"></script>
<style>
.icon-btn svg {
width: 24px;
height: 24px;
}
#main-layout {
display: flex;
flex-grow: 1;
width: 100%;
position: relative;
overflow: hidden;
}
/* 提示面板:绝对定位在左侧,全高 */
#hint-panel {
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 200px;
background-color: var(--bg);
border-right: 1px solid var(--border-empty);
z-index: 20;
display: flex;
flex-direction: column;
box-shadow: 2px 0 10px rgba(0, 0, 0, 0.05);
animation: slideIn 0.2s ease-out;
transform: translateX(-100%);
transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), visibility 0.3s;
visibility: hidden;
}
@keyframes slideIn {
from {
transform: translateX(-100%);
}
to {
transform: translateX(0);
}
}
#hint-panel.open {
transform: translateX(0);
visibility: visible;
transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
#hint-header {
padding: 15px 10px;
font-size: 13px;
font-weight: 700;
text-transform: uppercase;
border-bottom: 1px solid var(--border-empty);
text-align: center;
color: var(--text-muted);
letter-spacing: 0.5px;
}
#hint-list {
flex-grow: 1;
overflow-y: auto;
padding: 10px 0;
font-family: 'Libre Franklin', sans-serif;
font-weight: 700;
font-size: 17px;
line-height: 2.2;
text-transform: uppercase;
text-align: center;
}
#hint-list::-webkit-scrollbar {
width: 4px;
}
#hint-list::-webkit-scrollbar-thumb {
background: var(--border-empty);
}
/* 提示面板容器 */
#hint-list-container {
flex-grow: 1;
overflow-y: auto;
padding: 10px 0;
}
/* 高性能渲染区域:使用 pre 保留换行,textContent 写入 */
#hint-list-pre {
margin: 0;
font-family: 'Libre Franklin', sans-serif;
font-weight: 700;
font-size: 17px;
line-height: 2.2;
text-transform: uppercase;
text-align: center;
color: var(--text);
content-visibility: auto;
}
/* 隐藏滚动条但保留功能 (可选) */
#hint-list-container::-webkit-scrollbar {
width: 4px;
}
#hint-list-container::-webkit-scrollbar-thumb {
background: var(--border-empty);
}
#start-screen {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #e3e3e1;
/* 官网米灰色 */
color: #000000;
z-index: 1000;
display: flex;
justify-content: center;
align-items: center;
transition: opacity 0.4s ease-out;
}
.start-content {
text-align: center;
width: 100%;
max-width: 500px;
}
.start-logo {
width: 80px;
height: 80px;
margin-bottom: 12px;
}
.start-title {
font-family: 'Roboto Slab', serif;
font-size: 50px;
font-weight: 700;
margin: 0 0 12px;
}
.start-subtitle {
font-family: 'Roboto Slab', sans-serif;
font-size: 38px;
font-weight: 350;
margin: 0 0 36px;
}
.play-btn {
background: #000;
color: #fff;
border: none;
border-radius: 30px;
height: 48px;
width: 150px;
font-size: 16px;
font-weight: 700;
cursor: pointer;
transition: transform 0.1s;
}
.play-btn:active {
transform: scale(0.96);
}
/* 作答区:始终保持全屏居中 */
#game-area {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
}
#game-container {
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 10px;
}
#board {
display: flex;
flex-direction: column;
/* 垂直排列 */
gap: 5px;
/* 每一行的间距 */
width: auto;
height: auto;
padding: 10px;
visibility: visible;
/* 确保初始化后可见 */
}
/* 键盘:等宽设计 */
#keyboard {
width: 100%;
padding: 0 10px 20px;
flex-shrink: 0;
}
.keyboard-row {
display: flex;
justify-content: center;
margin-bottom: 8px;
gap: 6px;
}
.hidden {
opacity: 0;
pointer-events: none;
}
#modal-message {
margin-bottom: 24px;
font-size: 18px;
line-height: 1.4;
}
.modal-buttons {
display: flex;
gap: 12px;
justify-content: center;
}
.modal-btn {
padding: 12px 24px;
border-radius: 4px;
border: none;
cursor: pointer;
font-weight: 700;
font-size: 14px;
text-transform: uppercase;
transition: filter 0.2s;
}
/* 确定按钮:黑色(浅色模式)或白色(深色模式) */
.modal-btn.confirm {
background-color: var(--text);
color: var(--bg);
}
/* 取消按钮:灰色 */
.modal-btn.cancel {
background-color: var(--gray);
color: #ffffff;
}
.modal-btn:hover {
filter: brightness(1.2);
}
/* 高对比度模式下的根变量覆盖 */
body.high-contrast {
--green: #f5793a;
/* 橘色替代绿色 */
--yellow: #85c0f9;
/* 浅蓝替代黄色 */
}
/* 设置中的 Select 样式 */
#lang-select-settings {
background: var(--bg);
color: var(--text);
border: 1px solid var(--border-empty);
padding: 5px;
border-radius: 4px;
}
</style>
</head>
<body>
<div id="start-screen">
<div class="start-content">
<img src="wordle-icon.svg" class="start-logo">
<h1 class="start-title">WordlePro</h1>
<p class="start-subtitle">Loading...</p>
<button class="play-btn" onclick="dismissStartScreen()">Play</button>
</div>
</div>
<!-- Header 部分 -->
<header>
<button class="icon-btn" onclick="location.href='index.html'" title="Back">
<i class="fa-solid fa-arrow-left"></i>
</button>
<div class="header-controls">
<button class="icon-btn" id="reset-btn" title="Restart" onclick="resetGame()">
<i class="fa-solid fa-arrow-rotate-left"></i>
</button>
<button class="icon-btn" id="mode-toggle-btn" title="Toggle Mode" onclick="toggleWordMode()">
<i id="mode-icon" class="fa-solid fa-star"></i>
</button>
<button class="icon-btn" title="Hints" onclick="toggleHints()">
<i class="fa-solid fa-lightbulb"></i>
</button>
</div>
<img src="wordle-icon.svg" class="header-logo" alt="Logo">
<p class="header-title">WordlePro</p>
<div class="header-settings">
<button class="icon-btn" onclick="openSettings()">
<i class="fa-solid fa-gear"></i>
</button>
</div>
</header>
<!-- 设置模态框 -->
<div id="settings-modal" class="modal-overlay">
<div class="modal-content settings-content">
<div class="settings-header">
<h2 id="settings-title">SETTINGS</h2>
<button class="icon-btn" onclick="closeSettings()">
<i class="fa-solid fa-xmark"></i>
</button>
</div>
<div class="setting-item">
<div class="setting-text">
<div class="setting-label" id="label-hard-mode">Hard Mode</div>
<div class="setting-desc" id="desc-hard-mode">Any revealed hints must be used in subsequent guesses
</div>
</div>
<label class="switch">
<input type="checkbox" id="setting-hard-mode" onchange="toggleHardMode(this.checked)">
<span class="slider"></span>
</label>
</div>
<div class="setting-item">
<div class="setting-text">
<div class="setting-label" id="label-dark-mode">Dark Mode</div>
</div>
<label class="switch">
<input type="checkbox" id="setting-dark-mode" onchange="toggleTheme(this.checked)">
<span class="slider"></span>
</label>
</div>
<div class="setting-item">
<div class="setting-text">
<div class="setting-label" id="label-high-contrast">High Contrast Mode</div>
<div class="setting-desc" id="desc-high-contrast">For improved color vision</div>
</div>
<label class="switch">
<input type="checkbox" id="setting-high-contrast" onchange="toggleHighContrast(this.checked)">
<span class="slider"></span>
</label>
</div>
<div class="setting-item">
<div class="setting-text">
<div class="setting-label" id="label-language">Language</div>
</div>
<select id="lang-select-settings" onchange="changeLanguage(this.value)">
<option value="en">English</option>
<option value="zh">中文</option>
</select>
</div>
<div class="settings-footer">
<p>©
<script>document.write(new Date().getFullYear())</script> WordlePro
</p>
</div>
</div>
</div>
<div id="loading">Loading dictionary...</div>
<div id="toast-container"></div>
<div id="main-layout">
<aside id="hint-panel">
<div id="hint-header">Loading...</div>
<div id="hint-list-container">
<!-- 核心:使用 pre 标签处理成千上万个单词的换行 -->
<pre id="hint-list-pre"></pre>
</div>
</aside>
<main id="game-area">
<div id="game-container">
<div id="board"></div>
</div>
<div id="keyboard">
<div class="keyboard-row" id="row1"></div>
<div class="keyboard-row" id="row2"></div>
<div class="keyboard-row" id="row3"></div>
</div>
</main>
</div>
<!-- 自定义确认弹窗 -->
<div id="modal-overlay" class="modal-overlay">
<div class="modal-content">
<p id="modal-message"></p>
<div class="modal-buttons">
<button id="modal-cancel-btn" class="modal-btn cancel"></button>
<button id="modal-confirm-btn" class="modal-btn confirm"></button>
</div>
</div>
</div>
<script>
const ANSWERS_FILE = 'possible_words.txt';
const ALLOWED_FILE = 'allowed_words.txt';
let targetWord = "";
let validGuesses = new Set();
let answersPool = [];
let currentGuess = "";
let guesses = [];
let isAnimating = false;
let initialized = false;
let gameOverToast = null;
let gameStatus = "playing";
let hintsVisible = false;
let bounceTimeouts = []; // 用于追踪胜利跳动的计时器
let hardMode = localStorage.getItem('wordle_hard_mode') === 'true';
let constraints = {
correct: Array(5).fill(null),
present: {},
absent: new Set()
};
let gameMode = localStorage.getItem('wordle_game_mode') || "normal";
let combinedPool = []; // 存放所有合法词汇
let isResetting = false; // 重新加载按钮的冷却锁
let isModeChanging = false; // 冷却状态锁
const i18n = {
en: {
wordleTitle: "Standard Mode",
play: "Play",
subtitle: "Get 6 chances to guess a <span style='white-space:nowrap'>5-letter</span> word.",
newGame: "New Game Started",
notEnough: "Not enough letters",
notInList: "Not in word list",
allMode: "Mode: All Words",
commonMode: "Mode: Common Words",
confirmReset: "Are you sure you want to restart? Your current progress will be lost.",
confirmMode: "Switching modes will restart the game. Continue?",
possible: "Possible",
praises: ["Genius", "Magnificent", "Impressive", "Splendid", "Great", "Phew"],
cancel: "Cancel",
confirm: "Confirm",
settings: "SETTINGS",
hardMode: "Hard Mode",
hardModeDesc: "Any revealed hints must be used in subsequent guesses",
darkMode: "Dark Mode",
highContrast: "High Contrast Mode",
highContrastDesc: "For improved color vision",
language: "Language",
hardModeGreen: "Hard Mode: {pos} letter must be {char}",
hardModeYellow: "Hard Mode: Must contain {char}",
ordinals: ["1st", "2nd", "3rd", "4th", "5th"]
},
zh: {
wordleTitle: "标准模式",
play: "开始游戏",
subtitle: "你有 6 次机会猜出一个 5 个字母的单词。",
newGame: "新游戏已开始",
notEnough: "字数不足",
notInList: "单词不在词库中",
allMode: "模式:全部词汇",
commonMode: "模式:常用词汇",
confirmReset: "确定要重新开始吗?当前进度将丢失。",
confirmMode: "切换模式将重启游戏。是否继续?",
possible: "个可能",
praises: ["天才", "太棒了", "出色", "精彩", "不错", "险胜"],
cancel: "取消",
confirm: "确定",
settings: "设置",
hardMode: "困难模式",
hardModeDesc: "所有提示必须在后续猜测中使用",
darkMode: "深色模式",
highContrast: "高对比度模式",
highContrastDesc: "优化颜色区分",
language: "语言",
hardModeGreen: "困难模式:第 {pos} 个字母必须是 {char}",
hardModeYellow: "困难模式:必须包含 {char}",
ordinals: ["1", "2", "3", "4", "5"] // 中文直接用数字
}
};
// --- 初始化补充 ---
function initSettings() {
if (isHighContrast) document.body.classList.add('high-contrast');
// 更新设置面板翻译
updateUILanguage();
}
function toggleHardMode(checked) {
if (checked && isGameDirty()) {
showToast(currentLang === 'zh' ? "困难模式只能在游戏开始前开启" : "Hard mode can only be enabled at the start");
document.getElementById('setting-hard-mode').checked = false;
return;
}
hardMode = checked;
localStorage.setItem('wordle_hard_mode', hardMode);
}
function updateUILanguage() {
const t = i18n[currentLang];
// 更新起始页
const playBtn = document.querySelector('.play-btn');
if (playBtn) playBtn.textContent = t.play;
const subtitle = document.querySelector('.start-subtitle');
if (subtitle) subtitle.innerHTML = t.subtitle;
// 更新提示面板标题
const hintHeaderTitle = document.getElementById('hint-header-title');
// 更新选择框的值
document.querySelector('title').textContent = `WordlePro - ${t.wordleTitle}`;
document.getElementById('settings-title').textContent = t.settings;
document.getElementById('label-hard-mode').textContent = t.hardMode;
document.getElementById('desc-hard-mode').textContent = t.hardModeDesc;
document.getElementById('label-dark-mode').textContent = t.darkMode;
document.getElementById('label-high-contrast').textContent = t.highContrast;
document.getElementById('desc-high-contrast').textContent = t.highContrastDesc;
document.getElementById('label-language').textContent = t.language;
// 同步设置面板控件的值
const langSelect = document.getElementById('lang-select-settings');
if (langSelect) langSelect.value = currentLang;
const hardToggle = document.getElementById('setting-hard-mode');
if (hardToggle) hardToggle.checked = hardMode;
// 更新提示面板标题
const hintHeader = document.getElementById('hint-header');
if (hintHeader) hintHeader.textContent = t.possibleWords;
// 更新当前的提示列表(词频后缀等)
updateHintList();
}
async function initGame() {
try {
const [res1, res2] = await Promise.all([fetch(ANSWERS_FILE), fetch(ALLOWED_FILE)]);
const text1 = await res1.text();
const text2 = await res2.text();
// 正常模式词池 (常用词)
answersPool = text1.split(/\r?\n/).map(w => w.trim().toUpperCase()).filter(w => w.length === 5).sort();
const allowed = text2.split(/\r?\n/).map(w => w.trim().toUpperCase()).filter(w => w.length === 5);
// 验证集
validGuesses = new Set([...answersPool, ...allowed]);
// 所有词汇模式词池 (全量)
combinedPool = Array.from(validGuesses).sort();
// 确保初始化图标与保存的模式一致
const icon = document.getElementById('mode-icon');
if (gameMode === "all") {
icon.classList.remove("fa-star");
icon.classList.add("fa-earth-americas");
}
startNewRound();
document.getElementById('loading').style.display = 'none';
document.getElementById('board').style.visibility = 'visible';
initialized = true;
} catch (e) {
document.getElementById('loading').textContent = "Dictionary Load Error.";
}
}
function startNewRound() {
clearPersistentToast(); // 在开始新回合时清除持久化提示框
// 清理跳动计时器和数组
bounceTimeouts.forEach(clearTimeout);
bounceTimeouts = [];
const pool = (gameMode === "normal") ? answersPool : combinedPool;
targetWord = pool[Math.floor(Math.random() * pool.length)];
currentGuess = "";
guesses = [];
gameStatus = "playing";
constraints = { correct: Array(5).fill(null), present: {}, absent: new Set() };
updateHintList();
document.querySelectorAll('.tile').forEach(tile => {
tile.textContent = "";
tile.removeAttribute("data-state");
// 确保这里包含 "bounce"
tile.classList.remove("pop", "shake", "flip", "bounce");
});
document.querySelectorAll('.key').forEach(key => {
key.removeAttribute("data-state");
key.style.backgroundColor = "";
key.style.color = "";
});
updateClearBtnVisibility();
}
async function toggleWordMode() {
if (isModeChanging || isAnimating) return;
// 如果有进度,弹出原生确认框
if (isGameDirty()) {
// 核心修改:等待异步确认
const confirmed = await showCustomConfirm(i18n[currentLang].confirmMode);
if (!confirmed) return;
}
const btn = document.getElementById('mode-toggle-btn');
const icon = document.getElementById('mode-icon');
isModeChanging = true;
btn.disabled = true;
if (gameMode === "normal") {
gameMode = "all";
icon.classList.remove("fa-star");
icon.classList.add("fa-earth-americas");
showToast(i18n[currentLang].allMode);
} else {
gameMode = "normal";
icon.classList.remove("fa-earth-americas");
icon.classList.add("fa-star");
showToast(i18n[currentLang].commonMode);
}
// 保存到本地存储
localStorage.setItem('wordle_game_mode', gameMode);
startNewRound();
setTimeout(() => {
isModeChanging = false;
btn.disabled = false;
}, 5000);
}
// --- 新增模式切换函数 ---
function toggleMode() {
if (isAnimating) return;
const modeIcon = document.getElementById('mode-icon');
if (gameMode === "normal") {
gameMode = "all";
modeIcon.innerHTML.classList.remove("fa-star");
modeIcon.classList.add("fa-earth-americas");
showToast(i18n[currentLang].allMode);
} else {
gameMode = "normal";
modeIcon.innerHTML.classList.remove("fa-earth-americas");
modeIcon.classList.add("fa-star");
showToast(i18n[currentLang].commonMode);
}
startNewRound();
}
function toggleHints() {
hintsVisible = !hintsVisible;
const panel = document.getElementById('hint-panel');
if (hintsVisible) {
// 1. 立即开始动画
panel.classList.add('open');
// 2. 核心:延迟渲染内容
// 等待 300ms(动画时长)结束后,再渲染大数据
// 这样主线程在动画期间是空闲的
setTimeout(() => {
if (hintsVisible) updateHintList();
}, 300);
} else {
panel.classList.remove('open');
// 关闭时可以立即清空内容,减轻浏览器负担
setTimeout(() => {
if (!hintsVisible) document.getElementById('hint-list-pre').textContent = "";
}, 300);
}
if (document.activeElement) document.activeElement.blur();
}
function updateHintList() {
if (!hintsVisible) return;
const listPre = document.getElementById('hint-list-pre');
if (!listPre) return;
const currentPool = (gameMode === "normal") ? answersPool : combinedPool;
const possible = currentPool.filter(word => {
for (let i = 0; i < 5; i++) {
if (constraints.correct[i] && word[i] !== constraints.correct[i]) return false;
}
for (let char in constraints.present) {
if (!word.includes(char)) return false;
for (let pos of constraints.present[char]) {
if (word[pos] === char) return false;
}
}
for (let char of constraints.absent) {
if (constraints.correct.includes(char) || constraints.present[char]) continue;
if (word.includes(char)) return false;
}
return true;
});
document.getElementById('hint-header').textContent = `${possible.length} ${i18n[currentLang].possible}`;
// 使用 \n 换行,textContent 性能极高,不会导致卡死
listPre.textContent = possible.join('\n');
}
async function resetGame() {
if (isResetting || isAnimating) return;
if (isGameDirty()) {
// 核心修改:等待异步确认
const confirmed = await showCustomConfirm(i18n[currentLang].confirmReset);
if (!confirmed) return;
}
const btn = document.getElementById('reset-btn');
isResetting = true;
if (btn) btn.disabled = true;
if (document.activeElement) document.activeElement.blur();
startNewRound();
showToast(i18n[currentLang].newGame);
setTimeout(() => {
isResetting = false;
if (btn) btn.disabled = false;
}, 5000);
}
const board = document.getElementById("board");
board.innerHTML = ""; // 清空原有内容
for (let r = 0; r < 6; r++) {
const row = document.createElement("div");
row.className = "board-row";
// 创建 5 个字母方块的容器
const tilesGrid = document.createElement("div");
tilesGrid.className = "tiles-grid";
for (let c = 0; c < 5; c++) {
const tile = document.createElement("div");
tile.className = "tile";
tile.id = "tile-" + (r * 5 + c); // 保持原有的 tile-0 到 tile-29 ID 格式
tilesGrid.appendChild(tile);
}
// 创建清除按钮
const clearBtn = document.createElement("button");
clearBtn.className = "clear-row-btn";
clearBtn.id = `clear-row-${r}`;
clearBtn.innerHTML = `
<svg viewBox="0 0 24 24" width="24" height="24">
<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>
</svg>`;
clearBtn.onclick = (e) => {
clearCurrentLine();
if (e.currentTarget) {
e.currentTarget.blur();
}
}
row.appendChild(tilesGrid);
row.appendChild(clearBtn);
board.appendChild(row);
}
const keyRows = [['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'], ['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'], ['ENTER', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '⌫']];
keyRows.forEach((row, idx) => {
const rowEl = document.getElementById(`row${idx + 1}`);
row.forEach(key => {
const btn = document.createElement("button");
btn.className = "key";
if (key === "ENTER" || key === "⌫") btn.classList.add("wide");
btn.dataset.key = key;
if (key === "⌫") {
btn.innerHTML = `<svg viewBox="0 0 24 24"><path d="M22 3H7c-.69 0-1.23.35-1.59.88L0 12l5.41 8.11c.36.53.9.89 1.59.89h15c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 16H7.07L2.4 12l4.66-7H22v14zm-11.59-2L14 13.41 17.59 17 19 15.59 15.41 12 19 8.41 17.59 7 14 10.59 10.41 7 9 8.41 12.59 12 9 15.59z"></path></svg>`;
} else {
btn.textContent = key;
}
btn.onclick = (e) => { handleInput(key); e.currentTarget.blur(); };
rowEl.appendChild(btn);
});
});
function handleInput(key) {
if (!initialized || isAnimating || gameStatus !== "playing") return;
if (key === "ENTER") {
validateAndSubmit();
} else if (key === "⌫" || key === "Backspace") {
if (currentGuess.length > 0) {
const idx = guesses.length * 5 + currentGuess.length - 1;
const tile = document.getElementById(`tile-${idx}`);
tile.textContent = "";
tile.removeAttribute("data-state");
tile.classList.remove("pop");
currentGuess = currentGuess.slice(0, -1);
}
} else if (/^[A-Z]$/.test(key.toUpperCase()) && currentGuess.length < 5) {
const char = key.toUpperCase();
const idx = guesses.length * 5 + currentGuess.length;
const tile = document.getElementById(`tile-${idx}`);
tile.textContent = char;
tile.setAttribute("data-state", "filled");
tile.classList.remove("pop");
void tile.offsetWidth;
tile.classList.add("pop");
currentGuess += char;
}
updateClearBtnVisibility();
}
document.onkeydown = (e) => {
if (e.key === "Enter" && document.activeElement) document.activeElement.blur();
handleInput(e.key === "Enter" ? "ENTER" : e.key === "Backspace" ? "⌫" : e.key.toUpperCase());
};
// --- 困难模式逻辑拦截 ---
// 修改 validateAndSubmit 函数,在有效性检查后增加困难模式检查:
async function validateAndSubmit() {
if (currentGuess.length < 5) {
showToast(i18n[currentLang].notEnough);
shakeRow(); return;
}
// --- 困难模式校验逻辑 ---
if (hardMode && guesses.length > 0) {
const t = i18n[currentLang];
// 1. 检查绿色 (正确位置)
for (let i = 0; i < 5; i++) {
const requiredChar = constraints.correct[i];
if (requiredChar && currentGuess[i] !== requiredChar) {
const coloredChar = `<span class="toast-green">${requiredChar}</span>`;
// 替换模板中的 {pos} 和 {char}
const msg = t.hardModeGreen
.replace("{pos}", t.ordinals[i])
.replace("{char}", coloredChar);
showToast(msg);
shakeRow(); return;
}
}
// 2. 检查黄色 (必须包含)
for (let char in constraints.present) {
if (!currentGuess.includes(char)) {
const coloredChar = `<span class="toast-yellow">${char}</span>`;
// 替换模板中的 {char}
const msg = t.hardModeYellow
.replace("{char}", coloredChar);
showToast(msg);
shakeRow(); return;
}
}
}
// -----------------------
if (!validGuesses.has(currentGuess)) {
showToast(i18n[currentLang].notInList);
shakeRow(); return;
}
submitGuess();
}
function shakeRow() {
const startIdx = guesses.length * 5;
for (let i = 0; i < 5; i++) {
const tile = document.getElementById(`tile-${startIdx + i}`);
tile.classList.remove("shake");
void tile.offsetWidth;
tile.classList.add("shake");
setTimeout(() => tile.classList.remove("shake"), 600);
}
}
function submitGuess() {
isAnimating = true;
const startIdx = guesses.length * 5;
const guessString = currentGuess;
const guessArr = guessString.split("");
const targetArr = [...targetWord];
const states = Array(5).fill("absent");
guessArr.forEach((c, i) => {
if (c === targetArr[i]) {
states[i] = "correct";
targetArr[i] = null;
constraints.correct[i] = c;
}
});
guessArr.forEach((c, i) => {
if (states[i] !== "correct" && targetArr.includes(c)) {
states[i] = "present";
targetArr[targetArr.indexOf(c)] = null;
if (!constraints.present[c]) constraints.present[c] = [];
constraints.present[c].push(i);
} else if (states[i] === "absent") {
constraints.absent.add(c);
}
});
guessArr.forEach((char, i) => {
const tile = document.getElementById(`tile-${startIdx + i}`);
setTimeout(() => {
tile.classList.remove("pop", "shake", "flip");
void tile.offsetWidth;
tile.classList.add("flip");
setTimeout(() => tile.setAttribute("data-state", states[i]), 250);
}, i * 300);
});
// 修改 submitGuess 函数内部最后的 setTimeout 部分
setTimeout(() => {
guessArr.forEach((char, i) => updateKey(char, states[i]));
// 注意:这里不再立即执行 isAnimating = false;
if (guessString === targetWord) {
gameStatus = "won";
showToast(i18n[currentLang].praises[guesses.length - 1]);
// 执行跳动逻辑
const startIdx = (guesses.length - 1) * 5;
for (let i = 0; i < 5; i++) {
const tile = document.getElementById(`tile-${startIdx + i}`);
const tId = setTimeout(() => {
tile.classList.add("bounce");
}, i * 100);
bounceTimeouts.push(tId);
}
// 【核心修复】胜利时,额外锁定 1500ms (400ms延迟 + 1000ms动画 + 100ms缓冲)
// 只有跳完之后,isAnimating 才会变回 false,resetGame 才能继续
setTimeout(() => {
isAnimating = false;
if (hintsVisible) updateHintList();
}, 1500);
} else {
// 如果没赢,在翻牌结束后立即解锁
isAnimating = false;
if (guesses.length === 6) {
gameStatus = "lost";
showToast(targetWord, true);
}
if (hintsVisible) updateHintList();
}
updateClearBtnVisibility();
}, 1700);
guesses.push(guessString);
currentGuess = "";
}
function updateKey(char, state) {