# =========================== ## Author: Andrin Riss ## Created: 22.10.20 ## Last modfied: 24.10.20 ## Modified by: Andrin Riss ## R version: 4.0.2 ## RStudio version: 1.3.1093 # =========================== # Import list of animal names (can be a list of any words) animal_list <- scan("A3_Andrin_Riss_list_animals.txt", what = "character") # Source: http://www.manythings.org/vocabulary/lists/a/words.php?f=animals_1 WordPuzzle <- function(times_playing = 3) { # Simple function for a animal-spelling-gam # > inspired by Elena Finker's function "QUIZ" # times_playing: number of words per round # Package needed require("crayon") # Introduction cat("----------------------------------\nHello, welcome to the Word Puzzle! \n----------------------------------\n\n") # randomly select #times_playing animal names from the "animal_list" animal_sample <- sample(animal_list, times_playing) # Shuffle letters of each animal name animal_shuffle <- stringi::stri_rand_shuffle(animal_sample) # Set points to 0 point_counter <- 0 # Produces multiple questions (based on times_playing) for (i in 1:times_playing) { cat("Shuffeled animal name: ", animal_shuffle[i], "\n\n") answer <- readline(prompt = "Which animal are we looking for? Answer: ") if (answer == animal_sample[i]) { point_counter <- point_counter + 1 cat(green("That's correct!"), " + 1 Point for you.\n----------------------------------\n\n") Sys.sleep(1) } else { point_counter <- point_counter - 1 cat("That's wrong! Correct answer is: ", red(animal_sample[i]), " - 1 point less!" , "\n----------------------------------\n\n") Sys.sleep(1) } } # Print final number of points cat(paste("End. You scored", point_counter, "point(s).")) if (point_counter <= times_playing/2) cat("\n\nYou didn't even make half of the points. We recommend to read a basic biology textbook.") if (point_counter == times_playing) cat("\n\nYou got all points! Please contact https://www.srf.ch/ to apply as successor for Andreas Moser in Netz Natur.") }