Skip to content

Instantly share code, notes, and snippets.

@pingkunga
Last active February 8, 2025 06:01
Show Gist options
  • Save pingkunga/bce10fe7b21affd55d7344a34c3d0217 to your computer and use it in GitHub Desktop.
Save pingkunga/bce10fe7b21affd55d7344a34c3d0217 to your computer and use it in GitHub Desktop.
PaoYinChub
# 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()
@pingkunga
Copy link
Author

Start the game

play_game()
Round: 1
Enter your choice (rock, paper, scissors, quit): quit
Game Over!
You chose rock 0 times.
You chose paper 0 times.
You chose scissors 0 times.
=====================================
Game Stat!
player_win 0 times.
tie 0 times.
computer_win 0 times.
play_game()
Round: 1
Enter your choice (rock, paper, scissors, quit): rock
You chose: rock
Computer chose: paper
computer_win
Do you want to play again? (yes/no): yes
Round: 2
Enter your choice (rock, paper, scissors, quit): rock
You chose: rock
Computer chose: scissors
player_win
Do you want to play again? (yes/no): yes
Round: 3
Enter your choice (rock, paper, scissors, quit): rock
You chose: rock
Computer chose: paper
computer_win
Do you want to play again? (yes/no): yes
Round: 4
Enter your choice (rock, paper, scissors, quit): rock
You chose: rock
Computer chose: scissors
player_win
Do you want to play again? (yes/no): yes
Round: 5
Enter your choice (rock, paper, scissors, quit): rock
You chose: rock
Computer chose: paper
computer_win
Do you want to play again? (yes/no): yes
Round: 6
Enter your choice (rock, paper, scissors, quit): rock
You chose: rock
Computer chose: scissors
player_win
Do you want to play again? (yes/no): yes
Round: 7
Enter your choice (rock, paper, scissors, quit): rock
You chose: rock
Computer chose: paper
computer_win
Do you want to play again? (yes/no): yes
Round: 8
Enter your choice (rock, paper, scissors, quit): rock
You chose: rock
Computer chose: rock
tie
Do you want to play again? (yes/no): yes
Round: 9
Enter your choice (rock, paper, scissors, quit): rock
You chose: rock
Computer chose: scissors
player_win
Do you want to play again? (yes/no): yes
Round: 10
Enter your choice (rock, paper, scissors, quit): rock
You chose: rock
Computer chose: paper
computer_win
Do you want to play again? (yes/no): yes
Game Over!
You chose rock 10 times.
You chose paper 0 times.
You chose scissors 0 times.
=====================================
Game Stat!
player_win 4 times.
tie 1 times.
computer_win 5 times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment