forked from wch/shiny-wordle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-final.R
401 lines (356 loc) · 10.7 KB
/
app-final.R
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
library(shiny)
library(htmltools)
source("wordlist.R")
ui <- fluidPage(
theme = bslib::bs_theme(version = 4),
title = "Shiny wordle",
tags$style(HTML("
.container-fluid {
text-align: center;
height: calc(100vh - 30px);
display: grid;
grid-template-rows: 1fr auto;
}
.guesses {
overflow-y: auto;
height: 100%;
}
.guesses.finished {
overflow-y: visible;
}
.guesses .word {
margin: 5px;
}
.guesses .word > .letter {
display: inline-block;
width: 50px;
height: 50px;
text-align: center;
vertical-align: middle;
border-radius: 3px;
line-height: 50px;
font-size: 32px;
font-weight: bold;
vertical-align: middle;
user-select: none;
color: white;
font-family: 'Clear Sans', 'Helvetica Neue', Arial, sans-serif;
}
.guesses .word > .correct {
background-color: #6a5;
}
.guesses .word > .in-word {
background-color: #db5;
}
.guesses .word > .not-in-word {
background-color: #888;
}
.guesses .word > .guess {
color: black;
background-color: white;
border: 1px solid black;
}
.keyboard {
height: 240px;
user-select: none;
}
.keyboard .keyboard-row {
margin: 3px;
}
.keyboard .keyboard-row .key {
display: inline-block;
padding: 0;
width: 30px;
height: 50px;
text-align: center;
vertical-align: middle;
border-radius: 3px;
line-height: 50px;
font-size: 18px;
font-weight: bold;
vertical-align: middle;
color: black;
font-family: 'Clear Sans', 'Helvetica Neue', Arial, sans-serif;
background-color: #ddd;
touch-action: none;
}
.keyboard .keyboard-row .key:focus {
outline: none;
}
.keyboard .keyboard-row .key.wide-key {
font-size: 15px;
width: 50px;
}
.keyboard .keyboard-row .key.correct {
background-color: #6a5;
color: white;
}
.keyboard .keyboard-row .key.in-word {
background-color: #db5;
color: white;
}
.keyboard .keyboard-row .key.not-in-word {
background-color: #888;
color: white;
}
.endgame-content {
font-family: Helvetica, Arial, sans-serif;
display: inline-block;
line-height: 1.4;
letter-spacing: .2em;
margin: 20px 8px;
width: fit-content;
padding: 20px;
border-radius: 5px;
box-shadow: 4px 4px 19px rgb(0 0 0 / 17%);
}
")),
div(
class = "guesses",
h3("Shiny wordle"),
uiOutput("previous_guesses"),
uiOutput("current_guess"),
uiOutput("endgame"),
uiOutput("new_game_ui")
),
uiOutput("keyboard"),
# div(
# style="display: inline-block;",
# checkboxInput("hard", "Hard mode")
# ),
tags$script(HTML("
const letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
const all_key_ids = [ ...letters, 'Enter', 'Back'];
document.addEventListener('keydown', function(e) {
let key = e.code.replace(/^Key/, '');
if (letters.includes(key)) {
document.getElementById(key).click();
} else if (key == 'Enter') {
document.getElementById('Enter').click();
} else if (key == 'Backspace') {
document.getElementById('Back').click();
}
});
// For better responsiveness on touch devices, trigger a click on the button
// when a touchstart event occurs; don't wait for the touchend event. So
// that a click event doesn't happen when the touchend event happens (and
// cause the letter to be typed a second time), we set the 'pointer-events'
// CSS property to 'none' on the button. Then when there's _any_ touchend
// event, unset the 'pointer-events' CSS property on all of the buttons, so
// that the button can be touched again.
let in_button_touch = false;
document.addEventListener('touchstart', function(e) {
if (all_key_ids.includes(e.target.id)) {
e.target.click();
e.target.style.pointerEvents = 'none';
e.preventDefault(); // Disable text selection
in_button_touch = true;
}
});
document.addEventListener('touchend', function(e) {
all_key_ids.map((id) => {
document.getElementById(id).style.pointerEvents = null;
});
if (in_button_touch) {
if (all_key_ids.includes(e.target.id)) {
// Disable text selection and triggering of click event.
e.preventDefault();
}
in_button_touch = false;
}
});
"))
)
server <- function(input, output) {
target_word <- reactiveVal(sample(words_common, 1))
all_guesses <- reactiveVal(list())
finished <- reactiveVal(FALSE)
current_guess_letters <- reactiveVal(character(0))
reset_game <- function() {
target_word(sample(words_common, 1))
all_guesses(list())
finished(FALSE)
}
observeEvent(input$Enter, {
guess <- paste(current_guess_letters(), collapse = "")
if (! guess %in% words_all)
return()
# if (input$hard) {
# # Letters in the target word that the player has previously
# # guessed correctly.
# matched_letters = used_letters().intersection(set(target_word()))
# if not set(guess).issuperset(matched_letters):
# return
# }
all_guesses_new <- all_guesses()
check_result <- check_word(guess, target_word())
all_guesses_new[[length(all_guesses_new) + 1]] <- check_result
all_guesses(all_guesses_new)
if (isTRUE(check_result$win)) {
finished(TRUE)
}
current_guess_letters(character(0))
})
output$previous_guesses <- renderUI({
res <- lapply(all_guesses(), function(guess) {
letters <- guess$letters
row <- mapply(
letters,
guess$matches,
FUN = function(letter, match) {
# This will have the value "correct", "in-word", or "not-in-word", and
# those values are also used as CSS class names.
match_type <- match
div(toupper(letter), class = paste("letter", match_type))
},
SIMPLIFY = FALSE,
USE.NAMES = FALSE
)
div(class = "word", row)
})
scroll_js <- "
document.querySelector('.guesses')
.scrollTo(0, document.querySelector('.guesses').scrollHeight);
"
tagList(res, tags$script(HTML(scroll_js)))
})
output$current_guess <- renderUI({
if (finished()) return()
letters <- current_guess_letters()
# Fill in blanks for letters up to length of target word. If letters is:
# "a" "r"
# then result is:
# "a" "r" "" "" ""
target_length <- isolate(nchar(target_word()))
if (length(letters) < target_length) {
letters[(length(letters)+1) : target_length] <- ""
}
div(
class = "word",
lapply(letters, function(letter) {
div(toupper(letter), class ="letter guess")
})
)
})
output$new_game_ui <- renderUI({
if (!finished())
return()
actionButton("new_game", "New Game")
})
observeEvent(input$new_game, {
reset_game()
})
used_letters <- reactive({
# This is a named list. The structure will be something like:
# list(p = "not-in-word", a = "in-word", e = "correct")
letter_matches <- list()
# Populate `letter_matches` by iterating over all letters in all the guesses.
lapply(all_guesses(), function(guess) {
mapply(guess$letters, guess$matches, SIMPLIFY = FALSE, USE.NAMES = FALSE,
FUN = function(letter, match) {
prev_match <- letter_matches[[letter]]
if (is.null(prev_match)) {
# If there isn't an existing entry for that letter, just use it.
letter_matches[[letter]] <<- match
} else {
# If an entry is already present, it can be "upgraded":
# "not-in-word" < "in-word" < "correct"
if (match == "correct" && prev_match %in% c("not-in-word", "in-word")) {
letter_matches[[letter]] <<- match
} else if (match == "in-word" && prev_match == "not-in-word") {
letter_matches[[letter]] <<- match
}
}
}
)
})
letter_matches
})
keys <- list(
c("Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"),
c("A", "S", "D", "F", "G", "H", "J", "K", "L"),
c("Enter", "Z", "X", "C", "V", "B", "N", "M", "Back")
)
output$keyboard <- renderUI({
prev_match_type <- used_letters()
keyboard <- lapply(keys, function(row) {
row_keys <- lapply(row, function(key) {
class <- "key"
key_lower <- tolower(key)
if (!is.null(prev_match_type[[key_lower]])) {
class <- c(class, prev_match_type[[key_lower]])
}
if (key %in% c("Enter", "Back")) {
class <- c(class, "wide-key")
}
actionButton(key, key, class = class)
})
div(class = "keyboard-row", row_keys)
})
div(class = "keyboard", keyboard)
})
# Add listeners for each key, except Enter and Back
lapply(unlist(keys, recursive = FALSE), function(key) {
if (key %in% c("Enter", "Back")) return()
observeEvent(input[[key]], {
if (finished())
return()
cur <- current_guess_letters()
if (length(cur) >= 5)
return()
current_guess_letters(c(cur, tolower(key)))
})
})
observeEvent(input$Back, {
if (length(current_guess_letters()) > 0) {
current_guess_letters(current_guess_letters()[-length(current_guess_letters())])
}
})
output$endgame <- renderUI({
if (!finished())
return()
lines <- lapply(all_guesses(), function(guess) {
line <- vapply(guess$matches, function(match) {
switch(match,
"correct" = "🟩",
"in-word" = "🟨",
"not-in-word" = "⬜"
)
}, character(1))
div(paste(line, collapse = ""))
})
div(class = "endgame-content", lines)
})
}
check_word <- function(guess_str, target_str) {
guess <- strsplit(guess_str, "")[[1]]
target <- strsplit(target_str, "")[[1]]
remaining <- character(0)
if (length(guess) != length(target)) {
stop("Word lengths don't match.")
}
result <- rep("not-in-word", length(guess))
# First pass: find matches in correct position. Letters in the target that do
# not match the guess are added to the remaining list.
for (i in seq_along(guess)) {
if (guess[i] == target[i]) {
result[i] <- "correct"
} else {
remaining <- c(remaining, target[i])
}
}
for (i in seq_along(guess)) {
if (guess[i] != target[i] && guess[i] %in% remaining) {
result[i] <- "in-word"
remaining <- remaining[-match(guess[i], remaining)]
}
}
list(
word = guess_str,
letters = guess,
matches = result,
win = all(result == "correct")
)
}
shinyApp(ui, server)