Created
November 22, 2017 15:47
-
-
Save kingsleyh/cf2dc33883d660eeadb0f916c5ac4c76 to your computer and use it in GitHub Desktop.
Ksenia Number Game in Elm
This file contains hidden or 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 Main exposing (..) | |
import Html exposing (Html, button, div, input, p, program, text) | |
import Html.Attributes exposing (placeholder, type_) | |
import Html.Events exposing (onClick, onInput) | |
-- MODEL | |
type GameState | |
= Started | |
| Lost | |
| Won | |
| InProgress | |
type alias Model = | |
{ mysteryNumber : Int | |
, maxGuesses : Int | |
, guessesMade : Int | |
, gameState : GameState | |
, inputValue : Int | |
} | |
init : ( Model, Cmd Msg ) | |
init = | |
( { mysteryNumber = 50 | |
, maxGuesses = 10 | |
, guessesMade = 0 | |
, gameState = Started | |
, inputValue = 0 | |
} | |
, Cmd.none | |
) | |
-- MESSAGES | |
type Msg | |
= Guess | |
| Restart | |
| EnterText String | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
div | |
[] | |
[ messageDisplay model | |
, inputField | |
, guessButton model | |
] | |
messageDisplay model = | |
p [] [ text (gameMessage model) ] | |
guessButton model = | |
if | |
model.gameState | |
== Started | |
|| model.gameState | |
== InProgress | |
then | |
button [ onClick Guess ] [ text "guess" ] | |
else | |
button [ onClick Restart ] [ text "restart" ] | |
inputField = | |
input | |
[ placeholder "Enter a number..." | |
, type_ "number" | |
, onInput EnterText | |
] | |
[] | |
gameMessage model = | |
let | |
stateMessage model = | |
" Your guess: " | |
++ toString model.inputValue | |
++ ", State: " | |
++ toString model.gameState | |
++ ", Mystery Number: " | |
++ toString model.mysteryNumber | |
++ if model.gameState == Started || model.gameState == InProgress then | |
", Guess Number: " | |
++ toString model.guessesMade | |
++ ", Max Guesses: " | |
++ toString model.maxGuesses | |
else | |
"" | |
in | |
case model.gameState of | |
Started -> | |
"I am thinking of a number between 1 and 100." ++ stateMessage model | |
InProgress -> | |
if model.inputValue < model.mysteryNumber then | |
"That's too low." ++ stateMessage model | |
else | |
"That's too high." ++ stateMessage model | |
Lost -> | |
"You've run out of guesses! The mystery number was: " | |
++ toString model.mysteryNumber | |
++ ". " | |
++ "Game Over!" | |
++ stateMessage model | |
Won -> | |
"That's correct! It took you: " | |
++ toString model.guessesMade | |
++ " guesses. " | |
++ "Game Over!" | |
++ stateMessage model | |
-- UPDATE | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
Guess -> | |
let | |
checkGameState : Model -> GameState | |
checkGameState model = | |
if model.guessesMade >= model.maxGuesses && model.inputValue /= model.mysteryNumber then | |
Lost | |
else if model.guessesMade <= model.maxGuesses && model.inputValue == model.mysteryNumber then | |
Won | |
else | |
InProgress | |
in | |
( { model | guessesMade = model.guessesMade + 1, gameState = checkGameState model }, Cmd.none ) | |
EnterText text -> | |
let | |
intValue = | |
String.toInt text | |
|> Result.toMaybe | |
|> Maybe.withDefault 0 | |
in | |
( { model | inputValue = intValue }, Cmd.none ) | |
Restart -> | |
init | |
-- SUBSCRIPTIONS | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Sub.none | |
-- MAIN | |
main : Program Never Model Msg | |
main = | |
program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment