summaryrefslogtreecommitdiff
path: root/sirtet.c
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-05-31 22:45:15 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-05-31 22:45:15 +0800
commit5ae9641cbd6def9126bc039bf29384f8fed98de6 (patch)
tree726a5800040d3cfd0291a3005f173b09eb9fe899 /sirtet.c
parent3f963f08639000f14dee3a43d7cbbc5f780d7571 (diff)
Print map and pieces in ncurses
Diffstat (limited to 'sirtet.c')
-rw-r--r--sirtet.c70
1 files changed, 20 insertions, 50 deletions
diff --git a/sirtet.c b/sirtet.c
index 1517d98..204dae7 100644
--- a/sirtet.c
+++ b/sirtet.c
@@ -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[]) {