-
Notifications
You must be signed in to change notification settings - Fork 1
/
piece.c
153 lines (132 loc) · 4.14 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
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
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include <time.h>
#include "errors.h"
#include "piece.h"
#define DEBUG 0
#if DEBUG
#define TRACE(...) do { printf(__VA_ARGS__); } while (0)
#else
#define TRACE(...) do { } while (0)
#endif
piece_t piece_list[PIECE_COUNT] = {0};
const char* piece_to_str(piece_name_t piece)
{
switch (piece) {
case PIECE_STRAIGHT: return "STRAIGHT";
case PIECE_SQUARE: return "SQUARE";
case PIECE_T: return "T";
case PIECE_L: return "L";
case PIECE_L_REVERSE: return "L_REVERSE";
case PIECE_S: return "S";
case PIECE_S_REVERSE: return "S_REVERSE";
default: return "UNKNOWN";
}
}
void piece_init(void)
{
// PIECE_STRAIGHT
piece_list[PIECE_STRAIGHT].name = PIECE_STRAIGHT;
piece_list[PIECE_STRAIGHT].rotated_degrees = 0;
piece_list[PIECE_STRAIGHT].cells[2][0] = 1;
piece_list[PIECE_STRAIGHT].cells[2][1] = 1;
piece_list[PIECE_STRAIGHT].cells[2][2] = 1;
piece_list[PIECE_STRAIGHT].cells[2][3] = 1;
piece_list[PIECE_STRAIGHT].color = COLOR_CYAN;
// PIECE_SQUARE
piece_list[PIECE_SQUARE].name = PIECE_SQUARE;
piece_list[PIECE_SQUARE].rotated_degrees = 0;
piece_list[PIECE_SQUARE].cells[1][1] = 1;
piece_list[PIECE_SQUARE].cells[1][2] = 1;
piece_list[PIECE_SQUARE].cells[2][1] = 1;
piece_list[PIECE_SQUARE].cells[2][2] = 1;
piece_list[PIECE_SQUARE].color = COLOR_YELLOW;
// PIECE_T
piece_list[PIECE_T].name = PIECE_T;
piece_list[PIECE_T].rotated_degrees = 0;
piece_list[PIECE_T].cells[1][2] = 1;
piece_list[PIECE_T].cells[2][1] = 1;
piece_list[PIECE_T].cells[2][2] = 1;
piece_list[PIECE_T].cells[2][3] = 1;
piece_list[PIECE_T].color = COLOR_MAGENTA;
// PIECE_L
piece_list[PIECE_L].name = PIECE_L;
piece_list[PIECE_L].rotated_degrees = 0;
piece_list[PIECE_L].cells[0][1] = 1;
piece_list[PIECE_L].cells[1][1] = 1;
piece_list[PIECE_L].cells[2][1] = 1;
piece_list[PIECE_L].cells[2][2] = 1;
piece_list[PIECE_L].color = COLOR_WHITE;
// PIECE_L_REVERSE
piece_list[PIECE_L_REVERSE].name = PIECE_L_REVERSE;
piece_list[PIECE_L_REVERSE].rotated_degrees = 0;
piece_list[PIECE_L_REVERSE].cells[0][2] = 1;
piece_list[PIECE_L_REVERSE].cells[1][2] = 1;
piece_list[PIECE_L_REVERSE].cells[2][2] = 1;
piece_list[PIECE_L_REVERSE].cells[2][1] = 1;
piece_list[PIECE_L_REVERSE].color = COLOR_BLUE;
// PIECE_S
piece_list[PIECE_S].name = PIECE_S;
piece_list[PIECE_S].rotated_degrees = 0;
piece_list[PIECE_S].cells[1][2] = 1;
piece_list[PIECE_S].cells[1][3] = 1;
piece_list[PIECE_S].cells[2][1] = 1;
piece_list[PIECE_S].cells[2][2] = 1;
piece_list[PIECE_S].color = COLOR_GREEN;
// PIECE_S_REVERSE
piece_list[PIECE_S_REVERSE].name = PIECE_S_REVERSE;
piece_list[PIECE_S_REVERSE].rotated_degrees = 0;
piece_list[PIECE_S_REVERSE].cells[1][1] = 1;
piece_list[PIECE_S_REVERSE].cells[1][2] = 1;
piece_list[PIECE_S_REVERSE].cells[2][2] = 1;
piece_list[PIECE_S_REVERSE].cells[2][3] = 1;
piece_list[PIECE_S_REVERSE].color = COLOR_RED;
srand(time(NULL));
}
piece_t piece_gen(void)
{
piece_name_t name = rand() % PIECE_COUNT;
TRACE("generated a %d\n", name);
return piece_list[name];
}
bool piece_get_cell(piece_t* piece, int y, int x)
{
if (y < 0 || y > MAX_PIECE_HEIGHT)
return false;
if (x < 0 || x > MAX_PIECE_WIDTH)
return false;
switch (piece->name) {
// square doesn't need rotation
case PIECE_SQUARE:
return piece->cells[y][x];
// these pieces only need 180 rotation
case PIECE_S:
case PIECE_S_REVERSE:
case PIECE_STRAIGHT:
if (piece->rotated_degrees % 180 == 0)
return piece->cells[y][x];
return piece->cells[x][MAX_PIECE_HEIGHT-y-1];
// these pieces need all rotations
case PIECE_T:
case PIECE_L:
case PIECE_L_REVERSE:
if (piece->rotated_degrees == 90)
return piece->cells[MAX_PIECE_HEIGHT-x-1][y];
if (piece->rotated_degrees == 180)
return piece->cells[MAX_PIECE_HEIGHT-y-1][MAX_PIECE_HEIGHT-x-1];
if (piece->rotated_degrees == 270)
return piece->cells[x][MAX_PIECE_HEIGHT-y-1];
return piece->cells[y][x];
default:
assert(false);
}
}
void piece_rotate(piece_t* piece)
{
piece->rotated_degrees += 90;
piece->rotated_degrees %= 360;
}