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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
#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();
clear();
noecho();
cbreak();
keypad(stdscr, true);
curs_set(0); // set cursor invisible
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);
// select piece and position
bool confirmed = false;
int pc_idx;
struct piece* pc;
for (int i = 0; i < P; i++) {
// find first non-null piece
if (hand[i] != NULL) {
pc_idx = i;
pc = hand[i];
break;
}
}
int row = 0;
int col = 0;
while (!confirmed) {
printmap(map, 1, 1, H, W);
printpieces(hand, H + 2, 0, P, pc_idx);
bool pos_placeable = placeable(map, pc, row, col, H, W);
printpiece(pc, row + 1, col * 2 + 1, (pos_placeable ? 1 : 2));
// await user input
int ch = getch();
switch (ch) {
// enter: confirm choice
case 10:
if (pos_placeable)
confirmed = true;
break;
// arrow keys: move piece on map
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;
// [ and ]: switch pieces
case '[':
for (int i = 0; i < P; i++) {
// find previous non-null piece
pc_idx = (pc_idx + P - 1) % P;
if (hand[pc_idx] != NULL) {
pc = hand[pc_idx];
break;
}
}
break;
case ']':
for (int i = 0; i < P; i++) {
// find next non-null piece
pc_idx = (pc_idx + 1) % P;
if (hand[pc_idx] != NULL) {
pc = hand[pc_idx];
break;
}
}
break;
}
}
place(map, pc, row, col);
freepiece(pc);
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
mvprintw(3, 2 * W + 8, "Game over");
mvprintw(4, 2 * W + 8, "Press any key to exit");
refresh();
while (getch() == 0);
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;
}
|