# jovaneyck's solution

## to Hangman in the F# Track

Published at Jul 13 2018 · 1 comment
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

type Progress = Busy of int | Win | Lose
type State = {progress : Progress; maskedWord : string; guesses : Set<char>}
type Game(secretWord : string) =
let initialState = {progress = Busy 9; maskedWord = System.String('_', secretWord.Length); guesses = Set.empty}

let gameWon state = not <| state.maskedWord.Contains("_")
let updateGuesses guess state = { state with guesses = (state.guesses |> Set.add guess)}
let toMaskedLetter letter = if state.guesses |> Set.contains letter then string letter else "_"
secretWord
|> String.concat ""
let updateProgress isCorrectGuess state =
match state.progress with
| Win
| Lose -> state
| Busy 1 ->
{state with
progress =
if not isCorrectGuess then Lose
else Win}
| Busy failuresToAllow ->
{state with
progress =
if gameWon state then Win
elif not isCorrectGuess then Busy (failuresToAllow - 1)
else Busy failuresToAllow }

let isCorrectGuess = secretWord.Contains (string guess) && not (state.guesses |> Set.contains guess)
state
|> (updateGuesses guess)
|> (updateProgress isCorrectGuess)

//Input streams
let startStateStream = new Event<State>()
let guessStream = new Event<char>()

//Output stream
let gameStates =
Observable.merge
startStateStream.Publish //to have the initial state in the stream on game start
(guessStream.Publish |> Observable.scan updateState initialState) //game loop

member this.Start () = startStateStream.Trigger initialState
member this.MakeGuess guess = guessStream.Trigger guess
member this.States = gameStates

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

Solution Author
commented over 1 year ago

General introduction to Reactive Programming

FRP in fsharp with built-in concepts

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?