Created
September 16, 2016 19:38
-
-
Save dotcs/3b3626cfbe5b0744134f7af8edcb32c5 to your computer and use it in GitHub Desktop.
Elm example with two dices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html exposing (..) | |
import Html.App as App | |
import Html.Events exposing (onClick) | |
import Random | |
main = | |
App.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
-- MODEL | |
type alias Model = | |
{ dieFace1 : Int | |
, dieFace2 : Int | |
} | |
init : (Model, Cmd Msg) | |
init = | |
(Model 1 2, Cmd.none) | |
dieGenerator : Random.Generator Int | |
dieGenerator = | |
Random.int 1 6 | |
diePairGenerator : Random.Generator (Int, Int) | |
diePairGenerator = | |
Random.pair dieGenerator dieGenerator | |
-- UPDATE | |
type Msg | |
= Roll | |
| NewFaces (Int, Int) | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update msg model = | |
case msg of | |
Roll -> | |
(model, Random.generate NewFaces diePairGenerator) | |
NewFaces (newFace1, newFace2) -> | |
(Model newFace1 newFace2, Cmd.none) | |
-- SUBSCRIPTIONS | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Sub.none | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ h1 [] [ text (toString model.dieFace1) ] | |
, h1 [] [ text (toString model.dieFace2) ] | |
, button [ onClick Roll ] [ text "Roll" ] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment