-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
264 lines (243 loc) · 9.56 KB
/
index.js
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
const yaml = require('js-yaml');
const fs = require('fs');
const math = require('mathjs');
const color = require('colors')
try {
global.config = yaml.safeLoad(fs.readFileSync('./config.yml', 'utf8'))
}
catch (err) {
console.log(err);
throw new Error("Couldn't load the Config...", err);
}
var grid = math.matrix();
grid.resize([config.size.height, config.size.width]);
var newGrid = math.matrix();
newGrid.resize([config.size.height, config.size.width]);
//TESTING IF CUSTOM FILE SHOULD BE USED
if (config.custom.enabled) {
} else {
grid.forEach((value, index, matrix) => {
var chance = Math.floor(Math.random() * 101);
if (chance <= config.custom.chance) {
grid[index] = 2;
} else {
grid[index] = 0;
}
})
}
fs.writeFile('log.txt', '\n',()=>{});
const log = fs.createWriteStream('log.txt', 'utf8');
var iteration = 0;
var display = config.display;
var consoleOut = "";
function cycle() {
var livingCells = 0;
var consoleOut = "";
iteration++;
consoleOut += `Iteration: ${iteration}\n\r`.blue;
var line = ""
for (var i=0; i < config.size.width; i++) {
line+= '-';
}
log.write(`Iteration: ${iteration}\n${line}\n`);
for (var h=0; h<grid.size()[0]; h++) {
for (var w=0; w<grid.size()[1]; w++) {
//console.log(grid[[h,w]], grid.size(), h, w);
var out;
if (grid[[h,w]] == 0 && Math.random() <= config.custom.outbreaks.chance && config.custom.outbreaks.enabled) {
grid[[h,w]] = 2;
if (Math.random() <= config.custom.outbreaks.neighbourChance) {
if (grid[[h-1,w-1]] != undefined) {
grid[[h-1,w-1]] = 2;
newGrid[[h-1,w-1]] = 2;
}
}
if (Math.random() <= config.custom.outbreaks.neighbourChance) {
if (grid[[h-1,w]] != undefined) {
grid[[h-1,w]] = 2;
newGrid[[h-1,w]] = 2;
}
}
if (Math.random() <= config.custom.outbreaks.neighbourChance) {
if (grid[[h-1,w+1]] != undefined) {
grid[[h-1,w+1]] = 2;
newGrid[[h-1,w+1]] = 2;
}
}
if (Math.random() <= config.custom.outbreaks.neighbourChance) {
if (grid[[h,w-1]] != undefined) {
grid[[h,w-1]] = 2;
newGrid[[h,w-1]] = 2;
}
}
if (Math.random() <= config.custom.outbreaks.neighbourChance) {
if (grid[[h,w+1]] != undefined) {
grid[[h,w+1]] = 2;
newGrid[[h,w+1]] = 2;
}
}
if (Math.random() <= config.custom.outbreaks.neighbourChance) {
if (grid[[h+1,w-1]] != undefined) {
grid[[h+1,w-1]] = 2;
newGrid[[h+1,w-1]] = 2;
}
}
if (Math.random() <= config.custom.outbreaks.neighbourChance) {
if (grid[[h+1,w]] != undefined) {
grid[[h+1,w]] = 2;
newGrid[[h+1,w]] = 2;
}
}
if (Math.random() <= config.custom.outbreaks.neighbourChance) {
if (grid[[h+1,w+1]] != undefined) {
grid[[h+1,w+1]] = 2;
newGrid[[h+1,w+1]] = 2;
}
}
}
var livingAround = 0;
if (grid[[h-1,w-1]] != undefined && grid[[h-1,w-1]] >= 1) livingAround++;
if (grid[[h-1,w]] != undefined && grid[[h-1,w]] >= 1) livingAround++;
if (grid[[h-1,w+1]] != undefined && grid[[h-1,w+1]] >= 1) livingAround++;
if (grid[[h,w-1]] != undefined && grid[[h,w-1]] >= 1) livingAround++;
if (grid[[h,w+1]] != undefined && grid[[h,w+1]] >= 1) livingAround++;
if (grid[[h+1,w-1]] != undefined && grid[[h+1,w-1]] >= 1) livingAround++;
if (grid[[h+1,w]] != undefined && grid[[h+1,w]] == 1) livingAround++;
if (grid[[h+1,w+1]] != undefined && grid[[h+1,w+1]] >= 1) livingAround++;
if (grid[[h,w]] == -1) {
newGrid[[h,w]] = 0;
}
if (livingAround < 2 && grid[[h,w]] >= 1) {
newGrid[[h,w]] = -1;
}
if (livingAround >= 2 && livingAround <= 3 && grid[[h,w]] >= 1) {
newGrid[[h,w]] = 1;
}
if (livingAround == 3 && grid[[h,w]] == 0) {
newGrid[[h,w]] = 2;
}
if (livingAround > 3 && grid[[h,w]] >= 1) {
newGrid[[h,w]] = -1;
}
//SPREAD
if (grid[[h,w]] == 2) {
//STYLE CHECK
if (display.ascii.enabled) {
if (display.ascii.color) {
if (display.showSpread) {
out = config.symbols.spread.toString().green;
} else {
out = config.symbols.living.toString();
}
} else {
if (display.showSpread) {
out = config.symbols.spread.toString();
} else {
out = config.symbols.living.toString();
}
}
} else {
if (display.showSpread) {
out = ' '.bgGreen;
} else {
out = ' '.bgYellow;
}
}
if (!display.ascii.enabled) consoleOut +=out;
consoleOut +=out;
if (config.logging) log.write(config.symbols.spread.toString());
livingCells++;
}
//LIVING
if (grid[[h,w]] == 1) {
//STYLE CHECK
if (display.ascii.enabled) {
if (display.ascii.color) {
out = config.symbols.living.toString().white;
} else {
out = config.symbols.living.toString();
}
} else {
out = ' '.bgYellow;
}
if (!display.ascii.enabled) consoleOut +=out;
consoleOut +=out;
if (config.logging) log.write(config.symbols.living.toString());
livingCells++;
}
//NOTHING
if (grid[[h,w]] == 0) {
//STYLE CHECK
if (display.ascii.enabled) {
if (display.ascii.color) {
out = config.symbols.nothing.toString().white;
} else {
out = config.symbols.nothing.toString();
}
} else {
if (display.standardBackground) {
out = config.symbols.nothing.toString().black;
} else {
out = config.symbols.nothing.toString().grey;
}
}
if (!display.ascii.enabled) consoleOut +=out;
consoleOut +=out;
if (config.logging) log.write(config.symbols.nothing.toString());
}
//DIED
if (grid[[h,w]] == -1) {
if (display.ascii.enabled) {
if (display.ascii.color) {
if (display.showLoss) {
out = config.symbols.loss.toString().red;
} else {
out = config.symbols.nothing.toString();
}
} else {
if (display.showLoss) {
out = config.symbols.loss.toString();
} else {
out = config.symbols.nothing.toString();
}
}
} else {
if (display.showLoss) {
out = ' '.bgRed;
} else {
if (display.standardBackground) {
out = ' '.bgBlack;
} else {
out = ' '.bgGrey;
}
}
}
if (!display.ascii.enabled) consoleOut +=out;
consoleOut +=out;
if (config.logging) log.write('-');
}
}
consoleOut +='\x1b[0m\n\r';
log.write('\n');
}
var compare = 0;
newGrid.forEach((value, index) => {
var a = grid[index];
var b = newGrid[index];
if (a == b) compare++;
if (newGrid[index] != undefined) {
grid[index] = newGrid[index];
} else {
grid[index] = 0;
newGrid[index] = 0;
}
}, false)
consoleOut +=`Living Cells: ${livingCells.toString().yellow} | ${compare.toString().yellow} Fields Stayed the same\r`.blue;
console.clear();
console.log(consoleOut);
log.write(`${line}\nLiving Cells: ${livingCells}\n${line}\n\n`);
if (livingCells === 0 || (compare == config.size.height * config.size.width && config.quitOnIdle)) {
process.exit();
}
}
var intvID = setInterval(cycle, config.delay);