diff options
Diffstat (limited to 'sirtet.c')
-rw-r--r-- | sirtet.c | 47 |
1 files changed, 44 insertions, 3 deletions
@@ -41,14 +41,52 @@ void printpieces(struct piece** hand, int nhand) { } } -void clearmap(char** map, int nrow, int ncol) { - for (int r = 0; r < nrow; r++) { - for (int c = 0; c < ncol; c++) { +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 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 + bool* rows_to_clear = malloc(mapH * sizeof(bool)); + bool* cols_to_clear = malloc(mapW * sizeof(bool)); + for (int r = 0; r < mapH; r++) { + bool clear = true; + for (int c = 0; c < mapW; c++) { + if (map[r][c] == ' ') + clear = false; + } + rows_to_clear[r] = clear; + } + for (int c = 0; c < mapW; c++) { + bool clear = true; + for (int r = 0; r < mapH; r++) { + if (map[r][c] == ' ') + clear = false; + } + cols_to_clear[c] = clear; + } + for (int r = 0; r < mapH; r++) { + if (rows_to_clear[r]) { + for (int c = 0; c < mapW; c++) + map[r][c] = ' '; + } + } + for (int c = 0; c < mapW; c++) { + if (cols_to_clear[c]) { + for (int r = 0; r < mapW; r++) + map[r][c] = ' '; + } + } + free(rows_to_clear); + free(cols_to_clear); +} + 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) @@ -125,6 +163,9 @@ void sirtet() { freepiece(hand[pc_idx]); hand[pc_idx] = NULL; + // clear full rows and columns + clearfull(map, H, W); + // if player has no piece left, refill bool pieces_left = false; for (int i = 0; i < P; i++) { |