Skip to content

Instantly share code, notes, and snippets.

@reactormonk
Forked from anonymous/Main.elm
Last active August 11, 2016 14:12
Show Gist options
  • Save reactormonk/ca6ec8e93703c75ee590bd239e218714 to your computer and use it in GitHub Desktop.
Save reactormonk/ca6ec8e93703c75ee590bd239e218714 to your computer and use it in GitHub Desktop.
import Html exposing (..)
import Html.App as App
import Html.Events exposing (..)
import List
import Json.Decode as Json
join : List String -> String
join list = (List.foldr (++) "" list)
main : Program Never
main = App.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions}
type alias Model =
{blue: Int, red: Int}
init : (Model, Cmd Msg)
init = (Model 0 0, Cmd.none)
type Player
= Red | Blue
type Msg
= Player.Red | Player.Blue | Noop
update : Msg -> Model -> (Model, Cmd Msg)
update message model =
case message of
Player.Blue ->
(Model (model.blue + 1) model.red, Cmd.none)
Player.Red ->
(Model model.blue (model.red + 1), Cmd.none)
Noop ->
(model, Cmd.none)
subscriptions: Model -> Sub Msg
subscriptions model = Sub.none
keyCodeToMsg : Int -> Msg
keyCodeToMsg keyCode =
case keyCode of
83 -> Player.Blue
79 -> Player.Red
_ -> Noop
onKeyCode : (Int -> msg) -> Attribute msg
onKeyCode message =
on "keydown" (Json.map message keyCode)
view : Model -> Html Msg
view model =
div [ onKeyCode keyCodeToMsg ]
[ h1 [] [ text "Arm Wrestling" ]
, p [] [text ("Blue: " ++ (toString model.blue) ++ " Red: " ++ (toString model.red))]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment