Skip to content

Instantly share code, notes, and snippets.

@tmountain
Created March 1, 2017 18:58
Show Gist options
  • Save tmountain/53b40cd759225358d32f0f9d5a566217 to your computer and use it in GitHub Desktop.
Save tmountain/53b40cd759225358d32f0f9d5a566217 to your computer and use it in GitHub Desktop.
module RPS exposing (main)
import Html exposing (Html, div, text, button)
import Html.Events exposing (onClick)
import Random
-- MODEL
type Move
= Rock
| Paper
| Scissors
type alias Model =
{ playerMove : Maybe Move
, randMove : Maybe Move
}
model : Model
model =
{ playerMove = Nothing, randMove = Nothing }
type Msg
= Choose Move
| GenerateMove
| NewMove Move
type Outcome
= Win
| Lose
| Tie
-- UPDATE
outcome : Move -> Move -> Outcome
outcome move1 move2 =
case ( move1, move2 ) of
( Rock, Rock ) ->
Tie
( Rock, Paper ) ->
Lose
( Rock, Scissors ) ->
Win
( Paper, Rock ) ->
Win
( Paper, Paper ) ->
Tie
( Paper, Scissors ) ->
Lose
( Scissors, Rock ) ->
Lose
( Scissors, Paper ) ->
Win
( Scissors, Scissors ) ->
Tie
idxToMove : Int -> Move
idxToMove idx =
case idx of
1 ->
Rock
2 ->
Paper
3 ->
Scissors
_ ->
Rock
genMove : Random.Generator Move
genMove =
Random.map idxToMove (Random.int 1 3)
outcomeToString : Maybe Move -> Maybe Move -> String
outcomeToString possibleMove1 possibleMove2 =
case ( possibleMove1, possibleMove2 ) of
( Nothing, _ ) ->
""
( _, Nothing ) ->
""
( Just move1, Just move2 ) ->
(toString (outcome move1 move2))
moveToString : Maybe Move -> String
moveToString possibleMove =
case possibleMove of
Nothing ->
""
Just move ->
case move of
Rock ->
"Rock"
Paper ->
"Paper"
Scissors ->
"Scissors"
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Choose move ->
({ model | playerMove = Just move }
|> update GenerateMove
)
GenerateMove ->
( model, Random.generate NewMove genMove )
NewMove move ->
( { model | randMove = Just move }, Cmd.none )
-- VIEW
playerChoice : Model -> String
playerChoice model =
let
move =
moveToString model.playerMove
in
if String.isEmpty move then
"Player has not chosen."
else
"Player chooses " ++ move ++ "."
opponentChoice : Model -> String
opponentChoice model =
let
move =
moveToString model.randMove
in
if String.isEmpty move then
"Opponent has not chosen."
else
"Opponent chooses " ++ move ++ "."
view : Model -> Html Msg
view model =
div []
[ button [ onClick (Choose Rock) ] [ text "Rock" ]
, button [ onClick (Choose Paper) ] [ text "Paper" ]
, button [ onClick (Choose Scissors) ] [ text "Scissors" ]
, div [] [ text <| playerChoice model ]
, div [] [ text <| opponentChoice model ]
, div [] [ text <| outcomeToString model.playerMove model.randMove ]
]
main : Program Never Model Msg
main =
Html.program
{ view = view
, update = update
, subscriptions = \_ -> Sub.none
, init = ( model, Cmd.none )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment