From d94bf01fdd4b8e9f307463ea04454c8db4824859 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Wed, 1 Jun 2022 10:00:50 +0800 Subject: Select and place piece --- sirtet.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 80 insertions(+), 9 deletions(-) (limited to 'sirtet.c') diff --git a/sirtet.c b/sirtet.c index 204dae7..4347fc3 100644 --- a/sirtet.c +++ b/sirtet.c @@ -111,23 +111,94 @@ void sirtet() { // initialize ncurses setlocale(LC_ALL, ""); initscr(); + clear(); noecho(); + cbreak(); keypad(stdscr, true); curs_set(0); // set cursor invisible - printrect(0, 0, H, W); + start_color(); + init_pair(1, COLOR_GREEN, COLOR_BLACK); + init_pair(2, COLOR_RED, COLOR_BLACK); // begin game loop while (!over) { + clear(); + printrect(0, 0, H, W); printmap(map, 1, 1, H, W); - printpieces(hand, H + 2, 0, P); - // await user input + + // select piece + bool piece_confirmed = false; int pc_idx; - int row; - int col; - refresh(); - while(1); - place(map, hand[pc_idx], row, col); - freepiece(hand[pc_idx]); + for (int i = 0; i < P; i++) { + if (hand[i] != NULL) { + pc_idx = i; + break; + } + } + + while (!piece_confirmed) { + printpieces(hand, H + 2, 0, P, pc_idx); + refresh(); + // await user input + int ch = getch(); + switch (ch) { + case 10: // enter + piece_confirmed = true; + break; + case KEY_LEFT: + for (int i = 0; i < P; i++) { + // find previous non-null piece + pc_idx = (pc_idx + P - 1) % P; + if (hand[pc_idx] != NULL) + break; + } + break; + case KEY_RIGHT: + for (int i = 0; i < P; i++) { + // find next non-null piece + pc_idx = (pc_idx + 1) % P; + if (hand[pc_idx] != NULL) + break; + } + break; + } + } + + struct piece* pc = hand[pc_idx]; + + // select position + bool pos_confirmed = false; + int row = 0; + int col = 0; + while (!pos_confirmed) { + printmap(map, 1, 1, H, W); + + bool pos_placeable = placeable(map, pc, row, col, H, W); + printpiece(pc, row + 1, col * 2 + 1, (pos_placeable ? 1 : 2)); + + int ch = getch(); + switch (ch) { + case 10: // enter + if (pos_placeable) + pos_confirmed = true; + break; + case KEY_LEFT: + if (col > 0) col--; + break; + case KEY_RIGHT: + if (col + pc->w < W) col++; + break; + case KEY_UP: + if (row > 0) row--; + break; + case KEY_DOWN: + if (row + pc->h < H) row++; + break; + } + } + + place(map, pc, row, col); + freepiece(pc); hand[pc_idx] = NULL; // clear full rows and columns -- cgit v1.2.3