Skip to content

Instantly share code, notes, and snippets.

@jpvillaisaza
Last active August 31, 2015 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jpvillaisaza/6400128133081b5621ae to your computer and use it in GitHub Desktop.
Save jpvillaisaza/6400128133081b5621ae to your computer and use it in GitHub Desktop.
module Hangman where
import Char
import Html exposing (Html)
import Html.Attributes
import Html.Events
import StartApp.Simple as StartApp
import String
-- Model
type alias Model =
List (Char, Bool)
init : String -> Model
init word =
String.foldr (::) [] word
|> List.map (\letter -> (letter, False))
-- Update
type alias Action =
Char
update : Action -> Model -> Model
update guess letters =
let
match guess (letter, guessed) =
(letter, guess == letter || guessed)
in
List.map (match guess) letters
-- View
view : Signal.Address Action -> Model -> Html
view address model =
Html.div
[ Html.Attributes.style [ ("text-align", "center") ] ]
[ Html.h1 [] [ Html.text "Hangman" ]
, Html.h3 [] [ Html.text (toString model) ]
, Html.input
[ Html.Attributes.autofocus True
, Html.Attributes.placeholder "guess"
, Html.Attributes.style [ ("text-align", "center") ]
, Html.Events.onKeyPress address Char.fromCode
]
[]
]
toString : Model -> String
toString letters =
let
fromLetter (letter, guessed) =
if guessed then letter else '-'
in
List.map fromLetter letters
|> List.foldr String.cons ""
-- App
main : Signal Html
main =
StartApp.start
{ model = init "supercalifragilisticexpialidocious"
, view = view
, update = update
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment