-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
313 lines (275 loc) · 8.64 KB
/
app.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
#
# shiny app for helping with wordle
#
# load packages
list.of.packages <- c("shiny", "shinyjs", "DT")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
library(shiny)
library(shinyjs)
library(DT)
source("./functions.R")
# Define UI for application
ui <- fluidPage(
tags$head(
tags$style(HTML("
body {
background-color: white;
color: black;
}
h1 {
font-family: 'Yusei Magic', sans-serif;
}
.shiny-input-container {
color: #474747;
}
hr {
border-top: 1px solid #000000;
}
table, th, td {
background-clip: padding-box;
font-family: 'Yusei Magic', sans-serif;
font-size: 150%;
border-radius: 5px;
color: black;
height: 50px;
border: 2px solid white;
}"))
),
useShinyjs(),
# Application title
titlePanel("Wordle Assistant"),
sidebarLayout(
sidebarPanel(
div(
id = "words",
textInput("word1",
"1st word:"),
textInput("word2",
"2nd word:"),
textInput("word3",
"3rd word:"),
textInput("word4",
"4th word:"),
textInput("word5",
"5th word:"),
textInput("word6",
"6th word:")
),
actionButton("resetWords", "Reset words"),
actionButton("resetPositions", "Reset colours"),
br(),
br(),
hr(),
br(),
uiOutput("footer")
),
mainPanel(
h1("Type in your wordle attempt and click to match the colours"),
DTOutput("wordleGrid", height = "40em"),
h1("Possible words in our dictionary"),
textOutput("text"),
br(),
br(),
br()
)
)
# uiOutput("footer")
)
server <- function(input, output, session) {
# initiate wordle grid
wordle_grid = data.frame(
letter1 = rep("",6),
letter2 = rep("",6),
letter3 = rep("",6),
letter4 = rep("",6),
letter5 = rep("",6)
)
position_grid = data.frame(
pos1 = rep(0,6),
pos2 = rep(0,6),
pos3 = rep(0,6),
pos4 = rep(0,6),
pos5 = rep(0,6)
)
combined_grid <- cbind(wordle_grid,position_grid)
# function to update grid using word input
update_wordle_grid <- function(new_word, row_n, max_char = 5){
if(nchar(new_word)>0){
wordle_edit <- NULL
wordle_edit$row <- rep(row_n, min(max_char, nchar(new_word)))
wordle_edit$col <- 0:min(max_char-1, nchar(new_word)-1)
wordle_edit$value <- head(unlist(strsplit(new_word, "")), max_char)
wordle_edit <- data.frame(wordle_edit)
combined_grid <<- editData(combined_grid, wordle_edit, "wordleGrid", rownames = FALSE)
} else {
combined_grid <- combined_grid
}
return()
}
# main grid
output$wordleGrid <- renderDT({
datatable(combined_grid,
rownames = FALSE,
colnames = NULL,
select="none",
options = list(
dom = 't',
ordering = FALSE,
autoWidth = TRUE,
columnDefs = list(
list(className = 'dt-center',
targets = 0:4),
list(visible = FALSE,
targets = c(5,6,7,8,9)),
list(
width = '70px',
targets = "_all"
)
)
)
) %>%
formatStyle(
names(wordle_grid),
valueColumns = names(position_grid),
fontWeight = "bold",
color = "black",
background = styleEqual(c("0","1","2"), c('grey', 'yellow', 'green'))
)
})
# all listeners
toListen <- reactive({
list(
input$word1,
input$word2,
input$word3,
input$word4,
input$word5,
input$word6,
input$resetWords,
input$resetPositions,
input$wordleGrid_cell_clicked
)
})
# read updates
observeEvent(input$word1,{
# limit to 5 char
if(nchar(input$word1)>5){
word_trim <- str_sub(input$word1,1,5)
updateTextInput(session, "word1", value = word_trim)
}
update_wordle_grid(input$word1, 1)
})
observeEvent(input$word2,{
# limit to 5 char
if(nchar(input$word2)>5){
word_trim <- str_sub(input$word2,1,5)
updateTextInput(session, "word2", value = word_trim)
}
update_wordle_grid(input$word2, 2)
})
observeEvent(input$word3,{
# limit to 5 char
if(nchar(input$word3)>5){
word_trim <- str_sub(input$word3,1,5)
updateTextInput(session, "word3", value = word_trim)
}
update_wordle_grid(input$word3, 3)
})
observeEvent(input$word4,{
# limit to 5 char
if(nchar(input$word4)>5){
word_trim <- str_sub(input$word4,1,5)
updateTextInput(session, "word4", value = word_trim)
}
update_wordle_grid(input$word4, 4)
})
observeEvent(input$word5,{
# limit to 5 char
if(nchar(input$word5)>5){
word_trim <- str_sub(input$word5,1,5)
updateTextInput(session, "word5", value = word_trim)
}
update_wordle_grid(input$word5, 5)
})
observeEvent(input$word6,{
# limit to 5 char
if(nchar(input$word6)>5){
word_trim <- str_sub(input$word6,1,5)
updateTextInput(session, "word6", value = word_trim)
}
update_wordle_grid(input$word6, 6)
})
# reset buttons
observeEvent(input$resetWords, {
reset("words")
#reset grid
wordle_edit <- NULL
wordle_edit$row <- rep(1:6,5)
wordle_edit$col <- rep(0:4,each=6)
wordle_edit$value <- ""
wordle_edit <- data.frame(wordle_edit)
combined_grid <<- editData(combined_grid, wordle_edit, "wordleGrid", rownames = FALSE)
})
observeEvent(input$resetPositions, {
#reset grid colours
position_edit <- NULL
position_edit$row <- rep(1:6,5)
position_edit$col <- rep(5:9,each=6)
position_edit$value <- 0
position_edit <- data.frame(position_edit)
combined_grid <<- editData(combined_grid, position_edit, "wordleGrid", rownames = FALSE)
})
# colour click listener
observeEvent(input$wordleGrid_cell_clicked,{
req(length(input$wordleGrid_cell_clicked) > 0)
position_edit <- NULL
# note: some parts are zero indexed
position_edit$row <- input$wordleGrid_cell_clicked$row
position_edit$col <- input$wordleGrid_cell_clicked$col + 5
position_edit$value <- (combined_grid[position_edit$row,
position_edit$col+1] + 1) %%3
combined_grid <<- editData(combined_grid, position_edit, 'wordleGrid', rownames = FALSE)
})
# run wordle help
observeEvent(toListen(), {
print("try to eval")
word_list <- NULL
position_list <- NULL
total_entries <- sum(rowSums(combined_grid[,1:5]=="")==0)
print(total_entries)
if(total_entries>0){
# only operate on non empty words
for (word_n in 1:total_entries){
# extract info from grid
word_list <- c(word_list,
paste(combined_grid[word_n,1:5],collapse=""))
position_list <- c(position_list,
paste(combined_grid[word_n,6:10],collapse=""))
}
print(word_list)
print(position_list)
# search dictionary
an.error.occured <- FALSE
possible_words <- NULL
tryCatch({
possible_words <- wordle_help_multi(
tolower(word_list),
position_list)
}, error = function(e) {an.error.occured <<- TRUE}
)
if(an.error.occured)print("error: no word found")
output$text <- renderText({
paste(possible_words,
collapse = ", ")
})
}
})
# footer
url <- a("/kevinchtsang", href="https://github.com/kevinchtsang/wordle_assist")
output$footer <- renderUI({
tagList("Developed by ", url)
})
}
# Run the application
shinyApp(ui = ui, server = server)