-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.c
127 lines (116 loc) · 2.67 KB
/
matrix.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* matrix.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cquillet <cquillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 17:16:22 by cquillet #+# #+# */
/* Updated: 2016/11/30 22:38:03 by cquillet ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
int counting_rows(t_tetris *input, int nb_input, int side)
{
int i;
int nb_rows;
nb_rows = 0;
i = 0;
if (nb_input <= 0)
return (-1);
while (i < nb_input)
{
if (side < input[i].w || side < input[i].h)
return (-1);
nb_rows += ((side - input[i].w + 1) * (side - input[i].h + 1));
i++;
}
return (nb_rows);
}
char **alloc_matrix(int nb_pieces, int side, int nb_rows)
{
char **m;
int r;
int i;
m = (char **)malloc(nb_rows * sizeof(char *));
if (m == NULL || nb_rows == 0 || nb_pieces == 0 || side < 2)
return (NULL);
r = -1;
while (++r < nb_rows)
{
m[r] = (char *)malloc((nb_pieces + side * side) * sizeof(char));
if (m[r] == NULL)
{
while (r--)
free(m[r]);
free(m);
return (NULL);
}
i = 0;
while (i < nb_pieces + side * side)
m[r][i++] = EMPTY;
}
return (m);
}
int place_piece(char **m, t_tetris p, int nb_p, int side)
{
int i;
int j;
int k;
int r;
int pos;
r = 0;
j = -1;
while (++j < side - p.h + 1)
{
i = -1;
while (++i < side - p.w + 1)
{
pos = side * j + i;
k = 0;
while (k < 16 && pos + k / 4 * side + k % 4 < side * side)
{
m[r][nb_p + pos + k / 4 * side + k % 4] = (char)(p.tab[k]);
k++;
}
r++;
}
}
return (r);
}
char **build_matrix(t_tetris *input, int nb_pieces, int side, int nb_rows)
{
char **m;
char **m_ptr;
int p;
int r;
int nb_filled_rows;
if (!(m = alloc_matrix(nb_pieces, side, nb_rows)))
return (NULL);
m_ptr = m;
p = 0;
while (p < nb_pieces)
{
nb_filled_rows = place_piece(m_ptr, input[p], nb_pieces, side);
r = 0;
while (r < nb_filled_rows)
{
m_ptr[r][p] = FILLED;
r++;
}
m_ptr += (nb_filled_rows);
p++;
}
return (m);
}
void del_matrix(char **m, int nb_rows)
{
int r;
r = 0;
while (r < nb_rows)
{
free(m[r]);
r++;
}
free(m);
}