diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-06-01 10:00:50 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-06-01 10:00:50 +0800 |
commit | d94bf01fdd4b8e9f307463ea04454c8db4824859 (patch) | |
tree | f5762b4084e00a89b27cd2a1a50a3f5a28d89084 /sirtet.c | |
parent | 5ae9641cbd6def9126bc039bf29384f8fed98de6 (diff) |
Select and place piece
Diffstat (limited to 'sirtet.c')
-rw-r--r-- | sirtet.c | 89 |
1 files changed, 80 insertions, 9 deletions
@@ -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 |