This source file includes following definitions.
- total_surrounding
- display
- glider
- traffic_light
- main
1
2
3
4
5 #include "assert.h"
6 #include "iostream.h"
7 #include "conio.h"
8 #include "clheir.h"
9 #include "screen.h"
10 #include "conway.h"
11
12 #define max(x,y) ((x > y) ? x : y)
13 #define min(x,y) ((x > y) ? y : x)
14
15 const int num_rows = min(50, NUM_ROWS);
16 const int num_columns = 40;
17
18 class site *field_of_play[num_rows][num_columns];
19
20 int site::total_surrounding(void)
21 {
22 int i, j, imin, imax, jmin, jmax, total;
23
24 total = 0;
25 imin = max(0, x - 1);
26 imax = min(num_rows - 1, x + 1);
27 jmin = max(0, y - 1);
28 jmax = min(num_columns - 1, y + 1);
29
30 for (i = imin; i <= imax; i++)
31 for (j = jmin; j <= jmax; j++)
32 if (field_of_play[i][j]->read()) total++;
33 if (alive) total--;
34 return total;
35 }
36
37 void display(void)
38 {
39 int i, j;
40
41 for (i = 0; i < num_rows; i++)
42 for (j = 0; j < num_columns; j++)
43 {
44 if (field_of_play[i][j]->read()) write_xyc(2*j, i, 'X');
45 else write_xyc(2*j, i, '.');
46 }
47 hide_cursor();
48 }
49
50 void glider(int x, int y)
51 {
52 field_of_play[x - 1][y + 0]->set();
53 field_of_play[x - 1][y + 1]->set();
54 field_of_play[x + 0][y - 1]->set();
55 field_of_play[x + 0][y + 0]->set();
56 field_of_play[x + 1][y + 1]->set();
57 }
58
59 void traffic_light(int x, int y)
60 {
61 field_of_play[x - 1][y]->set();
62 field_of_play[x + 0][y]->set();
63 field_of_play[x + 1][y]->set();
64 }
65
66
67 void main(void)
68 {
69 int i, j, c;
70
71 init_registry();
72
73 for (i = 0; i < num_rows; i++)
74 for (j = 0; j < num_columns; j++)
75 field_of_play[i][j] = new site(i, j);
76
77 start_over:
78 traffic_light(num_rows/2 - 8, num_columns/2 - 8);
79 glider(num_rows/2 + 8, num_columns/2 + 8);
80
81 clear_screen();
82 while (1)
83 {
84 display();
85 if ((c = getch()) == 'q') { clear_screen(); return; }
86 if (c == 'i')
87 {
88 for (i = 0; i < num_rows; i++)
89 for (j = 0; j < num_columns; j++)
90 field_of_play[i][j]->clear();
91 goto start_over;
92 }
93 step_everybody();
94 }
95 }