This repository was archived by the owner on Mar 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication.R
More file actions
212 lines (164 loc) · 5.68 KB
/
application.R
File metadata and controls
212 lines (164 loc) · 5.68 KB
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
library(dplyr)
library(DT)
library(shiny)
library(shinyBS)
library(shinydashboard)
library(shinyjs)
library(shinyWidgets)
library(stringr)
library(words)
source("C:/My_RStudio/Workspace/R_hackathon/functions.R")
#Setting words list
word_list <- words::words %>%
filter(word_length == 5) %>%
select(word)
#Developing user interface - header, sidebar, input box and also buttons
header <- dashboardHeader(title = "Wordle Shiny")
sidebar <- dashboardSidebar(disable = T)
body <- dashboardBody(
actionButton(inputId = "help", label = "", icon = icon("circle-question"), class = "helpButton",
style = "position: absolute; right: 40px"),
useShinyjs(),
div(
class = "word-button",
textInput(
inputId = "word1_guess",
label = div("Enter your guess:", style="font-size:120%"),
value = "",
) %>%
tagAppendAttributes(class = "inline-element"),
actionButton(inputId = "go1", "Lock Guess", class = "lockButton")
),
letter_colour_ui("word1",1),
br(),
letter_colour_ui("word2",2),
br(),
letter_colour_ui("word3",3),
br(),
letter_colour_ui("word4",4),
br(),
letter_colour_ui("word5",5),
br(),
letter_colour_ui("word6",6)
)
#Custom .css for adding colours to boxes
ui <- dashboardPage(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")),
skin = "black",
header = header,
sidebar = sidebar,
body = body
)
# Setting up server / changing pieces
server <- function(input,output,session){
#Close app function to trigger after javascript code (4s delay)
observeEvent(input$closeApp,{
if(input$closeApp){
stopApp()
}
})
#Help button
observeEvent(input$help, {
showModal(modalDialog(
HTML('<img src="instructions.png" onmousedown="if (event.preventDefault) event.preventDefault()" />'),
easyClose = FALSE,
footer=tagList(
modalButton(div('Back', style="font-size:120%"))
)
))
})
#Setting counter to 0
counter <- reactiveValues(countervalue = 0)
previous_guesses <- reactiveValues()
#Setting target words
target <- sample(word_list$word,1)
target_val <- strsplit(target, "")[[1]]
#Hit the "Submit" button of a word guess, all the following happens:
observeEvent(input$go1, {
guess <- tolower(input$word1_guess)
#Ensuring word is 5 letters long
if(nchar(guess)!=5){
showModal(
modalDialog(
title = div("Error", style="font-size:160%"),
div("Guesses must be exactly 5 letters long.", style="font-size:120%"),
easyClose = FALSE,
footer=tagList(
modalButton(div('Back', style="font-size:120%"))
)
)
)
updateTextInput(session,"word1_guess",value="")
#Ensuring word is in word list
} else if(!(guess %in% word_list$word)){
showModal(
modalDialog(
title = div("Error", style="font-size:160%"),
div("Guesses must be a real word.", style="font-size:120%"),
easyClose = FALSE,
footer=tagList(
modalButton(div('Back', style="font-size:120%"))
)
)
)
updateTextInput(session,"word1_guess",value="")
#Checking if word has been guessed previously
} else {
if(guess %in% previous_guesses$guess){
showModal(
modalDialog(
title = div("Error", style="font-size:160%"),
div("Make sure that guesses are not used multiple times.", style="font-size:120%"),
easyClose = FALSE,
footer=tagList(
modalButton(div('Back', style="font-size:120%"))
)
)
)
updateTextInput(session,"word1_guess",value="")
}
else{
previous_guesses$guess <- c(isolate(previous_guesses$guess), isolate(guess))
guess_val <- strsplit(input$word1_guess, "")[[1]]
word <- word_checker(session, target_val, guess_val)
#Depending on counter value, updating the following boxes
if (counter$countervalue==0){
check_update_col(session, input, word, 1)
counter$countervalue <- counter$countervalue+1
guess_achieved(input$word1_guess,target)
}
else if (counter$countervalue==1){
check_update_col(session, input, word, 2)
counter$countervalue <- counter$countervalue+1
guess_achieved(input$word1_guess,target)
}
else if (counter$countervalue==2){
check_update_col(session, input, word, 3)
counter$countervalue <- counter$countervalue+1
guess_achieved(input$word1_guess,target)
}
else if (counter$countervalue==3){
check_update_col(session, input, word, 4)
counter$countervalue <- counter$countervalue+1
guess_achieved(input$word1_guess,target)
}
else if (counter$countervalue==4){
check_update_col(session, input, word, 5)
counter$countervalue <- counter$countervalue+1
guess_achieved(input$word1_guess,target)
}
else if (counter$countervalue==5){
check_update_col(session, input, word, 6)
counter$countervalue <- counter$countervalue+1
guess_achieved(input$word1_guess,target)
}}}
#Max guess' reached
if(counter$countervalue==6){
max_guesses()
}
updateTextInput(session,"word1_guess",value="")
})
}
#Run App
shinyApp(ui = ui, server = server)