# ErikSchierboom's solution

## to Hangman in the F# Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Implement the logic of the hangman game using functional reactive programming.

Hangman is a simple word guessing game.

Functional Reactive Programming is a way to write interactive programs. It differs from the usual perspective in that instead of saying "when the button is pressed increment the counter", you write "the value of the counter is the sum of the number of times the button is pressed."

Implement the basic logic behind hangman using functional reactive programming. You'll need to install an FRP library for this, this will be described in the language/track specific files of the exercise.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### HangmanTest.fs

``````// This file was created manually and its version is 1.0.0.

module HangmanTest

open Xunit
open FsUnit.Xunit

open Hangman

[<Fact>]
let ``Initially 9 failures are allowed`` () =
let game = createGame "foo"
let states = statesObservable game

let mutable lastProgress = Busy 9
states.Add(fun state -> lastProgress <- state.progress) |> ignore

startGame game |> ignore

lastProgress |> should equal <| Busy 9

[<Fact(Skip = "Remove to run test")>]
let ``Initially no letters are guessed`` () =
let game = createGame "foo"
let states = statesObservable game

startGame game |> ignore

[<Fact(Skip = "Remove to run test")>]
let ``After 10 failures the game is over`` () =
let game = createGame "foo"
let states = statesObservable game

let mutable lastProgress = Busy 9
states.Add(fun state -> lastProgress <- state.progress) |> ignore

startGame game |> ignore

[for x in 1..10 do makeGuess 'x' game] |> ignore

lastProgress |> should equal Lose

[<Fact(Skip = "Remove to run test")>]
let ``Feeding a correct letter removes underscores`` () =
let game = createGame "foobar"
let states = statesObservable game

let mutable lastState = None
states.Add(fun state -> lastState <- Some state) |> ignore

startGame game |> ignore

makeGuess 'b' game |> ignore

lastState.Value.progress |> should equal <| Busy 9

makeGuess 'o' game |> ignore

lastState.Value.progress |> should equal <| Busy 9

[<Fact(Skip = "Remove to run test")>]
let ``Feeding a correct letter twice counts as a failure`` () =
let game = createGame "foobar"
let states = statesObservable game

let mutable lastState = None
states.Add(fun state -> lastState <- Some state) |> ignore

startGame game |> ignore

makeGuess 'b' game |> ignore

lastState.Value.progress |> should equal <| Busy 9

makeGuess 'b' game |> ignore

lastState.Value.progress |> should equal <| Busy 8

[<Fact(Skip = "Remove to run test")>]
let ``Getting all the letters right makes for a win`` () =
let game = createGame "hello"
let states = statesObservable game

let mutable lastState = None
states.Add(fun state -> lastState <- Some state) |> ignore

startGame game |> ignore

makeGuess 'b' game |> ignore

lastState.Value.progress |> should equal <| Busy 8

makeGuess 'e' game |> ignore

lastState.Value.progress |> should equal <| Busy 8

makeGuess 'l' game |> ignore

lastState.Value.progress |> should equal <| Busy 8

makeGuess 'o' game |> ignore

lastState.Value.progress |> should equal <| Busy 8

makeGuess 'h' game |> ignore

lastState.Value.progress |> should equal Win
``````module Hangman

open System

let allowedFailures = 9

type Progress =
| Win
| Lose
| Busy of int

type State = { progress: Progress; maskedWord: string; guesses: Set<char> }

let updateProgress word guess (state: State) =
match state.progress with
| Win  -> { state with progress = Win }
| Lose -> { state with progress = Lose }
| Busy x ->
{ state with progress = Win }
elif x <= 0 then
{ state with progress = Lose }
elif not <| word.Contains(string guess) || Set.contains guess state.guesses then
{ state with progress = Busy (x - 1) }
else
state

let updateMaskedWord word guess (state: State) =
let updatedGuesses = Set.add guess state.guesses
word
|> Seq.map (fun x -> if Set.contains x updatedGuesses then x else '_')
|> Seq.toArray
|> System.String

let updateGuesses guess (state: State) = { state with guesses = Set.add guess state.guesses }

let updateState word guess (state: State) =
state
|> updateProgress word guess
|> updateGuesses guess

let mkStartState (word: string) = { progress = Busy allowedFailures; maskedWord = String('_', word.Length); guesses = Set.empty }

type Game(word) =
let initialState = new Event<State>()
let guesses = new Event<char>()
let startState = mkStartState word

member this.States = Observable.merge initialState.Publish (Observable.scan (fun state guess -> updateState word guess state) startState guesses.Publish)
member this.Start() = initialState.Trigger(startState)
member this.MakeGuess(guess) = guesses.Trigger(guess)

let createGame word = new Game(word)
let startGame (game: Game) = game.Start()
let makeGuess guess (game: Game) = game.MakeGuess(guess)
let statesObservable (game: Game) = game.States``````