Skip to content

Commit b0a80a9

Browse files
committed
Game can now be won
1 parent 6b0528d commit b0a80a9

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

MattEland.FSharpGeneticAlgorithm.Logic/Simulator.fs

+25-14
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ let moveActor state actor pos =
2222
let performMove =
2323
let actor = { actor with Pos = pos }
2424
match actor.ActorKind with
25-
| Squirrel _ -> { world with Squirrel = actor }
26-
| Tree -> { world with Tree = actor }
27-
| Acorn -> { world with Acorn = actor }
28-
| Rabbit -> { world with Rabbit = actor }
29-
| Doggo -> { world with Doggo = actor }
25+
| Squirrel _ -> { state with World={world with Squirrel = actor }}
26+
| Tree -> { state with World={world with Tree = actor }}
27+
| Acorn -> { state with World={world with Acorn = actor }}
28+
| Rabbit -> { state with World={world with Rabbit = actor }}
29+
| Doggo -> { state with World={world with Doggo = actor }}
3030

3131
let target = tryGetActor(pos.X, pos.Y) world
3232

@@ -38,16 +38,24 @@ let moveActor state actor pos =
3838
match actor.ActorKind with
3939
| Squirrel hasAcorn ->
4040
if not hasAcorn && otherActor.ActorKind = Acorn then
41-
{
42-
world with
43-
Squirrel = {ActorKind = Squirrel true; Pos = pos; IsActive = true}
44-
Acorn = {world.Acorn with IsActive = false}
41+
{state with World =
42+
{
43+
world with
44+
Squirrel = {ActorKind = Squirrel true; Pos = pos; IsActive = true}
45+
Acorn = {world.Acorn with IsActive = false}
46+
}
47+
}
48+
else if hasAcorn && otherActor.ActorKind = Tree then
49+
{
50+
state with SimState = Won; World = {
51+
world with Squirrel = {ActorKind = Squirrel true; Pos = pos; IsActive = true}
52+
}
4553
}
4654
else
4755
performMove
4856
| _ -> performMove
4957
else
50-
world
58+
state
5159

5260
let getCandidates (current: WorldPos, world: World, includeCenter: bool): WorldPos seq =
5361
let mutable candidates: WorldPos seq = Seq.empty
@@ -66,7 +74,7 @@ let moveRandomly state actor getRandomNumber =
6674
|> Seq.sortBy(fun _ -> getRandomNumber 1000)
6775
|> Seq.head
6876

69-
{state with World = moveActor state actor movedPos }
77+
moveActor state actor movedPos
7078

7179
let simulateActors (state: GameState) getRandomNumber =
7280
let mutable endState = state
@@ -91,7 +99,7 @@ let handlePlayerCommand state command =
9199
let movedPos = {X=player.Pos.X + xDelta; Y=player.Pos.Y + yDelta}
92100

93101
if isValidPos movedPos state.World then
94-
{state with World = moveActor state player movedPos}
102+
moveActor state player movedPos
95103
else
96104
state
97105

@@ -100,5 +108,8 @@ let playTurn state getRandomNumber command =
100108
match command with
101109
| Restart -> { World = makeWorld world.MaxX world.MaxY getRandomNumber; SimState = Simulating }
102110
| _ ->
103-
let newState = handlePlayerCommand state command
104-
simulateActors newState getRandomNumber
111+
match state.SimState with
112+
| Simulating ->
113+
let newState = handlePlayerCommand state command
114+
simulateActors newState getRandomNumber
115+
| _ -> state

MattEland.FSharpGeneticAlgorithm.Tests/Tests.fs

+15-3
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,21 @@ let ``Rabbit should move randomly`` () =
2626
// Arrange
2727
let randomizer = new Random(42)
2828
let world: World = makeTestWorld false
29-
let state: GameState = {World=world; SimState=SimulationState.Simulating}
29+
let state: GameState = {World=world; SimState=Simulating}
3030
let originalPos = state.World.Rabbit.Pos
3131

3232
// Act
3333
let newWorld = simulateActors state randomizer.Next
3434

3535
// Assert
3636
newWorld.World.Rabbit.Pos |> should not' (equal originalPos)
37-
3837

3938
[<Fact>]
4039
let ``Squirrel Getting Acorn Should Change how it Displays`` () =
4140
// Arrange
4241
let customSquirrel = {Pos=newPos 6 7; ActorKind = Squirrel false; IsActive = true}
4342
let world: World = {(makeTestWorld false) with Squirrel = customSquirrel}
44-
let state: GameState = {World=world; SimState=SimulationState.Simulating}
43+
let state: GameState = {World=world; SimState=Simulating}
4544
let command: GameCommand = MoveLeft
4645

4746
// Act
@@ -50,4 +49,17 @@ let ``Squirrel Getting Acorn Should Change how it Displays`` () =
5049
// Assert
5150
newState.World.Squirrel.ActorKind |> should equal (Squirrel true)
5251

52+
[<Fact>]
53+
let ``Squirrel Getting Acorn to Tree Should Win Game`` () =
54+
// Arrange
55+
let customSquirrel = {Pos=newPos 9 10; ActorKind = Squirrel true; IsActive = true}
56+
let world: World = {(makeTestWorld false) with Squirrel = customSquirrel}
57+
let state: GameState = {World=world; SimState=Simulating}
58+
let command: GameCommand = MoveLeft
59+
60+
// Act
61+
let newState = handlePlayerCommand state command
5362

63+
// Assert
64+
newState.World.Squirrel.Pos |> should equal world.Tree.Pos
65+
newState.SimState |> should equal Won

0 commit comments

Comments
 (0)