Lab 1: Control Structures
Function 1: Game Setup | create.game()
Psuedocode:
1. Create a vector with 3 doors (goat, goat, car)
2. Randomize positions for each game
3. Return new game
Code:
create.game <- function()
{
game <- sample (x = c("goat", "goat", "car"), size = 3, replace = F)
return(game)
}
Test Code:
create.game()
## [1] "goat" "goat" "car"
create.game()
## [1] "car" "goat" "goat"
create.game()
## [1] "goat" "car" "goat"
Function 2: Contestant Selection | select.door()
Psuedocode:
1. Create vector for door numbers (1, 2, 3)
2. Select a RANDOM door
3. Return randomly selected door
Code:
select.door <- function()
{
doors <- c(1, 2, 3)
pick <- sample(1:3, size =1, replace = F)
return(pick)
}
Test Code:
select.door()
## [1] 2
select.door()
## [1] 2
select.door()
## [1] 3
select.door()
## [1] 1
Function 3: Reveal Goat Door | open.goat.door()
Psuedocode:
1. Build off of previous create.game & select_door functions.
2. Create parameters for host to select door (must be goat, canโt be
contestants selection)
3. Select Door
Code:
open.goat.door <- function(game, pick) {
goat.doors <- which(game == "goat")
goat.doors<- goat.doors[! goat.doors %in% c(pick)]
if (game[goat.doors[1]] == "goat"){
host.pick <-goat.doors[1]
} else{
host.pick <- goat.doors[2]
}
return(host.pick)
}
Test Code:
this.game <- create.game()
this.game
## [1] "goat" "car" "goat"
my.initial.pick <- select.door()
my.initial.pick
## [1] 2
host.choice <- open.goat.door(this.game, my.initial.pick)
host.choice
## [1] 1
Function 4: Stay/Switch | change.door()
Psuedocode:
Create leftover doors object that removes the host selected door
Create if else statement that says IF stay = TRUE, keep pick (original
selection)
ELSE switch to the other unopened door
Code: change.door()
change.door <- function(stay=T, host.pick, pick)
{
doors <- c(1,2,3)
final.pick <- doors[!doors %in% c(host.pick, pick)]
if (stay) {
return(pick)
} else{
return(final.pick)
}
}
Test Code:
this.game
## [1] "goat" "car" "goat"
my.initial.pick
## [1] 2
host.choice
## [1] 1
change.door(stay = T, host.choice, my.initial.pick )
## [1] 2
change.door(stay = F, host.choice, my.initial.pick )
## [1] 3
Function 5: Determine winner | determine_winner()
Psuedocode:
1. Build off switch function and original create game function
2. Create if else statement where if final selection is car, WIN, if
anything else, LOSE.
Code: determine_winner()
determine_winner <- function(final.pick, game)
{
if (game[final.pick] == "car")
{return("WIN")
}
else {return("LOSE")}
}
Test Code:
this.game
## [1] "goat" "car" "goat"
my.initial.pick
## [1] 2
host.choice
## [1] 1
determine_winner(final.pick = 1, this.game)
## [1] "LOSE"
determine_winner(final.pick = 2, this.game)
## [1] "WIN"
determine_winner(final.pick = 3, this.game)
## [1] "LOSE"
Test Code:
game2 <- create.game()
g2pick <- select.door()
g2hostpick <- open.goat.door(game2, g2pick)
game2
## [1] "goat" "goat" "car"
g2pick
## [1] 2
g2hostpick
## [1] 1
my.final.pick <- change.door(stay = T,
host.pick = g2hostpick,
pick = g2pick)
determine_winner(final.pick = my.final.pick,
game=game2)
## [1] "LOSE"
my.final.pick <- change.door(stay = F,
host.pick = g2hostpick,
pick = g2pick)
determine_winner(final.pick=my.final.pick,
game = game2)
## [1] "WIN"
Testing the Code:
## [1] "GAME SETUP"
## [1] "goat" "goat" "car"
## [1] "My initial selection: 2"
## [1] "The opened goat door: 1"
## [1] "My final selection: 2"
## [1] "GAME OUTCOME:"
## [1] "LOSE"
## [1] "GAME SETUP"
## [1] "goat" "goat" "car"
## [1] "My initial selection: 2"
## [1] "The opened goat door: 1"
## [1] "My final selection: 3"
## [1] "GAME OUTCOME:"
## [1] "WIN"