From e31298da0ada6bfcbb6f71e3c27d9eb2825e7e27 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Wed, 1 Jun 2022 15:33:06 +0800 Subject: Game points --- sirtet.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'sirtet.c') diff --git a/sirtet.c b/sirtet.c index 1cbe475..9e9d0b2 100644 --- a/sirtet.c +++ b/sirtet.c @@ -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++) { -- cgit v1.2.3