#include #include #include #include "board.h" void draw_board(BoardUnit* board, 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); } } } void draw_structures(BoardUnit* board, 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].structure_group == 0 ? EMPTY : '0' + board[index].structure_group); } } } int main() { /* initialize curses */ initscr(); cbreak(); /* 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); 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); 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); wrefresh(messages_box); BoardUnit board[BOARD_UNITS]; initialize_board(board); Tile tileset[3] = { { "RRRR", 'R', 0 }, { "FCCC", 'C', 0 }, { "FFFC", 'C', 0 } }; Tile tile = { "FRCR", 'R', 0 }; place_tile(tile, translate_coordinate(24), board, 1); /* main loop */ char input_key; while (1) { /* prepare */ refresh_structure_groups(board); /* draw onto the screen */ draw_board(board, board_win); draw_structures(board, structures_win); wrefresh(board_win); wrefresh(structures_win); /* tile placement */ tile = tileset[rand() % 3]; 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; } place_tile(tile, translate_coordinate(position), board_preview, 1); draw_board(board_preview, board_win); wrefresh(board_win); input_key = wgetch(board_win); if (input_key == 10) 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); } 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); wrefresh(messages_win); } endwin(); return 0; }