Last active
August 31, 2015 21:56
-
-
Save jpvillaisaza/6400128133081b5621ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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