-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.c
127 lines (117 loc) · 2.77 KB
/
validator.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* validator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: conguyen <conguyen@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/09 14:11:26 by conguyen #+# #+# */
/* Updated: 2022/01/18 17:22:46 by conguyen ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
static int validate_tet(char **tets, int pos[4][2])
{
int x;
x = 0;
while (++x < 4)
{
if (tets[pos[x][0]][pos[x][1]] == '#')
return (0);
}
return (1);
}
static void transform_tet(char **tets, unsigned int ch, int x, int y)
{
if ((unsigned int)tets[x][y] == ch || tets[x][y] == '.')
return ;
tets[x][y] = ch;
if (x > 0)
transform_tet(tets, ch, x - 1, y);
if (y > 0)
transform_tet(tets, ch, x, y - 1);
if (x < 3)
transform_tet(tets, ch, x + 1, y);
if (y < 3)
transform_tet(tets, ch, x, y + 1);
}
static int check_tet(char **tets, int lines)
{
int y;
int pos[4][2];
int c;
int x;
c = -1;
x = -1;
while (++x < lines)
{
y = -1;
while (tets[x][++y] != '\0')
{
if (tets[x][y] != '.' && tets[x][y] != '#')
return (0);
if (tets[x][y] == '#' && ++c < 4)
pos[c][0] = x;
if (tets[x][y] == '#' && c < 4)
pos[c][1] = y;
}
if (y != 4 || (x == 3 && c != 3))
return (0);
}
transform_tet(tets, 65, pos[0][0], pos[0][1]);
return (validate_tet(tets, pos));
}
static int check_input(char **tets, int lines)
{
int x;
int num_tets;
int y;
x = -1;
num_tets = 0;
while (++x < lines)
{
y = 0;
while (tets[x][y] != '\0')
y++;
if ((x + 1) % 5 != 0 && y != 4)
return (0);
if ((x + 1) % 5 == 0)
{
if (y != 0)
return (0);
if (!check_tet(&tets[x - 4], 4))
return (0);
num_tets++;
}
}
if (num_tets * 5 - 1 != lines - 1)
return (0);
return (1);
}
int read_input(const int fd, t_tet **tetrimino)
{
char **tetsarray;
int x;
x = 0;
tetsarray = (char **)malloc(sizeof(*tetsarray) * 131);
if (!tetsarray)
return (0);
while (get_next_line(fd, &tetsarray[x]))
{
x++;
if (x > 130)
break ;
}
close(fd);
if (x > 130)
{
free_array(tetsarray, x - 1);
return (0);
}
if (check_input(tetsarray, x) == 0)
ft_putstr("error\n");
else
*tetrimino = create_list(tetsarray, x, 65);
free_array(tetsarray, x);
return (1);
}