aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-30 01:09:59 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-30 01:31:10 +0300
commit3247a2c20fde12dd9a58e04544006c988d231e7d (patch)
treeda885980564c8c88b7e0b89c8ec883c65c7a2c29
parent3dcc2e6a5bd9acfa6c6de62672e336bfacec05de (diff)
downloadcarcassonne-engine-c-3247a2c20fde12dd9a58e04544006c988d231e7d.tar.gz
refactor: move functions to separate file
-rw-r--r--src/board.c122
-rw-r--r--src/board.h2
-rw-r--r--src/main.c117
3 files changed, 125 insertions, 116 deletions
diff --git a/src/board.c b/src/board.c
new file mode 100644
index 0000000..8c33ae3
--- /dev/null
+++ b/src/board.c
@@ -0,0 +1,122 @@
+#include <stdio.h>
+#include "board.h"
+
+void print_board(char* board) {
+ for (int row = 0; row < BOARD_ROW_BYTES; row++) {
+ for (int i = 0; i < BOARD_ROW_BYTES; i++) {
+ int index = BOARD_ROW_BYTES * row + i;
+ if (board[index] == EMPTY && is_center_index(index)) printf("*");
+ else printf("%c", board[index]);
+ }
+ printf("\n");
+ }
+}
+
+void initialize_board(char* board) {
+ for (int i = 0; i < BOARD_BYTES; i++) {
+ board[i] = EMPTY;
+ }
+}
+
+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 index, char* board) {
+ if (board[index] != EMPTY) return 0;
+
+ int neighbor_count = 0;
+
+ for (int i = 0; i < 4; i++) {
+ char neighbor = board[index + neighbor_increments[i]];
+ if (neighbor != EMPTY) {
+ if (neighbor != tile[i + 1]) return 0;
+ neighbor_count++;
+ }
+ }
+
+ return neighbor_count > 0;
+}
+
+int place_tile(char* tile, int index, char* board, int force) {
+ if (!is_center_index(index)) {
+ printf("Not a valid tile index: %i\n", index);
+ return 0;
+ }
+
+ if (!force && !is_allowed_placement(tile, index, board)) {
+ printf("Can not place tile %s\n", tile);
+ return 0;
+ }
+
+ board[index] = tile[0];
+ for (int i = 0; i < 4; i++) {
+ board[index + neighbor_increments[i]] = tile[i + 1];
+ }
+
+ return 1;
+}
+
+void traverse_structure(char id, int index, char* board, char* structures) {
+ structures[index] = id;
+ for (int i = 0; i < 4; i++) {
+ int new_byte = index + neighbor_increments[i];
+ if (board[new_byte] == board[index] && structures[new_byte] == EMPTY) {
+ traverse_structure(id, new_byte, board, structures);
+ }
+ }
+}
+
+void create_structure_mask(char* board, char* structures) {
+ char structure_id = '1';
+ for (int i = 0; i < BOARD_BYTES; i++) {
+ if (structures[i] == EMPTY && board[i] != EMPTY && board[i] != 'F') {
+ traverse_structure(structure_id, i, board, structures);
+ structure_id += 1;
+ }
+ }
+}
+
+int translate_coordinate(int center_index) {
+ return (2 * (center_index / BOARD_WIDTH) + 1) *
+ (2 * 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;
+}
+
+void write_board(char* board, char* filename) {
+ FILE* file = fopen(filename, "w");
+ fprintf(file, "%s\n", board);
+ fclose(file);
+}
+
+void read_board(char* board, char* filename) {
+ FILE* file = fopen(filename, "r");
+ fgets(board, BOARD_BYTES, file);
+ fclose(file);
+}
diff --git a/src/board.h b/src/board.h
index a2e6793..caa4e69 100644
--- a/src/board.h
+++ b/src/board.h
@@ -27,7 +27,7 @@ int translate_coordinate(int index);
int is_center_index(int index);
-int place_tile(char* tile, int index, char* board);
+int place_tile(char* tile, int index, char* board, int force);
// Structures
void traverse_structure(char id, int index, char* board, char* structures);
diff --git a/src/main.c b/src/main.c
index 9d3205d..11b9df8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,126 +1,12 @@
#include <stdio.h>
#include "board.h"
-void print_board(char* board) {
- for (int row = 0; row < BOARD_ROW_BYTES; row++) {
- for (int i = 0; i < BOARD_ROW_BYTES; i++) {
- int index = BOARD_ROW_BYTES * row + i;
- if (board[index] == EMPTY && is_center_index(index)) printf("*");
- else printf("%c", board[index]);
- }
- printf("\n");
- }
-}
-
-void initialize_board(char* board) {
- for (int i = 0; i < BOARD_BYTES; i++) {
- board[i] = EMPTY;
- }
-}
-
-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 index, char* board) {
- if (board[index] != EMPTY) return 0;
-
- for (int i = 0; i < 4; 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 index, char* board) {
- if (!is_center_index(index)) {
- printf("Not a valid tile index: %i\n", index);
- return 0;
- }
-
- if (!is_allowed_placement(tile, index, board)) {
- printf("Can not place tile %s\n", tile);
- return 0;
- }
-
- board[index] = tile[0];
- for (int i = 0; i < 4; i++) {
- board[index + neighbor_increments[i]] = tile[i + 1];
- }
-
- return 1;
-}
-
-void traverse_structure(char id, int index, char* board, char* structures) {
- structures[index] = id;
- for (int i = 0; i < 4; i++) {
- int new_byte = index + neighbor_increments[i];
- if (board[new_byte] == board[index] && structures[new_byte] == EMPTY) {
- traverse_structure(id, new_byte, board, structures);
- }
- }
-}
-
-void create_structure_mask(char* board, char* structures) {
- char structure_id = '1';
- for (int i = 0; i < BOARD_BYTES; i++) {
- if (structures[i] == EMPTY && board[i] != EMPTY && board[i] != 'F') {
- traverse_structure(structure_id, i, board, structures);
- structure_id += 1;
- }
- }
-}
-
-int translate_coordinate(int center_index) {
- return (2 * (center_index / BOARD_WIDTH) + 1) *
- (2 * 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;
-}
-
-void write_board(char* board, char* filename) {
- FILE* file = fopen(filename, "w");
- fprintf(file, "%s\n", board);
- fclose(file);
-}
-
-void read_board(char* board, char* filename) {
- FILE* file = fopen(filename, "r");
- fgets(board, BOARD_BYTES, file);
- fclose(file);
-}
-
int main() {
char board[BOARD_BYTES];
char structures[BOARD_BYTES];
initialize_board(board);
- place_tile("RFRCR", translate_coordinate(24), board);
+ place_tile("RFRCR", translate_coordinate(24), board, 1);
write_board(board, "board.txt");
initialize_board(structures);
@@ -131,5 +17,6 @@ int main() {
print_board(structures);
evaluate_structure(translate_coordinate(24), board, structures);
+
return 0;
}