aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-29 13:20:05 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-29 13:20:05 +0300
commitb77e0ab18c4531c45394951bccc8d5d5b8181ee2 (patch)
tree33f5ea49662d71a58919e2e5443680cc022a73b4
parent792a22e042ae4499dcb4f89e3d5459adbc6ba7ad (diff)
downloadcarcassonne-engine-c-b77e0ab18c4531c45394951bccc8d5d5b8181ee2.tar.gz
feat: add structure evaluation
-rw-r--r--src/board.h14
-rw-r--r--src/main.c69
2 files changed, 57 insertions, 26 deletions
diff --git a/src/board.h b/src/board.h
index d5fb2d0..ad46098 100644
--- a/src/board.h
+++ b/src/board.h
@@ -1,4 +1,4 @@
-#define BOARD_WIDTH 2
+#define BOARD_WIDTH 5
#define BOARD_ROW_BYTES ((BOARD_WIDTH * 2) + 1)
#define BOARD_BYTES BOARD_ROW_BYTES * BOARD_ROW_BYTES
@@ -18,15 +18,17 @@ void initialize_board(char* board);
void print_board(char* board);
// Moves
-int is_allowed_placement(char* tile, int position, char* board);
+int is_allowed_placement(char* tile, int index, char* board);
-int translate_coordinate(int position);
+int translate_coordinate(int index);
-int is_center_position(int position);
+int is_center_index(int index);
-int place_tile(char* tile, int position, char* board);
+int place_tile(char* tile, int index, char* board);
// Structures
-void traverse_structure(char id, int byte, char* board, char* structures);
+void traverse_structure(char id, int index, char* board, char* structures);
void create_structure_mask(char* board, char* structures);
+
+int evaluate_structure(int index, char* board, char* structures);
diff --git a/src/main.c b/src/main.c
index e29406e..65567b8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,9 +4,9 @@
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]);
+ int index = BOARD_ROW_BYTES * row + i;
+ if (board[index] == EMPTY && is_center_index(index)) printf("*");
+ else printf("%c", board[index]);
}
printf("\n");
}
@@ -18,42 +18,42 @@ void initialize_board(char* board) {
}
}
-int is_center_position(int position) {
- return ((position / BOARD_ROW_BYTES) % 2 == 1) && ((position % BOARD_ROW_BYTES) % 2 == 1);
+int is_center_index(int index) {
+ return ((index / BOARD_ROW_BYTES) % 2 == 1) && ((index % BOARD_ROW_BYTES) % 2 == 1);
}
-int is_allowed_placement(char* tile, int position, char* board) {
+int is_allowed_placement(char* tile, int index, char* board) {
for (int i = 0; i < 4; i++) {
- char neighbor = board[position + neighbor_increments[i]];
+ char neighbor = board[index + neighbor_increments[i]];
if (neighbor != EMPTY && neighbor != tile[i + 1]) return 0;
}
return 1;
}
-int place_tile(char* tile, int position, char* board) {
- if (!is_center_position(position)) {
- printf("Not a valid tile position: %i\n", position);
+int place_tile(char* tile, int index, char* board) {
+ if (!is_center_index(index)) {
+ printf("Not a valid tile index: %i\n", index);
return 0;
}
- if (!is_allowed_placement(tile, position, board)) {
+ if (!is_allowed_placement(tile, index, board)) {
printf("Can not place tile %s\n", tile);
return 0;
}
- board[position] = tile[0];
+ board[index] = tile[0];
for (int i = 0; i < 4; i++) {
- board[position + neighbor_increments[i]] = tile[i + 1];
+ board[index + neighbor_increments[i]] = tile[i + 1];
}
return 1;
}
-void traverse_structure(char id, int byte, char* board, char* structures) {
- structures[byte] = id;
+void traverse_structure(char id, int index, char* board, char* structures) {
+ structures[index] = id;
for (int i = 0; i < 4; i++) {
- int new_byte = byte + neighbor_increments[i];
- if (board[new_byte] == board[byte] && structures[new_byte] == EMPTY) {
+ int new_byte = index + neighbor_increments[i];
+ if (board[new_byte] == board[index] && structures[new_byte] == EMPTY) {
traverse_structure(id, new_byte, board, structures);
}
}
@@ -69,10 +69,36 @@ void create_structure_mask(char* board, char* structures) {
}
}
-int translate_coordinate(int center_position) {
- return (2 * (center_position / BOARD_WIDTH) + 1) *
+int translate_coordinate(int center_index) {
+ return (2 * (center_index / BOARD_WIDTH) + 1) *
(2 * BOARD_WIDTH + 1) +
- (2 * (center_position % BOARD_WIDTH) + 1);
+ (2 * (center_index % BOARD_WIDTH) + 1);
+}
+
+int evaluate_structure(int index, char* board, char* structures) {
+ int value = 0;
+
+ char structure = board[index];
+ char structure_id = structures[index];
+ printf("Evaluating group %c (%c)\n", structure_id, structure);
+
+ for (int i = 0; i < BOARD_WIDTH; i++) {
+ for (int j = 0; j < BOARD_WIDTH; j++) {
+ int index = translate_coordinate(i * BOARD_WIDTH + j);
+ if (board[index] != EMPTY) { // Empty tiles doesn't count
+ for (int k = 0; k < 4; k++) {
+ if (structures[index + neighbor_increments[k]] == structure_id) {
+ printf("Found at tile %i\n", i * BOARD_WIDTH + j);
+ value++;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ printf("Value: %i\n", value);
+ return value;
}
int main() {
@@ -88,9 +114,12 @@ int main() {
place_tile("RRFRF", translate_coordinate(3), board);
create_structure_mask(board, structures);
+
print_board(board);
printf("\n\n");
print_board(structures);
+ evaluate_structure(translate_coordinate(0), board, structures);
+
return 0;
}