diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-04-01 00:55:44 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-04-01 01:08:30 +0300 |
commit | 87877154fa2386dc99aa21389144aa2debec656a (patch) | |
tree | 6bb0bc38c340a472f707beeeb96d896672481e1e | |
parent | f9da494260fb1b876378bdc3eadd7bc15aec0221 (diff) | |
download | carcassonne-engine-c-87877154fa2386dc99aa21389144aa2debec656a.tar.gz |
feat: indicate allowed placements with color
-rw-r--r-- | src/board.c | 11 | ||||
-rw-r--r-- | src/main.c | 28 |
2 files changed, 26 insertions, 13 deletions
diff --git a/src/board.c b/src/board.c index 056156c..a09b45c 100644 --- a/src/board.c +++ b/src/board.c @@ -30,15 +30,8 @@ int is_allowed_placement(Tile tile, int index, BoardUnit* board) { } 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; - } + if (!is_center_index(index)) return 0; + if (!force && !is_allowed_placement(tile, index, board)) return 0; board[index].feature = tile.center; for (int i = 0; i < 4; i++) { @@ -29,33 +29,43 @@ int main() { /* initialize curses */ initscr(); cbreak(); + use_default_colors(); + + /* colors */ + start_color(); + init_pair(1, COLOR_GREEN, COLOR_BLACK); /* create board window */ WINDOW* board_box = newwin(BOARD_ROW_UNITS + 2, BOARD_ROW_UNITS + 2, 0, 0); WINDOW* board_win = derwin(board_box, BOARD_ROW_UNITS, BOARD_ROW_UNITS, 1, 1); box(board_box, 0, 0); + mvwaddstr(board_box, 0, 1, "Board"); wrefresh(board_box); /* create structures window */ WINDOW* structures_box = newwin(BOARD_ROW_UNITS + 2, BOARD_ROW_UNITS + 2, 0, BOARD_ROW_UNITS + 3); WINDOW* structures_win = derwin(structures_box, BOARD_ROW_UNITS, BOARD_ROW_UNITS, 1, 1); box(structures_box, 0, 0); + mvwaddstr(structures_box, 0, 1, "Structures"); wrefresh(structures_box); /* create messages window */ WINDOW* messages_box = newwin(40, 60, BOARD_ROW_UNITS + 2, 0); WINDOW* messages_win = derwin(messages_box, 40 - 2, 60 - 2, 1, 1); box(messages_box, 0, 0); + mvwaddstr(messages_box, 0, 1, "Log"); wrefresh(messages_box); BoardUnit board[BOARD_UNITS]; initialize_board(board); - Tile tileset[3] = { + Tile tileset[5] = { { "RRRR", 'R', 0 }, { "FCCC", 'C', 0 }, - { "FFFC", 'C', 0 } + { "FFFC", 'C', 0 }, + { "CCRR", 'C', 0 }, + { "RFRF", 'R', 0 } }; Tile tile = { "FRCR", 'R', 0 }; @@ -74,7 +84,7 @@ int main() { wrefresh(structures_win); /* tile placement */ - tile = tileset[rand() % 3]; + tile = tileset[rand() % 5]; int position = 0; BoardUnit board_preview[BOARD_UNITS]; @@ -83,16 +93,26 @@ int main() { board_preview[i].feature = board[i].feature; } place_tile(tile, translate_coordinate(position), board_preview, 1); + + int is_allowed = is_allowed_placement(tile, translate_coordinate(position), board); + + if (is_allowed) wattron(board_win, COLOR_PAIR(1)); + else wattroff(board_win, COLOR_PAIR(1)); + draw_board(board_preview, board_win); wrefresh(board_win); input_key = wgetch(board_win); - if (input_key == 10) break; /* enter key */ + if (input_key == 10 && is_allowed) break; /* enter key */ else if (input_key == 'l') position += 1; else if (input_key == 'h') position -= 1; else if (input_key == 'j') position += BOARD_WIDTH; else if (input_key == 'k') position -= BOARD_WIDTH; else if (input_key == 'r') rotate_tile(&tile, 3); + else if (input_key == 'q') { + endwin(); + return 0; + } } int result = place_tile(tile, translate_coordinate(position), board, 0); |