Skip to content

Commit 1bf5cd9

Browse files
committed
Refactored out the static method on RandomGridSeeder and created an interface.
1 parent 274439e commit 1bf5cd9

File tree

9 files changed

+26
-34
lines changed

9 files changed

+26
-34
lines changed

ConsoleApplication/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class Program
1414
static void Main(string[] args)
1515
{
1616
var grid = new LifeGrid(NumberOfRows, NumberOfColumns);
17-
RandomGridSeeder.Seed(grid);
17+
var randomGridSeeder = new RandomGridSeeder();
18+
randomGridSeeder.Seed(grid);
1819

1920
while (true)
2021
{

Core/GameOfLife.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
</ItemGroup>
4444
<ItemGroup>
4545
<Compile Include="Coordinate.cs" />
46+
<Compile Include="IGridSeeder.cs" />
4647
<Compile Include="LifeGrid.cs" />
4748
<Compile Include="Properties\AssemblyInfo.cs" />
4849
<Compile Include="RandomGridSeeder.cs" />

Core/IGridSeeder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace GameOfLife.Core
2+
{
3+
public interface IGridSeeder
4+
{
5+
void Seed(LifeGrid grid);
6+
}
7+
}

Core/RandomGridSeeder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace GameOfLife.Core
44
{
5-
public class RandomGridSeeder
5+
public class RandomGridSeeder : IGridSeeder
66
{
7-
public static void Seed(LifeGrid grid)
7+
public void Seed(LifeGrid grid)
88
{
9-
var numberOfCells = grid.NumberOfRows * grid.NumberOfColumns;
109
var random = new Random();
1110

1211
for (var x = 0; x < grid.NumberOfColumns; x++)

SharpGlWpfApplication/MainWindow.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public MainWindow()
3838
private void StartSimulation()
3939
{
4040
lifeGrid = new LifeGrid(NumberOfRows, NumberOfColumns);
41-
RandomGridSeeder.Seed(lifeGrid);
41+
var randomGridSeeder = new RandomGridSeeder();
42+
randomGridSeeder.Seed(lifeGrid);
4243
timer.Start();
4344
}
4445

SignalRApplication/GameOfLifeExecutor.cs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ namespace SignalRApplication
1010
public class GameOfLifeExecutor
1111
{
1212
private readonly static Lazy<GameOfLifeExecutor> instance = new Lazy<GameOfLifeExecutor>(() => new GameOfLifeExecutor(GlobalHost.ConnectionManager.GetHubContext<GameOfLifeHub>().Clients));
13-
private readonly static LifeGrid lifeGrid = new LifeGrid(50, 50);
13+
private readonly static Lazy<LifeGrid> lifeGrid = new Lazy<LifeGrid>(() => new LifeGrid(150, 150));
14+
private readonly static Lazy<IGridSeeder> gridSeeder = new Lazy<IGridSeeder>(() => new RandomGridSeeder());
1415

1516
private readonly TimeSpan updateInterval = TimeSpan.FromMilliseconds(250);
16-
private readonly Object updateLivingCellsLock = new Object();
1717
private readonly Timer timer;
18-
private volatile Boolean updatingLivingCells = false;
1918

2019
private GameOfLifeExecutor(IHubConnectionContext clients)
2120
{
2221
Clients = clients;
23-
24-
RandomGridSeeder.Seed(lifeGrid);
25-
22+
gridSeeder.Value.Seed(lifeGrid.Value);
2623
timer = new Timer(UpdateLivingCells, null, updateInterval, updateInterval);
2724
}
2825

@@ -39,34 +36,18 @@ private IHubConnectionContext Clients
3936

4037
public IEnumerable<Coordinate> GetLivingCells()
4138
{
42-
return lifeGrid.GetLivingCells();
39+
return lifeGrid.Value.GetLivingCells();
4340
}
4441

4542
private void UpdateLivingCells(Object state)
4643
{
47-
lock (updateLivingCellsLock)
48-
{
49-
if (!updatingLivingCells)
50-
{
51-
updatingLivingCells = true;
52-
lifeGrid.Tick();
53-
Clients.All.updateLivingCells(lifeGrid.GetLivingCells());
54-
updatingLivingCells = false;
55-
}
56-
}
44+
lifeGrid.Value.Tick();
45+
Clients.All.updateLivingCells(lifeGrid.Value.GetLivingCells());
5746
}
5847

5948
public void Restart()
6049
{
61-
lock (updateLivingCellsLock)
62-
{
63-
if (!updatingLivingCells)
64-
{
65-
updatingLivingCells = true;
66-
RandomGridSeeder.Seed(lifeGrid);
67-
updatingLivingCells = false;
68-
}
69-
}
50+
gridSeeder.Value.Seed(lifeGrid.Value);
7051
}
7152
}
7253
}

SignalRApplication/Scripts/game-of-life.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
context.fillStyle = 'black';
1212
context.fill();
1313
context.lineWidth = 1;
14-
context.strokeStyle = 'black';
14+
context.strokeStyle = 'white';
1515
context.stroke();
1616
});
1717
};

UnitTests/LifeGridSpeedTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public class LifeGridSpeedTests
1414
public void TestInitialize()
1515
{
1616
grid = new LifeGrid(1000, 1000);
17-
RandomGridSeeder.Seed(grid);
17+
var randomGridSeeder = new RandomGridSeeder();
18+
randomGridSeeder.Seed(grid);
1819
}
1920

2021
[TestMethod]

WpfApplication/MainWindow.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ protected void backgroundWorker_ProgressChanged(Object sender, ProgressChangedEv
8787
protected void backgroundWorker_DoWork(Object sender, DoWorkEventArgs e)
8888
{
8989
var grid = new LifeGrid(NumberOfRows, NumberOfColumns);
90-
RandomGridSeeder.Seed(grid);
90+
var randomGridSeeder = new RandomGridSeeder();
91+
randomGridSeeder.Seed(grid);
9192

9293
while (true)
9394
{

0 commit comments

Comments
 (0)