-
Notifications
You must be signed in to change notification settings - Fork 0
/
generation-002.js
78 lines (63 loc) · 1.96 KB
/
generation-002.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
/**
* GOAL:
* - Write a mandelbrot function that draws ascii code
* - The ascii code should have 90 columns AND 30 rows (90 * 30 grid)
* - The ascii code should use the provided characters only
* - Log the ascii code to the console in EACH generation
*/
/**
* CHANGELOG:
* Generation 0: implement base script
* Generation 1: implement mandelbrot function
* Generation 2: separate generating mandelbrot from output, add option to save generation to a text file
*/
import { generations, evolve } from "./base.js";
import { writeFile } from "fs/promises"; /* !!! */
const generation = 2;
console.log(`Generation ${generation} spawned`);
async function generateMandelbrot(width, height, asciiCharacters) {
let result = "";
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const cx = x / width * 4 - 2;
const cy = y / height * 4 - 2;
let i = 0;
let zx = 0;
let zy = 0;
while (i < asciiCharacters.length * 2 && zx * zx + zy * zy < 4) {
const nx = zx * zx - zy * zy + cx;
zy = 2 * zx * zy + cy;
zx = nx;
i++;
}
result += asciiCharacters[i % asciiCharacters.length];
}
result += "\n";
}
return result;
}
async function output(saveToFile) {
const width = 90;
const height = 30;
const asciiCharacters = ["\u00B7", "\u2022", "\u2218", "\u22C5", "\u25CF", "\u25CB", "\u26AA"];
const result = await generateMandelbrot(width, height, asciiCharacters);
console.log(result);
if (saveToFile) {
try {
await writeFile(`generation_${generation}.txt`, result);
console.log(`Generation ${generation} saved to file`);
} catch (error) {
console.error(`Failed to save generation ${generation} to file`, error);
}
}
}
await output(true); /* Save to file on this generation */
if (generation < generations) {
try {
await evolve(generation);
} catch (error) {
console.error(error);
}
} else {
console.log("GOAL achieved!");
}