aboutsummaryrefslogtreecommitdiff
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
downloadcarcassonne-engine-c-5ed9b8981a1e37098a79411803c50e4cffade058.tar.gz
feat: initial commit
-rw-r--r--.gitignore2
-rwxr-xr-xMakefile23
-rw-r--r--src/board.h21
-rw-r--r--src/main.c71
4 files changed, 117 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a06368c
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+main
+obj
diff --git a/Makefile b/Makefile
new file mode 100755
index 0000000..94e43d6
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,23 @@
+CC=tcc
+CFLAGS=-g
+SRC=src
+OBJ=obj
+SOURCES=$(wildcard $(SRC)/*.c)
+OBJECTS=$(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SOURCES))
+BIN=main
+
+
+run: $(BIN)
+ ./$(BIN)
+
+build: $(BIN)
+
+$(BIN): $(OBJECTS)
+ $(CC) $(CFLAGS) $(OBJECTS) -o $(BIN)
+
+$(OBJ)/%.o: $(SRC)/%.c $(SRC)/*.h
+ $(CC) -c $< -o $@
+
+clean:
+ rm -r $(BIN) $(OBJ)/*.o
+
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;
+}