-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameOfLife.cs
108 lines (91 loc) · 3.04 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
using System;
using System.Text;
namespace GameOfLife
{
public class GameOfLife
{
public GameOfLife(int height, int width)
{
/* if (height<1 || width<1 )
{
throw new NotImplementedException();
}
Height = height;
Width = width;
Cell[][] TempCells = new Cell[Height][];
for (int h = 0; h <= Height-1; h++)
{
TempCells[h] = new Cell[Width];
for (int w = 0; w <= Width-1; w++)
{
TempCells[h][w] = Cell.Dead;
}
}
Cells = TempCells;*/
//
}
public int Height { get; set; }
public int Width { get; set; }
// public Cell[][] Cells { get; set; }
const char aliveCellChar = '\u2588';
public void TakeTurn()
{
/* var tempCells = Cells.Select(x => x.ToArray()).ToArray();
for (int h = 0; h <= Height - 1; h++)
{
for (int w = 0; w <= Width - 1; w++)
{
int aliveCount = CountLivingNeighbours(h, w);
if (aliveCount < 2 || aliveCount > 3)
{
tempCells[h][w] = Cell.Dead;
}
else if(aliveCount == 3)
{
tempCells[h][w] = Cell.Alive;
}
}
}
Cells = tempCells;*/
}
public int CountLivingNeighbours(int xC, int yC)
{
int live = 0;
/*if (xC-1 > -1)
{
if(yC-1 > -1 && Cells[xC-1][yC-1]==Cell.Alive){live++;}
if (Cells[xC - 1][yC] == Cell.Alive){live++;}
if (yC+1 < Width && Cells[xC - 1][yC+1] == Cell.Alive){live++;}
}
if (yC - 1 > -1 && Cells[xC][yC - 1] == Cell.Alive){live++;}
if (yC + 1 < Width && Cells[xC][yC + 1] == Cell.Alive){live++;}
if (xC + 1 < Height)
{
if (yC - 1 > -1 && Cells[xC +1][yC - 1] == Cell.Alive){live++;}
if (Cells[xC + 1][yC] == Cell.Alive){live++;}
if (yC + 1 < Width && Cells[xC + 1][yC + 1] == Cell.Alive){live++;}
}*/
return live;
}
public override string ToString()
{
StringBuilder output = new StringBuilder();
for (int h = 0; h <= Height - 1; h++)
{
for (int w = 0; w <= Width - 1; w++)
{
/*if (this.Cells[h][w] == Cell.Alive)
{
output.Append(aliveCellChar);
}
else
{
output.Append(" ");
}*/
}
output.AppendLine();
}
return output.ToString();
}
}
}