-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
387 lines (335 loc) · 11.8 KB
/
Program.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameOfLifeAscii
{
class Program
{
struct Point
{
int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public int X() { return x; }
public int Y() { return y; }
}
static int WIDTH = 16, HEIGHT = 16;
const char LIVE_CELL_CHAR = '#', DEAD_CELL_CHAR = '*';
static void Main(string[] args)
{
char[,] table = CreateGameTable(HEIGHT, WIDTH); //BE CAREFUL: FIRST Y, SECOND X
//We create 10 initial points. A basic patron.
for (int i = -5; i < 5; i++)
{
Point p = new Point(WIDTH / 2 + i, HEIGHT / 2);
AliveCell(table, p);
}
MainMenu(table);
}
//******MENU METHODS******
static void MainMenu(char[,] table)
{
ShowMenu(table);
Console.Write("\n> ");
bool runMenu = true;
while (runMenu)
{
int option = 0;
option = TryParseInt(Console.ReadLine());
switch (option)
{
case 1:
Console.Clear();
GameLoop(table);
ShowMenu(table);
break;
case 2:
AddCellOnPointMenu(table);
ShowMenu(table);
break;
case 3:
EliminateCellOnPointMenu(table);
ShowMenu(table);
break;
case 4:
ResizeTable();
table = CreateGameTable(HEIGHT, WIDTH);
ShowMenu(table);
break;
case 5:
Console.WriteLine("Bye!");
runMenu = false;
break;
}
if (runMenu)
{
Console.Write("\n> ");
}
}
}
static void ShowMenu(char[,] table)
{
Console.Clear();
ShowTabletop(table);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("\n");
Console.WriteLine("1. Start Game");
Console.WriteLine("2. Add Live Cell");
Console.WriteLine("3. Remove Live Cell");
Console.WriteLine("4. Resize Table. It erases all the live cells!");
Console.WriteLine("5. Exit");
}
//******GAME METHODS******
/// <summary>
/// Creates and returns the Game Table with a specific height and width
/// </summary>
/// <param name="height"></param>
/// <param name="width"></param>
/// <returns>The Game Table</returns>
static char[,] CreateGameTable(int height, int width)
{
char[,] table = new char[height, width];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
table[y, x] = DEAD_CELL_CHAR;
}
}
return table;
}
/// <summary>
/// Draw the Game Table in the console.
/// </summary>
/// <param name="table">Game Table</param>
static void ShowTabletop(char[,] table)
{
for (int y = 0; y < HEIGHT; y++)
{
Console.WriteLine();
for (int x = 0; x < WIDTH; x++)
{
if (table[y, x].Equals(LIVE_CELL_CHAR))
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
} else
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
Console.Write(table[y, x]);
Console.Write(" ");
}
}
}
/// <summary>
/// Apply the game rules on the given table, and return a table with the changes.
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
static char[,] GameOfLife(char[,] table)
{
char[,] auxTable = new char[table.GetLength(0), table.GetLength(1)];
Array.Copy(table, 0, auxTable, 0, table.Length);
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
{
Point point = new Point(x, y);
if (GetCellState(table, point) == DEAD_CELL_CHAR)
{
CheckBirth(table, auxTable, point);
} else
{
CheckDeath(table, auxTable, point);
}
}
}
return auxTable;
}
/// <summary>
/// Principal game loop, where the game starts and the rules are applied until you close the program.
/// </summary>
/// <param name="table"></param>
static void GameLoop(char[,] table)
{
while (true)
{
Console.CursorVisible = false;
ShowTabletop(table);
System.Threading.Thread.Sleep(100);
Console.Clear();
table = GameOfLife(table);
}
}
/// <summary>
/// Adds a live cell on the point indicated by user value.
/// </summary>
/// <param name="table">Game Table</param>
static void AddCellOnPointMenu(char[,] table)
{
int x = 0, y = 0;
do
{
Console.WriteLine("Add a live cell on a valid point (TABLE SIZE: {0} x {1}).", WIDTH, HEIGHT);
Console.Write("X: ");
x = TryParseInt(Console.ReadLine());
Console.Write("Y: ");
y = TryParseInt(Console.ReadLine());
} while (!((x >= 0 && x < WIDTH) && (y >= 0 && y < HEIGHT)));
AliveCell(table, new Point(x, y));
}
/// <summary>
/// Kills a cell on the point indicated by user value.
/// </summary>
/// <param name="table">Game Table</param>
static void EliminateCellOnPointMenu(char[,] table)
{
int x = 0, y = 0;
do
{
Console.WriteLine("Kill a live cell on a valid point (TABLE SIZE: {0} x {1}).", WIDTH, HEIGHT);
Console.Write("X: ");
x = TryParseInt(Console.ReadLine());
Console.Write("Y: ");
y = TryParseInt(Console.ReadLine());
} while (!((x >= 0 && x < WIDTH) && (y >= 0 && y < HEIGHT)));
KillCell(table, new Point(x, y));
}
/// <summary>
/// Resize the table to the user value.
/// </summary>
static void ResizeTable()
{
int x = 0, y = 0;
do
{
Console.WriteLine("Resize the table. Min: 1 - Max: 32. Actual Size: {0} x {1}", WIDTH, HEIGHT);
Console.Write("X: ");
x = TryParseInt(Console.ReadLine());
Console.Write("Y: ");
y = TryParseInt(Console.ReadLine());
} while (!((x > 0 && x <= 32) && (y > 0 && y <= 32)));
WIDTH = x;
HEIGHT = x;
}
//******GAME RULES******
/// <summary>
/// Check if a certain cell in a point will become live or not.
/// </summary>
/// <param name="table">Game Table</param>
/// <param name="auxTable">Aux Table which will replace the Game Table</param>
/// <param name="p">Cell Point</param>
static void CheckBirth(char[,] table, char[,] auxTable, Point p)
{
if (NeighboursNumber(table, p) == 3)
{
AliveCell(auxTable, p);
}
}
/// <summary>
/// Check if a certain cell in a point will die or not.
/// </summary>
/// <param name="table">Game Table</param>
/// <param name="auxTable">Aux Table which will replace the Game Table</param>
/// <param name="p">Cell Point</param>
static void CheckDeath(char[,] table, char[,] auxTable, Point p)
{
int neighbours = NeighboursNumber(table, p);
if (neighbours != 2 && neighbours != 3)
{
KillCell(auxTable, p);
}
}
/// <summary>
/// Check all neighbours cells and returns the number of living neighbours cells around a point.
/// </summary>
/// <param name="table">Game Table</param>
/// <param name="p">Point to check around</param>
/// <returns>Number of neigbour living cells around a point</returns>
static int NeighboursNumber(char[,] table, Point p)
{
int neighbours = 0;
for (int y = -1; y < 2; y++)
{
for (int x = -1; x < 2; x++)
{
if (!(x == 0 && y == 0))
{
Point point = new Point(p.X() - x, p.Y() - y);
if (ExistPoint(table, point))
{
if (GetCellState(table, point) == LIVE_CELL_CHAR)
neighbours++;
}
}
}
}
return neighbours;
}
/// <summary>
/// Set alive a cell in a certain point.
/// </summary>
/// <param name="table">Game Table</param>
/// <param name="p">Cell point</param>
static void AliveCell(char[,] table, Point p)
{
table[p.Y(), p.X()] = LIVE_CELL_CHAR;
}
/// <summary>
/// Set death a cell in a certain point.
/// </summary>
/// <param name="table">Game Table</param>
/// <param name="p">Cell point</param>
static void KillCell(char[,] table, Point p)
{
table[p.Y(), p.X()] = DEAD_CELL_CHAR;
}
//******AUX METHODS******
/// <summary>
/// Check if a Point is in the table.
/// </summary>
/// <param name="table">Game Table</param>
/// <param name="p">Point</param>
/// <returns>True if the point exists, false if not</returns>
static bool ExistPoint(char[,] table, Point p)
{
if ((p.X() >= 0 && p.X() < WIDTH && (p.Y() >= 0 && p.Y() < HEIGHT)))
{
return true;
}
return false;
}
/// <summary>
/// Returns the state (alive/dead) of a cell in a certain point.
/// </summary>
/// <param name="table">Game Table</param>
/// <param name="p">Cell point</param>
/// <returns>A char (alive/dead)</returns>
static char GetCellState(char[,] table, Point p)
{
return table[p.Y(), p.X()];
}
/// <summary>
/// Try to parse a number from a string input and returns that number. If the number is not correct, it returns -1.
/// </summary>
/// <param name="strInt">The string to parse</param>
/// <returns>The parsed int</returns>
static int TryParseInt(string strInt)
{
int number = 0;
if (int.TryParse(strInt, out int opt))
{
number = opt;
} else
{
number = -1;
}
return number;
}
}
}