-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.c
264 lines (238 loc) · 7.23 KB
/
board.c
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
#include <stdlib.h>
#include <stdbool.h>
#include <curses.h>
#include <unistd.h>
#include "board.h"
#include "color.h"
/* Returns if a piece in a particular position or rotation is valid on the
* given board. */
bool isPieceValid(Board* board, Piece piece);
void board_drawBoard(Board* boardPtr, WINDOW* subWin);
void board_shiftLine(Board* boardPtr, int line);
/* Draws a "chunk" on the board. */
void drawChunk(WINDOW* subWin, int x, int y, int col);
/* Draws a projection so down to where the piece will be */
void drawProjection(WINDOW* subWin, int x, int y, int col);
Board* board_init(int width, int height) {
Board* boardPtr = malloc(sizeof(Board));
if (boardPtr == NULL) {
return NULL;
}
boardPtr->width = width;
boardPtr->height = height;
boardPtr->board = calloc((unsigned long)(width*height), sizeof(char));
if (boardPtr->board == NULL) {
free(boardPtr);
return NULL;
}
boardPtr->piece = NULL;
boardPtr->cleared = calloc((unsigned long)height, sizeof(bool));
return boardPtr;
}
void board_free(Board* boardPtr) {
if (boardPtr != NULL) {
if (boardPtr->board != NULL) {
free(boardPtr->board);
}
if (boardPtr->cleared != NULL) {
free(boardPtr->cleared);
}
free(boardPtr);
}
}
void board_setPiece(Board* boardPtr, enum PieceType type) {
if (boardPtr == NULL) {
return;
}
if (boardPtr->piece == NULL) {
boardPtr->piece = malloc(sizeof(Piece));
}
piece_init(boardPtr->piece, type);
boardPtr->piece->pos.x = boardPtr->width/2 - 1;
}
void board_setCleared(Board* boardPtr, int line) {
if (boardPtr == NULL || boardPtr->cleared == NULL ||
line > boardPtr->height) {
return;
}
boardPtr->cleared[line] = true;
}
bool board_isLineFull(Board* boardPtr, int line) {
if (boardPtr == NULL || line < 0 || line >= boardPtr->height) {
return false;
}
int i;
for (i = 0; i < boardPtr->width; i++) {
if (boardPtr->board[COORD_INDEX(boardPtr, i, line)] == 0) {
return false;
}
}
return true;
}
bool board_movePiece(Board* boardPtr, int x, int y) {
if (boardPtr == NULL || boardPtr->piece == NULL) {
return false;
}
Piece newPiece = *(boardPtr->piece);
Coordinate offset = COORDINATE(x, y);
newPiece.pos = coordinate_add(newPiece.pos, offset);
if (!isPieceValid(boardPtr, newPiece)) {
return false;
}
// it's valid, so save it
*(boardPtr->piece) = newPiece;
return true;
}
bool board_rotatePiece(Board* boardPtr, bool counter) {
if (boardPtr == NULL || boardPtr->piece == NULL) {
return false;
}
Piece newPiece = *(boardPtr->piece);
piece_rotate(&newPiece, counter);
if (!isPieceValid(boardPtr, newPiece)) {
return false;
}
*(boardPtr->piece) = newPiece;
return true;
}
void board_cementPiece(Board* boardPtr) {
if (boardPtr == NULL || boardPtr->piece == NULL) {
return;
}
Piece piece = *(boardPtr->piece);
int i;
for (i = 0; i < BLOCKS_PER_PIECE; i++) {
Coordinate c = coordinate_add(piece.pos, piece.blocks[i]);
boardPtr->board[COORD_INDEX(boardPtr, c.x, c.y)] =
(unsigned char)piece.type;
}
boardPtr->piece = NULL;
}
void board_draw(Board* boardPtr, WINDOW* subWin, bool showProjection) {
if (boardPtr == NULL || subWin == NULL) {
return;
}
board_drawBoard(boardPtr, subWin);
// draw the piece
if (boardPtr->piece != NULL) {
int i;
Piece piece = *(boardPtr->piece);
// draw the projection to where the piece will be
if (showProjection) {
Piece projection = piece;
while (isPieceValid(boardPtr, projection) && piece.pos.y <= boardPtr->height) {
projection.pos.y++;
}
projection.pos.y--;
for (i = 0; i < BLOCKS_PER_PIECE; i++) {
Coordinate c = coordinate_add(projection.pos, projection.blocks[i]);
if (c.y > 0) {
drawProjection(subWin, c.x, c.y, (int)piece.type);
}
}
}
for (i = 0; i < BLOCKS_PER_PIECE; i++) {
Coordinate c = coordinate_add(piece.pos, piece.blocks[i]);
if (c.y > 0) {
drawChunk(subWin, c.x, c.y, (int)piece.type);
}
}
}
touchwin(subWin);
wrefresh(subWin);
}
void board_clearLines(Board* boardPtr, WINDOW* subWin, unsigned int delay, int count) {
if (boardPtr == NULL || subWin == NULL) {
return;
}
/* double count so we can use the mod 2 trick */
count *= 2;
int x, y;
for (; count > 0; count--) {
board_drawBoard(boardPtr, subWin);
if (count % 2 == 0)
{
for (y = 0; y < boardPtr->height; y++) {
if (boardPtr->cleared[y]){
for (x = 0; x < boardPtr->width; x++) {
drawChunk(subWin, x, y, COLOR_BLACK);
}
}
}
}
touchwin(subWin);
wrefresh(subWin);
usleep(delay);
}
for (y = 0; y < boardPtr->height; y++) {
if (boardPtr->cleared[y]) {
board_shiftLine(boardPtr, y);
boardPtr->cleared[y] = false;
}
}
board_drawBoard(boardPtr, subWin);
touchwin(subWin);
wrefresh(subWin);
}
void board_drawBoard(Board* boardPtr, WINDOW* subWin) {
box(subWin, 0, 0);
int x, y;
for (y = 0; y < boardPtr->height; y++) {
for (x = 0; x < boardPtr->width; x++) {
int i = COORD_INDEX(boardPtr, x, y);
unsigned char val = boardPtr->board[i];
drawChunk(subWin, x, y, val);
}
}
}
void board_shiftLine(Board* boardPtr, int line) {
if (boardPtr == NULL || line < 0 || line >= boardPtr->height) {
return;
}
int x;
for (x = 0; x < boardPtr->width; x++) {
int y;
for (y = line; y > 0; y--) {
boardPtr->board[COORD_INDEX(boardPtr, x, y)] =
boardPtr->board[COORD_INDEX(boardPtr, x, y-1)];
}
boardPtr->board[COORD_INDEX(boardPtr, x, 0)] = 0;
}
}
void board_clear(Board* boardPtr) {
if (boardPtr == NULL) {
return;
}
int i;
for (i = 0; i < boardPtr->width*boardPtr->height; i++) {
boardPtr->board[i] = 0;
}
}
bool isPieceValid(Board* boardPtr, Piece piece) {
int i;
for (i = 0; i < BLOCKS_PER_PIECE; i++) {
Coordinate c = coordinate_add(piece.pos, piece.blocks[i]);
if (c.x >= boardPtr->width || c.x < 0 ||
c.y >= boardPtr->height) {
return false;
}
// make sure that it's empty
unsigned char val = boardPtr->board[COORD_INDEX(boardPtr, c.x, c.y)];
if (val > 0) {
return false;
}
}
return true;
}
void drawChunk(WINDOW* subWin, int x, int y, int col) {
attr_t attrs = COLOR_PAIR(col);
wattron(subWin, attrs);
mvwprintw(subWin, y+1, (x*2)+1, " ");
wattroff(subWin, attrs);
}
void drawProjection(WINDOW* subWin, int x, int y, int col) {
attr_t attrs = COLOR_PAIR(col + 8);
wattron(subWin, attrs);
mvwprintw(subWin, y+1, (x*2)+1, "::");
wattroff(subWin, attrs);
}