-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
169 lines (146 loc) · 4.72 KB
/
app.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
const tileDisplay = document.querySelector('.tileContainer')
const keyboard = document.querySelector('.keyContainer')
const messageDisplay = document.querySelector('.messageContainer')
const wordle = 'SUPER'
// let wordle
// const getWordle = () => {
// fetch('http://localhost:8000/word')
// .then(response => response.json())
// .then(json => {
// console.log(json)
// wordle = json.toUpperCase()
// })
// .catch(err => console.log(err))
// }
// getWordle()
const keys = [
'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','<<',
]
const guessRows = [
['','','','','',],
['','','','','',],
['','','','','',],
['','','','','',],
['','','','','',],
['','','','','',],
]
let currentRow = 0
let currentTile = 0
let isGameOver = false
guessRows.forEach((guessRow,guessRowIndex) => {
const rowElement = document.createElement('div')
rowElement.setAttribute('id','guessRow-'+guessRowIndex)
guessRow.forEach((guess,guessIndex) => {
const tileElement = document.createElement('div')
tileElement.setAttribute('id','guessRow-'+guessRowIndex+'-tile-'+guessIndex)
tileElement.classList.add('tile')
rowElement.append(tileElement)
})
tileDisplay.append(rowElement)
})
keys.forEach(key => {
const buttonElement = document.createElement('button')
buttonElement.textContent = key
buttonElement.setAttribute('id', key)
buttonElement.addEventListener('click',() => handleClick(key))
keyboard.append(buttonElement)
})
const handleClick = (key) => {
console.log('clicked',key)
if (key == 'ENTER') {
checkRow()
console.log(guessRows)
return
}
if (key == '<<') {
// console.log('delete letter')
deleteLetter()
console.log(guessRows)
return
}
addLetter(key)
}
const addLetter = (letter) => {
const tile = document.getElementById('guessRow-'+currentRow+'-tile-'+currentTile)
tile.textContent = letter
guessRows[currentRow][currentTile] = letter
tile.setAttribute('data',letter)
currentTile++
// currentTile = (currentTile + 1)
// if (currentTile==5) currentRow++
// currentTile = currentTile % 5
console.log(guessRows)
}
const deleteLetter = () => {
if (currentTile == 0) return
currentTile--;
const tile = document.getElementById('guessRow-'+currentRow+'-tile-'+currentTile)
tile.textContent = ''
guessRows[currentRow][currentTile] = ''
tile.setAttribute('data','')
}
const checkRow = () => {
const guess = guessRows[currentRow].join('')
if (currentTile > 4) {
console.log('guess is '+guess, 'wordle is '+wordle)
flipTile()
if (guess === wordle) {
showMessage('Magnificent!')
isGameOver = true
return
}
else {
if (currentRow >= 5) {
isGameOver = true
showMessage('Game Over!')
return
}
if (currentRow<5) {
showMessage('Try again!')
currentRow++
currentTile = 0
return
}
}
}
}
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(keyLetter)
key.classList.add(color)
}
const flipTile = () => {
const rowTiles = document.querySelector('#guessRow-'+currentRow).childNodes
let checkWordle = wordle
const guess = []
rowTiles.forEach(tile => {
guess.push({letter :tile.getAttribute('data'),color: 'grayOverlay'})
})
guess.forEach((guess,index) => {
if (guess.letter == wordle[index]){
guess.color = 'greenOverlay'
checkWordle = checkWordle.replace(guess.letter,'')
}
})
guess.forEach((guess,index) => {
if (checkWordle.includes(guess.letter)){
guess.color = 'yellowOverlay'
checkWordle = checkWordle.replace(guess.letter,'')
}
})
rowTiles.forEach((tile,index) => {
const dataLetter = tile.getAttribute('data')
setTimeout(() => {
tile.classList.add('flip')
tile.classList.add(guess[index].color)
addColorToKey(guess[index].letter, guess[index].color)
} , 500*index)
})
}