-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
73 lines (59 loc) · 1.46 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
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include "life.h"
#define SECONDS_US 1000000
#define DELAY 0.1 * SECONDS_US //duration of one generation.
char* input_matrix(int width, int height);
int main(int argc, char* argv[]){
int w, h, t;
/*read options*/
if( argc < 4 ){
printf("usage: %s <w> <h> <t>\n",argv[0]);
return 1;
}
w = atoi(argv[1]);
h = atoi(argv[2]);
t = atoi(argv[3]);
system("clear");
/* initialize */
char* world = input_matrix(w, h);
struct life_s* life = life_begin(world, w, h);
for(int i = 0; i <= t; i++)
{
life_draw(life);
/* show time elapsed below */
printf("\033[%ddt: %d\n", h + 2, i);
usleep(DELAY);
life_update(life);
}
/* free memory */
life_end(life);
free(world);
return 0;
}
char *input_matrix(int width, int height)
{
char *matrix = malloc(sizeof(char) * (width * height + 1)); //+1 for null
char *line = NULL;
size_t size;
/* get matrix from stdin */
printf(">> insert %d x %d matrix (o: alive, else: empty)\n", width, height);
while (strlen(matrix) < width * height)
{
int n = getline(&line, &size, stdin) - 1; // do not count line feed
if (n > width)
{
printf("\033[1A\033[K"); // go up and erase line
continue;
}
/* Fill the rest with spaces */
memset(line + n, ' ', width - n);
line[width] = '\0'; // terminate modified string
printf("\033[1A%s|\n", line); // show priview of line
strncat(matrix, line, width);
}
free(line);
return matrix;
}