diff options
-rw-r--r-- | sirtet.c | 89 | ||||
-rw-r--r-- | ui.c | 31 | ||||
-rw-r--r-- | ui.h | 3 |
3 files changed, 103 insertions, 20 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 @@ -29,31 +29,42 @@ void printmap(char** map, int y, int x, int mapH, int mapW) { for (int r = 0; r < mapH; r++) { for (int c = 0; c < mapW; c++) { if (map[r][c] == '+') { - mvaddch(y + r + 1, x + 2 * c + 1, (' ' | A_REVERSE)); + mvaddch(y + r, x + 2 * c, (' ' | A_REVERSE)); addch(' ' | A_REVERSE); + } else { + mvaddch(y + r, x + 2 * c, (' ')); + addch(' '); } } } refresh(); } -void printpieces(struct piece** hand, int y, int x, int nhand) { +void printpiece(struct piece* pc, int y, int x, int color_pair) { + attron(COLOR_PAIR(color_pair)); + for (int r = 0; r < pc->h; r++) { + for (int c = 0; c < pc->w; c++) { + if (pc->blocks[r * pc->w + c] == '+') { + mvaddch(y + r, x + 2 * c, (' ' | A_REVERSE)); + addch(' ' | A_REVERSE); + } + } + } + attroff(COLOR_PAIR(color_pair)); +} + +void printpieces(struct piece** hand, int y, int x, int nhand, int highlight) { for (int i = 0; i < nhand; i++) { if (hand[i] == NULL) continue; struct piece* pc = hand[i]; move(y, x); + printw("%d", i); printrect(y + 1, x, pc->h + 2, pc->w + 2); - for (int r = 0; r < pc->h; r++) { - for (int c = 0; c < pc->w; c++) { - if (pc->blocks[r * pc->w + c] == '+') { - mvaddch(y + r + 3, x + 2 * c + 3, (' '| A_REVERSE)); - addch(' ' | A_REVERSE); - } - } - } + printpiece(pc, y + 3, x + 3, (i == highlight ? 1 : 0)); + x += 2 * pc->w + 8; } refresh(); @@ -2,6 +2,7 @@ #define UI_H void printrect(int y, int x, int h, int w); void printmap(char** map, int y, int x, int mapH, int mapW); -void printpieces(struct piece** hand, int y, int x, int nhand); +void printpiece(struct piece* pc, int y, int x, int color_pair); +void printpieces(struct piece** hand, int y, int x, int nhand, int highlight); #endif |