Skip to content

Commit 39f3514

Browse files
committed
Merge branch 'article4'
2 parents 3f0e97d + e5f794b commit 39f3514

File tree

23 files changed

+377
-15
lines changed

23 files changed

+377
-15
lines changed

MattEland.FSharpGeneticAgorithm.ConsoleTestApp/Program.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let main argv =
3333

3434
let world = makeTestWorld false
3535

36-
let mutable state = { World = world; SimState = Simulating; TurnsLeft=30}
36+
let mutable state = { World = world; SimState = SimulationState.Simulating; TurnsLeft=30}
3737
let mutable simulating: bool = true
3838

3939
while simulating do

MattEland.FSharpGeneticAlgorithm.Logic/Simulator.fs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ open MattEland.FSharpGeneticAlgorithm.Logic.Actors
66
open MattEland.FSharpGeneticAlgorithm.Logic.Commands
77
open MattEland.FSharpGeneticAlgorithm.Logic.WorldGeneration
88

9-
type SimulationState = Simulating | Won | Lost
9+
type SimulationState = Simulating=0 | Won=1 | Lost=2
1010

1111
type GameState = { World : World; SimState: SimulationState; TurnsLeft: int}
1212

@@ -36,7 +36,7 @@ let moveActor state actor pos =
3636
Doggo = {world.Doggo with Pos = pos}
3737
}}
3838
else
39-
{state with SimState = Lost; World = {world with
39+
{state with SimState = SimulationState.Lost; World = {world with
4040
Squirrel = {world.Squirrel with IsActive = false}
4141
Doggo = {world.Doggo with Pos = pos}
4242
}
@@ -55,7 +55,7 @@ let moveActor state actor pos =
5555
else if hasAcorn && otherActor.ActorKind = Tree then
5656
// Moving to the tree with the acorn - this should win the game
5757
{
58-
state with SimState = Won; World = {
58+
state with SimState = SimulationState.Won; World = {
5959
world with Squirrel = {ActorKind = Squirrel true; Pos = pos; IsActive = true}
6060
}
6161
}
@@ -108,11 +108,11 @@ let simulateDoggo (state: GameState) =
108108
state
109109

110110
let decreaseTimer (state: GameState) =
111-
if state.SimState = Simulating then
111+
if state.SimState = SimulationState.Simulating then
112112
if state.TurnsLeft > 0 then
113113
{state with TurnsLeft = state.TurnsLeft - 1}
114114
else
115-
{state with TurnsLeft = 0; SimState = Lost}
115+
{state with TurnsLeft = 0; SimState = SimulationState.Lost}
116116
else
117117
state
118118

@@ -144,10 +144,19 @@ let handlePlayerCommand state command =
144144
let playTurn state getRandomNumber command =
145145
let world = state.World
146146
match command with
147-
| Restart -> { World = makeWorld world.MaxX world.MaxY getRandomNumber; SimState = Simulating; TurnsLeft = 30 }
147+
| Restart -> { World = makeWorld world.MaxX world.MaxY getRandomNumber; SimState = SimulationState.Simulating; TurnsLeft = 30 }
148148
| _ ->
149149
match state.SimState with
150-
| Simulating ->
150+
| SimulationState.Simulating ->
151151
let newState = handlePlayerCommand state command
152152
simulateActors newState getRandomNumber
153153
| _ -> state
154+
155+
let simulateTurn state command =
156+
if state.SimState = SimulationState.Simulating then
157+
let random = new System.Random()
158+
let newState = handlePlayerCommand state command
159+
simulateActors(newState) random.Next
160+
else
161+
state
162+

MattEland.FSharpGeneticAlgorithm.Logic/WorldGeneration.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ let makeWorld maxX maxY random =
4848
Acorn = actors.[3]
4949
Rabbit = actors.[4] }
5050

51+
let makeDefaultWorld() =
52+
let random = new System.Random()
53+
makeWorld 13 13 random.Next
54+
5155
let makeTestWorld hasAcorn =
5256
{
5357
MaxX = 13;

MattEland.FSharpGeneticAlgorithm.Logic/WorldPos.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ let isAdjacentTo (posA: WorldPos) (posB: WorldPos): bool =
1111
result
1212

1313
let getRandomPos(maxX:int32, maxY:int32, getRandom): WorldPos =
14-
let x = getRandom maxX
15-
let y = getRandom maxY
14+
let x = 1 + getRandom maxX
15+
let y = 1 + getRandom maxY
1616
newPos x y

MattEland.FSharpGeneticAlgorithm.Tests/Tests.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let ``Point Adjaency Tests`` (x1, y1, x2, y2, expectedAdjacent) =
2121
isAdjacentTo pos1 pos2 |> should equal expectedAdjacent
2222

2323
let getRandomNumber (max: int): int = 42
24-
let buildTestState = {World=(makeTestWorld false); SimState=Simulating; TurnsLeft = 30}
24+
let buildTestState = {World=(makeTestWorld false); SimState=SimulationState.Simulating; TurnsLeft = 30}
2525

2626
[<Fact>]
2727
let ``Rabbit should move randomly`` () =
@@ -60,7 +60,7 @@ let ``Squirrel Getting Acorn to Tree Should Win Game`` () =
6060

6161
// Assert
6262
newState.World.Squirrel.Pos |> should equal state.World.Tree.Pos
63-
newState.SimState |> should equal Won
63+
newState.SimState |> should equal SimulationState.Won
6464

6565
[<Fact>]
6666
let ``Dog Should Eat Squirrel If Adjacent`` () =
@@ -75,7 +75,7 @@ let ``Dog Should Eat Squirrel If Adjacent`` () =
7575
// Assert
7676
newState.World.Doggo.Pos |> should equal newState.World.Squirrel.Pos
7777
newState.World.Squirrel.IsActive |> should equal false
78-
newState.SimState |> should equal Lost
78+
newState.SimState |> should equal SimulationState.Lost
7979

8080
[<Fact>]
8181
let ``Simulating actors should decrease the turns left counter`` () =
@@ -97,4 +97,4 @@ let ``Running out of turns should lose the simulation`` () =
9797
let newState = simulateActors state getRandomNumber
9898

9999
// Assert
100-
newState.SimState |> should equal Lost
100+
newState.SimState |> should equal SimulationState.Lost
Loading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Windows.Input;
3+
4+
namespace MattEland.FSharpGeneticAlgorithm.WindowsClient
5+
{
6+
public class ActionCommand : ICommand
7+
{
8+
private readonly Action<object> _invokedAction;
9+
10+
public ActionCommand(Action invokedAction)
11+
{
12+
_invokedAction = _ => invokedAction?.Invoke();
13+
}
14+
15+
public ActionCommand(Action<object> invokedAction)
16+
{
17+
_invokedAction = invokedAction;
18+
}
19+
20+
public bool CanExecute(object parameter) => true;
21+
22+
public void Execute(object parameter) => _invokedAction?.Invoke(parameter);
23+
24+
public event EventHandler CanExecuteChanged;
25+
}
26+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using MattEland.FSharpGeneticAlgorithm.Logic;
2+
3+
namespace MattEland.FSharpGeneticAlgorithm.WindowsClient
4+
{
5+
internal class ActorViewModel
6+
{
7+
private readonly Actors.Actor _actor;
8+
9+
public ActorViewModel(Actors.Actor actor)
10+
{
11+
_actor = actor;
12+
}
13+
14+
// Subtract 1 since my data's indexes start at 1 instead of 0
15+
public int PosX => (_actor.Pos.X - 1) * 10;
16+
public int PosY => (_actor.Pos.Y - 1) * 10;
17+
18+
public string Text => Actors.getChar(_actor).ToString();
19+
20+
public string ImagePath
21+
{
22+
get
23+
{
24+
if (_actor.ActorKind.Equals(Actors.ActorKind.Acorn))
25+
{
26+
return "Acorn.png";
27+
}
28+
29+
if (_actor.ActorKind.Equals(Actors.ActorKind.Doggo))
30+
{
31+
return "Doggo.png";
32+
}
33+
34+
if (_actor.ActorKind.Equals(Actors.ActorKind.Rabbit))
35+
{
36+
return "Rabbit.png";
37+
}
38+
39+
if (_actor.ActorKind.Equals(Actors.ActorKind.Tree))
40+
{
41+
return "Tree.png";
42+
}
43+
44+
if (_actor.ActorKind.Equals(Actors.ActorKind.NewSquirrel(true)))
45+
{
46+
return "SquirrelAcorn.png";
47+
}
48+
49+
if (_actor.ActorKind.Equals(Actors.ActorKind.NewSquirrel(false)))
50+
{
51+
return "Squirrel.png";
52+
}
53+
54+
return null;
55+
}
56+
}
57+
}
58+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Application x:Class="MattEland.FSharpGeneticAlgorithm.WindowsClient.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:MattEland.FSharpGeneticAlgorithm.WindowsClient"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
</Application.Resources>
8+
</Application>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace MattEland.FSharpGeneticAlgorithm.WindowsClient
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}

0 commit comments

Comments
 (0)