diff options
| author | Aiden Woodruff <aiden.woodruff@gmail.com> | 2018-07-25 23:14:18 -0500 |
|---|---|---|
| committer | Aiden Woodruff <aiden.woodruff@gmail.com> | 2018-07-25 23:14:18 -0500 |
| commit | 05eddaed51a324d24e20574a64a1cbc86e6aba69 (patch) | |
| tree | 0f1bb707c0e8ac954fae48beb4b96482551dddbb | |
| parent | 6ab6ad40e4cf083b4cac120e400327268594bacb (diff) | |
| download | life-05eddaed51a324d24e20574a64a1cbc86e6aba69.tar.gz life-05eddaed51a324d24e20574a64a1cbc86e6aba69.tar.bz2 life-05eddaed51a324d24e20574a64a1cbc86e6aba69.zip | |
Pressing space changes maplife-0.2.0
Only use map in life (no more base_map).
Cell character is more easily editable in code.
Signed-off-by: Aiden Woodruff <aiden.woodruff@gmail.com>
| -rw-r--r-- | life.c | 32 |
1 files changed, 18 insertions, 14 deletions
| @@ -1,5 +1,11 @@ | |||
| 1 | #include "life.h" | 1 | #include "life.h" |
| 2 | 2 | ||
| 3 | char * update_map (const char * base_map, const int width, const int height, char livecell, char deadcell, const int ruleint) { | ||
| 4 | char * ret = malloc((width * height)+1); | ||
| 5 | memcpy(ret, base_map, (width*height)+1); | ||
| 6 | return ret; | ||
| 7 | } | ||
| 8 | |||
| 3 | int main (int argc, char * argv[]) { | 9 | int main (int argc, char * argv[]) { |
| 4 | unsigned int RULE = 0; | 10 | unsigned int RULE = 0; |
| 5 | int rule_size = 0; | 11 | int rule_size = 0; |
| @@ -15,18 +21,12 @@ int main (int argc, char * argv[]) { | |||
| 15 | int ch = 0; | 21 | int ch = 0; |
| 16 | int width = 36; | 22 | int width = 36; |
| 17 | int height = 18; | 23 | int height = 18; |
| 18 | char * base_map = NULL; | 24 | int livecell = '#'; |
| 19 | base_map = malloc(height * width+1); | 25 | int deadcell = '.'; |
| 20 | if (base_map == NULL) { | ||
| 21 | fprintf(stderr, "Ran out of memory\n"); | ||
| 22 | exit(EXIT_FAILURE); | ||
| 23 | } | ||
| 24 | memset(base_map, 0, height * width + 1); | ||
| 25 | memset(base_map, '.', height * width); | ||
| 26 | char * map = NULL; | 26 | char * map = NULL; |
| 27 | map = malloc(height * width+1); | 27 | map = malloc((height * width)+1); |
| 28 | memset(map, 0, height * width + 1); | 28 | memset(map, 0, height * width + 1); |
| 29 | memcpy(map, base_map, height * width); | 29 | memset(map, deadcell, height * width); |
| 30 | int x = 3; | 30 | int x = 3; |
| 31 | int y = 3; | 31 | int y = 3; |
| 32 | int * invent_item_count = NULL; | 32 | int * invent_item_count = NULL; |
| @@ -57,13 +57,18 @@ int main (int argc, char * argv[]) { | |||
| 57 | } else if (ch == KEY_C3) { | 57 | } else if (ch == KEY_C3) { |
| 58 | if (x < width - 1) x++; | 58 | if (x < width - 1) x++; |
| 59 | if (y < height - 1) y++; | 59 | if (y < height - 1) y++; |
| 60 | } else if (ch == ' ') { | ||
| 61 | if (map[y*width+x] == deadcell) { | ||
| 62 | map[y*width+x] = livecell; | ||
| 63 | } else if (map[y*width+x] == livecell) { | ||
| 64 | map[(y*width)+x] = deadcell; | ||
| 65 | } | ||
| 60 | } | 66 | } |
| 61 | memcpy(map, base_map, height * width); | ||
| 62 | for (int i = 0; i < height; ++i) { | 67 | for (int i = 0; i < height; ++i) { |
| 63 | if (i == y) { | 68 | if (i == y) { |
| 64 | printw("%.*s", x-1, map + (i * width)); | 69 | printw("%.*s", x, map + (y * width)); |
| 65 | standout(); | 70 | standout(); |
| 66 | printw("%c", *(map + (i * width) + x)); | 71 | printw("%c", *(map + (y * width) + x)); |
| 67 | standend(); | 72 | standend(); |
| 68 | printw("%.*s\n", width-x, (map + (i * width) + x + 1)); | 73 | printw("%.*s\n", width-x, (map + (i * width) + x + 1)); |
| 69 | } else { | 74 | } else { |
| @@ -74,7 +79,6 @@ int main (int argc, char * argv[]) { | |||
| 74 | ch = getch(); | 79 | ch = getch(); |
| 75 | } | 80 | } |
| 76 | endwin(); | 81 | endwin(); |
| 77 | free(base_map); | ||
| 78 | free(map); | 82 | free(map); |
| 79 | exit(EXIT_SUCCESS); | 83 | exit(EXIT_SUCCESS); |
| 80 | } | 84 | } |
