-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
157 lines (134 loc) · 3.97 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
const tileDisplay = document.querySelector('.tile-container');
const keyboard = document.querySelector('.keyboard-container');
const messageDisplay = document.querySelector('.message-container');
let wordle;
const getWordle = () => {
fetch('http://localhost:8000/word')
.then(response => response.json())
.then(json => {
wordle = json.toUpperCase()
})
.catch(err => console.log(err))
}
getWordle()
let isGameOver;
const keys = [
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P',
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', '<-',
'Z', 'X', 'C', 'V', 'B', 'N', 'M', 'ENTER',
]
const guessRows = [
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', ''],
['', '', '', '', '']
]
let currentRow = 0, currentTile = 0;
guessRows.forEach((row, idx) => {
const rowElement = document.createElement('div');
rowElement.setAttribute('id', 'row_' + idx);
row.forEach((guess, guessIdx) => {
const tileElement = document.createElement('div');
tileElement.setAttribute('id', 'row_' + idx + '-guess_' + guessIdx)
tileElement.classList.add('tile');
rowElement.append(tileElement);
})
tileDisplay.append(rowElement);
})
keys.forEach(key => {
const buttonElement = document.createElement('button');
buttonElement.textContent = key;
buttonElement.setAttribute('id', "kbd_" + key)
buttonElement.addEventListener('click', () => handleClick(key))
keyboard.append(buttonElement);
})
const handleClick = (key) => {
console.log('clicked ' + key);
if (key == "<-") {
deleteLetter()
return
} else if (key == "ENTER") {
checkRow()
return
}
addLetter(key);
}
const addLetter = (letter) => {
if (currentTile < 5 && currentRow < 6) {
const tile = document.getElementById("row_" + currentRow + "-guess_" + currentTile);
tile.textContent = letter;
guessRows[currentRow][currentTile] = letter;
tile.setAttribute('data', letter);
currentTile++;
}
}
const deleteLetter = () => {
if (currentTile > 0) {
currentTile--;
const tile = document.getElementById('row_' + currentRow + "-guess_" + currentTile);
tile.textContent = '';
guessRows[currentRow][currentTile] = '';
tile.setAttribute('data', '')
}
}
const checkRow = () => {
const guess = guessRows[currentRow].join('');
if (currentTile == 5) {
console.log('guess is ' + guess, 'wordle is ' + wordle);
flipTile();
if (wordle == guess) {
showMessage('Great!');
isGameOver = true;
}else{
if (currentRow >= 5){
isGameOver = false;
showMessage('Game Over');
return;
}
if(currentRow < 5){
currentRow++;
currentTile = 0;
}
}
}
}
const showMessage = (message) => {
const messageElement = document.createElement('p')
messageElement.textContent = message;
messageDisplay.append(messageElement);
setTimeout(() => messageDisplay.removeChild(messageElement), 2000)
}
const addColorToKey = (keyLetter, color) => {
const key = document.getElementById("kbd_" + keyLetter)
key.classList.add(color)
}
//parado em 1:01:17 do video em https://www.youtube.com/watch?v=mpby4HiElek
const flipTile = () =>{
const rowTiles = document.querySelector('#row_' + currentRow).childNodes;
let checkWordle = wordle;
const guess = [];
rowTiles.forEach(tile =>{
guess.push({letter: tile.getAttribute('data'), color: 'grey-overlay'})
})
guess.forEach((guess, index) => {
if (guess.letter== wordle[index]){
guess.color = 'green-overlay'
checkWordle = checkWordle.replace(guess.letter, '')
}
})
guess.forEach(guess =>{
if(checkWordle.includes(guess.letter)){
guess.color = 'yellow-overlay';
checkWordle = checkWordle.replace(guess.letter, '');
}
})
rowTiles.forEach((tile, index) =>{
setTimeout(()=>{
tile.classList.add('flip')
tile.classList.add(guess[index].color);
addColorToKey(guess[index].letter, guess[index].color)
}, 500 * index)
})
}