Projects : Rock Paper Scissor (R)
<strong><code>deathgame <- function() {
  username <- readline("\\nFILL YOUR NAME : ")
  message("Welcome to my Death Game, ",username)
  cat("\\n---------------Rule---------------\\n",
      "Win a Reaper in 10 Round with\\n",
      "Rock / Paper / Scissor Game\\n",
      "If you want to stop type 'Quit'",
      "\\n----------------------------------\\n\\n"
      )
  round <- 0; wins <- 0; loses <- 0; draws <- 0; #Set Zero Variable
  hands <- c("Rock", "Paper", "Scissor")
  
  while(round < 10){
    comp_hand <- sample(hands, 1)
    user_hand <- readline("Choose Rock | Paper | Scissor : ")
    
    if (user_hand == "Quit") {
      cat("\\n---------------You are Lose---------------\\n")
      break # Quit Choice
    }
    
    if (!(user_hand %in% hands)) {
      cat("Wrong Answer")
      next # Skip when fill wrong
    }
    
    if ## Win Condition
    (user_hand == "Rock" & comp_hand == "Scissor"
     | user_hand == "Paper" & comp_hand == "Rock"
     | user_hand == "Scissor" & comp_hand == "Paper"){
      message("\\n # ROUND : ",round + 1)
      message(paste("| Player :",user_hand,"| Reaper : ",comp_hand,"|"))
      message("---------------  Win  ---------------\\n")
      round <- round+1
      wins <- wins+1
    }
    
    else if ## Lose Condition
    (user_hand == "Rock" & comp_hand == "Paper"
     | user_hand == "Paper" & comp_hand == "Scissor"
     | user_hand == "Scissor" & comp_hand == "Rock"){
      message("\\n # ROUND : ",round + 1)
      message(paste("| Player :",user_hand,"| Reaper : ",comp_hand,"|"))
      message("---------------  Lose  ---------------\\n")
      round <- round+1
      loses <- loses+1
    }
    
    else if ## Draw Condition
    (user_hand ==  comp_hand){
      message("\\n # ROUND : ",round + 1)
      message(paste("| Player :",user_hand,"| Reaper : ",comp_hand,"|"))
      message("---------------  Draw  ---------------\\n")
      round <- round+1
      draws <- draws+1
    }
  }
  if (round == 10 | user_hand == "Quit"){
    cat("\\n Result:",
        "\\n  Round:",round,
        "\\n  Wins:", wins,
        "\\n  Lose:", loses,
        "\\n  Draws:", draws,
        "\\n") ## Result when reach 10 round or Quit
  }
}
</code></strong>
Posted in ,

ใส่ความเห็น