summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-06-01 13:23:02 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-06-01 13:23:02 +0800
commit81666e3ae565ccd6de49877a5436e1b3154c6f32 (patch)
tree820548c1d1542753a812a5cfe9f2357eb54cee6b
parent90bff8fb828c99d8b1ef00fe783dd50887c81f70 (diff)
Move piece-map operations to piece.c
-rw-r--r--pieces.c39
-rw-r--r--pieces.h4
-rw-r--r--sirtet.c39
3 files changed, 42 insertions, 40 deletions
diff --git a/pieces.c b/pieces.c
index 95c9923..6199863 100644
--- a/pieces.c
+++ b/pieces.c
@@ -1,5 +1,5 @@
#include <stdlib.h>
-#include <stdio.h>
+#include <stdbool.h>
#include <string.h>
#include "pieces.h"
@@ -73,3 +73,40 @@ struct piece* randpiece() {
return pc;
}
+void refillpieces(struct piece** hand, int nhand) {
+ // randomly select `nhand` pieces to `hand`
+ for (int i = 0; i < nhand; i++) {
+ hand[i] = randpiece();
+ }
+}
+
+bool placeable(char** map, struct piece* pc, int row, int col, int mapH, int mapW) {
+ // boundary check
+ if (row < 0 || row + (pc->h) > mapH || col < 0 || col + (pc->w) > mapW)
+ return false;
+
+ // check if blocks to be taken by `pc` are vacant
+ for (int r = 0; r < pc->h; r++) {
+ for (int c = 0; c < pc->w; c++) {
+ if (map[row + r][col + c] == '+' && pc->blocks[r * pc->w + c] == '+')
+ return false;
+ }
+ }
+
+ return true;
+}
+
+void place(char** map, struct piece* pc, int row, int col) {
+ for (int r = 0; r < pc->h; r++) {
+ for (int c = 0; c < pc->w; c++) {
+ if (pc->blocks[r * (pc->w) + c] == '+')
+ map[row + r][col + c] = '+';
+ }
+ }
+}
+
+void freepiece(struct piece* pc) {
+ free(pc->blocks);
+ free(pc);
+}
+
diff --git a/pieces.h b/pieces.h
index ee2e371..c0d5eaa 100644
--- a/pieces.h
+++ b/pieces.h
@@ -7,5 +7,9 @@ struct piece {
};
struct piece* randpiece();
+void refillpieces(struct piece** hand, int nhand);
+bool placeable(char** map, struct piece* pc, int row, int col, int mapH, int mapW);
+void place(char** map, struct piece* pc, int row, int col);
+void freepiece(struct piece* pc);
#endif
diff --git a/sirtet.c b/sirtet.c
index c9221ce..1cbe475 100644
--- a/sirtet.c
+++ b/sirtet.c
@@ -1,5 +1,4 @@
#include <stdlib.h>
-#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <locale.h>
@@ -53,44 +52,6 @@ void clearfull(char** map, int mapH, int mapW) {
free(cols_to_clear);
}
-bool placeable(char** map, struct piece* pc, int row, int col, int mapH, int mapW) {
- // boundary check
- if (row < 0 || row + (pc->h) > mapH || col < 0 || col + (pc->w) > mapW)
- return false;
-
- // check if blocks to be taken by `pc` are vacant
- for (int r = 0; r < pc->h; r++) {
- for (int c = 0; c < pc->w; c++) {
- if (map[row + r][col + c] == '+' && pc->blocks[r * pc->w + c] == '+')
- return false;
- }
- }
-
- return true;
-}
-
-void place(char** map, struct piece* pc, int row, int col) {
- for (int r = 0; r < pc->h; r++) {
- for (int c = 0; c < pc->w; c++) {
- if (pc->blocks[r * (pc->w) + c] == '+')
- map[row + r][col + c] = '+';
- }
- }
-}
-
-void freepiece(struct piece* pc) {
- free(pc->blocks);
- free(pc);
-}
-
-void refillpieces(struct piece** hand, int nhand) {
- // randomly select `no` pieces from `pool` to `hand`
- // capacities of `hand` and `pool` are `nhand` and `npool` resp.
- for (int i = 0; i < nhand; i++) {
- hand[i] = randpiece();
- }
-}
-
void sirtet() {
const int H = 8; // canvas height
const int W = 8; // canvas width