let’s play a Pao Ying Chub game!
by bui
## Squid Game - DSB11
## Pao Ying Chub
## create a function
## play up to ten round
## if user choose 'quit', terminate the game
## summaries result
play_game <- function () {
## Set a variable and replace value with 0
cat("Welcome to the Squid Game!\\nchoose: hammer, scissor, paper\\n\\npress 'quit' to exit the game")
hands <- c("paper", "scissor", "hammer","quit")
rounds <- 0
user_win <- 0
comp_win <- 0
draw <- 0
## play up to ten round
while (rounds < 10) {
## random comp choice and get player choice
comp_hand <- sample(c("paper", "scissor", "hammer"), 1)
user_hand <- readline(paste("Round:",rounds+1,"Please choose one option: "))
## Verify choice
if (!(user_hand %in% hands)) {
print("Invalid choice. Please enter hammer, paper, or scissors.")
next
}
## if user choose 'quit', terminate the game
if(user_hand == "quit") {
break
}
## round count
cat(paste("rounds:",rounds+1),"\\n")
## draw condition
if (user_hand == comp_hand) {
cat("DRAW\\n")
draw <- draw+1
}
## win condition
else if ((user_hand == "scissor" & comp_hand =="paper")
| (user_hand == "paper" & comp_hand =="hammer")
| (user_hand == "hammer" & comp_hand =="scissor")) {
cat("YOU WIN\\n")
user_win <- user_win+1
}
## lose condition
else {
cat("COMPUTER WIN\\n")
comp_win <- comp_win+1
}
## show player and comp choice of each round
cat(paste("comp_hand:", comp_hand),"\\n")
cat(paste("user_hand:", user_hand),"\\n")
## next round
rounds <- rounds + 1
}
## game ending
cat("\\ngame over!\\n")
## show result "win, lose, draw"
if (user_win > comp_win) {
cat("YOU WIN! congratulations!")
} else if (user_win == comp_win) {
cat("DRAW!")
} else {
cat("YOU LOSE! wanna try again?")
}
## result and thank you
cat("\\nuser_win:",user_win)
cat(" comp_win:",comp_win)
cat(" draw:",draw)
cat("\\nthank you for playing!\\n")
}