Skip to content

Instantly share code, notes, and snippets.

@cobalamin
Forked from reactormonk/Main.elm
Created August 11, 2016 14:12
Show Gist options
  • Save cobalamin/56479cda785e29f1d34319dc256189de to your computer and use it in GitHub Desktop.
Save cobalamin/56479cda785e29f1d34319dc256189de to your computer and use it in GitHub Desktop.
module Temp exposing (..)
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
= PlayerMsg Player | Noop
update : Msg -> Model -> (Model, Cmd Msg)
update message model =
case message of
PlayerMsg playerMsg ->
case playerMsg of
Blue ->
(Model (model.blue + 1) model.red, Cmd.none)
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 -> PlayerMsg Blue
79 -> PlayerMsg 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