-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgameLife.js
86 lines (71 loc) · 1.98 KB
/
gameLife.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
const HEIGTH =40;
const WIDTH = 40;
const FILL = 0.5;
function getRandom() {
return Math.random() <= FILL;
}
function newBoard() {
var b = [];
for(var i = 0; i < HEIGTH; i++){
b[i] = [];
for(var j =0; j < WIDTH; j++){
b[i][j] = getRandom();
process.stdout.write(b[i][j].toString());
}
console.log('');
}
return b;
}
function outBoard() {
for (var i = 0; i < HEIGTH; i++) {
for (var j =0; j < WIDTH; j++) {
if(temp[i][j] == 1){
process.stdout.write('•');
}
else{
process.stdout.write('◦');
}
}
console.log('');
}
console.log('');
console.log('');
}
function validIndex(i, j) {
return i >= 0 && j >= 0 && i < HEIGTH && j < WIDTH;
}
function startLife(){
var neighborhood;
var temp2 = [];
for(var i = 0; i < HEIGTH; i++){
temp2[i]=[];
for(var j = 0; j < WIDTH; j++){
neighborhood = 0;
if (validIndex(i-1, j) && temp[i-1][j] == 1) neighborhood++;
if (validIndex(i,j+1) && temp[i][j+1] == 1) neighborhood++;
if (validIndex(i+1,j) && temp[i+1][j] == 1) neighborhood++;
if (validIndex(i,j-1) && temp[i][j-1] == 1) neighborhood++;
if (validIndex(i-1,j+1) && temp[i-1][j+1] == 1) neighborhood++;
if (validIndex(i+1,j+1) && temp[i+1][j+1] == 1) neighborhood++;
if (validIndex(i-1,j-1) && temp[i-1][j-1] == 1) neighborhood++;
if (validIndex(i+1,j-1) && temp[i+1][j-1] == 1) neighborhood++;
if (temp[i][j] == 1 && (neighborhood == 2 || neighborhood == 3)) {
temp2[i][j] = 1;
}
else if (temp[i][j] == 0 && neighborhood == 3) {
temp2[i][j] = 1;
}
else{
temp2[i][j] = 0;
}
}
}
console.log('');
temp = temp2;
console.log('\033c');
outBoard();
var time = setTimeout(startLife, 100);
}
var temp = newBoard();
outBoard();
var timer = setTimeout(startLife,100);