/
ft_checkarray.c
81 lines (72 loc) · 1.88 KB
/
ft_checkarray.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_checkarray.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mghazari <mghazari@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/10 13:38:34 by mghazari #+# #+# */
/* Updated: 2017/01/18 23:00:22 by mghazari ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
static int count_conn(char *tetri[], int i, int j)
{
int connections;
connections = 0;
if ((i - 1) > -1 && tetri[i - 1][j] != '.')
connections++;
if ((i + 1) < 4 && tetri[i + 1][j] != '.')
connections++;
if ((j - 1) > -1 && tetri[i][j - 1] != '.')
connections++;
if ((j + 1) < 4 && tetri[i][j + 1] != '.')
connections++;
return (connections);
}
static int connected(char *tetri[])
{
int total;
int i;
int j;
total = 0;
i = -1;
while (tetri[++i])
{
j = -1;
while (tetri[i][++j])
if (tetri[i][j] != '.')
total += count_conn(tetri, i, j);
}
return (total == 6 || total == 8);
}
static int four_blocks(char *tetri[])
{
int i;
int j;
int f;
f = 0;
i = -1;
while (tetri[++i])
{
j = -1;
while (tetri[i][++j])
if (tetri[i][j] != '.')
f++;
}
return (f == 4);
}
int ft_checkarray(char **array[])
{
int i;
i = -1;
while (array[++i])
{
if (!(four_blocks(array[i]) && connected(array[i])))
{
free_3d(array);
return (0);
}
}
return (1);
}