-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
197 lines (185 loc) · 7.05 KB
/
Copy pathProgram.cs
File metadata and controls
197 lines (185 loc) · 7.05 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using SkiaSharp;
namespace cli_life
{
public class Cell
{
public bool IsAlive;
public readonly List<Cell> neighbors = new List<Cell>();
private bool IsAliveNext;
public void DetermineNextLiveState()
{
int liveNeighbors = neighbors.Where(x => x.IsAlive).Count();
if (IsAlive)
IsAliveNext = liveNeighbors == 2 || liveNeighbors == 3;
else
IsAliveNext = liveNeighbors == 3;
}
public void Advance()
{
IsAlive = IsAliveNext;
}
}
public class Board
{
public readonly Cell[,] Cells;
public readonly int CellSize;
public int Columns { get { return Cells.GetLength(0); } }
public int Rows { get { return Cells.GetLength(1); } }
public int Width { get { return Columns * CellSize; } }
public int Height { get { return Rows * CellSize; } }
public Board(int width, int height, int cellSize, double liveDensity = .1)
{
CellSize = cellSize;
Cells = new Cell[width / cellSize, height / cellSize];
for (int x = 0; x < Columns; x++)
for (int y = 0; y < Rows; y++)
Cells[x, y] = new Cell();
ConnectNeighbors();
Randomize(liveDensity);
}
readonly Random rand = new Random();
public void Randomize(double liveDensity)
{
foreach (var cell in Cells)
cell.IsAlive = rand.NextDouble() < liveDensity;
}
public void Advance()
{
foreach (var cell in Cells)
cell.DetermineNextLiveState();
foreach (var cell in Cells)
cell.Advance();
}
private void ConnectNeighbors()
{
for (int x = 0; x < Columns; x++)
{
for (int y = 0; y < Rows; y++)
{
int xL = (x > 0) ? x - 1 : Columns - 1;
int xR = (x < Columns - 1) ? x + 1 : 0;
int yT = (y > 0) ? y - 1 : Rows - 1;
int yB = (y < Rows - 1) ? y + 1 : 0;
Cells[x, y].neighbors.Add(Cells[xL, yT]);
Cells[x, y].neighbors.Add(Cells[x, yT]);
Cells[x, y].neighbors.Add(Cells[xR, yT]);
Cells[x, y].neighbors.Add(Cells[xL, y]);
Cells[x, y].neighbors.Add(Cells[xR, y]);
Cells[x, y].neighbors.Add(Cells[xL, yB]);
Cells[x, y].neighbors.Add(Cells[x, yB]);
Cells[x, y].neighbors.Add(Cells[xR, yB]);
}
}
}
}
class Program
{
static Board board;
static private void Reset()
{
board = new Board(
width: 100,
height: 100,
cellSize: 1,
liveDensity: 0.5);
}
static void Render()
{
for (int row = 0; row < board.Rows; row++)
{
for (int col = 0; col < board.Columns; col++)
{
var cell = board.Cells[col, row];
if (cell.IsAlive)
{
Console.Write('*');
}
else
{
Console.Write(' ');
}
}
Console.Write('\n');
}
}
static void Main(string[] args)
{
Reset();
int genCount = 0;
while(true)
{
if(Console.KeyAvailable) {
//if(!Console.IsInputRedirected && Console.KeyAvailable) {
ConsoleKeyInfo name = Console.ReadKey();
if(name.KeyChar == 'q')
break;
else if(name.KeyChar == 's') {
string fname = "gen-" + genCount.ToString();
StreamWriter writer = new StreamWriter("Data/"+ fname + ".txt");
// Create an image and fill it blue
SKBitmap bmp = new(board.Columns * 10, board.Rows * 10);
using SKCanvas canvas = new(bmp);
canvas.Clear(SKColor.Parse("#003366"));
double[,] data = new double[board.Rows, board.Columns];
for (int row = 0; row < board.Rows; row++)
{
for (int col = 0; col < board.Columns; col++)
{
var cell = board.Cells[col, row];
if (cell.IsAlive) {
writer.Write('1');
data[row,col] = 1;
}
else {
writer.Write('0');
data[row,col] = 0;
}
writer.Write(',');
}
writer.Write("\n");
}
writer.Close();
SKPaint paint1 = new() {
Color = SKColors.White.WithAlpha(100),
IsAntialias = true
};
SKPaint paint0 = new() {
Color = SKColors.Black.WithAlpha(100),
IsAntialias = true
};
for (int row = 0; row < board.Rows; row++)
for (int col = 0; col < board.Columns; col++)
{
if(data[row,col] == 1) {
SKPoint pt1 = new(col*10, row*10);
paint1.StrokeWidth = 10;
canvas.DrawPoint(pt1, paint1);
}
else {
SKPoint pt0 = new(col*10, row*10);
paint0.StrokeWidth = 10;
canvas.DrawPoint(pt0, paint0);
}
}
// Save the image to disk
SKFileWStream fs = new("Data/" + fname + ".jpg");
Console.WriteLine(fs.IsValid);
bmp.Encode(fs, SKEncodedImageFormat.Jpeg, quality: 85);
//break;
}
}
Console.Clear();
Render();
board.Advance();
Thread.Sleep(1000);
++genCount;
}
}
}
}