-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathLifeGameLoop.cs
250 lines (213 loc) · 7.91 KB
/
LifeGameLoop.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Cysharp.Threading;
using Microsoft.Extensions.Logging;
namespace LoopHostingApp
{
public class LifeGameLoop
{
private static int _gameLoopSeq = 0;
public static ConcurrentBag<LifeGameLoop> All { get; } = new ConcurrentBag<LifeGameLoop>();
private readonly ILogger _logger;
public World World { get; }
public int Id { get; }
/// <summary>
/// Create a new life-game loop and register into the LooperPool.
/// </summary>
/// <param name="looperPool"></param>
/// <param name="logger"></param>
public static void CreateNew(ILogicLooperPool looperPool, ILogger logger)
{
var gameLoop = new LifeGameLoop(logger);
looperPool.RegisterActionAsync(gameLoop.UpdateFrame);
}
private LifeGameLoop(ILogger logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
Id = Interlocked.Increment(ref _gameLoopSeq);
World = new World(64, 64);
World.SetPattern(Patterns.GliderGun, 10, 10);
_logger.LogInformation($"{nameof(LifeGameLoop)}[{Id}]: Register");
All.Add(this);
}
public bool UpdateFrame(in LogicLooperActionContext ctx)
{
if (ctx.CancellationToken.IsCancellationRequested)
{
// If LooperPool begins shutting down, IsCancellationRequested will be `true`.
_logger.LogInformation($"{nameof(LifeGameLoop)}[{Id}]: Shutdown");
return false;
}
// Update the world every update cycle.
World.Update();
return World.AliveCount != 0;
}
}
public static class Patterns
{
// glider pattern
public static readonly int[,] Glider = new[,]
{
{ 1, 1, 1 },
{ 1, 0, 0 },
{ 0, 1, 0 }
};
public static readonly int[,] GliderGun = new[,]
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
{ 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
}
public class World
{
private readonly Cell[] _cells;
private readonly int _width;
private readonly int _height;
public bool[,] Snapshot { get; private set; }
public int AliveCount { get; private set; }
public World(int width, int height)
{
_width = width;
_height = height;
_cells = new Cell[width * height];
for (var x = 0; x < width; x++)
{
for (var y = 0; y < height; y++)
{
_cells[x + (y * width)] = new Cell();
}
}
}
public void Set(int x, int y, bool isAlive)
{
if (!TryGetCell(x, y, out var cell))
{
throw new ArgumentOutOfRangeException();
}
cell.IsAlive = isAlive;
}
public void SetPattern(int[,] pattern, int offsetX, int offsetY)
{
for (var x = 0; x <= pattern.GetUpperBound(0); x++)
{
for (var y = 0; y <= pattern.GetUpperBound(1); y++)
{
Set(offsetX + x, offsetY + y, pattern[x, y] == 1);
}
}
}
public void Update()
{
var count = 0;
for (var y = 0; y < _height; y++)
{
for (var x = 0; x < _width; x++)
{
if (TryGetCell(x, y, out var cell))
{
cell.NestState = GetCellNextState(x, y) switch
{
CellState.Alive => true,
CellState.Dead => false,
CellState.Remain => cell.IsAlive,
_ => throw new ArgumentOutOfRangeException()
};
}
}
}
// Apply new state and take a snapshot.
var snapshot = new bool[_width, _height];
for (var y = 0; y < _height; y++)
{
for (var x = 0; x < _width; x++)
{
if (TryGetCell(x, y, out var cell))
{
cell.IsAlive = cell.NestState;
if (cell.IsAlive)
{
count++;
}
snapshot[x, y] = cell.IsAlive;
}
}
}
AliveCount = count;
Snapshot = snapshot;
}
private enum CellState
{
Alive,
Dead,
Remain,
}
private CellState GetCellNextState(int x, int y)
{
var livingCellsCount = 0;
for (var x2 = x - 1; x2 <= x + 1; x2++)
{
for (var y2 = y - 1; y2 <= y + 1; y2++)
{
if (x2 == x && y2 == y) continue;
if (TryGetCell(x2, y2, out var cell) && cell.IsAlive)
{
livingCellsCount++;
}
}
}
return livingCellsCount == 2
? CellState.Remain
: livingCellsCount == 3
? CellState.Alive
: CellState.Dead;
}
private bool TryGetCell(int x, int y, out Cell cell)
{
if (x >= _width || y >= _height || x < 0 || y < 0)
{
cell = null;
return false;
}
cell = _cells[x + (y * _width)];
return true;
}
public override string ToString()
{
var sb = new StringBuilder();
for (var y = 0; y < _height; y++)
{
for (var x = 0; x < _width; x++)
{
if (TryGetCell(x, y, out var cell))
{
sb.Append(cell.IsAlive ? "x" : ".");
}
}
sb.AppendLine();
}
return sb.ToString();
}
public void Dump()
{
Console.WriteLine(Snapshot);
Console.WriteLine();
}
public class Cell
{
public bool IsAlive { get; set; }
public bool NestState { get; set; }
}
}
}