-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMGenerator.cs
More file actions
130 lines (114 loc) · 3.24 KB
/
CMGenerator.cs
File metadata and controls
130 lines (114 loc) · 3.24 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
using System;
using System.Collections.Generic;
using System.Text;
namespace CellularAutomataConsole
{
public class CMGenerator
{
public static int width = 128;
public static int height = 80;
public static int randomize = 48;
private static Random rng = new Random();
private static int[,] mapGrid = new int[width, height];
// DEFAULT CONSTRUCTOR
public CMGenerator()
{
rng = new Random();
mapGrid = new int[width, height];
}
// CONSTRUCTOR OVERLOAD 1
public CMGenerator(int w, int h, int r)
{
width = w;
height = h;
randomize = r;
rng = new Random();
mapGrid = new int[width, height];
}
// DRAW GRID TO CONSOLE
public void drawGrid()
{
Console.Clear();
Console.WriteLine();
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
if (mapGrid[i, j] == 1)
Console.Write("* ");
else
Console.Write(" ");
}
// NEW LINE
Console.WriteLine();
}
// NEW LINE END
Console.WriteLine();
for (int i = 0; i < width; i++)
{
Console.Write("==");
}
Console.WriteLine("\n");
}
// RANDOMIZE CELLS
public void randomizeGrid()
{
int rnd1;
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
if ((i == 0) || (i == width - 1) || (j == 0) || (j == height - 1))
{
mapGrid[i, j] = 1;
}
else
{
rnd1 = rng.Next(101);
if (rnd1 < randomize)
mapGrid[i, j] = 1;
else
mapGrid[i, j] = 0;
}
}
}
}
// GET NEIGHBOURS 1
private int CellR1(int x1, int y1)
{
int n = 0;
for (int i = x1-1; i <= x1+1; i++)
{
for (int j = y1-1; j <= y1+1; j++)
{
// OUT OF BOUNDS CHECK
if ((i < 0) || (i > width-1) || (j < 0) || (j > height-1) )
{
n++;
}
else
{
if (mapGrid[i, j] == 1)
n++;
}
}
}
// RETURN N
return n;
}
// SMOOTHING
public void Smooth1()
{
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
if (CellR1(i, j) >= 5)
mapGrid[i, j] = 1;
else
mapGrid[i, j] = 0;
}
}
}
}
}