Skip to content

Commit 9790ffb

Browse files
committed
Removed player control features
1 parent f79eb0d commit 9790ffb

13 files changed

+13
-148
lines changed

MattEland.FSharpGeneticAlgorithm.Logic/Commands.fs

-7
This file was deleted.

MattEland.FSharpGeneticAlgorithm.Logic/Genetics.fs

-18
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11
module MattEland.FSharpGeneticAlgorithm.Genetics.Genes
22

3-
open GeneticSharp.Domain.Chromosomes
4-
open GeneticSharp.Domain.Fitnesses
53
open MattEland.FSharpGeneticAlgorithm.Logic.WorldPos
64
open MattEland.FSharpGeneticAlgorithm.Logic.Actors
75
open MattEland.FSharpGeneticAlgorithm.Logic.World
86

9-
type SquirrelChromosome() =
10-
inherit ChromosomeBase(5) // Number of genes in the chromosome
11-
12-
override this.CreateNew(): IChromosome =
13-
new SquirrelChromosome() :> IChromosome
14-
15-
override this.GenerateGene(index: int): Gene =
16-
new Gene(42)
17-
18-
type SquirrelFitness() =
19-
20-
interface IFitness with
21-
member this.Evaluate(chromosome: IChromosome): double =
22-
let domainChromosome = chromosome :?> SquirrelChromosome
23-
42.0
24-
257
type SquirrelPriorities =
268
{
279
dogImportance: double

MattEland.FSharpGeneticAlgorithm.Logic/MattEland.FSharpGeneticAlgorithm.Logic.fsproj

-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@
99
<Compile Include="Actors.fs" />
1010
<Compile Include="World.fs" />
1111
<Compile Include="WorldGeneration.fs" />
12-
<Compile Include="Commands.fs" />
1312
<Compile Include="Genetics.fs" />
1413
<Compile Include="Simulator.fs" />
1514
</ItemGroup>
1615

17-
<ItemGroup>
18-
<PackageReference Include="GeneticSharp" Version="2.6.0" />
19-
</ItemGroup>
20-
2116
</Project>

MattEland.FSharpGeneticAlgorithm.Logic/Simulator.fs

-40
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
open MattEland.FSharpGeneticAlgorithm.Logic.WorldPos
44
open MattEland.FSharpGeneticAlgorithm.Logic.World
55
open MattEland.FSharpGeneticAlgorithm.Logic.Actors
6-
open MattEland.FSharpGeneticAlgorithm.Logic.Commands
76
open MattEland.FSharpGeneticAlgorithm.Logic.WorldGeneration
87
open MattEland.FSharpGeneticAlgorithm.Genetics.Genes
98

@@ -122,45 +121,6 @@ let simulateActors (state: GameState) getRandomNumber =
122121
|> simulateDoggo
123122
|> decreaseTimer
124123

125-
let handlePlayerCommand state command =
126-
let player = state.World.Squirrel
127-
let xDelta =
128-
match command with
129-
| MoveLeft | MoveDownLeft | MoveUpLeft -> -1
130-
| MoveRight | MoveDownRight | MoveUpRight -> 1
131-
| _ -> 0
132-
let yDelta =
133-
match command with
134-
| MoveUpLeft | MoveUp | MoveUpRight -> -1
135-
| MoveDownLeft | MoveDown | MoveDownRight -> 1
136-
| _ -> 0
137-
138-
let movedPos = {X=player.Pos.X + xDelta; Y=player.Pos.Y + yDelta}
139-
140-
if isValidPos movedPos state.World then
141-
moveActor state player movedPos
142-
else
143-
state
144-
145-
let playTurn state (getRandomNumber: int -> int) command =
146-
let world = state.World
147-
match command with
148-
| Restart -> { World = makeWorld world.MaxX world.MaxY getRandomNumber; SimState = SimulationState.Simulating; TurnsLeft = 30 }
149-
| _ ->
150-
match state.SimState with
151-
| SimulationState.Simulating ->
152-
let newState = handlePlayerCommand state command
153-
simulateActors newState getRandomNumber
154-
| _ -> state
155-
156-
let simulateTurn state command =
157-
if state.SimState = SimulationState.Simulating then
158-
let random = System.Random()
159-
let newState = handlePlayerCommand state command
160-
simulateActors(newState) random.Next
161-
else
162-
state
163-
164124
let handleBrainMove brain state (random: System.Random) =
165125
if state.SimState = SimulationState.Simulating then
166126
let current = state.World.Squirrel.Pos

MattEland.FSharpGeneticAlgorithm.Logic/WorldPos.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ let getDistance(a: WorldPos, b: WorldPos): float =
2020
let x2 = float(b.X)
2121
let y1 = float(a.Y)
2222
let y2 = float(b.Y)
23-
System.Math.Sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
23+
System.Math.Sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) // Calculate distance via C^2 = A^2 + B^2

MattEland.FSharpGeneticAlgorithm.Tests/Tests.fs

-28
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ open MattEland.FSharpGeneticAlgorithm.Logic.Actors
77
open MattEland.FSharpGeneticAlgorithm.Logic.World
88
open MattEland.FSharpGeneticAlgorithm.Logic.WorldGeneration
99
open MattEland.FSharpGeneticAlgorithm.Logic.Simulator
10-
open MattEland.FSharpGeneticAlgorithm.Logic.Commands
1110

1211
[<Theory>]
1312
[<InlineData(4, 2, 4, 1, true)>]
@@ -35,33 +34,6 @@ let ``Rabbit should move randomly`` () =
3534
// Assert
3635
newWorld.World.Rabbit.Pos |> should not' (equal originalPos)
3736

38-
[<Fact>]
39-
let ``Squirrel Getting Acorn Should Change how it Displays`` () =
40-
// Arrange
41-
let customSquirrel = {Pos=newPos 6 7; ActorKind = Squirrel false; IsActive = true}
42-
let testState = buildTestState
43-
let state: GameState = {testState with World = {testState.World with Squirrel = customSquirrel}}
44-
45-
// Act
46-
let newState = handlePlayerCommand state MoveLeft
47-
48-
// Assert
49-
newState.World.Squirrel.ActorKind |> should equal (Squirrel true)
50-
51-
[<Fact>]
52-
let ``Squirrel Getting Acorn to Tree Should Win Game`` () =
53-
// Arrange
54-
let customSquirrel = {Pos=newPos 9 10; ActorKind = Squirrel true; IsActive = true}
55-
let testState = buildTestState
56-
let state: GameState = {testState with World = {testState.World with Squirrel = customSquirrel}}
57-
58-
// Act
59-
let newState = handlePlayerCommand state MoveLeft
60-
61-
// Assert
62-
newState.World.Squirrel.Pos |> should equal state.World.Tree.Pos
63-
newState.SimState |> should equal SimulationState.Won
64-
6537
[<Fact>]
6638
let ``Dog Should Eat Squirrel If Adjacent`` () =
6739
// Arrange

MattEland.FSharpGeneticAlgorithm.WindowsClient/ActionCommand.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
using System;
22
using System.Windows.Input;
3+
using JetBrains.Annotations;
34

45
namespace MattEland.FSharpGeneticAlgorithm.WindowsClient
56
{
67
public class ActionCommand : ICommand
78
{
89
private readonly Action<object> _invokedAction;
910

11+
[PublicAPI]
1012
public ActionCommand(Action invokedAction)
1113
{
1214
_invokedAction = _ => invokedAction?.Invoke();
1315
}
1416

17+
[PublicAPI]
1518
public ActionCommand(Action<object> invokedAction)
1619
{
1720
_invokedAction = invokedAction;

MattEland.FSharpGeneticAlgorithm.WindowsClient/MainViewModel.cs

-31
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public MainViewModel()
1818
RandomizeCommand = new ActionCommand(RandomizeBrain);
1919
BrainCommand = new ActionCommand(GetArtificialIntelligenceMove);
2020
ResetCommand = new ActionCommand(Reset);
21-
MoveCommand = new ActionCommand(Move);
2221

2322
RandomizeBrain();
2423

@@ -31,7 +30,6 @@ private void RandomizeBrain()
3130
}
3231

3332
public ActionCommand RandomizeCommand { get; }
34-
public ActionCommand MoveCommand { get; }
3533

3634
public IEnumerable<ActorViewModel> Actors => _actors;
3735

@@ -40,35 +38,6 @@ private void GetArtificialIntelligenceMove()
4038
State = Simulator.handleBrainMove(_brain, _state, _random);
4139
}
4240

43-
private void Move(object direction)
44-
{
45-
// Parameter validation / cleansing
46-
direction ??= "";
47-
direction = direction.ToString().ToLowerInvariant();
48-
49-
// Translate from the command parameter to the GameCommand in F#
50-
Commands.GameCommand command = direction switch
51-
{
52-
"nw" => Commands.GameCommand.MoveUpLeft,
53-
"n" => Commands.GameCommand.MoveUp,
54-
"ne" => Commands.GameCommand.MoveUpRight,
55-
"w" => Commands.GameCommand.MoveLeft,
56-
"e" => Commands.GameCommand.MoveRight,
57-
"sw" => Commands.GameCommand.MoveDownLeft,
58-
"s" => Commands.GameCommand.MoveDown,
59-
"se" => Commands.GameCommand.MoveDownRight,
60-
_ => Commands.GameCommand.Wait
61-
};
62-
63-
HandlePlayerCommand(command);
64-
}
65-
66-
private void HandlePlayerCommand(Commands.GameCommand command)
67-
{
68-
// Process the action and update our new state
69-
State = Simulator.simulateTurn(_state, command);
70-
}
71-
7241
public ActionCommand ResetCommand { get; }
7342
public ActionCommand BrainCommand { get; }
7443

MattEland.FSharpGeneticAlgorithm.WindowsClient/MainWindow.xaml

-11
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,6 @@
2929
</StatusBar>
3030
<!-- Commands Area -->
3131
<StackPanel DockPanel.Dock="Right" Width="150">
32-
<UniformGrid Rows="3" Columns="3">
33-
<Button Style="{StaticResource MoveButtonStyle}" Command="{Binding MoveCommand}" CommandParameter="NW">NW</Button>
34-
<Button Style="{StaticResource MoveButtonStyle}" Command="{Binding MoveCommand}" CommandParameter="N">N</Button>
35-
<Button Style="{StaticResource MoveButtonStyle}" Command="{Binding MoveCommand}" CommandParameter="NE">NE</Button>
36-
<Button Style="{StaticResource MoveButtonStyle}" Command="{Binding MoveCommand}" CommandParameter="W">W</Button>
37-
<Button Style="{StaticResource MoveButtonStyle}" Command="{Binding MoveCommand}" CommandParameter="Wait">Wait</Button>
38-
<Button Style="{StaticResource MoveButtonStyle}" Command="{Binding MoveCommand}" CommandParameter="E">E</Button>
39-
<Button Style="{StaticResource MoveButtonStyle}" Command="{Binding MoveCommand}" CommandParameter="SW">SW</Button>
40-
<Button Style="{StaticResource MoveButtonStyle}" Command="{Binding MoveCommand}" CommandParameter="S">S</Button>
41-
<Button Style="{StaticResource MoveButtonStyle}" Command="{Binding MoveCommand}" CommandParameter="SE">SE</Button>
42-
</UniformGrid>
4332
<Button Style="{StaticResource CommandButtonStyle}" Command="{Binding BrainCommand}">AI Move</Button>
4433
<Button Style="{StaticResource CommandButtonStyle}" Command="{Binding RandomizeCommand}">Randomize Brain</Button>
4534
<Button Style="{StaticResource CommandButtonStyle}" Command="{Binding ResetCommand}">Restart</Button>

MattEland.FSharpGeneticAlgorithm.WindowsClient/MainWindow.xaml.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System.Windows;
2+
using JetBrains.Annotations;
23

34
namespace MattEland.FSharpGeneticAlgorithm.WindowsClient
45
{
56
/// <summary>
67
/// Interaction logic for MainWindow.xaml
78
/// </summary>
8-
public partial class MainWindow : Window
9+
[UsedImplicitly]
10+
public partial class MainWindow
911
{
1012
public MainWindow()
1113
{

MattEland.FSharpGeneticAlgorithm.WindowsClient/MattEland.FSharpGeneticAlgorithm.WindowsClient.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
<None Remove="Tree.png" />
1818
</ItemGroup>
1919

20+
<ItemGroup>
21+
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
22+
</ItemGroup>
23+
2024
<ItemGroup>
2125
<ProjectReference Include="..\MattEland.FSharpGeneticAlgorithm.Logic\MattEland.FSharpGeneticAlgorithm.Logic.fsproj" />
2226
</ItemGroup>

MattEland.FSharpGeneticAlgorithm.WindowsClient/NotifyPropertyChangedBase.cs

+2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System.ComponentModel;
22
using System.Runtime.CompilerServices;
3+
using JetBrains.Annotations;
34

45
namespace MattEland.FSharpGeneticAlgorithm.WindowsClient
56
{
67
internal abstract class NotifyPropertyChangedBase : INotifyPropertyChanged
78
{
89
public event PropertyChangedEventHandler PropertyChanged;
910

11+
[NotifyPropertyChangedInvocator]
1012
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
1113
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
1214
}

MattEland.FSharpGeneticAlgorithm.sln

-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ VisualStudioVersion = 16.0.29324.140
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "MattEland.FSharpGeneticAlgorithm.Logic", "MattEland.FSharpGeneticAlgorithm.Logic\MattEland.FSharpGeneticAlgorithm.Logic.fsproj", "{022CF0FD-4C4A-470F-BA5B-48D1FAAE8A1E}"
77
EndProject
8-
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "MattEland.FSharpGeneticAgorithm.ConsoleTestApp", "MattEland.FSharpGeneticAgorithm.ConsoleTestApp\MattEland.FSharpGeneticAgorithm.ConsoleTestApp.fsproj", "{B6D8C81B-288E-4B7D-BED2-B88888077390}"
9-
EndProject
108
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "MattEland.FSharpGeneticAlgorithm.Tests", "MattEland.FSharpGeneticAlgorithm.Tests\MattEland.FSharpGeneticAlgorithm.Tests.fsproj", "{4FBB57BA-F439-480D-81C7-3770D377EC28}"
119
EndProject
1210
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MattEland.FSharpGeneticAlgorithm.WindowsClient", "MattEland.FSharpGeneticAlgorithm.WindowsClient\MattEland.FSharpGeneticAlgorithm.WindowsClient.csproj", "{A9D0C5F9-3197-48AB-977B-6B323664252B}"
@@ -21,10 +19,6 @@ Global
2119
{022CF0FD-4C4A-470F-BA5B-48D1FAAE8A1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
2220
{022CF0FD-4C4A-470F-BA5B-48D1FAAE8A1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
2321
{022CF0FD-4C4A-470F-BA5B-48D1FAAE8A1E}.Release|Any CPU.Build.0 = Release|Any CPU
24-
{B6D8C81B-288E-4B7D-BED2-B88888077390}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25-
{B6D8C81B-288E-4B7D-BED2-B88888077390}.Debug|Any CPU.Build.0 = Debug|Any CPU
26-
{B6D8C81B-288E-4B7D-BED2-B88888077390}.Release|Any CPU.ActiveCfg = Release|Any CPU
27-
{B6D8C81B-288E-4B7D-BED2-B88888077390}.Release|Any CPU.Build.0 = Release|Any CPU
2822
{4FBB57BA-F439-480D-81C7-3770D377EC28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2923
{4FBB57BA-F439-480D-81C7-3770D377EC28}.Debug|Any CPU.Build.0 = Debug|Any CPU
3024
{4FBB57BA-F439-480D-81C7-3770D377EC28}.Release|Any CPU.ActiveCfg = Release|Any CPU

0 commit comments

Comments
 (0)