-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
99 lines (90 loc) · 2.24 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nhennigh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/01 13:35:06 by nhennigh #+# #+# */
/* Updated: 2019/03/01 13:35:07 by nhennigh ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include "./libft/libft.h"
#include "./includes/fillit.h"
char *empty_map(int size)
{
char *str;
int x;
int y;
str = ft_strnew((size + 1) * (size));
y = 0;
while (y < size)
{
x = 0;
while (x < size)
{
str[y * (size + 1) + x] = '.';
x++;
}
str[y * (size + 1) + x] = '\n';
y++;
}
return (str);
}
void print(t_etris *t, int count, int size)
{
char *str;
int x;
int y;
str = empty_map(size);
while (count > 0)
{
y = 0;
while (y < t->height)
{
x = 0;
while (x < t->width)
{
if ((t->value >> (16 * (y + 1) - 1 - x)) & 1)
str[(t->y + y) * (size + 1) + x + t->x] = t->id;
x++;
}
y++;
}
t++;
count--;
}
ft_putstr(str);
ft_strdel(&str);
}
int check_die(const char *str)
{
if (!(str))
return (1);
else if (str[19] == '\n' && str[20] == '\n')
return (2);
return (0);
}
int die(char *str)
{
ft_putendl(str);
return (1);
}
int main(int argc, char **argv)
{
t_etris tetris[MAX_TETRI + 1];
uint16_t map[16];
int count;
int size;
if (argc != 2)
return (die("usage: ./fillit [input_file]"));
ft_bzero(tetris, sizeof(t_etris) * (MAX_TETRI + 1));
if ((count = read_tetri(open(argv[1], O_RDONLY), tetris)) == 0)
return (die("error"));
ft_bzero(map, sizeof(uint16_t) * 16);
if ((size = solve(tetris, count, map)) == 0)
return (die("error"));
print(tetris, count, size);
return (0);
}