aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-29 02:32:47 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-29 02:32:47 +0300
commit7315e36cd66b9f2626dbb3d89f7e0ea8b9fcafd0 (patch)
tree7a772c4114a98e9acbcf85866fd6c0309f955d2d
parent5ed9b8981a1e37098a79411803c50e4cffade058 (diff)
downloadcarcassonne-engine-c-7315e36cd66b9f2626dbb3d89f7e0ea8b9fcafd0.tar.gz
feat: share edges between neighboring tiles
-rw-r--r--README.md11
-rw-r--r--src/board.h24
-rw-r--r--src/main.c87
3 files changed, 65 insertions, 57 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..6e455d0
--- /dev/null
+++ b/README.md
@@ -0,0 +1,11 @@
+# Board example
+Here's example 3x2 board:
+```
+ F C
+RRRRC*
+ F R
+ *FRF*
+ R
+```
+
+Stars `*` symbolize available centers tile placement.
diff --git a/src/board.h b/src/board.h
index 442b03d..f5d2da3 100644
--- a/src/board.h
+++ b/src/board.h
@@ -1,12 +1,15 @@
-#define TILE_SIZE 5
-#define BOARD_WIDTH 6
-#define BOARD_BYTES TILE_SIZE * (BOARD_WIDTH * BOARD_WIDTH)
+#define BOARD_WIDTH 2
+#define BOARD_ROW_BYTES ((BOARD_WIDTH * 2) + 1)
+#define BOARD_BYTES BOARD_ROW_BYTES * BOARD_ROW_BYTES
-int direction_increments[] = {
- - TILE_SIZE * BOARD_WIDTH + 2,
- TILE_SIZE + 2,
- TILE_SIZE * BOARD_WIDTH - 2,
- - TILE_SIZE - 2
+char EMPTY = ' ';
+char FORBIDDEN = ' ';
+
+int neighbor_increments[] = {
+ - BOARD_ROW_BYTES,
+ 1,
+ BOARD_ROW_BYTES,
+ - 1
};
// Board
@@ -15,7 +18,8 @@ void initialize_board(char* board);
void print_board(char* board);
// Moves
-int check_allowed_tile(char* tile, int position, char* board);
+int is_allowed_placement(char* tile, int position, char* board);
-void place_tile(char* tile, int position, char* board);
+int is_center_position(int position);
+int place_tile(char* tile, int position, char* board);
diff --git a/src/main.c b/src/main.c
index 71ae7f3..a872cab 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,69 +1,62 @@
+#include <stdio.h>
#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;
- }
+void print_board(char* board) {
+ for (int row = 0; row < BOARD_ROW_BYTES; row++) {
+ for (int i = 0; i < BOARD_ROW_BYTES; i++) {
+ int position = BOARD_ROW_BYTES * row + i;
+ if (board[position] == EMPTY && is_center_position(position)) printf("*");
+ else printf("%c", board[position]);
}
+ printf("\n");
}
- 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 initialize_board(char* board) {
+ for (int i = 0; i < BOARD_BYTES; i++) {
+ board[i] = EMPTY;
}
}
-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");
+int is_center_position(int position) {
+ return ((position / BOARD_ROW_BYTES) % 2 == 1) && ((position % BOARD_ROW_BYTES) % 2 == 1);
+}
+
+int is_allowed_placement(char* tile, int position, char* board) {
+ for (int i = 0; i < 4; i++) {
+ char neighbor = board[position + neighbor_increments[i]];
+ if (neighbor != EMPTY && neighbor != tile[i + 1]) return 0;
}
+ return 1;
}
-void initialize_board(char* board) {
- for (int i = 0; i < BOARD_BYTES; i++) {
- board[i] = '_';
+int place_tile(char* tile, int position, char* board) {
+ if (!is_center_position(position)) {
+ printf("Not a valid tile position: %i\n", position);
+ return 0;
}
+
+ if (!is_allowed_placement(tile, position, board)) {
+ printf("Can not place tile %s\n", tile);
+ return 0;
+ }
+
+ board[position] = tile[0];
+ for (int i = 0; i < 4; i++) {
+ board[position + neighbor_increments[i]] = tile[i + 1];
+ }
+
+ return 1;
}
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);
+ place_tile("RFRFR", 6, board);
+ place_tile("RCCRR", 8, board);
+ place_tile("RRFRF", 18, board);
+ place_tile("CFFFC", 16, board);
print_board(board);