-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameofLife.cs
363 lines (330 loc) · 17.1 KB
/
GameofLife.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System;
namespace Conway
{
public class GameofLife : Game
{
private int GridSize; //The size of the grid/ zoom level
public static int ChunkSize; //The size of one chunk
public int CellScale; //How large the Cell's are
private Texture2D CellTexure; //The texture Cell's render with
public static List<Cell> AliveCells; //Buffer of all alive Cell's
public static List<Cell> SpawnBuffer; //Cell's awaiting to be created
public static List<Cell> KillBuffer; //Cell's to be terminated
public static List<(int, int)> CheckedDeadCells; //Preventing checking same cell multiple times
//private static (int, int) lastSummonedCell = (0, 0); //For debugging
List<(int, int)> _blacklistedCells = new List<(int, int)>(); //List of already checked cells; For use in resurrecting
private static Vector2 CameraPosition = new Vector2(0, 0);
private int _oldMouseWheelValue = Mouse.GetState().ScrollWheelValue;
private Stopwatch DeltaTime = new Stopwatch();
private Random _random = new Random();
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
//Return an alive Cell at the grid or else null
//Cell isAlive( (int, int) position ) { return new Thread( () => isAliveThreaded( (1,1))); }
Cell isAlive( (int, int) position) {
Chunk[] chunks = new Chunk[9];
List<Cell> cells;
{ //Add each grid to check
chunks[0] = (Chunk.GetChunk((position.Item1 - 0, position.Item2 - 0)));
chunks[1] = (Chunk.GetChunk((chunks[0].Position.Item1 + 0, chunks[0].Position.Item2 - 1), true));
chunks[2] = (Chunk.GetChunk((chunks[0].Position.Item1 + 1, chunks[0].Position.Item2 - 1), true));
chunks[3] = (Chunk.GetChunk((chunks[0].Position.Item1 - 1, chunks[0].Position.Item2 + 0), true));
chunks[4] = (Chunk.GetChunk((chunks[0].Position.Item1 + 1, chunks[0].Position.Item2 + 0), true));
chunks[5] = (Chunk.GetChunk((chunks[0].Position.Item1 - 1, chunks[0].Position.Item2 + 1), true));
chunks[6] = (Chunk.GetChunk((chunks[0].Position.Item1 + 0, chunks[0].Position.Item2 + 1), true));
chunks[7] = (Chunk.GetChunk((chunks[0].Position.Item1 + 1, chunks[0].Position.Item2 + 1), true));
chunks[8] = (Chunk.GetChunk((chunks[0].Position.Item1 - 1, chunks[0].Position.Item2 - 1), true));
}
foreach (Chunk chunk in chunks)
{
foreach (Cell cell in chunk.GetCells()) cells.Add(cell)
{
}
foreach (Cell cell in cells)
{ if (cell._position == position) return cell; }
}
return null;
}
//Return neighbors of a cell or empty array if none
Cell[] GridNeighbours ( (int, int) position)
{
List<Cell> _nearCells = new List<Cell>();
(int, int)[] _neighbours = new (int, int)[8];
{ //Add each grid to check
_neighbours[0] = (position.Item1 - 1, position.Item2 - 1);
_neighbours[1] = (position.Item1 + 0, position.Item2 - 1);
_neighbours[2] = (position.Item1 + 1, position.Item2 - 1);
_neighbours[3] = (position.Item1 - 1, position.Item2 + 0);
_neighbours[4] = (position.Item1 + 1, position.Item2 + 0);
_neighbours[5] = (position.Item1 - 1, position.Item2 + 1);
_neighbours[6] = (position.Item1 + 0, position.Item2 + 1);
_neighbours[7] = (position.Item1 + 1, position.Item2 + 1);
}
foreach ( (int, int) _grid in _neighbours) {
if (isAlive(_grid) != null) _nearCells.Add(isAlive(_grid));
//else Resurrect(_grid);
}
return _nearCells.ToArray();
}
void Resurrect( (int, int) position)
{
if (isAlive(position) != null) return;
int _aliveNeighbours = 0;
(int, int)[] _neighbours = new (int, int)[8];
{ //Add each grid to check
_neighbours[0] = (position.Item1 - 1, position.Item2 - 1);
_neighbours[1] = (position.Item1 + 0, position.Item2 - 1);
_neighbours[2] = (position.Item1 + 1, position.Item2 - 1);
_neighbours[3] = (position.Item1 - 1, position.Item2 + 0);
_neighbours[4] = (position.Item1 + 1, position.Item2 + 0);
_neighbours[5] = (position.Item1 - 1, position.Item2 + 1);
_neighbours[6] = (position.Item1 + 0, position.Item2 + 1);
_neighbours[7] = (position.Item1 + 1, position.Item2 + 1);
}
foreach ( (int, int) _grid in _neighbours) if (isAlive(_grid) != null) _aliveNeighbours++;
if (_aliveNeighbours == 3) SpawnBuffer.Add(new Cell(position)); Console.WriteLine("Summoned new cell at {0} {1}", position, isAlive(position));
}
public GameofLife()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
CellScale = 5;
GridSize = 100;
ChunkSize = 20;
AliveCells = new List<Cell>();
SpawnBuffer = new List<Cell>();
KillBuffer = new List<Cell>();
CheckedDeadCells = new List<(int, int)> ();
//Initialize and start cameraThread
Thread CameraThread = new Thread(cameraThread);
CameraThread.Start();
//Create the 'aircraft carrier'
/*AliveCells.Add(new Cell( (5, 5) ) );
AliveCells.Add(new Cell( (6, 5) ) );
AliveCells.Add(new Cell( (5, 6) ) );
AliveCells.Add(new Cell( (8, 7) ) );
AliveCells.Add(new Cell( (8, 8) ) );
AliveCells.Add(new Cell( (7, 8) ) );
AliveCells.Add(new Cell( (6, 6) ) );*/
//Create a replicator
/*AliveCells.Add(new Cell( (3, 3) ) );
AliveCells.Add(new Cell( (4, 3) ) );
AliveCells.Add(new Cell( (5, 3) ) );
AliveCells.Add(new Cell( (3, 4) ) );
AliveCells.Add(new Cell( (5, 4) ) );
AliveCells.Add(new Cell( (3, 5) ) );
AliveCells.Add(new Cell( (4, 5) ) );
AliveCells.Add(new Cell( (5, 5) ) );*/
//Create a replicator
AliveCells.Add(new Cell( (100, 100) ) );
AliveCells.Add(new Cell( (103, 100) ) );
AliveCells.Add(new Cell( (100, 101) ) );
AliveCells.Add(new Cell( (101, 101) ) );
AliveCells.Add(new Cell( (102, 101) ) );
AliveCells.Add(new Cell( (101, 102) ) );
_graphics.PreferredBackBufferWidth = 1280;
_graphics.PreferredBackBufferHeight = 920;
_graphics.ApplyChanges();
IsMouseVisible = true;
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
refreshTextures();
}
protected override void Update(GameTime gameTime)
{
//Spawn cells when spacebar is held
if (Keyboard.GetState().IsKeyDown(Keys.Space)) {
for (int _index = 0; _index < 600; _index++)
{
(int, int) _newCoords;
do {
_newCoords = (_random.Next(0, GridSize), _random.Next(0, GridSize));
} while (isAlive(_newCoords) != null);
AliveCells.Add(new Cell(_newCoords));
}
}
//Check simulation if enough time has passed
DeltaTime.Start(); //Make sure timer is started
if (DeltaTime.Elapsed > TimeSpan.FromMilliseconds(20))
{ DeltaTime.Reset(); //Reset the delta
//Before anything else: remove duplicates
{
List<Cell> _checkedCells = new List<Cell>();
int _targetIndex = AliveCells.Count;
for (int _index = 0; _index < _targetIndex; _index++)
{
bool _valid = true;
foreach (Cell _cellChecked in _checkedCells)
{
if (_cellChecked._position == AliveCells[_index]._position) {
AliveCells.Remove(AliveCells[_index]);
_targetIndex--; _index--; _valid = false;
break;
}
}
if (_valid) _checkedCells.Add(AliveCells[_index]);
}
}
//Check which cells gets killed
foreach (Cell _cell in AliveCells)
{
Cell[] _neighbours = GridNeighbours(_cell._position);
if (_neighbours.Length < 2 || _neighbours.Length > 3) KillBuffer.Add(_cell);
}
//Check which neighbours should be resurrected (if there are any)
if (AliveCells.Count > 0)
{
_blacklistedCells.Clear(); //Clear the blacklisted cells before beginning
List<Thread> _concurrentThreads = new List<Thread>();
int _threads = (int)Math.Ceiling((double)AliveCells.Count / 100);
for (int _index = 0; _index < _threads; _index++) //Check each cell to have neighbours resurrected
{
int _endRange;
int _startRange = _index * 100;
if (_index * 100 + 99 > AliveCells.Count) _endRange = AliveCells.Count;
else _endRange = _index * 100 + 99;
Cell[] _cellArray = new Cell[_endRange - _startRange];
for (int _field = 0; _field < _cellArray.Length; _field++)
_cellArray[_field] = AliveCells[_index * 100 + _field];
_concurrentThreads.Add(new Thread(() => resurrectMethod(_cellArray)));
_concurrentThreads[_index].Start();
}
foreach (Thread _thread in _concurrentThreads)
_thread.Join(); //Wait for all threads to finalize
}
//If _spawn || _kill buffers != empty then
//TODO: Make it not start a thread when under threshold
if (SpawnBuffer.Count != 0)
/*{
List<Thread> _concurrentThreads = new List<Thread>();
int _threads = (int)Math.Ceiling((double)SpawnBuffer.Count / 400);
for (int _index = 0; _index < _threads; _index++)
{
_concurrentThreads.Add(new Thread(() => spawnBufferMethod(_index * 400, _index * 400 + 399)));
_concurrentThreads[_index].Start();
}
foreach (Thread _thread in _concurrentThreads)
_thread.Join(); //Wait for all threads to finalize
SpawnBuffer.Clear(); //Clear buffer
}*/
{
foreach (Cell _cell in SpawnBuffer)
{
AliveCells.Add(_cell);
} SpawnBuffer.Clear(); //Clear buffer
}
if (KillBuffer.Count != 0)
{
foreach (Cell _cell in KillBuffer)
{
if (!AliveCells.Remove(_cell)) Console.WriteLine("FAILED TO KILL CELL {0}!", _cell._position);
//Console.WriteLine("Killed cell at {0}", _cell._position);
} KillBuffer.Clear(); //Clear buffer
}
//End of update block
}
base.Update(gameTime);
}
void resurrectMethod(Cell[] cellArray)
{
foreach (Cell cell in cellArray)
{
(int, int)[] _neighbours = new (int, int)[8]; //Save this cells neighbouring positions
{ //Add each grid to check
_neighbours[0] = (cell._position.Item1 - 1, cell._position.Item2 - 1);
_neighbours[1] = (cell._position.Item1 + 0, cell._position.Item2 - 1);
_neighbours[2] = (cell._position.Item1 + 1, cell._position.Item2 - 1);
_neighbours[3] = (cell._position.Item1 - 1, cell._position.Item2 + 0);
_neighbours[4] = (cell._position.Item1 + 1, cell._position.Item2 + 0);
_neighbours[5] = (cell._position.Item1 - 1, cell._position.Item2 + 1);
_neighbours[6] = (cell._position.Item1 + 0, cell._position.Item2 + 1);
_neighbours[7] = (cell._position.Item1 + 1, cell._position.Item2 + 1);
}
foreach ((int, int) _neighbourCell in _neighbours) { //For each neighbour...:
if (isAlive(_neighbourCell) != null) continue; //Skip if the cell is already alive
bool _isChecked = false;
for (int _index = 0; _index < _blacklistedCells.Count; _index++) { //Check if this cell has already been checked
if (_neighbourCell == _blacklistedCells[_index]) _isChecked = true; break; //If so, skip
} if (_isChecked) continue; //Else:
if (GridNeighbours(_neighbourCell).Length == 3) { //Does it have 3 neighbours?
SpawnBuffer.Add(new Cell(_neighbourCell));
//Console.WriteLine("Summoned Cell at {0}", _neighbourCell);
}
_blacklistedCells.Add(_neighbourCell);
}
}
}
void spawnBufferMethod(int startRange, int endRange)
{
if (endRange > SpawnBuffer.Count) endRange = SpawnBuffer.Count;
for (int _index = startRange; _index < endRange; _index++)
{
AliveCells.Add(SpawnBuffer[_index]);
}
}
void refreshTextures() //Make new textures using the scales
{
CellTexure = new Texture2D(GraphicsDevice, CellScale, CellScale); //Declare texture
Color[] _textureColor = new Color[CellScale * CellScale]; //New Color[] to fill texture with
for (int _index = 0; _index < _textureColor.Length; _index++) //Populate _textureColor
{ _textureColor[_index] = Color.Black; }
for (int _index = 0; _index < CellScale * CellScale; _index++) { //Fill texture with _textureColor
CellTexure.SetData<Color>(_textureColor);
}
}
void cameraThread()
{
Stopwatch _stopWatch = new Stopwatch();
_stopWatch.Start();
while (true)
{
CameraPosition.X += 1 * _stopWatch.ElapsedMilliseconds;
if (Keyboard.GetState().IsKeyDown(Keys.D)) CameraPosition.X -= 1 * _stopWatch.ElapsedMilliseconds;
if (Keyboard.GetState().IsKeyDown(Keys.W)) CameraPosition.Y += 1 * _stopWatch.ElapsedMilliseconds;
if (Keyboard.GetState().IsKeyDown(Keys.S)) CameraPosition.Y -= 1 * _stopWatch.ElapsedMilliseconds;
if (Mouse.GetState().ScrollWheelValue > _oldMouseWheelValue && CellScale < 10) //If the wheel has increased
{
CellScale++;
//Math.Clamp(CellScale, 1, 10); //Make sure it does not enter extremes
refreshTextures();
}
else if (Mouse.GetState().ScrollWheelValue < _oldMouseWheelValue && CellScale > 1) //If the wheel has decreased
{
CellScale--;
//Math.Clamp(CellScale, 1, 10); //Make sure it does not enter extremes
refreshTextures();
}
_oldMouseWheelValue = Mouse.GetState().ScrollWheelValue; //Set new _old value for mouse wheel
//Console.WriteLine(Mouse.GetState().ScrollWheelValue);
//Console.WriteLine(CellScale);
_stopWatch.Reset();
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Wheat);
_spriteBatch.Begin();
try {
foreach (Cell element in AliveCells)
{
Vector2 _position = new Vector2(element._position.Item1*CellScale, element._position.Item2*CellScale);
_spriteBatch.Draw(CellTexure, _position + CameraPosition, Color.White);
} } catch {}
_spriteBatch.End();
base.Draw(gameTime);
}
}
}