Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions 2021/2021-01/Submissions/PaulaSpinola/game.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# This script codes the interactive game Rock-Paper-Scissors between a user and the computer
# Author: Paula Spinola
# Date: 20th Jan 2021
rm(list=ls())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine for your own usage but probably don't remove everything in my R environment or I'll cry


# installing packages
#install.packages("azuremlsdk")
library(azuremlsdk)
Comment on lines +6 to +8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# installing packages
#install.packages("azuremlsdk")
library(azuremlsdk)


# option to choose from
options <- c("Rock","Paper","Scissors")

# function that gets user input
user_question <- function() {
user_play <- readline(prompt="Enter play option: ")
while(!user_play %in% options){
user_play <-readline(prompt = "Please choose between Rock, Paper and Scissors: ")
}
return(user_play)
}

# function that simulates the game
game_simulation <- function(p1,p2) {
if(p1== "Rock") {
if(p2 == "Scissors") {
p1_output <- "Rock crushes scissors. You won :)"
}
if(p2 == "Paper") {
p1_output <- "Paper covers rock. You lost :("
}
else{
p1_output <- "Tie. No one wins :|"
}
}
if(p1== "Paper") {
if(p2 == "Rock") {
p1_output <- "Paper covers rock. You won :)"
}
if(p2 == "Scissors") {
p1_output <- "Scissors cut paper. You lost :("
}
else{
p1_output <- "Tie. No one wins :|"
}
}
if(p1== "Scissors") {
if(p2 == "Paper") {
p1_output <- "Scissors cut paper. You won :)"
}
if(p2 == "Rock") {
Comment on lines +49 to +50

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
if(p2 == "Rock") {
} else if(p2 == "Rock") {

p1_output <- "Rock crushes scissors. You lost :("
}
else{
p1_output <- "Tie. No one wins :|"
}
}
return(p1_output)
Comment on lines +24 to +57

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be done by dataframe lookup (row player 1, column for player 2 and outcome string in the cell)

}


# generate computer play option
computer_play <- options[sample(1:3, 1)] # did not manage to use randint()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
computer_play <- options[sample(1:3, 1)] # did not manage to use randint()
computer_play <- options[sample(1:3, 1)]


# ask user play option
user_play <- user_question() # why R sometimes transforms first character as lower case?
Comment on lines +11 to +65

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could have the options as all lower case and then transform the input to lower case text here.


# running game
game_decision <- game_simulation(p1=user_play,p2=computer_play)
print(game_decision) # there is an issue with the code, sometimes it is outputing Tie when it should not