Skip to content

Instantly share code, notes, and snippets.

@davidchase
Last active May 23, 2016 19:05
Show Gist options
  • Save davidchase/40c27042bccfb00d786af0360b5bc3ea to your computer and use it in GitHub Desktop.
Save davidchase/40c27042bccfb00d786af0360b5bc3ea to your computer and use it in GitHub Desktop.
Roll Two Dice Randomly
import Html exposing (div, img, button, text, Html)
import Html.App as App
import Html.Events exposing (onClick)
import Random exposing (generate, int)
import Array exposing (fromList, get)
import Maybe exposing (withDefault)
import Html.Attributes exposing (src)
import Platform.Cmd exposing (batch, none)
main =
App.program
{ init = init 1 5
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}
images = [
"http://www.speedymath.com/images/dice/1-border-red.gif"
,"http://www.speedymath.com/images/dice/2-border-red.gif"
,"http://www.speedymath.com/images/dice/3-border-red.gif"
,"http://www.speedymath.com/images/dice/4-border-red.gif"
,"http://www.speedymath.com/images/dice/5-border-red.gif"
,"http://www.speedymath.com/images/dice/6-border-red.gif"
]
getItem : Int -> List String -> String
getItem n xs = withDefault "nada.jpg" <| get n <| fromList xs
type alias Model =
{ faceL : Int
, faceR : Int
}
init : Int -> Int -> (Model, Cmd Msg)
init faceL faceR =
(Model faceL faceR, none)
type Msg
= Roll
| NewFaceL Int
| NewFaceR Int
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Roll ->
(model, batch [generate NewFaceL <| int 0 5
,generate NewFaceR <| int 0 5])
NewFaceL newFaceL ->
({model | faceL = newFaceL}, none)
NewFaceR newFaceR ->
({model | faceR = newFaceR}, none)
view : Model -> Html Msg
view model =
div []
[ img [src <| getItem model.faceL images ] []
, img [src <| getItem model.faceR images ] []
, button [ onClick Roll ] [ text "Roll" ]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment