Skip to content

Commit bdf6b0b

Browse files
committed
Extracted a GameStateViewModel
1 parent d089e48 commit bdf6b0b

File tree

5 files changed

+62
-38
lines changed

5 files changed

+62
-38
lines changed

MattEland.FSharpGeneticAlgorithm.WindowsClient/MainWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</Window.Resources>
1616
<DockPanel>
1717
<!-- Status Bar -->
18-
<StatusBar DockPanel.Dock="Bottom" Background="{Binding GameStatusBrush}" FontWeight="Bold">
18+
<StatusBar DockPanel.Dock="Bottom" DataContext="{Binding State}" Background="{Binding GameStatusBrush}" FontWeight="Bold">
1919
<StatusBarItem>
2020
<TextBlock Text="{Binding TurnsLeftText}"></TextBlock>
2121
</StatusBarItem>
@@ -33,7 +33,7 @@
3333
</StackPanel>
3434
</Border>
3535
<!-- Main UI -->
36-
<Grid Margin="10" Background="Green">
36+
<Grid Margin="10" Background="Green" DataContext="{Binding State}">
3737
<Image Source="Images/Grass.png" Stretch="Fill" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Image>
3838
<Viewbox Stretch="Uniform">
3939
<Border BorderThickness="1" BorderBrush="DarkGreen">

MattEland.FSharpGeneticAlgorithm.WindowsClient/ViewModels/ActorViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MattEland.FSharpGeneticAlgorithm.WindowsClient.ViewModels
44
{
5-
internal class ActorViewModel
5+
public class ActorViewModel
66
{
77
private readonly Actors.Actor _actor;
88

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Windows.Media;
5+
using JetBrains.Annotations;
6+
using MattEland.FSharpGeneticAlgorithm.Logic;
7+
8+
namespace MattEland.FSharpGeneticAlgorithm.WindowsClient.ViewModels
9+
{
10+
/// <summary>
11+
/// Represents a single state in a single squirrel's simulation run
12+
/// </summary>
13+
public class GameStateViewModel : NotifyPropertyChangedBase
14+
{
15+
private readonly Simulator.GameState _state;
16+
17+
public GameStateViewModel([NotNull] Simulator.GameState state)
18+
{
19+
_state = state ?? throw new ArgumentNullException(nameof(state));
20+
21+
foreach (var actor in _state.World.Actors.Where(a => a.IsActive))
22+
{
23+
Actors.Add(new ActorViewModel(actor));
24+
}
25+
}
26+
27+
public ICollection<ActorViewModel> Actors { get; } = new List<ActorViewModel>();
28+
29+
public string GameStatusText => _state.SimState switch
30+
{
31+
Simulator.SimulationState.Won => "Won",
32+
Simulator.SimulationState.Lost => "Lost",
33+
_ => "Simulating"
34+
};
35+
36+
public Brush GameStatusBrush => _state.SimState switch
37+
{
38+
Simulator.SimulationState.Won => Brushes.MediumSeaGreen,
39+
Simulator.SimulationState.Lost => Brushes.LightCoral,
40+
_ => Brushes.LightGray
41+
};
42+
43+
public string TurnsLeftText => _state.TurnsLeft == 1
44+
? "1 Turn Left"
45+
: $"{_state.TurnsLeft} Turns Left";
46+
}
47+
}

MattEland.FSharpGeneticAlgorithm.WindowsClient/ViewModels/MainViewModel.cs

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.ObjectModel;
42
using System.Linq;
5-
using System.Windows.Media;
63
using MattEland.FSharpGeneticAlgorithm.Genetics;
74
using MattEland.FSharpGeneticAlgorithm.Logic;
85

@@ -34,8 +31,6 @@ public BrainInfoViewModel Brain
3431

3532
public ActionCommand RandomizeCommand { get; }
3633

37-
public IEnumerable<ActorViewModel> Actors => _actors;
38-
3934
private void RandomizeBrain()
4035
{
4136
Brain = new BrainInfoViewModel(Genes.getRandomChromosome(_random));
@@ -56,43 +51,25 @@ public Simulator.GameState[] GameResult
5651

5752
_gameResult = value;
5853

59-
State = value.Last();
60-
61-
_actors.Clear();
62-
foreach (var actor in State.World.Actors.Where(a => a.IsActive))
63-
{
64-
_actors.Add(new ActorViewModel(actor));
65-
}
66-
67-
// Notify all properties changed
68-
OnPropertyChanged(string.Empty);
54+
State = new GameStateViewModel(value.Last());
6955
}
7056
}
7157

7258
public ActionCommand BrainCommand { get; }
7359

74-
private readonly ObservableCollection<ActorViewModel> _actors = new ObservableCollection<ActorViewModel>();
7560
private BrainInfoViewModel _brain;
7661
private Simulator.GameState[] _gameResult;
62+
private GameStateViewModel _state;
7763

78-
public Simulator.GameState State { get; private set; }
79-
80-
public string GameStatusText => State.SimState switch
81-
{
82-
Simulator.SimulationState.Won => "Won",
83-
Simulator.SimulationState.Lost => "Lost",
84-
_ => "Simulating"
85-
};
86-
87-
public Brush GameStatusBrush => State.SimState switch
64+
public GameStateViewModel State
65+
{
66+
get => _state;
67+
private set
8868
{
89-
Simulator.SimulationState.Won => Brushes.MediumSeaGreen,
90-
Simulator.SimulationState.Lost => Brushes.LightCoral,
91-
_ => Brushes.LightGray
92-
};
93-
94-
public string TurnsLeftText => State.TurnsLeft == 1
95-
? "1 Turn Left"
96-
: $"{State.TurnsLeft} Turns Left";
69+
if (Equals(value, _state)) return;
70+
_state = value;
71+
OnPropertyChanged();
72+
}
73+
}
9774
}
9875
}

MattEland.FSharpGeneticAlgorithm.WindowsClient/ViewModels/NotifyPropertyChangedBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace MattEland.FSharpGeneticAlgorithm.WindowsClient.ViewModels
66
{
7-
internal abstract class NotifyPropertyChangedBase : INotifyPropertyChanged
7+
public abstract class NotifyPropertyChangedBase : INotifyPropertyChanged
88
{
99
public event PropertyChangedEventHandler PropertyChanged;
1010

0 commit comments

Comments
 (0)