-
Notifications
You must be signed in to change notification settings - Fork 0
/
generation-004.js
131 lines (110 loc) · 3.47 KB
/
generation-004.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
/**
* 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
* Generation 3: add option to load previous generation from a text file
* Generation 4: add zoom option to mandelbrot, user input password validation
*/
import { generations, evolve } from "./base.js";
import { writeFile, readFile } from "fs/promises"; /* !!! */
import { createInterface } from "readline"; /* !!! */
const generation = 4;
console.log(`Generation ${generation} spawned`);
const password = "mySuperSecretPassword";
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
async function validatePassword() {
return new Promise((resolve, reject) => {
rl.question("Please enter the password: ", (answer) => {
if (answer === password) {
console.log("Password correct!");
resolve();
} else {
console.log("Wrong password. Try again.");
reject();
}
rl.close();
});
});
}
async function generateMandelbrot(width, height, xstart, ystart, xend, yend, asciiCharacters) {
let result = "";
const xstep = (xend - xstart) / width;
const ystep = (yend - ystart) / height;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const cx = xstart + x * xstep;
const cy = ystart + y * ystep;
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"];
let result;
if (saveToFile) {
const xstart = -2.5;
const ystart = -1;
const xend = 1;
const yend = 1;
result = await generateMandelbrot(width, height, xstart, ystart, xend, yend, asciiCharacters);
console.log(result);
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);
}
} else {
try {
result = await readFile(`generation_${generation - 1}.txt`, "utf8");
console.log(`Generation ${generation - 1} loaded from file`);
console.log(result);
} catch (error) {
console.error(`Failed to load generation ${generation - 1} from file`, error);
result = await generateMandelbrot(width, height, -2.5, -1, 1, 1, asciiCharacters);
console.log(result);
}
}
}
async function main() {
try {
await validatePassword();
await output(generation !== 1); /* Always save on this generation, otherwise load from file */
} catch (error) {
console.error(error);
}
if (generation < generations) {
try {
await evolve(generation);
} catch (error) {
console.error(error);
}
} else {
console.log("GOAL achieved!");
}
}
main();