#include #include "board.h" void initialize_board(BoardUnit* board) { for (int i = 0; i < BOARD_UNITS; i++) { board[i].feature = EMPTY; board[i].meeple = 0; board[i].structure_group = 0; } } int is_center_index(int index) { return ((index / BOARD_ROW_UNITS) % 2 == 1) && ((index % BOARD_ROW_UNITS) % 2 == 1); } int is_allowed_placement(Tile tile, int index, BoardUnit* board) { if (board[index].feature != EMPTY) return 0; int neighbor_count = 0; for (int i = 0; i < 4; i++) { char neighbor = board[index + NEIGHBOR_INCREMENTS[i]].feature; if (neighbor != EMPTY) { if (neighbor != tile.edges[i]) return 0; neighbor_count++; } } return neighbor_count > 0; } int place_tile(Tile tile, int index, BoardUnit* 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 (%c)\n", tile.edges, tile.center); return 0; } board[index].feature = tile.center; for (int i = 0; i < 4; i++) { board[index + NEIGHBOR_INCREMENTS[i]].feature = tile.edges[i]; } return 1; } void traverse_structure(int group, int index, BoardUnit* board) { board[index].structure_group = group; for (int i = 0; i < 4; i++) { int new_unit = index + NEIGHBOR_INCREMENTS[i]; if (board[new_unit].feature == board[index].feature && board[new_unit].structure_group == 0) { traverse_structure(group, new_unit, board); } } } void refresh_structure_groups(BoardUnit* board) { for (int i = 0; i < BOARD_UNITS; i++) board[i].structure_group = 0; int structure_group = 1; for (int i = 0; i < BOARD_UNITS; i++) { if (board[i].structure_group == 0 && board[i].feature != EMPTY && board[i].feature != 'F') { traverse_structure(structure_group, i, board); structure_group += 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, BoardUnit* board) { int value = 0; char feature = board[index].feature; int structure_group = board[index].structure_group; printf("Evaluating group %i (%c)\n", structure_group, feature); 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].feature != EMPTY) { // Empty tiles doesn't count for (int k = 0; k < 4; k++) { if (board[index + NEIGHBOR_INCREMENTS[k]].structure_group == structure_group) { printf("Found at tile %i\n", i * BOARD_WIDTH + j); value++; break; } } } } } printf("Value: %i\n", value); return value; }