Skip to content

Commit ae99cbc

Browse files
committed
Fixed a few logic issues after the merge
1 parent a9c2b5e commit ae99cbc

File tree

3 files changed

+47
-39
lines changed

3 files changed

+47
-39
lines changed

MattEland.FSharpGeneticAgorithm.ConsoleTestApp/Display.fs

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ let displayWorld (world: World) =
1818
let char = world |> getCharacterAtCell(x,y)
1919
printCell char (x = world.MaxX)
2020

21-
let getUserInput(): ConsoleKeyInfo =
21+
let getUserInput(world: World): ConsoleKeyInfo =
22+
displayWorld world
2223
printfn ""
2324
printfn "Press Arrow Keys to move, R to regenerate, or X to exit"
2425

25-
Console.ReadKey(true)
26+
let key = Console.ReadKey(true)
27+
Console.Clear()
28+
key
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
open System
2-
open MattEland.FSharpGeneticAlgorithm.Logic.Actors
32
open MattEland.FSharpGeneticAlgorithm.Logic.World
43
open MattEland.FSharpGeneticAlgorithm.Logic.Simulator
54
open MattEland.FSharpGeneticAlgorithm.ConsoleTestApp.Display
65

7-
type GameCommand =
8-
| MoveLeft | MoveRight
9-
| MoveUp | MoveDown
10-
| MoveUpLeft | MoveUpRight
11-
| MoveDownLeft | MoveDownRight
12-
| Wait
13-
| Restart
14-
156
type Command =
167
| Action of GameCommand
178
| Exit
@@ -24,15 +15,13 @@ let tryParseInput (info:ConsoleKeyInfo) =
2415
| ConsoleKey.DownArrow -> Some (Action MoveDown)
2516
| ConsoleKey.NumPad7 | ConsoleKey.Home -> Some (Action MoveUpLeft)
2617
| ConsoleKey.NumPad9 | ConsoleKey.PageUp -> Some (Action MoveUpRight)
27-
| ConsoleKey.NumPad1 | ConsoleKey.End -> Some (Action MoveDownRight)
18+
| ConsoleKey.NumPad1 | ConsoleKey.End -> Some (Action MoveDownLeft)
2819
| ConsoleKey.NumPad3 | ConsoleKey.PageDown -> Some (Action MoveDownRight)
2920
| ConsoleKey.NumPad5 | ConsoleKey.Spacebar | ConsoleKey.Clear -> Some (Action Wait)
3021
| ConsoleKey.X -> Some Exit
3122
| ConsoleKey.R -> Some (Action Restart)
3223
| _ -> None
3324

34-
type GameState = { World : World; Player : Actor }
35-
3625
[<EntryPoint>]
3726
let main argv =
3827
printfn "F# Console Application Tutorial by Matt Eland"
@@ -41,32 +30,20 @@ let main argv =
4130
let r = Random()
4231
fun max -> (r.Next max) + 1
4332

44-
let endState =
45-
let world = makeWorld 13 13 getRandomNumber
46-
let player = world.Squirrel
47-
let state = { World = world; Player = world.Squirrel }
33+
let world = makeWorld 13 13 getRandomNumber
4834

49-
let playTurn state command =
50-
match command with
51-
| MoveLeft -> { state with World = moveActor world player -1 0 }
52-
| MoveRight -> { state with World = moveActor world player 1 0 }
53-
| MoveUp -> { state with World = moveActor world player 0 -1 }
54-
| MoveDown -> { state with World = moveActor world player 0 1 }
55-
| MoveUpLeft -> { state with World = moveActor world player -1 -1 }
56-
| MoveUpRight -> { state with World = moveActor world player 1 -1 }
57-
| MoveDownLeft -> { state with World = moveActor world player -1 1 }
58-
| MoveDownRight -> { state with World = moveActor world player 1 1 }
59-
| Wait ->
60-
printfn "Time Passes..."
61-
state
62-
| Restart ->
63-
let world = makeWorld 13 13 getRandomNumber
64-
{ World = world; Player = world.Squirrel }
35+
let mutable state = { World = world; Player = world.Squirrel }
36+
let mutable simulating: bool = true
6537

66-
Seq.initInfinite(fun _ -> getUserInput())
67-
|> Seq.choose tryParseInput
68-
|> Seq.takeWhile (function | Exit -> false | _ -> true)
69-
|> Seq.choose(function | Exit -> None | Action gameCommand -> Some gameCommand)
70-
|> Seq.fold playTurn state
38+
while simulating do
39+
let player = state.World.Squirrel
40+
let userCommand = getUserInput(state.World) |> tryParseInput
7141

42+
match userCommand with
43+
| Some command ->
44+
match command with
45+
| Exit -> simulating <- false
46+
| Action gameCommand -> state <- playTurn state player getRandomNumber gameCommand
47+
| None -> printfn "Invalid input"
48+
7249
0 // return an integer exit code

MattEland.FSharpGeneticAlgorithm.Logic/Simulator.fs

+28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ open MattEland.FSharpGeneticAlgorithm.Logic.WorldPos
44
open MattEland.FSharpGeneticAlgorithm.Logic.World
55
open MattEland.FSharpGeneticAlgorithm.Logic.Actors
66

7+
type GameState = { World : World; Player : Actor }
8+
79
let isValidPos pos (world: World): bool =
810
pos.X >= 1 && pos.Y >= 1 && pos.X <= world.MaxX && pos.Y <= world.MaxY
911

@@ -25,4 +27,30 @@ let moveActor world actor xDiff yDiff =
2527
else
2628
world
2729

30+
type GameCommand =
31+
| MoveLeft | MoveRight
32+
| MoveUp | MoveDown
33+
| MoveUpLeft | MoveUpRight
34+
| MoveDownLeft | MoveDownRight
35+
| Wait
36+
| Restart
37+
38+
let playTurn state player getRandomNumber command =
39+
let world = state.World
40+
match command with
41+
| MoveLeft -> { state with World = moveActor world player -1 0 }
42+
| MoveRight -> { state with World = moveActor world player 1 0 }
43+
| MoveUp -> { state with World = moveActor world player 0 -1 }
44+
| MoveDown -> { state with World = moveActor world player 0 1 }
45+
| MoveUpLeft -> { state with World = moveActor world player -1 -1 }
46+
| MoveUpRight -> { state with World = moveActor world player 1 -1 }
47+
| MoveDownLeft -> { state with World = moveActor world player -1 1 }
48+
| MoveDownRight -> { state with World = moveActor world player 1 1 }
49+
| Wait ->
50+
printfn "Time Passes..."
51+
state
52+
| Restart ->
53+
let world = makeWorld 13 13 getRandomNumber
54+
{ World = world; Player = world.Squirrel }
55+
2856
// TODO: I'll need a way of simulating an actor's turn

0 commit comments

Comments
 (0)