-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
106 lines (86 loc) · 1.92 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <include/conway.h>
#include <include/readfile.h>
#define TIME_UNIT 100000
int conway(char* file, int iterations, int dsec);
void help(FILE* stream)
{
fputs("USAGE:\n"
"conway [-f FILE] [-i ITERATIONS] [-s INTERVAL]\n"
"\n"
"FLAGS:\n"
"\t-f\tfile where to read the configuration from\n"
"\t\t(default = a.dat)\n"
"\t-i\tnumber of iterations to visualize (default = 10)\n"
"\t-s\ttenth of seconds to wait between frames (default = 1)\n"
"\n"
"Written by Giovanni Zaccaria - LoZack19\n"
"\n"
"", stream);
}
int main(int argc, char* argv[])
{
char* file = "a.dat";
int iterations = 10;
int dsec = 1;
for (char c = 0; c != -1; c = getopt(argc, argv, "f:i:s:h")) {
switch (c) {
case 0:
break;
case '?':
help(stderr);
return -1;
case 'f':
if (optarg)
file = optarg;
else
file = argv[optind];
break;
case 'i':
if (optarg)
iterations = strtol(optarg, NULL, 10);
else
iterations = strtol(argv[optind], NULL, 10);
break;
case 's':
if (optarg)
dsec = strtol(optarg, NULL, 10);
else
dsec = strtol(argv[optind], NULL, 10);
break;
case 'h':
help(stdout);
return 0;
default:
help(stderr);
return -1;
}
}
return conway(file, iterations, dsec);
}
int conway(char* file, int iterations, int dsec)
{
int y, x;
char** old_gen = init_matrix(file, &y, &x);
if (!old_gen)
return -1;
hide_cursor();
printmatrix(old_gen, y, x);
char** new_gen = matrixdup(old_gen, y, x);
for (int i = 1; i < iterations; i++) {
clear();
new_gen = next_generation(old_gen, new_gen, &y, &x);
matrixcpy(old_gen, new_gen, y, x);
printmatrix(new_gen, y, x);
usleep(dsec * TIME_UNIT);
}
free_matrix(&new_gen, y);
free_matrix(&old_gen, y);
show_cursor();
clear();
return 0;
}