summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-06-01 15:33:06 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-06-01 15:33:06 +0800
commite31298da0ada6bfcbb6f71e3c27d9eb2825e7e27 (patch)
tree32ac1a42d53896696326b5b74df7844bf0e2eac0
parent81666e3ae565ccd6de49877a5436e1b3154c6f32 (diff)
Game points
-rw-r--r--pieces.c35
-rw-r--r--pieces.h1
-rw-r--r--sirtet.c22
-rw-r--r--ui.c7
4 files changed, 40 insertions, 25 deletions
diff --git a/pieces.c b/pieces.c
index 6199863..f1ed17c 100644
--- a/pieces.c
+++ b/pieces.c
@@ -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
diff --git a/pieces.h b/pieces.h
index c0d5eaa..7c3ce6c 100644
--- a/pieces.h
+++ b/pieces.h
@@ -3,6 +3,7 @@
struct piece {
int h;
int w;
+ int points;
char* blocks;
};
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++) {
diff --git a/ui.c b/ui.c
index 28f6f5c..6f5bae1 100644
--- a/ui.c
+++ b/ui.c
@@ -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;
}