1
1
open System
2
- open MattEland.FSharpGeneticAlgorithm .Logic .Actors
3
2
open MattEland.FSharpGeneticAlgorithm .Logic .World
4
3
open MattEland.FSharpGeneticAlgorithm .Logic .Simulator
5
4
open MattEland.FSharpGeneticAlgorithm .ConsoleTestApp .Display
6
5
7
- type GameCommand =
8
- | MoveLeft | MoveRight
9
- | MoveUp | MoveDown
10
- | MoveUpLeft | MoveUpRight
11
- | MoveDownLeft | MoveDownRight
12
- | Wait
13
- | Restart
14
-
15
6
type Command =
16
7
| Action of GameCommand
17
8
| Exit
@@ -24,15 +15,13 @@ let tryParseInput (info:ConsoleKeyInfo) =
24
15
| ConsoleKey.DownArrow -> Some ( Action MoveDown)
25
16
| ConsoleKey.NumPad7 | ConsoleKey.Home -> Some ( Action MoveUpLeft)
26
17
| ConsoleKey.NumPad9 | ConsoleKey.PageUp -> Some ( Action MoveUpRight)
27
- | ConsoleKey.NumPad1 | ConsoleKey.End -> Some ( Action MoveDownRight )
18
+ | ConsoleKey.NumPad1 | ConsoleKey.End -> Some ( Action MoveDownLeft )
28
19
| ConsoleKey.NumPad3 | ConsoleKey.PageDown -> Some ( Action MoveDownRight)
29
20
| ConsoleKey.NumPad5 | ConsoleKey.Spacebar | ConsoleKey.Clear -> Some ( Action Wait)
30
21
| ConsoleKey.X -> Some Exit
31
22
| ConsoleKey.R -> Some ( Action Restart)
32
23
| _ -> None
33
24
34
- type GameState = { World : World ; Player : Actor }
35
-
36
25
[<EntryPoint>]
37
26
let main argv =
38
27
printfn " F# Console Application Tutorial by Matt Eland"
@@ -41,32 +30,20 @@ let main argv =
41
30
let r = Random()
42
31
fun max -> ( r.Next max) + 1
43
32
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
48
34
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
65
37
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
71
41
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
+
72
49
0 // return an integer exit code
0 commit comments