aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2022-03-31 23:38:44 +0300
committereug-vs <eugene@eug-vs.xyz>2022-03-31 23:38:44 +0300
commit757b45c1ff1968f01b46cab6014686a43ecc17d6 (patch)
tree15798ce80c0ac1d8538a2ad31cb9a660c31ec57f
parent7dbd21f32c633e6c0c7fe9c55accda3bfc9fc88c (diff)
downloadcarcassonne-engine-c-757b45c1ff1968f01b46cab6014686a43ecc17d6.tar.gz
feat: add tile preview with rotation
-rw-r--r--src/board.c11
-rw-r--r--src/board.h1
-rw-r--r--src/main.c45
3 files changed, 50 insertions, 7 deletions
diff --git a/src/board.c b/src/board.c
index 263e205..056156c 100644
--- a/src/board.c
+++ b/src/board.c
@@ -48,6 +48,17 @@ int place_tile(Tile tile, int index, BoardUnit* board, int force) {
return 1;
}
+void rotate_tile(Tile* tile, int increment) {
+ char buffer[8];
+ for (int i = 0; i < 8; i++) {
+ buffer[i] = tile->edges[i % 4];
+ }
+
+ for (int i = 0; i < 4; i++) {
+ tile->edges[i] = buffer[i + (increment % 4)];
+ }
+}
+
void traverse_structure(int group, int index, BoardUnit* board) {
board[index].structure_group = group;
for (int i = 0; i < 4; i++) {
diff --git a/src/board.h b/src/board.h
index e0acd1f..9745e3a 100644
--- a/src/board.h
+++ b/src/board.h
@@ -28,6 +28,7 @@ int is_center_index(int index);
/* tiles */
int is_allowed_placement(Tile tile, int index, BoardUnit* board);
int place_tile(Tile tile, int index, BoardUnit* board, int force);
+void rotate_tile(Tile* tile, int increment);
/* structures */
void traverse_structure(int group, int index, BoardUnit* board);
diff --git a/src/main.c b/src/main.c
index 680b9c3..09ca804 100644
--- a/src/main.c
+++ b/src/main.c
@@ -25,6 +25,14 @@ void draw_structures(BoardUnit* board, WINDOW* win) {
}
}
+void preview_tile(Tile tile, WINDOW* win) {
+ mvwaddch(win, 1, 1, tile.center);
+ mvwaddch(win, 0, 1, tile.edges[0]);
+ mvwaddch(win, 1, 2, tile.edges[1]);
+ mvwaddch(win, 2, 1, tile.edges[2]);
+ mvwaddch(win, 1, 0, tile.edges[3]);
+}
+
int main() {
/* initialize curses */
initscr();
@@ -48,6 +56,12 @@ int main() {
box(input_box, 0, 0);
wrefresh(input_box);
+ /* create tile inspector window */
+ WINDOW* tile_box = newwin(5, 5, BOARD_ROW_UNITS + 3, 60);
+ WINDOW* tile_win = derwin(tile_box, 3, 3, 1, 1);
+ box(tile_box, 0, 0);
+ wrefresh(tile_box);
+
/* create messages window */
WINDOW* messages_box = newwin(40, 60, BOARD_ROW_UNITS + 6, 0);
WINDOW* messages_win = derwin(messages_box, 40 - 2, 60 - 2, 1, 1);
@@ -58,11 +72,18 @@ int main() {
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 position[3];
+ char rotation_input;
while (1) {
/* prepare */
refresh_structure_groups(board);
@@ -74,18 +95,28 @@ int main() {
wrefresh(board_win);
wrefresh(structures_win);
- /* user input */
- box(input_box, 0, 0);
- mvwaddstr(input_box, 0, 0, "Enter tile:");
- wrefresh(input_box);
- wgetnstr(input_win, tile.edges, 5);
- wclear(input_win);
- if (tile.edges[0] == 'q') break;
+ /* tile pickup */
+ tile = tileset[rand() % 3];
+
+ while (1) {
+ preview_tile(tile, tile_win);
+ wrefresh(tile_win);
+ rotation_input = wgetch(tile_win);
+ if (rotation_input == 10) break; /* enter key */
+ else if (rotation_input == 'j') rotate_tile(&tile, 1);
+ else if (rotation_input == 'k') rotate_tile(&tile, 3);
+ }
+
+ /* position prompt */
+ echo();
box(input_box, 0, 0);
mvwaddstr(input_box, 0, 0, "Enter position:");
wrefresh(input_box);
wgetnstr(input_win, position, 3);
+ noecho();
+
+ if (position[0] == 'q') break;
int result = place_tile(tile, translate_coordinate(atoi(position)), board, 0);
if (result) wprintw(messages_win, "Placed tile %s (%c) at position %i\n", tile.edges, tile.center, atoi(position));