diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | sirtet.c | 70 | ||||
-rw-r--r-- | ui.c | 61 | ||||
-rw-r--r-- | ui.h | 7 |
4 files changed, 89 insertions, 51 deletions
@@ -1,7 +1,7 @@ all: compile run compile: - gcc -g sirtet.c pieces.c -o sirtet + gcc -g sirtet.c pieces.c ui.c -lncurses -o sirtet run: ./sirtet @@ -2,44 +2,10 @@ #include <stdio.h> #include <stdbool.h> #include <time.h> +#include <locale.h> +#include <ncurses.h> #include "pieces.h" - -void printmap(char** map, int nrow, int ncol) { - printf(" "); - for (int c = 0; c < ncol; c++) { - printf("-"); - } - printf("\n"); - for (int r = 0; r < nrow; r++) { - printf("|"); - for (int c = 0; c < ncol; c++) { - printf("%c", map[r][c]); - } - printf("|\n"); - } - printf(" "); - for (int c = 0; c < ncol; c++) { - printf("-"); - } - printf("\n"); -} - -void printpieces(struct piece** hand, int nhand) { - for (int i = 0; i < nhand; i++) { - if (hand[i] == NULL) - continue; - - printf("Piece %d\n", i); - struct piece* pc = hand[i]; - for (int r = 0; r < pc->h; r++) { - for (int c = 0; c < pc->w; c++) { - printf("%c", pc->blocks[r * pc->w + c]); - } - printf("\n"); - } - printf("\n"); - } -} +#include "ui.h" void clearmap(char** map, int mapH, int mapW) { for (int r = 0; r < mapH; r++) { @@ -142,30 +108,30 @@ void sirtet() { bool over = false; refillpieces(hand, P); + // initialize ncurses + setlocale(LC_ALL, ""); + initscr(); + noecho(); + keypad(stdscr, true); + curs_set(0); // set cursor invisible + printrect(0, 0, H, W); + // begin game loop while (!over) { - printmap(map, H, W); - printpieces(hand, P); + printmap(map, 1, 1, H, W); + printpieces(hand, H + 2, 0, P); // await user input int pc_idx; int row; int col; - printf("Input [piece #] [row #] [col #]: "); - scanf("%d %d %d", &pc_idx, &row, &col); - while ( - pc_idx < 0 || pc_idx >= P || hand[pc_idx] == NULL || - !placeable(map, hand[pc_idx], row, col, H, W) - ) { - printf("You cannot do that. Try again: "); - scanf("%d %d %d", &pc_idx, &row, &col); - } + refresh(); + while(1); place(map, hand[pc_idx], row, col); freepiece(hand[pc_idx]); hand[pc_idx] = NULL; // clear full rows and columns clearfull(map, H, W); - // if player has no piece left, refill bool pieces_left = false; for (int i = 0; i < P; i++) { @@ -192,7 +158,8 @@ void sirtet() { } if (!has_placeable) { // game over - printf("Game over\n"); + printw("Game over\n"); + refresh(); over = true; } } @@ -207,6 +174,9 @@ void sirtet() { freepiece(hand[i]); } free(hand); + + // end ncurses + endwin(); } int main(int argc, char *argv[]) { @@ -0,0 +1,61 @@ +#include <ncurses.h> +#include "pieces.h" + +/* + * ncurses ui has an x scale factor of 2 + */ + +void printrect(int y, int x, int h, int w) { + // print rectangle whose left corner is at (y, x) and internal area is h*w + // top & bottom + for (int c = 0; c < 2 * w; c++) { + mvaddch(y, x + c + 1, ACS_HLINE); + mvaddch(y + h + 1, x + c + 1, ACS_HLINE); + } + // left & right + for (int r = 0; r < h; r++) { + mvaddch(y + r + 1, x, ACS_VLINE); + mvaddch(y + r + 1, x + 2 * w + 1, ACS_VLINE); + } + // corners + mvaddch(y, x, ACS_ULCORNER); + mvaddch(y, x + 2 * w + 1, ACS_URCORNER); + mvaddch(y + h + 1, x, ACS_LLCORNER); + mvaddch(y + h + 1, x + 2 * w + 1, ACS_LRCORNER); + refresh(); +} + +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)); + addch(' ' | A_REVERSE); + } + } + } + refresh(); +} + +void printpieces(struct piece** hand, int y, int x, int nhand) { + 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); + } + } + } + x += 2 * pc->w + 8; + } + refresh(); +} + @@ -0,0 +1,7 @@ +#ifndef UI_H +#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); +#endif + |