forked from wch/shiny-wordle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-part1.R
86 lines (68 loc) · 1.81 KB
/
app-part1.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
library(shiny)
source("wordlist.R")
ui <- fluidPage(
textInput("guess", ""),
actionButton("go", "Go"),
verbatimTextOutput("result", placeholder = TRUE),
)
# Set the random seed based on the date, so that the same word is used during
# each day.
set.seed(as.integer(Sys.Date()))
target <- sample(words_common, 1)
server <- function(input, output) {
output$result <- renderText({
if (! input$guess %in% words_all) {
req(FALSE, cancelOutput = TRUE)
}
result <- check_words(target, input$guess)
format_result(result)
}) |>
bindEvent(input$go)
}
format_result <- function(r) {
out_str <- ""
for (i in seq_along(r$letters)) {
if (r$result[i] == "correct") {
out_str <- paste0(out_str, "[", r$letters[i], "]")
} else if (r$result[i] == "in-word") {
out_str <- paste0(out_str, "(", r$letters[i], ")")
} else {
out_str <- paste0(out_str, " ", r$letters[i], " ")
}
}
out_str
}
# target: "gives"
# guess: "aisle"
compare_words <- function(target_str, guess_str) {
if (nchar(target_str) != nchar(guess_str)) {
stop("target and guess string must be the same length.")
}
target <- strsplit(target_str, "")[[1]]
guess <- strsplit(guess_str, "")[[1]]
result <- character(nchar(guess_str))
for (i in seq_along(target)) {
if (guess[i] == target[i]) {
result[i] <- "correct"
} else if (guess[i] %in% target) {
result[i] <- "in-word"
} else {
result[i] <- "not-in-word"
}
}
result
}
check_words <- function(target_str, guess_str) {
compare_result <- compare_words(target_str, guess_str)
correct <- FALSE
if (all(compare_result == "correct")) {
correct <- TRUE
}
list(
word = guess_str,
letters = strsplit(guess_str, "")[[1]],
result = compare_result,
correct = correct
)
}
shinyApp(ui, server)