Created
June 4, 2016 16:15
-
-
Save ElliotJH/29ad9520925f5d8ee86980f80ada1f59 to your computer and use it in GitHub Desktop.
A dice app while I learn elm
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 Html | |
import Html.Events exposing (..) | |
import Svg exposing (svg, Svg, rect, circle) | |
import Svg.Attributes exposing (..) | |
import Random | |
main = | |
Html.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
type alias Model = | |
{ faces : List Int | |
} | |
init : (Model, Cmd Msg) | |
init = | |
(Model [1, 2, 3, 4, 5], Cmd.none) | |
type Msg | |
= Roll | |
| NewFaces (List Int) | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update msg model = | |
case msg of | |
Roll -> | |
(model, Random.generate NewFaces (Random.list 5 (Random.int 1 6))) | |
NewFaces i -> | |
(Model i, Cmd.none) | |
dot n valid x y = if (List.member n valid) then | |
circle [cx x, cy y, r "5", fill "white"] [] | |
else | |
circle [cx x, cy y, r "5", fill "black"] [] | |
dice n = [ rect [x "10", y "10", width "100", height "100", rx "15", ry "15" ] [] | |
, dot n [4, 5, 6] "40" "40" | |
, dot n [6] "60" "40" | |
, dot n [4, 5, 6] "80" "40" | |
, dot n [2, 3] "40" "60" | |
, dot n [1, 3, 5] "60" "60" | |
, dot n [2, 3] "80" "60" | |
, dot n [4, 5, 6] "40" "80" | |
, dot n [6] "60" "80" | |
, dot n [4, 5, 6] "80" "80" | |
] | |
fullDice k = Svg.svg | |
[ onClick Roll, width "120", height "120" , viewBox "0 0 120 120" ] | |
(dice k) | |
view : Model -> Html Msg | |
view model = | |
div [] | |
( | |
[h1 [] [text "Perudo"]] ++ | |
(List.map fullDice model.faces)) | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Sub.none |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment