Skip to content

Conversation

@PaulaSpinola
Copy link

codequest 2021-01: Rock-Paper-Scissors
Contents

  • ./game.R: main code program

codequest 2021-01: Rock-Paper-Scissors
Contents
- ./game.R: main code program
Copy link

@stefpiatek stefpiatek left a comment

Choose a reason for hiding this comment

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

Nice work, some tiding up and streamlining

# 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

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

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)

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

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)]

Comment on lines +11 to +65
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") {
p1_output <- "Rock crushes scissors. You lost :("
}
else{
p1_output <- "Tie. No one wins :|"
}
}
return(p1_output)
}


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

# ask user play option
user_play <- user_question() # why R sometimes transforms first character as lower case?

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.

Comment on lines +49 to +50
}
if(p2 == "Rock") {

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") {

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants