/
main.cpp
110 lines (89 loc) · 1.67 KB
/
main.cpp
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
#include <stdio.h>
#include <unistd.h>
#include <fstream>
#include <sys/ioctl.h>
#include <getopt.h>
#include <string.h>
#include "TheLife.h"
int main(int argc, char** argv) {
char *fileName = NULL;
int width = 0, height = 0;
bool manual = false;
bool random = true;
int step = 50000;
struct option long_options[] = {
{"file", required_argument, 0, 'f'},
{"width", required_argument, 0, 'w'},
{"height", required_argument, 0, 'h'},
{"manual", optional_argument, 0, 'm'},
{"random", optional_argument, 0, 'r'},
{"step", required_argument, 0, 't'},
{NULL, 0, NULL, 0}
};
int option_index = 0;
int c;
while ((c = getopt_long(argc, argv, "f:w:h:mrt:", long_options, &option_index)) != -1) {
switch(c) {
case 'f':
fileName = optarg;
break;
case 'w':
width = atoi(optarg);
break;
case 'h':
height = atoi(optarg);
break;
case 'm':
manual = true;
break;
case 'r':
random = true;
break;
case 't':
step = atoi(optarg) * 1000;
break;
}
}
if(width == 0 || height == 0) {
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
if(width == 0) {
width = w.ws_col - 2;
}
if(height == 0) {
height = w.ws_row - 3;
}
}
TheLife life(width, height);
if(fileName) {
FILE* f = fopen(fileName, "r");
if(f == NULL) {
printf("Failed to open file");
return 1;
}
int x = 0, y = 0;
char c;
while(fscanf(f, "%c", &c) == 1) {
if(c == '\n') {
x = 0;
y++;
}
if(c == '1') {
life.setLive(x, y);
}
x++;
}
} else if(random) {
life.generate();
}
life.render();
while(1) {
if(manual) {
getchar();
} else {
usleep(step);
}
life.step();
life.render();
}
}