-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
156 lines (130 loc) · 4.66 KB
/
Program.cs
File metadata and controls
156 lines (130 loc) · 4.66 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
using System;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
namespace gameoflife
{
class Program
{
static int Height = Console.WindowHeight - 1;
static int Width = Console.WindowWidth - 1;
static bool[,] firstCells = new bool[Height, Width];
static bool[,] cells = new bool[Height, Width];
static bool[,] cellsNext = new bool[Height, Width];
static int generation = 0;
static void Main(string[] args)
{
Build();
Array.Copy(cells, firstCells, Height * Width);
while (true) {
Draw();
Grow();
System.Threading.Thread.Sleep(100);
generation++;
}
}
static void Build()
{
ConsoleKey key;
int left = 0, top = 0;
Console.Clear();
Console.SetCursorPosition(0, 0);
do {
key = Console.ReadKey(true).Key;
switch (key) {
case ConsoleKey.W:
if (top == 0) Console.SetCursorPosition(left, top += Height - 1);
else Console.SetCursorPosition(left, --top);
break;
case ConsoleKey.A:
if (left == 0) Console.SetCursorPosition(left += Width - 1, top);
else Console.SetCursorPosition(--left, top);
break;
case ConsoleKey.S:
if (top == Height - 1) Console.SetCursorPosition(left, top -= top);
else Console.SetCursorPosition(left, ++top);
break;
case ConsoleKey.D:
if (left == Width - 1) Console.SetCursorPosition(left -= left, top);
else Console.SetCursorPosition(++left, top);
break;
case ConsoleKey.Spacebar:
if (!cells[top, left]) Console.Write("O");
else Console.Write(" ");
cells[top, left] = !cells[top, left];
Console.SetCursorPosition(left, top);
break;
default:
break;
}
} while (key != ConsoleKey.Enter);
}
static void Draw()
{
Console.Clear();
Console.Title = "Gen: " + generation;
for (int y = 0; y < Height; y++) {
for (int x = 0; x < Width; x++) {
if (cells[y, x]) {
Console.SetCursorPosition(x, y);
Console.Write("X");
}
}
}
}
static void Grow()
{
int near;
Array.Copy(cells, cellsNext, Height * Width);
for (int y = 0; y < Height; y++) {
for (int x = 0; x < Width; x++) {
near = GetNear(y, x);
if (cells[y, x]) {
if (near < 2) cellsNext[y, x] = false;
if (near > 3) cellsNext[y, x] = false;
} else {
if (near == 3) {
cellsNext[y, x] = true;
}
}
}
}
bool same = true;
for (int i = 0; i < Height; i++) {
for (int j = 0; j < Width; j++) {
if (!cells[i, j] == cellsNext[i, j]) same = false;
}
}
if (same) End();
Array.Copy(cellsNext, cells, Height * Width);
}
static int GetNear(int y, int x)
{
int near = 0;
for (int i = y - 1; i < y + 2; i++) {
for (int j = x - 1; j < x + 2; j++) {
if (i >= 0 && i < Height && j >= 0 && j < Width && !(i == y && j == x)) {
if (cells[i, j]) near++;
}
}
}
return near;
}
static void End()
{
string seed = null;
for (int i = 0; i < Height; i++) {
for (int j = 0; j < Width; j++) {
seed += Convert.ToByte(firstCells[i, j]);
}
}
using (StreamWriter sw = new StreamWriter(@"log.txt", true)) {
sw.WriteLine($"{generation};{seed}");
}
Console.Clear();
Console.WriteLine("end");
Console.ReadKey();
System.Environment.Exit(0);
}
}
}