@@ -8,7 +8,7 @@ open MattEland.FSharpGeneticAlgorithm.Logic.WorldGeneration
8
8
9
9
type SimulationState = Simulating | Won | Lost
10
10
11
- type GameState = { World : World ; SimState: SimulationState }
11
+ type GameState = { World : World ; SimState: SimulationState ; TurnsLeft : int }
12
12
13
13
let canEnterActorCell actor target =
14
14
match target with
@@ -19,6 +19,7 @@ let canEnterActorCell actor target =
19
19
20
20
let moveActor state actor pos =
21
21
let world = state.World
22
+
22
23
let performMove =
23
24
let actor = { actor with Pos = pos }
24
25
match actor.ActorKind with
@@ -28,46 +29,48 @@ let moveActor state actor pos =
28
29
| Rabbit -> { state with World={ world with Rabbit = actor }}
29
30
| Doggo -> { state with World={ world with Doggo = actor }}
30
31
32
+ let handleDogMove state otherActor =
33
+ if otherActor.ActorKind = Rabbit then
34
+ { state with World = { world with
35
+ Rabbit = { world.Rabbit with IsActive = false }
36
+ Doggo = { world.Doggo with Pos = pos}
37
+ }}
38
+ else
39
+ { state with SimState = Lost; World = { world with
40
+ Squirrel = { world.Squirrel with IsActive = false }
41
+ Doggo = { world.Doggo with Pos = pos}
42
+ }
43
+ }
44
+
45
+ let handleSquirrelMove otherActor hasAcorn =
46
+ if not hasAcorn && otherActor.ActorKind = Acorn && otherActor.IsActive then
47
+ // Moving to the acorn for the first time should give the squirrel the acorn
48
+ { state with World =
49
+ {
50
+ world with
51
+ Squirrel = { ActorKind = Squirrel true ; Pos = pos; IsActive = true }
52
+ Acorn = { world.Acorn with IsActive = false }
53
+ }
54
+ }
55
+ else if hasAcorn && otherActor.ActorKind = Tree then
56
+ // Moving to the tree with the acorn - this should win the game
57
+ {
58
+ state with SimState = Won; World = {
59
+ world with Squirrel = { ActorKind = Squirrel true ; Pos = pos; IsActive = true }
60
+ }
61
+ }
62
+ else
63
+ performMove
64
+
31
65
let target = tryGetActor( pos.X, pos.Y) world
32
66
33
67
match target with
34
68
| None -> performMove
35
69
| Some otherActor ->
36
70
if otherActor <> actor && canEnterActorCell actor.ActorKind otherActor.ActorKind then
37
-
38
71
match actor.ActorKind with
39
- | Doggo ->
40
- if otherActor.ActorKind = Rabbit then
41
- { state with World = { world with
42
- Rabbit = { world.Rabbit with IsActive = false }
43
- Doggo = { world.Doggo with Pos = pos}
44
- }}
45
- else
46
- { state with SimState = Lost; World = { world with
47
- Squirrel = { world.Squirrel with IsActive = false }
48
- Doggo = { world.Doggo with Pos = pos}
49
- }
50
- }
51
-
52
- | Squirrel hasAcorn ->
53
- if not hasAcorn && otherActor.ActorKind = Acorn && not otherActor.IsActive then
54
- // Moving to the acorn for the first time should give the squirrel the acorn
55
- { state with World =
56
- {
57
- world with
58
- Squirrel = { ActorKind = Squirrel true ; Pos = pos; IsActive = true }
59
- Acorn = { world.Acorn with IsActive = false }
60
- }
61
- }
62
- else if hasAcorn && otherActor.ActorKind = Tree then
63
- // Moving to the tree with the acorn - this should win the game
64
- {
65
- state with SimState = Won; World = {
66
- world with Squirrel = { ActorKind = Squirrel true ; Pos = pos; IsActive = true }
67
- }
68
- }
69
- else
70
- performMove
72
+ | Doggo -> handleDogMove state otherActor
73
+ | Squirrel hasAcorn -> handleSquirrelMove otherActor hasAcorn
71
74
| _ -> performMove
72
75
else
73
76
state
@@ -104,9 +107,19 @@ let simulateDoggo (state: GameState) =
104
107
else
105
108
state
106
109
110
+ let decreaseTimer ( state : GameState ) =
111
+ if state.SimState = Simulating then
112
+ if state.TurnsLeft > 0 then
113
+ { state with TurnsLeft = state.TurnsLeft - 1 }
114
+ else
115
+ { state with TurnsLeft = 0 ; SimState = Lost}
116
+ else
117
+ state
118
+
107
119
let simulateActors ( state : GameState ) getRandomNumber =
108
120
moveRandomly state state.World.Rabbit getRandomNumber
109
- |> simulateDoggo
121
+ |> simulateDoggo
122
+ |> decreaseTimer
110
123
111
124
let handlePlayerCommand state command =
112
125
let player = state.World.Squirrel
@@ -131,7 +144,7 @@ let handlePlayerCommand state command =
131
144
let playTurn state getRandomNumber command =
132
145
let world = state.World
133
146
match command with
134
- | Restart -> { World = makeWorld world.MaxX world.MaxY getRandomNumber; SimState = Simulating }
147
+ | Restart -> { World = makeWorld world.MaxX world.MaxY getRandomNumber; SimState = Simulating; TurnsLeft = 30 }
135
148
| _ ->
136
149
match state.SimState with
137
150
| Simulating ->
0 commit comments