summaryrefslogtreecommitdiff
path: root/sirtet.c
blob: 9a2ef6970dca966167f0231438520eca7b237d6b (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <getopt.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] = ' ';
        }
    }
}

int 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
    // returns total number of blocks cleared
    bool* rows_to_clear = malloc(mapH * sizeof(bool));
    bool* cols_to_clear = malloc(mapW * sizeof(bool));
    int rtc_count = 0;
    int ctc_count = 0;
    // scan all rows and columns
    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;
    }
    // clear rows and columns marked true in `{rows,columns}_to_clear`
    for (int r = 0; r < mapH; r++) {
        if (rows_to_clear[r]) {
            rtc_count++;
            for (int c = 0; c < mapW; c++)
                map[r][c] = ' ';
        }
    }
    for (int c = 0; c < mapW; c++) {
        if (cols_to_clear[c]) {
            ctc_count++;
            for (int r = 0; r < mapH; r++)
                map[r][c] = ' ';
        }
    }
    free(rows_to_clear);
    free(cols_to_clear);
    return (rtc_count * mapW + ctc_count * mapH - rtc_count * ctc_count);
}

void sirtet(int H, int W, int P) {
    // 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);
    refillpieces(hand, P);

    bool over = false;
    int points = 0;

    // 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);
        printhelp(3, 2 * W + 8);

        // display game stats
        mvprintw(1, 2 * W + 8, "Points: %d", points);
        refresh();

        // 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:
                case 'a':
                case 'h':
                    if (col > 0) col--;
                    break;
                case KEY_RIGHT:
                case 'd':
                case 'l':
                    if (col + pc->w < W) col++;
                    break;
                case KEY_UP:
                case 'w':
                case 'k':
                    if (row > 0) row--;
                    break;
                case KEY_DOWN:
                case 's':
                case 'j':
                    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];
                            // move piece inside boundary if necessary
                            if (row + (pc->h) > H)
                                row = H - pc->h;
                            if (col + (pc->w) > W)
                                col = W - pc->w;
                            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];
                            if (row + (pc->h) > H)
                                row = H - pc->h;
                            if (col + (pc->w) > W)
                                col = W - pc->w;
                            break;
                        }
                    }
                    break;
                // q: quit
                case 'q':
                    confirmed = true;
                    over = true;
                    break;
            }
        }

        place(map, pc, row, col);
        points += pc->points;
        freepiece(pc);
        hand[pc_idx] = NULL;

        // clear full rows and columns
        int blocks_cleared = clearfull(map, H, W);
        points += blocks_cleared;

        // 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(7, 2 * W + 8, "Game over");
            mvprintw(8, 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[]) {
    int H = 8; // map height
    int W = 8; // map width
    int P = 3; // max # of pieces at hand

    // handle CLI arguments
    int opt = 0;
    while (opt != -1) {
        opt = getopt(argc, argv, "h:w:p:");
        switch (opt) {
            case 'h':
                sscanf(optarg, "%d", &H);
                if (H < 5) {
                    printf("Map height cannot be less than 5\n");
                    return 1;
                }
                break;
            case 'w':
                sscanf(optarg, "%d", &W);
                if (W < 5) {
                    printf("Map width cannot be less than 5\n");
                    return 1;
                }
                break;
            case 'p':
                sscanf(optarg, "%d", &P);
                if (P < 1) {
                    printf("You must wield at least one piece\n");
                    return 1;
                }
                break;
        }
    }
    sirtet(H, W, P);
    return 0;
}