-
Notifications
You must be signed in to change notification settings - Fork 1
/
tetriminos.c
113 lines (99 loc) · 3.51 KB
/
tetriminos.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
#include <stdlib.h>
#include <curses.h>
#include "dotris.h"
#ifdef DEBUG
#define ASSERT(expr) \
do { \
if (!(expr)) \
abort(); \
} while (0)
#else
#define ASSERT(expr)
#endif
uint8_t const I_STATES[4][4][4] = {
{{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}},
{{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}},
{{0, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}},
};
uint8_t const J_STATES[4][4][4] = {
{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 1, 1, 1}, {0, 0, 0, 1}},
{{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 1, 1}},
{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 1, 0, 0}, {0, 1, 1, 1}},
{{0, 0, 0, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}},
};
uint8_t const L_STATES[4][4][4] = {
{{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 1, 0}, {1, 0, 0, 0}},
{{0, 0, 0, 0}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}},
{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 1, 0}, {1, 1, 1, 0}},
{{0, 0, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 1, 0}},
};
uint8_t const O_STATES[4][4][4] = {
{{0, 0, 0, 0}, {0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}},
};
uint8_t const S_STATES[4][4][4] = {
{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 1, 1}, {0, 1, 1, 0}},
{{0, 0, 0, 0}, {0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}},
{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 1, 1}, {0, 1, 1, 0}},
{{0, 0, 0, 0}, {0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}},
};
uint8_t const T_STATES[4][4][4] = {
{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 1, 1, 1}, {0, 0, 1, 0}},
{{0, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}},
{{0, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 1, 1}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 0, 1, 0}},
};
uint8_t const Z_STATES[4][4][4] = {
{{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 0, 0}, {0, 1, 1, 0}},
{{0, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}},
{{0, 0, 0, 0}, {0, 0, 0, 0}, {1, 1, 0, 0}, {0, 1, 1, 0}},
{{0, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}},
};
// clang-format on
// Makes a new Tetrimino in the default position.
Tetrimino tetrimino_create(TetriminoType type) {
Tetrimino new;
new.type = type;
new.state = FIRST_STATE;
const uint8_t(*c)[4][4][4] = NULL;
switch (type) {
case I:
c = &I_STATES;
break;
case J:
c = &J_STATES;
break;
case L:
c = &L_STATES;
break;
case O:
c = &O_STATES;
break;
case S:
c = &S_STATES;
break;
case T:
c = &T_STATES;
break;
case Z:
c = &Z_STATES;
break;
default:
ASSERT(0 && "unreachable");
break;
}
new.states = c;
new.x = 2;
new.y = 0;
return new;
}
// Makes a random Tetrimino
Tetrimino tetrimino_create_random(void) {
return tetrimino_create((TetriminoType)rand() % TETRIMINO_CNT);
}
TetriminoState tetrimino_next_state(TetriminoState state) {
return (state == FOURTH_STATE) ? FIRST_STATE : state + 1;
}