1
+ const tileDisplay = document . querySelector ( '.tile-container' ) ;
2
+ const keyBoard = document . querySelector ( '.key-container' ) ;
3
+ const messageDisplay = document . querySelector ( '.message-container' ) ;
4
+
5
+
6
+ const getWordle = ( ) => {
7
+ fetch ( 'http://localhost:8000/word' )
8
+ . then ( response => response . json ( ) )
9
+ . then ( json => {
10
+ wordle = json . toUpperCase ( ) ;
11
+ console . log ( wordle ) ;
12
+ } )
13
+ . catch ( err => console . log ( err ) ) ;
14
+ } ;
15
+
16
+ const keys = [
17
+ 'Q' ,
18
+ 'W' ,
19
+ 'E' ,
20
+ 'R' ,
21
+ 'T' ,
22
+ 'Y' ,
23
+ 'U' ,
24
+ 'I' ,
25
+ 'O' ,
26
+ 'P' ,
27
+ 'A' ,
28
+ 'S' ,
29
+ 'D' ,
30
+ 'F' ,
31
+ 'G' ,
32
+ 'H' ,
33
+ 'J' ,
34
+ 'K' ,
35
+ 'L' ,
36
+ 'ENTER' ,
37
+ 'Z' ,
38
+ 'X' ,
39
+ 'C' ,
40
+ 'V' ,
41
+ 'B' ,
42
+ 'N' ,
43
+ 'M' ,
44
+ '<<'
45
+ ] ;
46
+
47
+ const guessRows = [
48
+ [ '' , '' , '' , '' , '' ] ,
49
+ [ '' , '' , '' , '' , '' ] ,
50
+ [ '' , '' , '' , '' , '' ] ,
51
+ [ '' , '' , '' , '' , '' ] ,
52
+ [ '' , '' , '' , '' , '' ] ,
53
+ [ '' , '' , '' , '' , '' ]
54
+ ] ;
55
+
56
+ let wordle = '' ;
57
+ let currentRow = 0 ;
58
+ let currentTile = 0 ;
59
+ let isGameOver = false ;
60
+
61
+ getWordle ( ) ;
62
+
63
+ guessRows . forEach ( ( guessRows , guessRowIndex ) => {
64
+ const rowElement = document . createElement ( 'div' ) ;
65
+ rowElement . setAttribute ( 'id' , 'guessRow-' + guessRowIndex ) ;
66
+
67
+ guessRows . forEach ( ( guess , guessIndex ) => {
68
+ const tileElement = document . createElement ( 'div' ) ;
69
+ tileElement . setAttribute ( 'id' , 'guessRow-' + guessRowIndex + '-tile-' + guessIndex ) ;
70
+ tileElement . classList . add ( 'tile' ) ;
71
+ rowElement . append ( tileElement ) ;
72
+ } ) ;
73
+ tileDisplay . append ( rowElement ) ;
74
+ } ) ;
75
+
76
+ const handleClick = ( key ) => {
77
+ console . log ( 'clicked' , key ) ;
78
+
79
+ if ( key == '<<' )
80
+ {
81
+ deleteLetter ( ) ;
82
+ console . log ( 'guessRows' , guessRows ) ;
83
+ return ;
84
+ }
85
+
86
+ if ( key == 'ENTER' )
87
+ {
88
+ checkRow ( ) ;
89
+ console . log ( 'guessRows' , guessRows ) ;
90
+ return ;
91
+ }
92
+
93
+ addLetter ( key ) ;
94
+ } ;
95
+
96
+ keys . forEach ( key => {
97
+ const buttonElement = document . createElement ( 'button' ) ;
98
+ buttonElement . textContent = key ;
99
+ buttonElement . setAttribute ( 'id' , key ) ;
100
+ buttonElement . addEventListener ( 'click' , ( ) => handleClick ( key ) ) ;
101
+ keyBoard . append ( buttonElement ) ;
102
+ } ) ;
103
+
104
+
105
+ const addLetter = ( letter ) => {
106
+ if ( currentTile < 5 && currentRow < 6 )
107
+ {
108
+ const tile = document . getElementById ( 'guessRow-' + currentRow + '-tile-' + currentTile ) ;
109
+ tile . textContent = letter ;
110
+ guessRows [ currentRow ] [ currentTile ] = letter ;
111
+ tile . setAttribute ( 'data' , letter ) ;
112
+ currentTile ++ ;
113
+ }
114
+ } ;
115
+
116
+ const deleteLetter = ( ) => {
117
+ if ( currentTile > 0 )
118
+ {
119
+ currentTile -- ;
120
+ const tile = document . getElementById ( 'guessRow-' + currentRow + '-tile-' + currentTile )
121
+ tile . textContent = '' ;
122
+ guessRows [ currentRow ] [ currentTile ] ;
123
+ tile . setAttribute ( 'data' , '' ) ;
124
+ }
125
+ }
126
+
127
+ const checkRow = ( ) => {
128
+ const guess = guessRows [ currentRow ] . join ( '' ) ;
129
+
130
+ if ( currentTile > 4 )
131
+ {
132
+ console . log ( 'guess is ' + guess ) ;
133
+ flipTile ( ) ;
134
+ if ( wordle == guess )
135
+ {
136
+ showMessage ( 'You guessed the right word' ) ;
137
+ isGameOver = true ;
138
+ return ;
139
+ }
140
+ else
141
+ {
142
+ if ( currentRow < 5 )
143
+ {
144
+ currentRow ++ ;
145
+ currentTile = 0 ;
146
+ }
147
+
148
+ else
149
+ {
150
+ isGameOver = true ;
151
+ return ;
152
+ }
153
+ }
154
+
155
+ }
156
+ }
157
+
158
+ const showMessage = ( message ) => {
159
+ const messageElement = document . createElement ( 'p' ) ;
160
+ messageElement . textContent = message ;
161
+ messageDisplay . append ( messageElement ) ;
162
+ setTimeout ( ( ) => {
163
+ messageDisplay . removeChild ( messageElement ) , 20000
164
+ } ) ;
165
+ } ;
166
+
167
+ const addColorToKey = ( letter , color ) => {
168
+ const key = document . getElementById ( letter ) ;
169
+ key . classList . add ( color ) ;
170
+ }
171
+
172
+ const flipTile = ( ) => {
173
+ const rowTiles = document . querySelector ( '#guessRow-' + currentRow ) . childNodes ;
174
+ let checkWordle = wordle ;
175
+ const guess = [ ] ;
176
+
177
+ rowTiles . forEach ( tile => {
178
+ guess . push ( { letter : tile . getAttribute ( 'data' ) , color : 'grey-overlay' } ) ;
179
+ } ) ;
180
+
181
+ guess . forEach ( ( guess , index ) => {
182
+ if ( guess . letter == wordle [ index ] )
183
+ {
184
+ guess . color = 'green-overlay' ;
185
+ checkWordle = checkWordle . replace ( guess . letter , '' ) ;
186
+ }
187
+ } )
188
+
189
+ guess . forEach ( ( guess , index ) => {
190
+ if ( guess . letter == wordle [ index ] )
191
+ {
192
+ guess . color = 'green-overlay' ;
193
+ checkWordle = checkWordle . replace ( guess . letter , '' ) ;
194
+ }
195
+ } )
196
+
197
+ rowTiles . forEach ( ( tile , index ) => {
198
+ setTimeout ( ( ) => {
199
+ tile . classList . add ( 'flip' ) ;
200
+ tile . classList . add ( guess [ index ] . color ) ;
201
+ addColorToKey ( guess [ index ] . letter , guess [ index ] . color ) ;
202
+ } , 500 * index ) ;
203
+ } ) ;
204
+ } ;
0 commit comments