From e3bdfd349bceed60ccfd0c79ac176847d13f1a6c Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Mon, 27 Apr 2020 12:12:32 +0930 Subject: support playing demos with different height/widths previously it would crahs if you attempted to play a demo using a different width/height to the one in the compiled binary --- ncsweeper.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ncsweeper.c b/ncsweeper.c index 504c4fc..301bfbf 100644 --- a/ncsweeper.c +++ b/ncsweeper.c @@ -24,7 +24,7 @@ #define WIDTH 15 #define HEIGHT 15 -#define MINECOUNT 20 +#define MINECOUNT 31 #define TILEGAP 2 #define DOWN 0 #define UP 1 @@ -172,9 +172,9 @@ int generateboard() { srand(time(NULL)); - board = malloc(sizeof (struct tile) * (game.width * game.height)); if (!game.is_demo) { + board = malloc(sizeof (struct tile) * (game.width * game.height)); /* place mines */ int mx, my; for (int x = 0; x < game.minecount; x++) @@ -198,6 +198,8 @@ place_mine: } } + /* create window here because if we're playing a demo we need the width/height */ + window = newwin(game.height+TILEGAP, (game.width*TILEGAP)+1, 1, 8); /* figure out neighbors */ for (int y = 0; y < game.height; y++) { @@ -513,7 +515,8 @@ load_demo() game.width = header.width; game.height = header.height; game.minecount = header.mine_count; - + /* malloc board here because we need the header information from the demo */ + board = malloc(sizeof (struct tile) * (game.width * game.height)); /* read and set mine data */ struct demo_mine demo_mine; for (int mc = 0; mc < game.minecount; mc++) @@ -643,7 +646,6 @@ main(int argc, char **argv) game.height = HEIGHT; game.minecount = MINECOUNT; game.action_count = 0; - window = newwin(game.height+TILEGAP, (game.width*TILEGAP)+1, 1, 8); action_head = malloc(sizeof(struct action_node)); action_head->action = NULL; action_head->next = NULL; -- cgit v1.2.3