-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.c
executable file
·165 lines (150 loc) · 2.98 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
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
154
155
156
157
158
159
160
161
162
163
164
165
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* matrix.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ismelich <ismelich@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/28 10:44:44 by ismelich #+# #+# */
/* Updated: 2019/12/09 11:23:22 by ismelich ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
/*
** Here we are generating a matrix and filling it with dots.
*/
char ***gen_matrix(int size, int m, int n, char c)
{
char ***matrix;
int b;
int i;
int j;
b = 0;
i = 0;
matrix = (char***)malloc(sizeof(char**) * (size + 1));
while (b < size)
{
matrix[b] = (char**)malloc(sizeof(char*) * (m + 1));
while (i < m)
{
j = 0;
matrix[b][i] = ft_strnew(n);
while (j < n)
matrix[b][i][j++] = c;
i++;
}
matrix[b][i] = NULL;
b++;
i = 0;
}
matrix[b] = NULL;
return (matrix);
}
/*
** Here we are converting the # to letters and putting the letters
** into the matrix from the str.
*/
int fill_matrix(char ***matrix, char **str)
{
int b;
int i;
int j;
b = 0;
i = 0;
j = 0;
while (matrix[b] != 0)
{
while (matrix[b][i] != 0)
{
while (matrix[b][i][j] != 0)
{
if (str[b][i * 5 + j++] == '#')
matrix[b][i][j - 1] = str[b][i * 5 + j - 1] + 30 + b;
}
i++;
j = 0;
}
b++;
i = 0;
}
return (0);
}
/*
** We clean the the rows in the matrix, that only have .... rows.
*/
int clean_row_matrix(char ***matrix)
{
int b;
int i;
int i2;
b = 0;
i = 0;
while (matrix[b] != 0)
{
while (matrix[b][i] != 0)
{
if (!ft_strcmp(matrix[b][i], "...."))
{
free(matrix[b][i]);
i2 = i--;
while (matrix[b][i2++] != 0)
matrix[b][i2 - 1] = matrix[b][i2];
}
i++;
}
i = 0;
b++;
}
return (0);
}
/*
** Here we clean just the single dots inside our blocks, so that in the end
** just the tetra with our letters would be left.
*/
int clean_single_column(char ***matrix, int b, int i, int j)
{
int j2;
j2 = 0;
while (matrix[b][i] != 0)
{
if (matrix[b][i][j] == '.')
j2++;
i++;
}
if (j2 == i)
{
i = 0;
while (matrix[b][i] != 0)
{
j2 = j;
while (matrix[b][i][j2] != 0)
{
matrix[b][i][j2] = matrix[b][i][j2 + 1];
j2++;
}
i++;
}
j--;
}
return (j);
}
int clean_column_matrix(char ***matrix)
{
int b;
int i;
int j;
b = 0;
i = 0;
j = 0;
while (matrix[b] != 0)
{
while (matrix[b][i][j] != 0)
{
j = clean_single_column(matrix, b, i, j);
j++;
}
b++;
j = 0;
}
return (0);
}