summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-05-31 20:45:40 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-05-31 20:45:40 +0800
commit3f963f08639000f14dee3a43d7cbbc5f780d7571 (patch)
treecb783419906b8bba48c304427fb2bb65729fe2c4
parent137de73dd908602b3879d29abd67ee96007cf2ee (diff)
Clear full rows and columns
-rw-r--r--sirtet.c47
1 files changed, 44 insertions, 3 deletions
diff --git a/sirtet.c b/sirtet.c
index b09c316..1517d98 100644
--- a/sirtet.c
+++ b/sirtet.c
@@ -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++) {