-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
356 lines (310 loc) · 11 KB
/
Copy pathscript.js
File metadata and controls
356 lines (310 loc) · 11 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
let words = [];
let targetWord = "";
let currentGuess = "";
let currentRow = 0;
let isCustomWord = false; // Flag to indicate if a custom word is being used
let hintGiven = false;
let hintMessage = '';
const keyboardLayout = ["QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"];
// Fetch words from words.txt
fetch('words.txt')
.then(response => response.text())
.then(data => {
words = data.split('\n').map(word => word.trim().toUpperCase()).filter(word => word.length === 5);
newRandomWord();
})
.catch(error => console.error('Error loading words:', error));
document.addEventListener('DOMContentLoaded', () => {
setupKeyboard();
setupBoard();
setupEventListeners();
loadStreak();
const menuButton = document.getElementById('menu-button');
const dropdownMenu = document.getElementById('dropdown-menu');
menuButton.addEventListener('click', function() {
dropdownMenu.style.display = dropdownMenu.style.display === 'block' ? 'none' : 'block';
});
// Close the dropdown if clicked outside
window.addEventListener('click', function(event) {
if (!menuButton.contains(event.target) && !dropdownMenu.contains(event.target)) {
dropdownMenu.style.display = 'none';
}
});
});
let streak = 0;
function loadStreak() {
const savedStreak = localStorage.getItem('wordle-streak');
streak = savedStreak ? parseInt(savedStreak, 10) : 0;
updateStreakDisplay();
console.log(`Loaded streak: ${streak}`);
}
function updateStreakDisplay() {
const feedback = document.getElementById('feedback');
feedback.innerText = `Current Streak: ${streak}`;
}
function checkGuess() {
const boxes = document.querySelectorAll(`[data-row='${currentRow}']`);
const targetWordArray = targetWord.split('');
const guessArray = currentGuess.split('');
// First pass: check for correct positions
for (let i = 0; i < 5; i++) {
const char = guessArray[i];
const box = boxes[i];
if (char === targetWordArray[i]) {
box.classList.add('green');
targetWordArray[i] = null;
guessArray[i] = null;
}
}
// Second pass: check for present but misplaced letters
for (let i = 0; i < 5; i++) {
const char = guessArray[i];
const box = boxes[i];
if (char && targetWordArray.includes(char)) {
box.classList.add('yellow');
targetWordArray[targetWordArray.indexOf(char)] = null;
} else if (char) {
box.classList.add('gray');
}
}
updateKeyboardColors();
if (currentGuess.trim() === targetWord) {
alert("Congratulations! You've guessed the word!");
streak++;
localStorage.setItem('wordle-streak', streak);
updateStreakDisplay();
} else if (currentRow < 5) {
currentRow++;
currentGuess = '';
const nextRowBoxes = document.querySelectorAll(`[data-row='${currentRow}']`);
nextRowBoxes.forEach(box => {
box.contentEditable = true;
box.addEventListener('input', handleInput);
box.addEventListener('keydown', handleKeyDown);
});
nextRowBoxes[0].focus();
} else {
alert(`Game over! The word was: ${targetWord}`);
streak = 0;
localStorage.setItem('wordle-streak', streak);
updateStreakDisplay();
}
}
function setupEventListeners() {
document.getElementById('generate-code-button').addEventListener('click', generateCode);
document.getElementById('start-game-button').addEventListener('click', startGame);
document.getElementById('new-random-word-button').addEventListener('click', newRandomWord);
document.getElementById('hint-button').addEventListener('click', provideHint);
document.getElementById('close-hint').addEventListener('click', closeHintPopup); // Close button listener
}
function provideHint() {
if (!hintGiven) {
const randomPosition = Math.floor(Math.random() * 5); // Random number between 0 and 4
const letter = targetWord[randomPosition];
const ordinalNumbers = ["1st", "2nd", "3rd", "4th", "5th"];
hintMessage = `The ${ordinalNumbers[randomPosition]} letter is ${letter}`;
hintGiven = true;
}
displayHintPopup(hintMessage);
}
function displayHintPopup(message) {
const hintPopup = document.getElementById('hint-popup');
const hintMessageElement = document.getElementById('hint-message');
hintMessageElement.innerText = message;
hintPopup.classList.add('show');
}
function closeHintPopup() {
const hintPopup = document.getElementById('hint-popup');
hintPopup.classList.remove('show');
}
function setupKeyboard() {
const keyboard = document.getElementById('keyboard');
keyboard.innerHTML = '';
keyboardLayout.forEach(row => {
const rowElement = document.createElement('div');
rowElement.classList.add('keyboard-row');
for (let char of row) {
const key = document.createElement('div');
key.classList.add('key');
key.innerText = char;
key.addEventListener('click', () => handleKeyPress(char));
rowElement.appendChild(key);
}
keyboard.appendChild(rowElement);
});
}
function generateCode() {
const wordInput = document.getElementById('word-input').value.toUpperCase();
if (wordInput.length === 5) {
const code = btoa(wordInput);
alert(`Share this code: ${code}`);
} else {
alert('Please enter a 5-letter word.');
}
}
function startGame() {
const codeInput = document.getElementById('code-input').value;
const decodedWord = decodeCode(codeInput);
if (decodedWord) {
targetWord = decodedWord;
isCustomWord = true; // Set flag for custom word
} else {
targetWord = getRandomWord();
isCustomWord = false; // Reset flag if not a custom word
}
setupBoard();
hintGiven = false; // Reset hint flag for new game
}
function newRandomWord() {
targetWord = getRandomWord();
isCustomWord = false; // Reset flag for random word
setupBoard();
hintGiven = false; // Reset hint flag for new game
}
function decodeCode(code) {
try {
const decoded = atob(code);
return decoded.length === 5 ? decoded.toUpperCase() : null;
} catch (e) {
return null;
}
}
function getRandomWord() {
return words[Math.floor(Math.random() * words.length)];
}
function setupBoard() {
const board = document.getElementById('game-board');
board.innerHTML = '';
currentGuess = '';
currentRow = 0;
for (let i = 0; i < 6; i++) {
for (let j = 0; j < 5; j++) {
const box = document.createElement('div');
box.classList.add('letter-box');
box.setAttribute('data-row', i);
box.setAttribute('data-index', j);
if (i === 0) {
box.contentEditable = true;
box.addEventListener('input', handleInput);
box.addEventListener('keydown', handleKeyDown);
}
board.appendChild(box);
}
}
document.querySelector(`[data-row='0'][data-index='0']`).focus();
}
function handleKeyPress(char) {
const activeBox = document.querySelector(`[data-row='${currentRow}'] .letter-box:empty`);
if (activeBox) {
activeBox.innerText = char;
handleInput({target: activeBox});
}
}
function handleInput(event) {
const box = event.target;
const index = parseInt(box.getAttribute('data-index'));
const row = parseInt(box.getAttribute('data-row'));
if (row !== currentRow) return;
let char = box.innerText.trim().toUpperCase();
if (char.length > 1) {
char = char[0];
box.innerText = char;
}
currentGuess = currentGuess.substring(0, index) + char + currentGuess.substring(index + 1);
currentGuess = currentGuess.padEnd(5, ' ');
if (char && index < 4) {
const nextBox = document.querySelector(`[data-row='${currentRow}'][data-index='${index + 1}']`);
if (nextBox) {
nextBox.focus();
}
}
}
function handleKeyDown(event) {
const box = event.target;
const index = parseInt(box.getAttribute('data-index'));
if (event.key === 'Enter') {
event.preventDefault();
const guess = currentGuess.trim().toUpperCase();
if (guess.length === 5 && (isCustomWord || words.includes(guess))) {
checkGuess();
} else {
showInvalidMessage();
}
} else if (event.key === 'Backspace') {
event.preventDefault();
if (box.innerText.length > 0) {
box.innerText = '';
currentGuess = currentGuess.substring(0, index) + ' ' + currentGuess.substring(index + 1);
} else if (index > 0) {
const prevBox = document.querySelector(`[data-row='${currentRow}'][data-index='${index - 1}']`);
if (prevBox) {
prevBox.focus();
prevBox.innerText = '';
currentGuess = currentGuess.substring(0, index - 1) + ' ' + currentGuess.substring(index);
}
}
}
}
function showInvalidMessage() {
const message = document.getElementById('invalid-message');
message.classList.add('show');
setTimeout(() => {
message.classList.remove('show');
}, 2000);
}
let keyColorState = {};
function updateKeyboardColors() {
const keys = document.querySelectorAll('.key');
// Initialize keyColorState with existing colors if not set
keys.forEach(key => {
const char = key.innerText;
if (!keyColorState[char]) {
keyColorState[char] = '';
}
});
const targetWordArray = targetWord.split('');
const guessArray = currentGuess.split('');
// First pass: check for green matches
for (let i = 0; i < 5; i++) {
const char = guessArray[i];
if (char === targetWordArray[i]) {
keyColorState[char] = 'green';
targetWordArray[i] = null;
guessArray[i] = null;
}
}
// Second pass: check for yellow matches
for (let i = 0; i < 5; i++) {
const char = guessArray[i];
if (char && targetWordArray.includes(char)) {
if (keyColorState[char] !== 'green') {
keyColorState[char] = 'yellow';
}
targetWordArray[targetWordArray.indexOf(char)] = null;
}
}
// Third pass: assign gray if not already green or yellow
for (let i = 0; i < 5; i++) {
const char = guessArray[i];
if (char && !targetWordArray.includes(char) && keyColorState[char] === '') {
keyColorState[char] = 'gray';
}
}
// Update the keyboard keys
keys.forEach(key => {
const char = key.innerText;
key.style.backgroundColor = getColor(keyColorState[char]);
});
}
function getColor(status) {
switch (status) {
case 'green':
return '#6aaa64';
case 'yellow':
return '#c9b458';
case 'gray':
return '#787c7e';
default:
return '#eee'; // Default background color
}
}