forked from akshat-jn-crypto/Wordle-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
199 lines (173 loc) · 4.74 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
import targetWords from "./targetWords.js";
import dictionary from "./dictionary.js";
const WORD_LENGTH = 5
const FLIP_ANIMATION_DURATION = 500
const DANCE_ANIMATION_DURATION = 500
const keyboard = document.querySelector("[data-keyboard]")
const alertContainer = document.querySelector("[data-alert-container]")
const guessGrid = document.querySelector("[data-guess-grid]")
const offsetFromDate = new Date(2022, 0, 1)
const msOffset = Date.now() - offsetFromDate
const dayOffset = msOffset / 1000 / 60 / 60 / 24
const targetWord = targetWords[Math.floor(dayOffset) % targetWords.length];
const allWords = Array.from(new Set([...targetWords, ...dictionary]));
startInteraction()
function startInteraction() {
document.addEventListener("click", handleMouseClick)
document.addEventListener("keydown", handleKeyPress)
}
function stopInteraction() {
document.removeEventListener("click", handleMouseClick)
document.removeEventListener("keydown", handleKeyPress)
}
function handleMouseClick(e) {
if (e.target.matches("[data-key]")) {
pressKey(e.target.dataset.key)
return
}
if (e.target.matches("[data-enter]")) {
submitGuess()
return
}
if (e.target.matches("[data-delete]")) {
deleteKey()
return
}
}
function handleKeyPress(e) {
if (e.key === "Enter") {
submitGuess()
return
}
if (e.key === "Backspace" || e.key === "Delete") {
deleteKey()
return
}
if (e.key.match(/^[a-z]$/)) {
pressKey(e.key)
return
}
}
function pressKey(key) {
const activeTiles = getActiveTiles()
if (activeTiles.length >= WORD_LENGTH) return
const nextTile = guessGrid.querySelector(":not([data-letter])")
nextTile.dataset.letter = key.toLowerCase()
nextTile.textContent = key
nextTile.dataset.state = "active"
}
function deleteKey() {
const activeTiles = getActiveTiles()
const lastTile = activeTiles[activeTiles.length - 1]
if (lastTile == null) return
lastTile.textContent = ""
delete lastTile.dataset.state
delete lastTile.dataset.letter
}
function submitGuess() {
const activeTiles = [...getActiveTiles()]
if (activeTiles.length !== WORD_LENGTH) {
showAlert("Not enough letters")
shakeTiles(activeTiles)
return
}
const guess = activeTiles.reduce((word, tile) => {
return word + tile.dataset.letter
}, "")
if (!allWords.includes(guess)) {
showAlert("Not in word list")
shakeTiles(activeTiles)
return
}
stopInteraction()
activeTiles.forEach((...params) => flipTile(...params, guess))
}
function flipTile(tile, index, array, guess) {
const letter = tile.dataset.letter
const key = keyboard.querySelector(`[data-key="${letter}"i]`)
setTimeout(() => {
tile.classList.add("flip")
}, (index * FLIP_ANIMATION_DURATION) / 2)
tile.addEventListener(
"transitionend",
() => {
tile.classList.remove("flip")
if (targetWord[index] === letter) {
tile.dataset.state = "correct"
key.classList.add("correct")
} else if (targetWord.includes(letter)) {
tile.dataset.state = "wrong-location"
key.classList.add("wrong-location")
} else {
tile.dataset.state = "wrong"
key.classList.add("wrong")
}
if (index === array.length - 1) {
tile.addEventListener(
"transitionend",
() => {
startInteraction()
checkWinLose(guess, array)
},
{ once: true }
)
}
},
{ once: true }
)
}
function getActiveTiles() {
return guessGrid.querySelectorAll('[data-state="active"]')
}
function showAlert(message, duration = 1000) {
const alert = document.createElement("div")
alert.textContent = message
alert.classList.add("alert")
alertContainer.prepend(alert)
if (duration == null) return
setTimeout(() => {
alert.classList.add("hide")
alert.addEventListener("transitionend", () => {
alert.remove()
})
}, duration)
}
function shakeTiles(tiles) {
tiles.forEach(tile => {
tile.classList.add("shake")
tile.addEventListener(
"animationend",
() => {
tile.classList.remove("shake")
},
{ once: true }
)
})
}
function checkWinLose(guess, tiles) {
if (guess === targetWord) {
showAlert("You Win", 5000)
danceTiles(tiles)
stopInteraction()
return
}
const remainingTiles = guessGrid.querySelectorAll(":not([data-letter])")
if (remainingTiles.length === 0) {
showAlert(targetWord.toUpperCase(), null)
stopInteraction()
}
}
function danceTiles(tiles) {
tiles.forEach((tile, index) => {
setTimeout(() => {
tile.classList.add("dance")
tile.addEventListener(
"animationend",
() => {
tile.classList.remove("dance")
},
{ once: true }
)
}, (index * DANCE_ANIMATION_DURATION) / 5)
})
}