diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-06-06 11:49:25 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-06-06 11:49:25 +0800 |
commit | d7ac8dcb88a5cd93f505f1f00b1daec8ac6cceaf (patch) | |
tree | 31cd7a7425e26e64c3c1e23605da8097a949f9e4 | |
parent | fc9ae26aacede490d32f0b31d314f2c8286d158a (diff) |
Flatten map to array of size H*W
-rw-r--r-- | pieces.c | 8 | ||||
-rw-r--r-- | pieces.h | 4 | ||||
-rw-r--r-- | sirtet.c | 30 | ||||
-rw-r--r-- | ui.c | 4 | ||||
-rw-r--r-- | ui.h | 2 |
5 files changed, 19 insertions, 29 deletions
@@ -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] = '+'; } } } @@ -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 @@ -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) @@ -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 { @@ -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); |