From 81666e3ae565ccd6de49877a5436e1b3154c6f32 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Wed, 1 Jun 2022 13:23:02 +0800 Subject: Move piece-map operations to piece.c --- pieces.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'pieces.c') diff --git a/pieces.c b/pieces.c index 95c9923..6199863 100644 --- a/pieces.c +++ b/pieces.c @@ -1,5 +1,5 @@ #include -#include +#include #include #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); +} + -- cgit v1.2.3