summaryrefslogtreecommitdiff
path: root/sirtet.c
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-06-06 11:49:25 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-06-06 11:49:25 +0800
commitd7ac8dcb88a5cd93f505f1f00b1daec8ac6cceaf (patch)
tree31cd7a7425e26e64c3c1e23605da8097a949f9e4 /sirtet.c
parentfc9ae26aacede490d32f0b31d314f2c8286d158a (diff)
Flatten map to array of size H*W
Diffstat (limited to 'sirtet.c')
-rw-r--r--sirtet.c30
1 files changed, 10 insertions, 20 deletions
diff --git a/sirtet.c b/sirtet.c
index 61427c1..3332aeb 100644
--- a/sirtet.c
+++ b/sirtet.c
@@ -9,15 +9,13 @@
#include "ui.h"
#include "util.h"
-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 clearmap(char* map, int mapH, int mapW) {
+ for (int b = 0; b < mapH * mapW; b++) {
+ map[b] = ' ';
}
}
-int 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
@@ -33,7 +31,7 @@ int clearfull(char** map, int mapH, int mapW) {
for (int r = 0; r < mapH; r++) {
bool clear = true;
for (int c = 0; c < mapW; c++) {
- if (map[r][c] == ' ')
+ if (map[r * mapW + c] == ' ')
clear = false;
}
rows_to_clear[r] = clear;
@@ -41,7 +39,7 @@ int clearfull(char** map, int mapH, int mapW) {
for (int c = 0; c < mapW; c++) {
bool clear = true;
for (int r = 0; r < mapH; r++) {
- if (map[r][c] == ' ')
+ if (map[r * mapW + c] == ' ')
clear = false;
}
cols_to_clear[c] = clear;
@@ -51,14 +49,14 @@ int clearfull(char** map, int mapH, int mapW) {
if (rows_to_clear[r]) {
rtc_count++;
for (int c = 0; c < mapW; c++)
- map[r][c] = ' ';
+ map[r * mapW + c] = ' ';
}
}
for (int c = 0; c < mapW; c++) {
if (cols_to_clear[c]) {
ctc_count++;
for (int r = 0; r < mapH; r++)
- map[r][c] = ' ';
+ map[r * mapW + c] = ' ';
}
}
free(rows_to_clear);
@@ -68,15 +66,10 @@ int clearfull(char** map, int mapH, int mapW) {
void sirtet(int H, int W, int P) {
// init memory and game state
- char** map = malloc(H * sizeof(char*));
+ char* map = malloc(H * W * sizeof(char));
if (map == NULL)
mallocfail();
- for (int r = 0; r < H; r++) {
- map[r] = malloc(W * sizeof(char));
- if (map[r] == NULL)
- mallocfail();
- }
struct piece** hand = malloc(P * sizeof(struct piece*));
if (hand == NULL)
mallocfail();
@@ -201,7 +194,7 @@ void sirtet(int H, int W, int P) {
}
}
- place(map, pc, row, col);
+ place(map, pc, row, col, W);
points += pc->points;
freepiece(pc);
hand[pc_idx] = NULL;
@@ -246,9 +239,6 @@ void sirtet(int H, int W, int P) {
}
// free memory
- for (int r = 0; r < H; r++) {
- free(map[r]);
- }
free(map);
for (int i = 0; i < P; i++) {
if (hand[i] != NULL)