penneysgame <- function() {

  
  
  

  first <- sample(c("PC", "Human"), 1)

  
  
  

  if (first == "PC") { 

    pc.seq <- sample(c("H", "T"), 3, replace = TRUE)
    cat(paste("\nI choose first and will win on first seeing", paste(pc.seq, collapse = ""), "in the list of tosses.\n\n"))
    human.seq <- readline("What sequence of three Heads/Tails will you win with: ")
    human.seq <- unlist(strsplit(human.seq, ""))

  } else if (first == "Human") { 

    cat(paste("\nYou can choose your winning sequence first.\n\n"))
    human.seq <- readline("What sequence of three Heads/Tails will you win with: ")
    human.seq <- unlist(strsplit(human.seq, "")) 
    pc.seq <- c(human.seq[2], human.seq[1:2]) 
    pc.seq[1] <- ifelse(pc.seq[1] == "H", "T", "H") 
    cat(paste("\nI win on first seeing", paste(pc.seq, collapse = ""), "in the list of tosses.\n"))

  }

  
  
  

  cat("\nThrowing:\n")

  ran.seq <- NULL

  while(TRUE) {

    ran.seq <- c(ran.seq, sample(c("H", "T"), 1)) 

    cat("\n", paste(ran.seq, sep = "", collapse = "")) 

    if (length(ran.seq) >= 3 && all(tail(ran.seq, 3) == pc.seq)) {
      cat("\n\nI win!\n")
      break
    }

    if (length(ran.seq) >= 3 && all(tail(ran.seq, 3) == human.seq)) {
      cat("\n\nYou win!\n")
      break
    }

    Sys.sleep(0.5) 

  }
}