-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
138 lines (127 loc) · 3.26 KB
/
main.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kceder <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/08 14:04:32 by kceder #+# #+# */
/* Updated: 2022/02/08 14:04:37 by kceder ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
int check_line_correct(char *line, int i, int ret)
{
int count;
count = 0;
if (i == 5 && ret == 0)
return (-1);
if (ret == 0)
return (1);
if (i == 4)
return (line_correct_util(line[0], ret, i));
else
{
if (line == NULL)
return (0);
while (line[count])
{
if (line[count] != '.' && line[count] != '#')
return (0);
count++;
}
if (count == 4)
return (1);
return (0);
}
}
unsigned short check_around(char **array, int i, int find)
{
int count;
int leftmost;
int upmost;
count = 0;
leftmost = 5;
upmost = 5;
count = check_around_loop(array, &find, &leftmost, &upmost);
if (count >= 6 && find == 4)
{
if (i > 5)
return (to_binary(array, i - 6, upmost, leftmost));
else
return (to_binary(array, 0, upmost, leftmost));
}
return (0);
}
unsigned short array_util(unsigned short *tetriminos, int *i,
char **array, unsigned short *tetri_count)
{
int temp;
temp = *i;
array[temp] = NULL;
tetriminos[*tetri_count] = check_around(array, 0, 0);
if (!(tetriminos[*tetri_count]))
return (0);
*tetri_count = *tetri_count + 1;
*i = 0;
while (*i < temp)
{
ft_bzero(array[*i], 5);
free(array[*i]);
array[*i] = NULL;
(*i)++;
}
*i = 0;
return (1);
}
unsigned short *read_to_array(char **array, int fd, int i, int ret)
{
char *line;
unsigned short tetri_count;
unsigned short *tetriminos;
int result;
result = 0;
tetriminos = init_tetrimonos(27);
line = NULL;
tetri_count = 0;
while (ret > 0)
{
ret = get_next_line(fd, &line);
result = check_line_correct(line, i, ret);
if (result == 0 || (ret == 0 && i < 4))
return (free_tetri_and_line(line, tetriminos, result, ret));
if (result == -1)
return (free_tetri_and_line(line, tetriminos, result, ret));
if (i == 5 || ret == 0)
if (!array_util(tetriminos, &i, array, &tetri_count))
return (free_tetri_and_line(line, tetriminos, -1, ret));
if (!free_func(array, line, i, ret))
break ;
i++;
}
return (tetriminos);
}
int main(int argc, char **argv)
{
int fd;
char **array;
unsigned short *tetriminos;
if (argc != 2)
{
ft_putendl("usage: ./fillit <source file>");
return (0);
}
array = NULL;
array = ft_new_array(array, 6);
fd = open(argv[1], O_RDONLY);
tetriminos = read_to_array(array, fd, 0, 1);
if (!tetriminos)
{
ft_free_array(array);
ft_putendl("error");
return (0);
}
free(array);
solve_tetrimino(tetriminos);
return (1);
}