Current section
Files
Jump to
Current section
Files
src/rock_paper_scissors.gleam
pub type Move {
Rock
Paper
Scissors
}
type GameResult {
Player1Wins(threw: Move, beat: Move)
Player2Wins(threw: Move, beat: Move)
Draw(Move)
}
fn move_to_string(move: Move) {
case move {
Rock -> "Rock"
Paper -> "Paper"
Scissors -> "Scissors"
}
}
fn game_result_to_string(result) {
case result {
Player1Wins(threw, beat) ->
"Player 1 won by throwing "
<> move_to_string(threw)
<> " against "
<> move_to_string(beat)
Player2Wins(threw, beat) ->
"Player 2 won by throwing "
<> move_to_string(threw)
<> " against "
<> move_to_string(beat)
Draw(move) -> "Draw! Both players threw " <> move_to_string(move) <> "s"
}
}
pub fn play(player_1_move: Move, player_2_move: Move) {
case player_1_move, player_2_move {
Rock, Scissors -> Player1Wins(Rock, Scissors)
Rock, Paper -> Player2Wins(Paper, Rock)
Paper, Rock -> Player1Wins(Rock, Paper)
Paper, Scissors -> Player2Wins(Scissors, Paper)
Scissors, Rock -> Player2Wins(Rock, Scissors)
Scissors, Paper -> Player1Wins(Scissors, Paper)
_, _ -> Draw(player_1_move)
}
|> game_result_to_string
}