1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <locale.h>
#include <ncurses.h>
#include "pieces.h"
#include "ui.h"
void clearmap(char** map, int mapH, int mapW) {
for (int r = 0; r < mapH; r++) {
for (int c = 0; c < mapW; c++) {
map[r][c] = ' ';
}
}
}
void clearfull(char** map, int mapH, int mapW) {
// if a row or column is full, clear it
// if a row and a column are full at the same time, clear both
// which is why we need to scan all rows/columns before actually clearing them
bool* rows_to_clear = malloc(mapH * sizeof(bool));
bool* cols_to_clear = malloc(mapW * sizeof(bool));
for (int r = 0; r < mapH; r++) {
bool clear = true;
for (int c = 0; c < mapW; c++) {
if (map[r][c] == ' ')
clear = false;
}
rows_to_clear[r] = clear;
}
for (int c = 0; c < mapW; c++) {
bool clear = true;
for (int r = 0; r < mapH; r++) {
if (map[r][c] == ' ')
clear = false;
}
cols_to_clear[c] = clear;
}
for (int r = 0; r < mapH; r++) {
if (rows_to_clear[r]) {
for (int c = 0; c < mapW; c++)
map[r][c] = ' ';
}
}
for (int c = 0; c < mapW; c++) {
if (cols_to_clear[c]) {
for (int r = 0; r < mapW; r++)
map[r][c] = ' ';
}
}
free(rows_to_clear);
free(cols_to_clear);
}
bool placeable(char** map, struct piece* pc, int row, int col, int mapH, int mapW) {
// boundary check
if (row < 0 || row + (pc->h) > mapH || col < 0 || col + (pc->w) > mapW)
return false;
// check if blocks to be taken by `pc` are vacant
for (int r = 0; r < pc->h; r++) {
for (int c = 0; c < pc->w; c++) {
if (map[row + r][col + c] == '+' && pc->blocks[r * pc->w + c] == '+')
return false;
}
}
return true;
}
void place(char** map, struct piece* pc, int row, int col) {
for (int r = 0; r < pc->h; r++) {
for (int c = 0; c < pc->w; c++) {
if (pc->blocks[r * (pc->w) + c] == '+')
map[row + r][col + c] = '+';
}
}
}
void freepiece(struct piece* pc) {
free(pc->blocks);
free(pc);
}
void refillpieces(struct piece** hand, int nhand) {
// randomly select `no` pieces from `pool` to `hand`
// capacities of `hand` and `pool` are `nhand` and `npool` resp.
for (int i = 0; i < nhand; i++) {
hand[i] = randpiece();
}
}
void sirtet() {
const int H = 8; // canvas height
const int W = 8; // canvas width
const int P = 3; // maximum # of pieces at hand
// init memory and game state
char** map = malloc(H * sizeof(char*));
for (int r = 0; r < H; r++) {
map[r] = malloc(W * sizeof(char));
}
struct piece** hand = malloc(P * sizeof(struct piece*));
srand(time(NULL));
clearmap(map, H, W);
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, 1, 1, H, W);
printpieces(hand, H + 2, 0, P);
// await user input
int pc_idx;
int row;
int 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++) {
if (hand[i] != NULL) {
pieces_left = true;
break;
}
}
if (!pieces_left)
refillpieces(hand, P);
// game over if none of the pieces are placeable
bool has_placeable = false;
for (int i = 0; i < P; i++) {
if (hand[i] == NULL)
continue;
for (int r = 0; r <= H - hand[i]->h; r++) {
for (int c = 0; c <= W - hand[i]->w; c++) {
if (placeable(map, hand[i], r, c, H, W))
has_placeable = true;
}
}
}
if (!has_placeable) {
// game over
printw("Game over\n");
refresh();
over = true;
}
}
// free memory
for (int r = 0; r < H; r++) {
free(map[r]);
}
free(map);
for (int i = 0; i < P; i++) {
if (hand[i] != NULL)
freepiece(hand[i]);
}
free(hand);
// end ncurses
endwin();
}
int main(int argc, char *argv[]) {
sirtet();
return 0;
}
|