Last active
February 8, 2025 06:01
-
-
Save pingkunga/bce10fe7b21affd55d7344a34c3d0217 to your computer and use it in GitHub Desktop.
PaoYinChub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to get the player's choice | |
get_player_choice <- function() { | |
choice <- tolower(readline("Enter your choice (rock, paper, scissors, quit): ")) | |
while (!(choice %in% c("rock", "paper", "scissors", "quit"))) { | |
cat("Invalid choice. Please enter rock, paper, scissors, quit : ") | |
choice <- tolower(readline()) | |
} | |
return(choice) | |
} | |
# Function to get the computer's choice | |
get_computer_choice <- function() { | |
choices <- c("rock", "paper", "scissors") | |
choice <- sample(choices, 1) | |
return(choice) | |
} | |
# Function to determine the winner | |
determine_winner <- function(player_choice, computer_choice) { | |
if (player_choice == computer_choice) { | |
return("tie") | |
} else if ((player_choice == "rock" & computer_choice == "scissors") || | |
(player_choice == "paper" & computer_choice == "rock") || | |
(player_choice == "scissors" & computer_choice == "paper")) { | |
return("player_win") | |
} else { | |
return("computer_win") | |
} | |
} | |
# Main game function | |
play_game <- function() { | |
player_stat <- list(player_win = 0, tie = 0, computer_win = 0) | |
player_count <- list(rock = 0, paper = 0, scissors = 0) | |
max_round = 10 | |
round = 1 | |
while (round <= max_round) { | |
cat("Round:", round, "\n") | |
player_choice <- get_player_choice() | |
if (player_choice == "quit") | |
{ | |
break | |
} | |
computer_choice <- get_computer_choice() | |
cat("You chose:", player_choice, "\n") | |
cat("Computer chose:", computer_choice, "\n") | |
result <- determine_winner(player_choice, computer_choice) | |
cat(result, "\n") | |
player_stat[[result]] <- player_stat[[result]] + 1 | |
# Update player count | |
player_count[[player_choice]] <- player_count[[player_choice]] + 1 | |
# Ask if the player wants to play again | |
play_again_response <- tolower(readline("Do you want to play again? (yes/no): ")) | |
if (play_again_response != "yes") { | |
round = 11 | |
} | |
round <- round + 1 | |
} | |
# Print the count of each action | |
cat("Game Over!\n") | |
cat("You chose rock", player_count$rock, "times.\n") | |
cat("You chose paper", player_count$paper, "times.\n") | |
cat("You chose scissors", player_count$scissors, "times.\n") | |
cat("=====================================\n") | |
cat("Game Stat!\n") | |
cat("player_win", player_stat$player_win, "times.\n") | |
cat("tie", player_stat$tie, "times.\n") | |
cat("computer_win", player_stat$computer_win, "times.\n") | |
} | |
# Start the game | |
play_game() |
Author
pingkunga
commented
Feb 8, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment