diff options
author | eug-vs <eugene@eug-vs.xyz> | 2022-04-10 22:32:36 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2022-04-10 22:32:36 +0300 |
commit | cecf821c9526ba2e93288f4f0ff354b2cb98c452 (patch) | |
tree | 7bf2275ee933d8a1cfbc8b5f6188af5c64df6001 | |
parent | 5f583e476f3f711a581f417319670ee401a716c4 (diff) | |
download | carcassonne-engine-c-cecf821c9526ba2e93288f4f0ff354b2cb98c452.tar.gz |
feat: highlight player-controlled areas
-rw-r--r-- | src/board.c | 35 | ||||
-rw-r--r-- | src/board.h | 8 | ||||
-rw-r--r-- | src/main.c | 27 |
3 files changed, 63 insertions, 7 deletions
diff --git a/src/board.c b/src/board.c index 0e66f16..95b8738 100644 --- a/src/board.c +++ b/src/board.c @@ -111,3 +111,38 @@ int evaluate_structure(int index, BoardUnit* board) { return value; } + +void refresh_meeple_map(BoardUnit* board, int* meeple_map) { + for (int i = 0; i < MAX_STRUCTURES * PLAYERS; i++) meeple_map[i] = 0; + + for (int i = 0; i < BOARD_UNITS; i++) { + if (board[i].meeple) { + meeple_map[board[i].structure_group * PLAYERS + (board[i].meeple - 1)]++; + } + } +} + +int is_allowed_meeple(int meeple, int index, BoardUnit* board, int* meeple_map) { + if (board[index].meeple + || board[index].feature == 'F' + || board[index].feature == ANY + || board[index].feature == SEPARATOR + ) return 0; + + int dominator = get_structure_dominator(board[index].structure_group, meeple_map); + return dominator == meeple || dominator == 0; +} + +int get_structure_dominator(int structure_group, int* meeple_map) { + int dominator = 0; + int dominator_meeples = 0; + + for (int player = 0; player < PLAYERS; player++) { + int meeples = meeple_map[structure_group * PLAYERS + player]; + if (meeples > dominator_meeples) { + dominator_meeples = meeples; + dominator = player + 1; + } + } + return dominator; +} diff --git a/src/board.h b/src/board.h index a993680..cd78901 100644 --- a/src/board.h +++ b/src/board.h @@ -2,7 +2,7 @@ #define BOARD_ROW_UNITS ((BOARD_WIDTH * 2) + 1) #define BOARD_UNITS BOARD_ROW_UNITS * BOARD_ROW_UNITS #define PLAYERS 2 - +#define MAX_STRUCTURES 100 /* constants */ const char EMPTY = ' '; @@ -38,3 +38,9 @@ void rotate_tile(Tile* tile, int increment); void traverse_structure(int group, int index, BoardUnit* board); void refresh_structure_groups(BoardUnit* board); int evaluate_structure(int index, BoardUnit* board); + + +/* meeples */ +int is_allowed_meeple(int meeple, int index, BoardUnit* board, int* meeple_map); +void refresh_meeple_map(BoardUnit* board, int* meeple_map); +int get_structure_dominator(int structure_group, int* meeple_map); @@ -3,13 +3,21 @@ #include <curses.h> #include "board.h" -void draw_board(BoardUnit* board, WINDOW* win) { +int get_player_color(int player) { + return player ? COLOR_PAIR(player + 2) : COLOR_PAIR(0); +} + +void draw_board(BoardUnit* board, int* meeple_map, WINDOW* win) { wmove(win, 0, 0); for (int row = 0; row < BOARD_ROW_UNITS; row++) { for (int i = 0; i < BOARD_ROW_UNITS; i++) { int index = BOARD_ROW_UNITS * row + i; if (board[index].feature == EMPTY && is_center_index(index)) waddch(win, '.'); - else waddch(win, board[index].feature | COLOR_PAIR(board[index].structure_group % 6)); + else waddch( + win, + board[index].feature + | get_player_color(get_structure_dominator(board[index].structure_group, meeple_map)) + ); } } wrefresh(win); @@ -40,6 +48,7 @@ int main() { wrefresh(messages_box); + int meeple_map[MAX_STRUCTURES * PLAYERS]; BoardUnit board[BOARD_UNITS]; initialize_board(board); @@ -59,7 +68,8 @@ int main() { for (int move = 0; ; move++) { /* board */ refresh_structure_groups(board); - draw_board(board, board_win); + refresh_meeple_map(board, meeple_map); + draw_board(board, meeple_map, board_win); /* tile placement */ tile = tileset[rand() % 5]; @@ -76,7 +86,7 @@ int main() { int is_allowed = is_allowed_placement(tile, translate_coordinate(position), board); if (is_allowed) wattron(board_win, COLOR_PAIR(2)); - draw_board(board_preview, board_win); + draw_board(board_preview, meeple_map, board_win); wattroff(board_win, COLOR_PAIR(2)); input_key = wgetch(board_win); @@ -96,6 +106,8 @@ int main() { if (result) wprintw(messages_win, "Placed tile %s (%c) at position %i\n", tile.edges, tile.center, position); else wprintw(messages_win, "Could not place tile %s (%c) at position %i\n", tile.edges, tile.center, position); + refresh_structure_groups(board); + refresh_meeple_map(board, meeple_map); /* meeple placement */ input_key = wgetch(board_win); @@ -108,8 +120,11 @@ int main() { else if (input_key != 10) meeple_index = -1; if (meeple_index >= 0) { - board[meeple_index].meeple = move % PLAYERS; - wprintw(messages_win, "Placed meeple #%i at index %i\n", move % PLAYERS, meeple_index); + int meeple = (move % PLAYERS) + 1; + if (is_allowed_meeple(meeple, meeple_index, board, meeple_map)) { + board[meeple_index].meeple = meeple; + wprintw(messages_win, "Placed meeple #%i at index %i\n", meeple, meeple_index); + } else wprintw(messages_win, "Could not place meeple #%i at index %i\n", meeple, meeple_index); } wrefresh(messages_win); |