Skip to content

Commit ca28d0c

Browse files
committed
Doggos now eat things that come near
1 parent b0a80a9 commit ca28d0c

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

MattEland.FSharpGeneticAlgorithm.Logic/Simulator.fs

+30-5
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,22 @@ let moveActor state actor pos =
3636
if otherActor <> actor && canEnterActorCell actor.ActorKind otherActor.ActorKind then
3737

3838
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+
3952
| Squirrel hasAcorn ->
40-
if not hasAcorn && otherActor.ActorKind = Acorn then
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
4155
{state with World =
4256
{
4357
world with
@@ -46,6 +60,7 @@ let moveActor state actor pos =
4660
}
4761
}
4862
else if hasAcorn && otherActor.ActorKind = Tree then
63+
// Moving to the tree with the acorn - this should win the game
4964
{
5065
state with SimState = Won; World = {
5166
world with Squirrel = {ActorKind = Squirrel true; Pos = pos; IsActive = true}
@@ -76,12 +91,22 @@ let moveRandomly state actor getRandomNumber =
7691

7792
moveActor state actor movedPos
7893

79-
let simulateActors (state: GameState) getRandomNumber =
80-
let mutable endState = state
94+
let simulateDoggo (state: GameState) =
95+
let doggo = state.World.Doggo
96+
let rabbit = state.World.Rabbit
97+
let squirrel = state.World.Squirrel
8198

82-
endState <- moveRandomly endState endState.World.Rabbit getRandomNumber
99+
// Eat any adjacent actor
100+
if rabbit.IsActive && isAdjacentTo doggo.Pos rabbit.Pos then
101+
moveActor state doggo rabbit.Pos
102+
else if squirrel.IsActive && isAdjacentTo doggo.Pos squirrel.Pos then
103+
moveActor state doggo squirrel.Pos
104+
else
105+
state
83106

84-
endState
107+
let simulateActors (state: GameState) getRandomNumber =
108+
moveRandomly state state.World.Rabbit getRandomNumber
109+
|> simulateDoggo
85110

86111
let handlePlayerCommand state command =
87112
let player = state.World.Squirrel

MattEland.FSharpGeneticAlgorithm.Tests/Tests.fs

+18
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,21 @@ let ``Squirrel Getting Acorn to Tree Should Win Game`` () =
6363
// Assert
6464
newState.World.Squirrel.Pos |> should equal world.Tree.Pos
6565
newState.SimState |> should equal Won
66+
67+
68+
[<Fact>]
69+
let ``Squirrel Moving Next to Dog Should End With Squirrel Eaten`` () =
70+
// Arrange
71+
let randomizer = new Random(42)
72+
let customSquirrel = {Pos=newPos 3 6; ActorKind = Squirrel false; IsActive = true}
73+
let world: World = {(makeTestWorld false) with Squirrel = customSquirrel}
74+
let state: GameState = {World=world; SimState=Simulating}
75+
76+
// Act
77+
let newState = simulateActors state randomizer.Next
78+
79+
// Assert
80+
newState.World.Doggo.Pos |> should equal world.Squirrel.Pos
81+
newState.World.Squirrel.IsActive |> should equal false
82+
newState.SimState |> should equal Lost
83+

0 commit comments

Comments
 (0)