Repository navigation
Expand file tree
/
Copy pathlife.js
More file actions
191 lines (183 loc) · 4.91 KB
/
Copy pathlife.js
File metadata and controls
191 lines (183 loc) · 4.91 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
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
// LIFE
//
// Converted from BASIC to Javascript by Oscar Toledo G. (nanochess)
//
function print(str)
{
document.getElementById("output").appendChild(document.createTextNode(str));
}
function input()
{
var input_element;
var input_str;
return new Promise(function (resolve) {
input_element = document.createElement("INPUT");
print("? ");
input_element.setAttribute("type", "text");
input_element.setAttribute("length", "50");
document.getElementById("output").appendChild(input_element);
input_element.focus();
input_str = undefined;
input_element.addEventListener("keydown", function (event) {
if (event.keyCode == 13) {
input_str = input_element.value;
document.getElementById("output").removeChild(input_element);
print(input_str);
print("\n");
resolve(input_str);
}
});
});
}
function tab(space)
{
var str = "";
while (space-- > 0)
str += " ";
return str;
}
var bs = [];
var a = [];
// Main program
async function main()
{
print(tab(34) + "LIFE\n");
print(tab(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
print("\n");
print("\n");
print("\n");
print("ENTER YOUR PATTERN:\n");
x1 = 1;
y1 = 1;
x2 = 24;
y2 = 70;
for (c = 1; c <= 24; c++) {
bs[c] = "";
a[c] = [];
for (d = 1; d <= 70; d++)
a[c][d] = 0;
}
c = 1;
while (1) {
bs[c] = await input();
if (bs[c] == "DONE") {
bs[c] = "";
break;
}
if (bs[c].substr(0, 1) == ".")
bs[c] = " " + bs[c].substr(1);
c++;
}
c--;
l = 0;
for (x = 1; x <= c - 1; x++) {
if (bs[x].length > l)
l = bs[x].length;
}
x1 = 11 - (c >> 1);
y1 = 33 - (l >> 1);
p = 0;
for (x = 1; x <= c; x++) {
for (y = 1; y <= bs[x].length; y++) {
if (bs[x][y - 1] != " ") {
a[x1 + x][y1 + y] = 1;
p++;
}
}
}
print("\n");
print("\n");
print("\n");
i9 = false;
g = 0;
while (g < 100) {
print("GENERATION: " + g + " POPULATION: " + p + " ");
if (i9)
print("INVALID!");
x3 = 24;
y3 = 70;
x4 = 1;
y4 = 1;
p = 0;
g++;
for (x = 1; x <= x1 - 1; x++)
print("\n");
for (x = x1; x <= x2; x++) {
print("\n");
str = "";
for (y = y1; y <= y2; y++) {
if (a[x][y] == 2) {
a[x][y] = 0;
continue;
} else if (a[x][y] == 3) {
a[x][y] = 1;
} else if (a[x][y] != 1) {
continue;
}
while (str.length < y)
str += " ";
str += "*";
if (x < x3)
x3 = x;
if (x > x4)
x4 = x;
if (y < y3)
y3 = y;
if (y > y4)
y4 = y;
}
print(str);
}
for (x = x2 + 1; x <= 24; x++)
print("\n");
x1 = x3;
x2 = x4;
y1 = y3;
y2 = y4;
if (x1 < 3) {
x1 = 3;
i9 = true;
}
if (x2 > 22) {
x2 = 22;
i9 = true;
}
if (y1 < 3) {
y1 = 3;
i9 = true;
}
if (y2 > 68) {
y2 = 68;
i9 = true;
}
p = 0;
for (x = x1 - 1; x <= x2 + 1; x++) {
for (y = y1 - 1; y <= y2 + 1; y++) {
c = 0;
for (i = x - 1; i <= x + 1; i++) {
for (j = y - 1; j <= y + 1; j++) {
if (a[i][j] == 1 || a[i][j] == 2)
c++;
}
}
if (a[x][y] == 0) {
if (c == 3) {
a[x][y] = 3;
p++;
}
} else {
if (c < 3 || c > 4) {
a[x][y] = 2;
} else {
p++;
}
}
}
}
x1--;
y1--;
x2++;
y2++;
}
}
main();