Skip to content

Instantly share code, notes, and snippets.

@jsangilve
Created May 31, 2016 18:07
Show Gist options
  • Save jsangilve/1ffeabe24ec6c8ccb0739c640f0ce81d to your computer and use it in GitHub Desktop.
Save jsangilve/1ffeabe24ec6c8ccb0739c640f0ce81d to your computer and use it in GitHub Desktop.
import Html exposing (..)
import Html.App as Html
import Html.Events exposing (..)
import Random
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ diceFace1 : Int
, diceFace2: Int
}
init : (Model, Cmd Msg)
init =
(Model 1 1, Cmd.none)
-- UPDATE
type Msg
= Roll | Roll1 | Roll2
| NewFace1 Int | NewFace2 Int
| NewFaces (Int, Int)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Roll1 ->
(model, Random.generate NewFace1 (Random.int 1 6))
Roll2 ->
(model, Random.generate NewFace2 (Random.int 1 6))
Roll ->
(model, Random.generate NewFaces doubleRandom)
NewFace1 newFace ->
({ model | diceFace1 = newFace}, Cmd.none)
NewFace2 newFace ->
({ model | diceFace2 = newFace}, Cmd.none)
NewFaces (newFace1, newFace2) ->
(Model newFace1 newFace2, Cmd.none)
doubleRandom: Random.Generator (Int, Int)
doubleRandom =
Random.map2 (,) (Random.int 1 6) (Random.int 1 6)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
div []
[ h1 [] [ text (toString model.diceFace1) ]
, h1 [] [ text (toString model.diceFace2) ]
, button [ onClick Roll ] [ text "Roll" ]
, button [ onClick Roll1] [ text "Roll1" ]
, button [ onClick Roll2] [ text "Roll2" ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment