Skip to content

Instantly share code, notes, and snippets.

@epequeno
Last active June 13, 2016 02:04
Show Gist options
  • Save epequeno/f105846cf3a62fe1149807c96405c5c5 to your computer and use it in GitHub Desktop.
Save epequeno/f105846cf3a62fe1149807c96405c5c5 to your computer and use it in GitHub Desktop.
import Html exposing (..)
import Html.App as App
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import Random
-- MAIN
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
subscriptions model =
Sub.none
-- MODEL
type alias Model
= { dieFace1: Int
, dieFace2: Int
}
init: (Model, Cmd Msg)
init =
(Model 1 1, Cmd.none)
-- VIEW
view model =
div []
[ img [src (makeSrcUrl model.dieFace1), height 100, width 100] []
, img [src (makeSrcUrl model.dieFace2), height 100, width 100] []
, br [] []
, button [onClick Roll] [text "roll"]
]
makeSrcUrl num =
"./static/die_face_" ++ (toString num) ++ ".png"
-- UPDATE
type Msg
= Roll
| NewFace (Int, Int)
update: Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Roll ->
(model, Random.generate NewFace (Random.pair (Random.int 1 6) (Random.int 1 6)))
NewFace (newFace1, newFace2) ->
(Model newFace1 newFace2, Cmd.none)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment