#include #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 position = BOARD_ROW_BYTES * row + i; if (board[position] == EMPTY && is_center_position(position)) printf("*"); else printf("%c", board[position]); } printf("\n"); } } void initialize_board(char* board) { for (int i = 0; i < BOARD_BYTES; i++) { board[i] = EMPTY; } } 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; } 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; } void traverse_structure(char id, int byte, char* board, char* structures) { structures[byte] = 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) { 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 main() { char board[BOARD_BYTES]; char structures[BOARD_BYTES]; initialize_board(board); initialize_board(structures); place_tile("RFRFR", 6, board); place_tile("RCCRR", 8, board); place_tile("RRFRF", 18, board); place_tile("CFFCC", 16, board); create_structure_mask(board, structures); print_board(board); printf("\n\n"); print_board(structures); return 0; }