From d7ac8dcb88a5cd93f505f1f00b1daec8ac6cceaf Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Mon, 6 Jun 2022 11:49:25 +0800 Subject: Flatten map to array of size H*W --- pieces.c | 8 ++++---- pieces.h | 4 ++-- sirtet.c | 30 ++++++++++-------------------- ui.c | 4 ++-- ui.h | 2 +- 5 files changed, 19 insertions(+), 29 deletions(-) diff --git a/pieces.c b/pieces.c index 3b71003..001c9df 100644 --- a/pieces.c +++ b/pieces.c @@ -94,7 +94,7 @@ void refillpieces(struct piece** hand, int nhand) { } } -bool placeable(char** map, struct piece* pc, int row, int col, int mapH, int mapW) { +bool placeable(char* map, struct piece* pc, int row, int col, int mapH, int mapW) { // boundary check if (row < 0 || row + (pc->h) > mapH || col < 0 || col + (pc->w) > mapW) return false; @@ -102,7 +102,7 @@ bool placeable(char** map, struct piece* pc, int row, int col, int mapH, int map // check if blocks to be taken by `pc` are vacant for (int r = 0; r < pc->h; r++) { for (int c = 0; c < pc->w; c++) { - if (map[row + r][col + c] == '+' && pc->blocks[r * pc->w + c] == '+') + if (map[(row + r) * mapW + col + c] == '+' && pc->blocks[r * pc->w + c] == '+') return false; } } @@ -110,11 +110,11 @@ bool placeable(char** map, struct piece* pc, int row, int col, int mapH, int map return true; } -void place(char** map, struct piece* pc, int row, int col) { +void place(char* map, struct piece* pc, int row, int col, int mapW) { for (int r = 0; r < pc->h; r++) { for (int c = 0; c < pc->w; c++) { if (pc->blocks[r * (pc->w) + c] == '+') - map[row + r][col + c] = '+'; + map[(row + r) * mapW + col + c] = '+'; } } } diff --git a/pieces.h b/pieces.h index 7c3ce6c..1a3e247 100644 --- a/pieces.h +++ b/pieces.h @@ -9,8 +9,8 @@ struct piece { struct piece* randpiece(); void refillpieces(struct piece** hand, int nhand); -bool placeable(char** map, struct piece* pc, int row, int col, int mapH, int mapW); -void place(char** map, struct piece* pc, int row, int col); +bool placeable(char* map, struct piece* pc, int row, int col, int mapH, int mapW); +void place(char* map, struct piece* pc, int row, int col, int mapW); void freepiece(struct piece* pc); #endif diff --git a/sirtet.c b/sirtet.c index 61427c1..3332aeb 100644 --- a/sirtet.c +++ b/sirtet.c @@ -9,15 +9,13 @@ #include "ui.h" #include "util.h" -void clearmap(char** map, int mapH, int mapW) { - for (int r = 0; r < mapH; r++) { - for (int c = 0; c < mapW; c++) { - map[r][c] = ' '; - } +void clearmap(char* map, int mapH, int mapW) { + for (int b = 0; b < mapH * mapW; b++) { + map[b] = ' '; } } -int clearfull(char** map, int mapH, int mapW) { +int clearfull(char* map, int mapH, int mapW) { // if a row or column is full, clear it // if a row and a column are full at the same time, clear both // which is why we need to scan all rows/columns before actually clearing them @@ -33,7 +31,7 @@ int clearfull(char** map, int mapH, int mapW) { for (int r = 0; r < mapH; r++) { bool clear = true; for (int c = 0; c < mapW; c++) { - if (map[r][c] == ' ') + if (map[r * mapW + c] == ' ') clear = false; } rows_to_clear[r] = clear; @@ -41,7 +39,7 @@ int clearfull(char** map, int mapH, int mapW) { for (int c = 0; c < mapW; c++) { bool clear = true; for (int r = 0; r < mapH; r++) { - if (map[r][c] == ' ') + if (map[r * mapW + c] == ' ') clear = false; } cols_to_clear[c] = clear; @@ -51,14 +49,14 @@ int clearfull(char** map, int mapH, int mapW) { if (rows_to_clear[r]) { rtc_count++; for (int c = 0; c < mapW; c++) - map[r][c] = ' '; + map[r * mapW + c] = ' '; } } for (int c = 0; c < mapW; c++) { if (cols_to_clear[c]) { ctc_count++; for (int r = 0; r < mapH; r++) - map[r][c] = ' '; + map[r * mapW + c] = ' '; } } free(rows_to_clear); @@ -68,15 +66,10 @@ int clearfull(char** map, int mapH, int mapW) { void sirtet(int H, int W, int P) { // init memory and game state - char** map = malloc(H * sizeof(char*)); + char* map = malloc(H * W * sizeof(char)); if (map == NULL) mallocfail(); - for (int r = 0; r < H; r++) { - map[r] = malloc(W * sizeof(char)); - if (map[r] == NULL) - mallocfail(); - } struct piece** hand = malloc(P * sizeof(struct piece*)); if (hand == NULL) mallocfail(); @@ -201,7 +194,7 @@ void sirtet(int H, int W, int P) { } } - place(map, pc, row, col); + place(map, pc, row, col, W); points += pc->points; freepiece(pc); hand[pc_idx] = NULL; @@ -246,9 +239,6 @@ void sirtet(int H, int W, int P) { } // free memory - for (int r = 0; r < H; r++) { - free(map[r]); - } free(map); for (int i = 0; i < P; i++) { if (hand[i] != NULL) diff --git a/ui.c b/ui.c index 1c1eea3..70bfe5c 100644 --- a/ui.c +++ b/ui.c @@ -25,10 +25,10 @@ void printrect(int y, int x, int h, int w) { refresh(); } -void printmap(char** map, int y, int x, int mapH, int mapW) { +void printmap(char* map, int y, int x, int mapH, int mapW) { for (int r = 0; r < mapH; r++) { for (int c = 0; c < mapW; c++) { - if (map[r][c] == '+') { + if (map[r * mapW + c] == '+') { mvaddch(y + r, x + 2 * c, (' ' | A_REVERSE)); addch(' ' | A_REVERSE); } else { diff --git a/ui.h b/ui.h index 8d25ab0..0a4c2e1 100644 --- a/ui.h +++ b/ui.h @@ -1,7 +1,7 @@ #ifndef UI_H #define UI_H void printrect(int y, int x, int h, int w); -void printmap(char** map, int y, int x, int mapH, int mapW); +void printmap(char* map, int y, int x, int mapH, int mapW); void printpiece(struct piece* pc, int y, int x, int color_pair); void printpieces(struct piece** hand, int y, int x, int nhand, int highlight); void printhelp(int y, int x); -- cgit v1.2.3