-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttrs_mino.c
101 lines (92 loc) · 1.52 KB
/
ttrs_mino.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
#include <stdlib.h>
#include <string.h>
#include "tetris.h"
#include "ttrs_mino.h"
// clang-format off
static const t_mino g_minos[] = {
{
.shape = {
{0, 1, 1},
{1, 1, 0},
{0, 0, 0}
},
.len = 3,
},
{
.shape = {
{1, 1, 0},
{0, 1, 1},
{0, 0, 0}
},
.len = 3
},
{
.shape = {
{0, 1, 0},
{1, 1, 1},
{0, 0, 0}
},
.len = 3
},
{
.shape = {
{0, 0, 1},
{1, 1, 1},
{0, 0, 0}
},
.len = 3
},
{
.shape = {
{1, 0, 0},
{1, 1, 1},
{0, 0, 0}
},
.len = 3
},
{
.shape = {
{1, 1},
{1, 1}
},
.len = 2
},
{
.shape = {
{0, 0, 0, 0},
{1, 1, 1, 1},
{0, 0, 0, 0},
{0, 0, 0, 0}
},
.len = 4
}
};
// clang-format on
void spin_right(t_mino *mino)
{
t_shape saved_shape;
memcpy(saved_shape, mino->shape, sizeof(t_shape));
for (int row = 0; row < mino->len; row++) {
for (int col = 0, back_col = mino->len - 1; col < mino->len; col++, back_col--) {
mino->shape[row][col] = saved_shape[back_col][row];
}
}
}
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
static t_mino get_random_mino(void)
{
const int randon_index = rand() % (int)ARRAY_SIZE(g_minos);
return g_minos[randon_index];
}
static int get_random_col(int mino_length)
{
return rand() % (MATRIX_COL - mino_length + 1);
}
t_current_mino generate_random_mino(void)
{
t_current_mino current_mino;
current_mino.mino = get_random_mino();
const int random_col = get_random_col(current_mino.mino.len);
current_mino.pos = (t_position){.col = random_col};
return current_mino;
}