Skip to content

Instantly share code, notes, and snippets.

@A-gambit
Created March 7, 2016 08:01
Show Gist options
  • Save A-gambit/a30c27c4494ab0d5e83f to your computer and use it in GitHub Desktop.
Save A-gambit/a30c27c4494ab0d5e83f to your computer and use it in GitHub Desktop.

Revisions

  1. A-gambit created this gist Mar 7, 2016.
    81 changes: 81 additions & 0 deletions 1.elm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    module Game where

    import Html exposing (..)
    import Html.Attributes exposing (..)
    import Html.Events exposing (..)

    import Signal exposing (Address)

    import StartApp.Simple as StartApp

    -- MODEL

    type alias Model =
    { row : List Int,
    cur : Int
    }

    initialModel : Model
    initialModel =
    { row = [ 0, 0, 0 ],
    cur = 1
    }

    -- UPDATE

    type Action
    = NoOp
    | Move Int

    update : Action -> Model -> Model
    update action model =
    case action of
    NoOp ->
    model

    Move index ->
    let
    updateRow eIndex e =
    if eIndex == index then model.cur else e
    next =
    if model.cur == 1 then 2 else 1
    in
    { model |
    row = List.indexedMap updateRow model.row,
    cur = next
    }

    -- VIEW

    view : Address Action -> Model -> Html
    view address model =
    div
    [ id "container" ]
    [ title,
    squareList address model.row
    ]

    title : Html
    title =
    h1 [] [ text "The Game!" ]

    squareList : Address Action -> List Int -> Html
    squareList address row =
    let
    items = List.indexedMap (item address) row
    in
    ul [ ] items

    item : Address Action -> Int -> Int -> Html
    item address index val =
    li [ onClick address (Move index) ] [ text ((toString val) ++ " " ++ (toString index)) ]

    -- MAIN

    main : Signal Html
    main =
    StartApp.start
    { model = initialModel,
    view = view,
    update = update
    }