-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve_core.c
106 lines (95 loc) · 2.39 KB
/
solve_core.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* solve_core.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: juazouz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/20 11:56:50 by juazouz #+# #+# */
/* Updated: 2018/11/26 13:35:50 by juazouz ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
static int can_place(t_grid *grid, t_tetriminos *tetri, int x, int y)
{
int i;
t_point point;
i = 0;
while (i < TETRIMINOS_SIZE)
{
point = tetri->points[i];
if (cell_at(grid, point.x + x, point.y + y) != NULL)
return (0);
i++;
}
return (1);
}
static void do_place(t_grid *grid, t_tetriminos *tetri, int x, int y)
{
int i;
t_point point;
i = 0;
while (i < TETRIMINOS_SIZE)
{
point = tetri->points[i];
set_cell_at(grid, point.x + x, point.y + y, tetri);
i++;
}
}
static void remove_at(t_grid *grid, t_tetriminos *tetri, int x, int y)
{
int i;
t_point point;
i = 0;
while (i < TETRIMINOS_SIZE)
{
point = tetri->points[i];
set_cell_at(grid, point.x + x, point.y + y, NULL);
i++;
}
}
static int find_space(t_grid *grid, t_tetriminos *tetri, int *x, int *y)
{
int right;
int bottom;
right = *x + tetri->width - 1;
bottom = *y + tetri->height - 1;
while (bottom < grid->size)
{
while (right < grid->size)
{
if (can_place(grid, tetri, *x, *y))
{
return (1);
}
(*x)++;
right++;
}
(*x) = 0;
right = tetri->width - 1;
(*y)++;
bottom++;
}
return (0);
}
int solve_core(t_grid *grid,
t_tetriminos *tetri,
int count,
int index)
{
int x;
int y;
if (index == count)
return (1);
x = 0;
y = 0;
while (find_space(grid, &tetri[index], &x, &y))
{
do_place(grid, &tetri[index], x, y);
if (solve_core(grid, tetri, count, index + 1))
return (1);
remove_at(grid, &tetri[index], x, y);
x++;
}
return (0);
}