Skip to content

Instantly share code, notes, and snippets.

@nathanmalishev
Created April 2, 2020 00:25
Show Gist options
  • Save nathanmalishev/0866a0c7a1ea7b1174c2c7cb1b3e0be3 to your computer and use it in GitHub Desktop.
Save nathanmalishev/0866a0c7a1ea7b1174c2c7cb1b3e0be3 to your computer and use it in GitHub Desktop.
-- Press a button to generate a random number between 1 and 6.
--
-- Read how it works:
-- https://guide.elm-lang.org/effects/random.html
--
import Browser
import Html exposing (..)
import Html.Attributes exposing (class, classList, href, style)
import Html.Events exposing (..)
import Random
import Svg exposing (circle, svg, rect)
import Svg.Attributes exposing (cx, cy, r, fill, stroke, x, y, width, height, rx, ry)
-- MAIN
main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
-- MODEL
type alias Model =
{ dieFace : Int
}
init : () -> (Model, Cmd Msg)
init _ =
( Model 1
, Cmd.none
)
-- UPDATE
type Msg
= Roll
| NewFace Int
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Roll ->
( model
, Random.generate NewFace (Random.int 1 6)
)
NewFace newFace ->
( Model newFace
, Cmd.none
)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
div []
[ h1 [] [ Html.text (String.fromInt model.dieFace) ]
, svg
[ style "width" "120", style "height" "120", style "viewBox" "0 0 120 120", style "fill" "white", style "stroke" "black", style "strokeWidth" "3", style "padding-left" "20px" ]
(List.append
[ rect [ x "1", y "1", width "100", height "100", rx "15", ry "15" ] [] ]
(svgCirclesForDieFace model.dieFace)
)
, button [ onClick Roll ] [ Html.text "Roll" ]
]
svgCirclesForDieFace : Int -> List (Svg.Svg Msg)
svgCirclesForDieFace dieFace =
case dieFace of
1 ->
[ circle [ cx "50", cy "50", r "10", fill "black" ] [] ]
2 ->
[ circle [ cx "25", cy "25", r "10", fill "black" ] []
, circle [ cx "75", cy "75", r "10", fill "black" ] []
]
3 ->
[ circle [ cx "25", cy "25", r "10", fill "black" ] []
, circle [ cx "50", cy "50", r "10", fill "black" ] []
, circle [ cx "75", cy "75", r "10", fill "black" ] []
]
4 ->
[ circle [ cx "25", cy "25", r "10", fill "black" ] []
, circle [ cx "75", cy "25", r "10", fill "black" ] []
, circle [ cx "25", cy "75", r "10", fill "black" ] []
, circle [ cx "75", cy "75", r "10", fill "black" ] []
]
5 ->
[ circle [ cx "25", cy "25", r "10", fill "black" ] []
, circle [ cx "75", cy "25", r "10", fill "black" ] []
, circle [ cx "25", cy "75", r "10", fill "black" ] []
, circle [ cx "75", cy "75", r "10", fill "black" ] []
, circle [ cx "50", cy "50", r "10", fill "black" ] []
]
6 ->
[ circle [ cx "25", cy "20", r "10", fill "black" ] []
, circle [ cx "25", cy "50", r "10", fill "black" ] []
, circle [ cx "25", cy "80", r "10", fill "black" ] []
, circle [ cx "75", cy "20", r "10", fill "black" ] []
, circle [ cx "75", cy "50", r "10", fill "black" ] []
, circle [ cx "75", cy "80", r "10", fill "black" ] []
]
_ ->
[ circle [ cx "50", cy "50", r "50", fill "red", stroke "none" ] [] ]
@nathanmalishev
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment