diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 87 |
1 files changed, 40 insertions, 47 deletions
@@ -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); |