From 5b9a799146f25dd23d108b01abed72dc50556076 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Tue, 31 May 2022 19:00:42 +0800 Subject: Initial commit: placing pieces --- sirtet.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 sirtet.c (limited to 'sirtet.c') diff --git a/sirtet.c b/sirtet.c new file mode 100644 index 0000000..94ca5a1 --- /dev/null +++ b/sirtet.c @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include "pieces.h" + +void printmap(char** map, int nrow, int ncol) { + printf(" "); + for (int c = 0; c < ncol; c++) { + printf("-"); + } + printf("\n"); + for (int r = 0; r < nrow; r++) { + printf("|"); + for (int c = 0; c < ncol; c++) { + printf("%c", map[r][c]); + } + printf("|\n"); + } + printf(" "); + for (int c = 0; c < ncol; c++) { + printf("-"); + } + printf("\n"); +} + +void printpieces(struct piece** hand, int nhand) { + for (int i = 0; i < nhand; i++) { + printf("Piece %d\n", i); + struct piece* pc = hand[i]; + for (int r = 0; r < pc->h; r++) { + for (int c = 0; c < pc->w; c++) { + printf("%c", pc->blocks[r * pc->w + c]); + } + printf("\n"); + } + printf("\n"); + } +} + +void clearmap(char** map, int nrow, int ncol) { + for (int r = 0; r < nrow; r++) { + for (int c = 0; c < ncol; c++) { + map[r][c] = ' '; + } + } +} + +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 freepieces(struct piece** hand, int nhand) { + for (int i = 0; i < nhand; i++) { + free(hand[i]->blocks); + free(hand[i]); + } +} + +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 + const int P = 3; // maximum # of pieces at hand + + // init memory and game state + char** map = malloc(H * sizeof(char*)); + for (int r = 0; r < H; r++) { + map[r] = malloc(W * sizeof(char)); + } + struct piece** hand = malloc(P * sizeof(struct piece*)); + int pieces_left = P; + srand(time(NULL)); + clearmap(map, H, W); + + bool over = false; + refillpieces(hand, P); + + // begin game loop + while (!over) { + printmap(map, H, W); + printpieces(hand, P); + // await user input + int pc_idx; + int row; + int col; + printf("Input [piece #] [row #] [col #]: "); + scanf("%d %d %d", &pc_idx, &row, &col); + while (pc_idx < 0 || pc_idx >= P || !placeable(map, hand[pc_idx], row, col, H, W)) { + printf("You cannot do that. Try again: "); + scanf("%d %d %d", &pc_idx, &row, &col); + } + place(map, hand[pc_idx], row, col); + } + + // free memory + freepieces(hand, P); + for (int r = 0; r < H; r++) { + free(map[r]); + } + free(map); + free(hand); +} + +int main(int argc, char *argv[]) { + sirtet(); + return 0; +} -- cgit v1.2.3