DEV Community

Discussion on: F# Squirrel Brains: Adding Actors and Getting Functional

Collapse
 
vasar007 profile image
Vasily Vasilyev • Edited

Thanks for the great series! Keep up the good work.

By the way, I found a little issue in the current implementation. It's related to GameState record and how it's used.

First of all, additional player value actually isn't needed here:

while simulating do
  let player = state.World.Squirrel // <- ???
  let userCommand = getUserInput(state.World) |> tryParseInput

state value already has Player value and we could use it. However, there is a problem because playTurn method may create new world but doesn't update Player value.

I think GameState record should be implemented as:

type GameState =
  {
    World: World
  }

  member this.Player = this.World.Squirrel

Such version won't cause problems with incorrect references to the actual player. Finally, we can remove player parameter from playTurn method and simplify a liitle bit main:

let mutable state = {
  World = world
}
let mutable simulating = true

while simulating do
  let userCommand =
    getUserInput state.World
    |> tryParseInput

    match userCommand with
      | None -> printfn "Invalid input"
      | Some command ->
        match command with
          | Exit ->
            simulating <- false
          | Action gameCommand ->
            state <- playTurn state getRandomNumber gameCommand
Collapse
 
integerman profile image
Matt Eland

Love the detailed suggestion. I'm aiming to get the game simulation further along and have a unit-test focused article up this weekend.

On the GameState.Player suggestion - yeah, I did find that issue in my article3 branch I'm working on for the next article and have made a fix for that issue.