-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.R
125 lines (101 loc) · 3.52 KB
/
hangman.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
library(shiny)
# Define a list of words for the game
words <- c("hangman", "programming", "shiny", "webr", "webassembly", "serviceworker")
ui <- fluidPage(
titlePanel("Hangman Game"),
mainPanel(
h3("Guess the word!"),
textOutput("word_display"),
br(),
textInput("guess_input", "Enter a letter:"),
tagAppendAttributes(style = "background-color:steelblue;color:white;",
actionButton("guess_button", icon = icon("lightbulb"), "Guess")),
br(),
br(),
h4("Incorrect Guesses:"),
textOutput("incorrect_guesses"),
br(),
h4("Remaining Chances:"),
textOutput("remaining_chances"),
br(),
tagAppendAttributes(style = "background-color:orange;color:white;",
actionButton("reset_button", icon = icon("redo"), "Reset"))
)
)
server <- function(input, output, session) {
# Initialize game state
game_state <- reactiveValues(
word = sample(words, 1), # Randomly select a word from the list
guessed_letters = character(0), # Store guessed letters
incorrect_guesses = 0, # Count of incorrect guesses
remaining_chances = 7 # Total chances before game over
)
# Function to update game state based on user guess
update_game_state <- function() {
guess <- tolower(substr(input$guess_input, 1, 1)) # Extract first character of user's guess
if (guess %in% game_state$guessed_letters) {
# Letter has already been guessed, do nothing
return()
}
game_state$guessed_letters <- c(game_state$guessed_letters, guess)
if (!(guess %in% strsplit(game_state$word, "")[[1]])) {
# Incorrect guess
game_state$incorrect_guesses <- game_state$incorrect_guesses + 1
}
if (game_state$incorrect_guesses >= game_state$remaining_chances) {
# Game over
showGameOverMessage()
}
}
# Action when the guess button is clicked
observeEvent(input$guess_button, {
update_game_state()
})
# Function to display the word with guessed letters filled in
output$word_display <- renderText({
word <- game_state$word
guessed_letters <- game_state$guessed_letters
displayed_word <- sapply(strsplit(word, "")[[1]], function(x) {
if (x %in% guessed_letters) {
x
} else {
"_"
}
})
paste(displayed_word, collapse = " ")
})
# Display incorrect guesses
output$incorrect_guesses <- renderText({
if(length(game_state$guessed_letters) == 0){
"No incorrect guesses yet 👀 "
} else {
paste(game_state$guessed_letters[!(game_state$guessed_letters %in% strsplit(game_state$word, "")[[1]])], collapse = ", ")
}
})
# Display remaining chances
output$remaining_chances <- renderText({
game_state$remaining_chances - game_state$incorrect_guesses
})
# Function to display game over message
showGameOverMessage <- function() {
showModal(modalDialog(
title = "Game Over",
paste("You ran out of chances! The word was", game_state$word),
easyClose = TRUE
))
# Reset game state
game_state$word <- sample(words, 1)
game_state$guessed_letters <- character(0)
game_state$incorrect_guesses <- 0
}
observeEvent(input$reset_button, {
game_state$word <- sample(words, 1)
game_state$guessed_letters <- character(0)
game_state$incorrect_guesses <- 0
game_state$remaining_chances <- 7
updateTextInput(session = session,
inputId = "guess_input",
value = "")
})
}
shinyApp(ui, server)