diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-06-01 15:33:06 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-06-01 15:33:06 +0800 |
commit | e31298da0ada6bfcbb6f71e3c27d9eb2825e7e27 (patch) | |
tree | 32ac1a42d53896696326b5b74df7844bf0e2eac0 /sirtet.c | |
parent | 81666e3ae565ccd6de49877a5436e1b3154c6f32 (diff) |
Game points
Diffstat (limited to 'sirtet.c')
-rw-r--r-- | sirtet.c | 22 |
1 files changed, 19 insertions, 3 deletions
@@ -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++) { |