#include #include #include #include #include "carcassonne.h" #include "gui.h" int main() { /* initialize curses */ initscr(); cbreak(); srand(time(NULL)); initialize_colors(); WINDOW* board_win = create_framed_window("Board", BOARD_ROW_UNITS + 2, BOARD_ROW_UNITS + 2, 0, 0); WINDOW* messages_win = create_framed_window("Log", BOARD_ROW_UNITS + 2, 80, 0, BOARD_ROW_UNITS + 3); scrollok(messages_win, 1); Tile tileset[69]; BoardUnit board[BOARD_UNITS]; int available_meeples[PLAYERS]; int meeple_map[MAX_STRUCTURES * PLAYERS]; Tile tile; initialize_tileset(tileset); initialize_board(board); initialize_available_meeples(available_meeples); place_tile((Tile){ "FRCR", 'R', 0 }, translate_coordinate(BOARD_WIDTH * BOARD_WIDTH / 2), board, 1); /* main loop */ int tile_index; char input_key; for (int move = 0; ; move++) { int player = (move % PLAYERS) + 1; /* board */ refresh_structure_groups(board); refresh_meeple_map(board, meeple_map); draw_board(board, meeple_map, board_win); wattron(messages_win, get_player_color(player)); wprintw(messages_win, "Move %i, (player #%i, %i meeples available)\n", move, player, available_meeples[player - 1]); wrefresh(messages_win); /* tile placement */ while (1) { tile_index = rand() % 69; tile = tileset[tile_index]; if (tile.center != USED) break; } int position = 0; BoardUnit board_preview[BOARD_UNITS]; while (1) { for (int i = 0; i < BOARD_UNITS; i++) { board_preview[i].feature = board[i].feature; board_preview[i].structure_group = board[i].structure_group; } /* highlight available placements */ for (int k = 0; k < BOARD_WIDTH * BOARD_WIDTH; k++) { if (is_allowed_placement(tile, translate_coordinate(k), board)) { board_preview[translate_coordinate(k)].feature = PREVIEW_AVAILABLE; } } 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(2)); draw_board(board_preview, meeple_map, board_win); wattroff(board_win, COLOR_PAIR(2)); input_key = wgetch(board_win); 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); 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); complete_structures(board, available_meeples); refresh_meeple_map(board, meeple_map); /* meeple placement */ input_key = wgetch(board_win); int meeple_index = translate_coordinate(position); if (input_key == 'l') meeple_index += 1; else if (input_key == 'h') meeple_index -= 1; else if (input_key == 'j') meeple_index += BOARD_ROW_UNITS; else if (input_key == 'k') meeple_index -= BOARD_ROW_UNITS; else if (input_key != 10) meeple_index = -1; if (meeple_index >= 0 && available_meeples[player - 1] > 0) { if (is_allowed_meeple(player, meeple_index, board, meeple_map)) { board[meeple_index].meeple = player; available_meeples[player - 1]--; wprintw(messages_win, "Placed meeple at %c\n", board[meeple_index].feature); } else wprintw(messages_win, "Could not place meeple at %c\n", board[meeple_index].feature); } tileset[tile_index].center = USED; wrefresh(messages_win); } endwin(); return 0; }