-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.js
139 lines (130 loc) · 4.02 KB
/
wordle.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
/**
* Wordle game implementation in JavaScript.
*/
// Initialize current row and column
let curRow = 0;
let curCol = 0;
// Select the main element
const main = document.querySelector('#main');
startGame()
/**
* Function to draw the game grid.
*/
function drawGrid() {
// Create rows and columns
for (let i = 0; i < 6; i++) {
const row = document.createElement('div');
row.classList.add('row', `r${i}`);
for (let j = 0; j < 5; j++) {
const col = document.createElement('div');
col.id = `r${i}c${j}`;
row.appendChild(col);
}
main.appendChild(row);
}
}
/**
* Function to fetch a random word from an API.
* @returns {Promise<string>} A promise that resolves to a random word.
*/
async function getRandomWord() {
const response = await fetch("https://random-word-api.herokuapp.com/word?length=5");
const data = await response.json();
return data;
}
/**
* Function to handle the game logic.
*/
function handleGame() {
// Fetch a random word and attach event listeners
getRandomWord().then(randomWord => {
const word = randomWord.toString()
console.log(word);
attachListener(curRow, curCol);
document.querySelector('#enterBtn').addEventListener('click', () => {
if (curCol > 4) {
const userWord = getWord(curRow);
if (checkWord(word.toLowerCase(), userWord.toLowerCase(), curRow)) {
document.querySelector('#heading').textContent = "You Won!"
return;
}
if (curRow >= 5) {
document.querySelector('#heading').innerHTML = `You Lose!<br>The word was ${word}`;
return;
}
curRow++;
curCol = 0;
attachListener(curRow, curCol);
} else {
alert("Please fill the whole row!");
}
});
}).catch(error => {
console.log(`ERROR: ${error}`);
});
}
/**
* Function to attach keypress event listener.
* @param {number} row - The row number.
* @param {number} col - The column number.
*/
function attachListener(row, col) {
const handler = (event) => {
const box = document.querySelector(`#r${row}c${col}`);
box.textContent = event.key.toUpperCase();
box.classList.add('glow');
setTimeout(() => { box.classList.remove('glow') }, 200);
curCol++;
if (curCol > 4) {
document.removeEventListener('keypress', handler);
return;
}
document.removeEventListener('keypress', handler);
if (curRow <= 5 && curCol <= 4) {
attachListener(curRow, curCol);
}
}
document.addEventListener('keypress', handler);
}
/**
* Function to get the word from the row.
* @param {number} row - The row number.
* @returns {string} The word from the row.
*/
function getWord(row) {
let word = "";
document.querySelectorAll(`.r${row} > div`).forEach(box => {
word += box.textContent.trim();
})
return word;
}
/**
* Function to check the user's word against the actual word.
* @param {string} actualWord - The actual word.
* @param {string} word - The user's word.
* @param {number} row - The row number.
* @returns {boolean} True if the user's word matches the actual word, false otherwise.
*/
function checkWord(actualWord, word, row) {
const actualWordArr = actualWord.split('');
const wordArr = word.split('');
for (let i = 0; i < actualWordArr.length; i++) {
let box = document.querySelector(`#r${row}c${i}`);
if (actualWordArr[i] === wordArr[i]) {
box.style.background = 'green';
} else if (actualWordArr.indexOf(wordArr[i]) != -1) {
box.style.background = 'yellow';
} else {
box.style.background = '#B4B4B3';
}
}
return actualWord === word;
}
/**
* Function to start the game.
*/
function startGame() {
main.innerHTML = '';
drawGrid();
handleGame();
}