-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.c
91 lines (82 loc) · 1.59 KB
/
display.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
#include<ncurses.h>
#include<stdlib.h>
#include<sys/time.h>
#include"lifelib.h"
//uncomment below to use random characters to define live cells
//#define RANDOMCHAR 1
void draw_cells(cellgrid *cgrid);
void microsleep(int micros);
int random_char(void);
char livecellchar='O';
int main(int argc, char**argv)
{
cellgrid *cgrid;
cellgrid *tmpgrid;
//*initialize program
initscr();
start_color();
cbreak();
noecho();
nonl();
intrflush(stdscr,FALSE);
keypad(stdscr, TRUE);
assume_default_colors(COLOR_GREEN,COLOR_BLACK);
clear();
gol_set_default_rules();
cgrid=create_cellgrid(LINES-2,COLS-1);
generate_random_cells(cgrid);
while(1)
{
draw_cells(cgrid);
//getch();
gol_do_iteration(&cgrid);
if(cgrid->iteration%5==0){
if(gol_is_repeating(cgrid)==TRUE){
sleep(2);
free_cellgrid(cgrid);
cgrid=create_cellgrid(LINES-2,COLS-1);
generate_random_cells(cgrid);
//generate_pulsar(cgrid);
}
}
}
sleep(3);
endwin();
return 0;
}
char status[100];
void draw_cells(cellgrid *cgrid)
{
int x,y;
erase();
//clearok(stdscr,TRUE);
for(y=0;y<=cgrid->max_y;y++){
for(x=0;x<=cgrid->max_x;x++){
if(get_cell(cgrid,x,y)==TRUE){
#ifndef RANDOMCHAR
mvaddch(x,y,livecellchar);
#else
mvaddch(x,y,random_char()|A_BOLD);
#endif
}
}
}
sprintf(status,"Iteration: %lu\t\tLive cells: %u",cgrid->iteration,cgrid->live_cells);
mvaddstr(LINES-1,0,status);
refresh();
//microsleep(100);
}
void microsleep(int micros)
{
struct timeval time;
time.tv_sec=0;
time.tv_usec=micros;
select(0,NULL,NULL,NULL,&time);
}
int random_char()
{
static int firsttime=1;
if(firsttime==1){srand(time(NULL));
firsttime=0;}
return (rand()%106)+21;
}