aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAiden Woodruff <aiden.woodruff@gmail.com>2018-07-25 23:20:28 -0500
committerAiden Woodruff <aiden.woodruff@gmail.com>2018-07-25 23:20:28 -0500
commitbd9fde3a2aea12b34f5c2d5351ef1d47fe780f71 (patch)
tree8d06dd99a01f3901afc57c42d571f567997a42ac
parent05c683d407b0a2dfa3042f5f734ae57627e4e081 (diff)
downloadlife-bd9fde3a2aea12b34f5c2d5351ef1d47fe780f71.tar.gz
life-bd9fde3a2aea12b34f5c2d5351ef1d47fe780f71.tar.bz2
life-bd9fde3a2aea12b34f5c2d5351ef1d47fe780f71.zip
Correctly update maplife-0.3.0
Wait with ncurses, not usleep(). Print (x,y) at bottom of screen. Pass ruleint, width, height, delay as commandline-args (in that order) Define TRUE and FALSE as macros Signed-off-by: Aiden Woodruff <aiden.woodruff@gmail.com>
-rw-r--r--life.c131
1 files changed, 104 insertions, 27 deletions
diff --git a/life.c b/life.c
index 14f9cdf..9af8c3f 100644
--- a/life.c
+++ b/life.c
@@ -1,9 +1,60 @@
1#include "life.h" 1#include "life.h"
2 2
3char * update_map (char * base_map, const int width, const int height, char livecell, char deadcell, const int ruleint) { 3// Declare TRUE and FALSE
4#ifndef TRUE
5#define TRUE (1==1)
6#define FALSE (!TRUE)
7#endif
8
9char * update_map (char * base_map, const int width, const int height, char livecell, char deadcell, const unsigned int ruleint) {
4 char * intermap = malloc((width * height)+1); 10 char * intermap = malloc((width * height)+1);
5 memcpy(intermap, base_map, (width*height)+1); 11 memcpy(intermap, base_map, (width*height)+1);
6 // Change intermap and stuff 12 // Change intermap and stuff
13 for (int i = 0, x = 0, y = 0, surround = 0; i < width*height; i++, surround = 0) {
14 x = i % width;
15 y = (i - x)/width;
16 /*
17 Where i is the current cell:
18 A1 | A2 | A3
19 ----|----|----
20 B1 | i | B3
21 ----|----|----
22 C1 | C2 | C3
23 */
24 if (x > 0) {
25 if (base_map[i-1] == livecell) surround++; // B1
26 if (y > 0) {
27 if (base_map[i-width-1] == livecell) surround++; // A1
28 }
29 if (y < height) {
30 if (base_map[i-1+width] == livecell) surround++; // C1
31 }
32 }
33 if (x < width - 1) {
34 if (base_map[i+1] == livecell) surround++; // B3
35 if (y > 0) {
36 if (base_map[i+1-width] == livecell) surround++; // A3
37 }
38 if (y < height) {
39 if (base_map[i+1+width] == livecell) surround++; // C3
40 }
41 }
42 if (y > 0) {
43 if (base_map[i-width] == livecell) surround++;
44 }
45 if (y < height) {
46 if (base_map[i+width] == livecell) surround++;
47 }
48 if (base_map[i] == deadcell) {
49 if ((ruleint & (1 << surround)) == (1 << surround)) {
50 intermap[i] = livecell;
51 }
52 } else if (base_map[i] == livecell) {
53 if ((ruleint & (1 << (surround + 9))) != (1 << (surround + 9))) {
54 intermap[i] = deadcell;
55 }
56 }
57 }
7 // Rewrite map 58 // Rewrite map
8 memcpy(base_map, intermap, (width*height)+1); 59 memcpy(base_map, intermap, (width*height)+1);
9 free(intermap); 60 free(intermap);
@@ -14,8 +65,10 @@ int main (int argc, char * argv[]) {
14 unsigned int RULE = 0; 65 unsigned int RULE = 0;
15 int rule_size = 0; 66 int rule_size = 0;
16 char * rulestr = NULL; 67 char * rulestr = NULL;
68 if (argc == 1) {
17 printf("Rule Integer: "); 69 printf("Rule Integer: ");
18 getline(&rulestr, (size_t *) &rule_size, stdin); 70 getline(&rulestr, (size_t *) &rule_size, stdin);
71 } else if (argc > 1) rulestr = argv[1];
19 RULE = atoi(rulestr); 72 RULE = atoi(rulestr);
20 if (RULE > 262143) { 73 if (RULE > 262143) {
21 fprintf(stderr, "Invalid integer\n"); 74 fprintf(stderr, "Invalid integer\n");
@@ -25,8 +78,15 @@ int main (int argc, char * argv[]) {
25 int ch = 0; 78 int ch = 0;
26 int width = 36; 79 int width = 36;
27 int height = 18; 80 int height = 18;
81 int playing = 0;
82 unsigned int delay = 0; // Counts up to 10 so display is slow
28 int livecell = '#'; 83 int livecell = '#';
29 int deadcell = '.'; 84 int deadcell = '.';
85 if (argc > 3) {
86 width = atoi(argv[2]);
87 height = atoi(argv[3]);
88 }
89 if (argc > 4) delay = atoi(argv[4]);
30 char * map = NULL; 90 char * map = NULL;
31 map = malloc((height * width)+1); 91 map = malloc((height * width)+1);
32 memset(map, 0, height * width + 1); 92 memset(map, 0, height * width + 1);
@@ -41,31 +101,41 @@ int main (int argc, char * argv[]) {
41 noecho(); 101 noecho();
42 while (ch != 'q') { 102 while (ch != 'q') {
43 erase(); 103 erase();
44 if (ch == KEY_UP) { 104 if (playing == FALSE) {
45 if (y > 0) y--; 105 if (ch == KEY_UP) {
46 } else if (ch == KEY_DOWN) { 106 if (y > 0) y--;
47 if (y < height - 1) y++; 107 } else if (ch == KEY_DOWN) {
48 } else if (ch == KEY_LEFT) { 108 if (y < height - 1) y++;
49 if (x > 0) x--; 109 } else if (ch == KEY_LEFT) {
50 } else if (ch == KEY_RIGHT) { 110 if (x > 0) x--;
51 if (x < width - 1) x++; 111 } else if (ch == KEY_RIGHT) {
52 } else if (ch == KEY_A1) { 112 if (x < width - 1) x++;
53 if (x > 0) x--; 113 } else if (ch == KEY_A1) {
54 if (y > 0) y--; 114 if (x > 0) x--;
55 } else if (ch == KEY_A3) { 115 if (y > 0) y--;
56 if (x < width - 1) x++; 116 } else if (ch == KEY_A3) {
57 if (y > 0) y--; 117 if (x < width - 1) x++;
58 } else if (ch == KEY_C1) { 118 if (y > 0) y--;
59 if (x > 0) x--; 119 } else if (ch == KEY_C1) {
60 if (y < height - 1) y++; 120 if (x > 0) x--;
61 } else if (ch == KEY_C3) { 121 if (y < height - 1) y++;
62 if (x < width - 1) x++; 122 } else if (ch == KEY_C3) {
63 if (y < height - 1) y++; 123 if (x < width - 1) x++;
64 } else if (ch == ' ') { 124 if (y < height - 1) y++;
65 if (map[y*width+x] == deadcell) { 125 } else if (ch == ' ') {
66 map[y*width+x] = livecell; 126 if (map[y*width+x] == deadcell) {
67 } else if (map[y*width+x] == livecell) { 127 map[y*width+x] = livecell;
68 map[(y*width)+x] = deadcell; 128 } else if (map[y*width+x] == livecell) {
129 map[(y*width)+x] = deadcell;
130 }
131 } else if (ch == '\n') {
132 playing = TRUE;
133 timeout(50);
134 }
135 } else {
136 if (ch == '\n') {
137 playing = FALSE;
138 timeout(-1);
69 } 139 }
70 } 140 }
71 for (int i = 0; i < height; ++i) { 141 for (int i = 0; i < height; ++i) {
@@ -79,9 +149,16 @@ int main (int argc, char * argv[]) {
79 printw("%.*s\n", width, (map + (i * width))); 149 printw("%.*s\n", width, (map + (i * width)));
80 } 150 }
81 } 151 }
152 printw("(%d, %d)\n", x, y);
82 refresh(); 153 refresh();
154 if (playing == TRUE) {
155 delay++;
156 if (delay == 10) {
157 delay = 0;
158 update_map(map, width, height, livecell, deadcell, RULE);
159 }
160 }
83 ch = getch(); 161 ch = getch();
84 usleep(50);
85 } 162 }
86 endwin(); 163 endwin();
87 free(map); 164 free(map);