aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-29 00:23:59 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-29 00:23:59 +0300
commit5ed9b8981a1e37098a79411803c50e4cffade058 (patch)
treeed9ca421ad8a6a61c35b46331ee1b13fcf6f730a /src
downloadcarcassonne-engine-c-5ed9b8981a1e37098a79411803c50e4cffade058.tar.gz
feat: initial commit
Diffstat (limited to 'src')
-rw-r--r--src/board.h21
-rw-r--r--src/main.c71
2 files changed, 92 insertions, 0 deletions
diff --git a/src/board.h b/src/board.h
new file mode 100644
index 0000000..442b03d
--- /dev/null
+++ b/src/board.h
@@ -0,0 +1,21 @@
+#define TILE_SIZE 5
+#define BOARD_WIDTH 6
+#define BOARD_BYTES TILE_SIZE * (BOARD_WIDTH * BOARD_WIDTH)
+
+int direction_increments[] = {
+ - TILE_SIZE * BOARD_WIDTH + 2,
+ TILE_SIZE + 2,
+ TILE_SIZE * BOARD_WIDTH - 2,
+ - TILE_SIZE - 2
+};
+
+// Board
+void initialize_board(char* board);
+
+void print_board(char* board);
+
+// Moves
+int check_allowed_tile(char* tile, int position, char* board);
+
+void place_tile(char* tile, int position, char* board);
+
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..71ae7f3
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,71 @@
+#include "board.h"
+
+
+int check_allowed_tile(char* tile, int position, char* board) {
+ for (int i = 0; i < 4; i++) {
+ int index = (position * TILE_SIZE + i) + direction_increments[i];
+ printf("Checking board index %i\n", index);
+ // TODO: board should have sentinel row/column
+ if (index >= 0 && index < BOARD_BYTES) {
+ char neighboring_edge = board[index];
+ if (neighboring_edge > 0 && neighboring_edge != tile[i] && neighboring_edge != '_') {
+ printf("Failed on edge %i (%c). Neighboring edge is %c\n", i, tile[i], neighboring_edge);
+ return 0;
+ }
+ }
+ }
+ return 1;
+}
+
+void place_tile(char* tile, int position, char* board) {
+ if (!check_allowed_tile(tile, position, board)) {
+ printf("Could not place the tile!");
+ return;
+ }
+ for (int i = 0; i < TILE_SIZE; i++) {
+ board[position * TILE_SIZE + i] = tile[i];
+ }
+}
+
+void print_board(char* board) {
+ for (int row = 0; row < BOARD_WIDTH; row++) {
+ for (int tile = 0; tile < BOARD_WIDTH; tile++) {
+ printf("_%c_", board[TILE_SIZE * (row * BOARD_WIDTH + tile)]);
+ }
+ printf("\n");
+ for (int tile = 0; tile < BOARD_WIDTH; tile++) {
+ printf(
+ "%c%c%c",
+ board[TILE_SIZE * (row * BOARD_WIDTH + tile) + 3],
+ board[TILE_SIZE * (row * BOARD_WIDTH + tile) + 4],
+ board[TILE_SIZE * (row * BOARD_WIDTH + tile) + 1]
+ );
+ }
+ printf("\n");
+ for (int tile = 0; tile < BOARD_WIDTH; tile++) {
+ printf("_%c_", board[TILE_SIZE * (row * BOARD_WIDTH + tile) + 2]);
+ }
+ printf("\n");
+ }
+}
+
+void initialize_board(char* board) {
+ for (int i = 0; i < BOARD_BYTES; i++) {
+ board[i] = '_';
+ }
+}
+
+int main() {
+ // Board
+ char board[BOARD_BYTES];
+ initialize_board(board);
+
+ place_tile("ABCDE", 7, board);
+ place_tile("FGHBJ", 8, board);
+ place_tile("FGHGJ", 9, board);
+ place_tile("FGAGJ", 1, board);
+
+ print_board(board);
+
+ return 0;
+}