diff options
-rw-r--r-- | pieces.c | 35 | ||||
-rw-r--r-- | pieces.h | 1 | ||||
-rw-r--r-- | sirtet.c | 22 | ||||
-rw-r--r-- | ui.c | 7 |
4 files changed, 40 insertions, 25 deletions
@@ -5,23 +5,23 @@ const int N_SHAPES = 17; const struct piece SHAPES[] = { - {1, 1, "+"}, - {1, 2, "++"}, - {1, 3, "+++"}, - {1, 4, "++++"}, - {1, 5, "+++++"}, - {2, 2, "+ +"}, // 2x2 diagonal - {2, 2, "++++"}, // 2x2 square - {2, 3, "++++ "}, // L shape - {2, 3, "++ ++"}, // Z shape - {2, 3, "+++ + "}, // T shape - {2, 3, "++++++"}, // 2x3 rectangle - {2, 4, "+++++ "}, // long L shape - {2, 4, "++++++++"}, // 2x4 rectangle - {3, 3, "++++ + "}, // large L shape - {3, 3, "+++ + + "}, // large T shape - {3, 3, "+ + +"}, // 3x3 diagonal - {3, 3, "+++++++++"}, // 3x3 square + {1, 1, 1, "+"}, + {1, 2, 2, "++"}, + {1, 3, 3, "+++"}, + {1, 4, 4, "++++"}, + {1, 5, 5, "+++++"}, + {2, 2, 2, "+ +"}, // 2x2 diagonal + {2, 2, 4, "++++"}, // 2x2 square + {2, 3, 4, "++++ "}, // L shape + {2, 3, 4, "++ ++"}, // Z shape + {2, 3, 4, "+++ + "}, // T shape + {2, 3, 6, "++++++"}, // 2x3 rectangle + {2, 4, 5, "+++++ "}, // long L shape + {2, 4, 8, "++++++++"}, // 2x4 rectangle + {3, 3, 5, "++++ + "}, // large L shape + {3, 3, 5, "+++ + + "}, // large T shape + {3, 3, 3, "+ + +"}, // 3x3 diagonal + {3, 3, 9, "+++++++++"}, // 3x3 square }; void transpose(struct piece* pc) { @@ -66,6 +66,7 @@ struct piece* randpiece() { struct piece* pc = malloc(sizeof(struct piece)); pc->h = shape.h; pc->w = shape.w; + pc->points = shape.points; pc->blocks = blocks; if (rand() % 2) transpose(pc); for (int i = rand() % 4; i > 0; i--) // 0 to 3 times @@ -3,6 +3,7 @@ struct piece { int h; int w; + int points; char* blocks; }; @@ -14,12 +14,16 @@ void clearmap(char** map, int mapH, int mapW) { } } -void 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 + // returns total number of blocks cleared bool* rows_to_clear = malloc(mapH * sizeof(bool)); bool* cols_to_clear = malloc(mapW * sizeof(bool)); + int rtc_count = 0; + int ctc_count = 0; + // scan all rows and columns for (int r = 0; r < mapH; r++) { bool clear = true; for (int c = 0; c < mapW; c++) { @@ -36,20 +40,24 @@ void clearfull(char** map, int mapH, int mapW) { } cols_to_clear[c] = clear; } + // clear rows and columns marked true in `{rows,columns}_to_clear` for (int r = 0; r < mapH; r++) { if (rows_to_clear[r]) { + rtc_count++; for (int c = 0; c < mapW; c++) map[r][c] = ' '; } } for (int c = 0; c < mapW; c++) { if (cols_to_clear[c]) { + ctc_count++; for (int r = 0; r < mapW; r++) map[r][c] = ' '; } } free(rows_to_clear); free(cols_to_clear); + return (rtc_count * mapW + ctc_count * mapH - rtc_count * ctc_count); } void sirtet() { @@ -65,9 +73,10 @@ void sirtet() { struct piece** hand = malloc(P * sizeof(struct piece*)); srand(time(NULL)); clearmap(map, H, W); + refillpieces(hand, P); bool over = false; - refillpieces(hand, P); + int points = 0; // initialize ncurses setlocale(LC_ALL, ""); @@ -87,6 +96,10 @@ void sirtet() { printrect(0, 0, H, W); printmap(map, 1, 1, H, W); + // display game stats + mvprintw(1, 2 * W + 8, "Points: %d", points); + refresh(); + // select piece and position bool confirmed = false; @@ -156,11 +169,14 @@ void sirtet() { } place(map, pc, row, col); + points += pc->points; freepiece(pc); hand[pc_idx] = NULL; // clear full rows and columns - clearfull(map, H, W); + int blocks_cleared = clearfull(map, H, W); + points += blocks_cleared; + // if player has no piece left, refill bool pieces_left = false; for (int i = 0; i < P; i++) { @@ -60,11 +60,8 @@ void printpieces(struct piece** hand, int y, int x, int nhand, int highlight) { continue; struct piece* pc = hand[i]; - move(y, x); - - printw("%d", i); - printrect(y + 1, x, pc->h + 2, pc->w + 2); - printpiece(pc, y + 3, x + 3, (i == highlight ? 1 : 0)); + printrect(y, x, pc->h + 2, pc->w + 2); + printpiece(pc, y + 2, x + 3, (i == highlight ? 1 : 0)); x += 2 * pc->w + 8; } |