-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.c
67 lines (63 loc) · 1.61 KB
/
piece.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
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "piece.h"
static Coordinate PIECES[7][BLOCKS_PER_PIECE] = {
{
COORDINATE(0, -1), COORDINATE(0, 0), COORDINATE(0, 1), COORDINATE(-1, 1)
},
{
COORDINATE(0, -1), COORDINATE(0, 0), COORDINATE(0, 1), COORDINATE(1, 1)
},
{
COORDINATE(0, -1), COORDINATE(0, 0), COORDINATE(-1, 0), COORDINATE(-1, 1)
},
{
COORDINATE(0, -1), COORDINATE(0, 0), COORDINATE(1, 0), COORDINATE(1, 1)
},
{
COORDINATE(-1, 0), COORDINATE(0, 0), COORDINATE(0, 1), COORDINATE(1, 0)
},
{
COORDINATE(0, 0), COORDINATE(1, 0), COORDINATE(1, 1), COORDINATE(0, 1)
},
{
COORDINATE(-1, 0), COORDINATE(0, 0), COORDINATE(1, 0), COORDINATE(2, 0),
}
};
bool piece_init(Piece* piecePtr, enum PieceType type) {
if (piecePtr == NULL) {
return false;
}
piecePtr->type = type;
piecePtr->pos.x = 0;
piecePtr->pos.y = 0;
int i;
for (i = 0; i < BLOCKS_PER_PIECE; i++) {
piecePtr->blocks[i] = PIECES[type-1][i];
}
return true;
}
void piece_rotate(Piece* piecePtr, bool counter) {
if (piecePtr == NULL) {
return;
}
/* Block pieces are special and don't get rotated. */
if (piecePtr->type == BLOCK_PIECE) {
return;
}
int i;
Coordinate c;
for (i = 0; i < BLOCKS_PER_PIECE; i++) {
c = piecePtr->blocks[i];
int ox = c.x, oy = c.y;
if (counter) {
c.x = oy;
c.y = -ox;
} else {
c.x = -oy;
c.y = ox;
}
piecePtr->blocks[i] = c;
}
}