Associated Material
Zoom notes: Module 07 - Functions and
Choices
Readings:
Topics
User-Defined Functions
- Declaration
- Input arguments
- Returning a result
Scope
Flow of Control
- Types
- Conditional execution (branching)
- if, else, and else if
- Comparison operators
- Boolean logic operators
- Complex conditionals
Exercise
Even seemingly simple programs can involve elaborate flow of control.
The simple game Rock, Paper, Scissors (https://www.wikihow.com/Play-Rock,-Paper,-Scissors) can
be implemented with a complex conditional structure. Write a function to
play the game.
In the example solution below, we use the R function
sample
to randomly select a move for each of two players
(https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/sample).
Alternatively, the players’ choices could be passed in as input
arguments.
Note that there are many ways that the conditional logic can be
implemented, so your solution may be different. The general rule is that
we try to keep our conditional logic as succinct as possible, while
still making it easy for someone reading your code to understand.
rock_paper_scissors <- function()
{
# Vector of the three possible moves
moves <- c("rock", "paper", "scissors")
# Randomly select a move for Bob
bob_move <- sample(moves,1)
# Randomly select a move for Fred
fred_move <- sample(moves,1)
# Work through the game win conditions
if (bob_move == fred_move){
winner = "draw"
}
if( bob_move == "rock"){
if (fred_move == "paper"){
winner = "fred"
}
if (fred_move == "scissors"){
winner = "bob"
}
}
if (bob_move == "paper"){
if (fred_move == "rock"){
winner = "bob"
}
if (fred_move == "scissors"){
winner = "fred"
}
}
if (bob_move == "scissors") {
if (fred_move == "rock"){
winner = "fred"
}
if (fred_move == "paper"){
winner = "bob"
}
}
# Return the winner
return(winner)
} # end rock_paper_scissors
LS0tCnRpdGxlOiAiWm9vbSBOb3RlcyAwNzogRnVuY3Rpb25zIGFuZCBDaG9pY2VzIgpkYXRlOiAiU2VtZXN0ZXIgMiwgMjAyMiIKb3V0cHV0OgogIGh0bWxfZG9jdW1lbnQ6CiAgICB0b2M6IHRydWUKICAgIHRvY19mbG9hdDogdHJ1ZQogICAgdG9jX2RlcHRoOiAzCiAgICBjb2RlX2Rvd25sb2FkOiB0cnVlCiAgICBjb2RlX2ZvbGRpbmc6IHNob3cKLS0tCgpgYGB7ciBzZXR1cCwg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WxlY3QgYSBtb3ZlIGZvciBCb2IKICBib2JfbW92ZSA8LSBzYW1wbGUobW92ZXMsMSkKICAKICAjIFJhbmRvbWx5IHNlbGVjdCBhIG1vdmUgZm9yIEZyZWQKICBmcmVkX21vdmUgPC0gc2FtcGxlKG1vdmVzLDEpCiAgCiAgCiAgIyBXb3JrIHRocm91Z2ggdGhlIGdhbWUgd2luIGNvbmRpdGlvbnMKICBpZiAoYm9iX21vdmUgPT0gZnJlZF9tb3ZlKXsKICAgIHdpbm5lciA9ICJkcmF3IgogIH0KICAKICBpZiggYm9iX21vdmUgPT0gInJvY2siKXsKICAgIGlmIChmcmVkX21vdmUgPT0gInBhcGVyIil7CiAgICAgIHdpbm5lciA9ICJmcmVkIgogICAgfQogICAgaWYgKGZyZWRfbW92ZSA9PSAic2Npc3NvcnMiKXsKICAgICAgd2lubmVyID0gImJvYiIKICAgIH0KICAgICAgCiAgfQogIAogIGlmIChib2JfbW92ZSA9PSAicGFwZXIiKXsKICAgIGlmIChmcmVkX21vdmUgPT0gInJvY2siKXsKICAgICAgd2lubmVyID0gImJvYiIKICAgIH0KICAgIGlmIChmcmVkX21vdmUgPT0gInNjaXNzb3JzIil7CiAgICAgIHdpbm5lciA9ICJmcmVkIgogICAgfQogIH0KICAKICBpZiAoYm9iX21vdmUgPT0gInNjaXNzb3JzIikgewogICAgaWYgKGZyZWRfbW92ZSA9PSAicm9jayIpewogICAgICB3aW5uZXIgPSAiZnJlZCIKICAgIH0KICAgIGlmIChmcmVkX21vdmUgPT0gInBhcGVyIil7CiAgICAgIHdpbm5lciA9ICJib2IiCiAgICB9CiAgfQogIAogICMgUmV0dXJuIHRoZSB3aW5uZXIKICByZXR1cm4od2lubmVyKQp9ICMgZW5kIHJvY2tfcGFwZXJfc2Npc3NvcnMKYGBgCgo=