-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver_3.c
113 lines (103 loc) · 2.64 KB
/
solver_3.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* solver_3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kceder <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/08 14:03:53 by kceder #+# #+# */
/* Updated: 2022/02/08 14:03:58 by kceder ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
void move_by_one(unsigned short *ptr, int flag)
{
unsigned short i;
i = 0;
if (flag == 0)
return ;
if (flag == -1)
{
while (i < 4)
{
ptr[i] = ptr[i] >> 1;
i++;
}
}
else
{
while (i < 4)
{
ptr[i] = ptr[i] << flag;
i++;
}
}
}
void remove_from_grid(unsigned short *ptr, unsigned short *arr,
unsigned short start)
{
int i;
i = 0;
while (i < 4)
{
ptr[start + i] = ptr[start + i] ^ arr[i];
i++;
}
}
void add_to_grid(unsigned short *tetrimino, unsigned short *grid,
unsigned short start, unsigned short counter)
{
unsigned short i;
i = 0;
while (i < 4)
{
grid[start + i] = grid[start + i] | tetrimino[i];
tetrimino[8] = counter;
tetrimino[7] = start;
i++;
}
}
int find_tetri_place(unsigned short *arr, unsigned short *grid,
unsigned short *start, unsigned short min_sqr)
{
unsigned short counter;
counter = find_temp_counter(arr);
while (1)
{
if (within_borders(&counter, min_sqr, arr, start) == -1)
return (-1);
if (((grid[*start] | arr[0]) == (grid[*start] ^ arr[0]))
&& ((grid[*start + 1] | arr[1]) == (grid[*start + 1] ^ arr[1]))
&& ((grid[*start + 2] | arr[2]) == (grid[*start + 2] ^ arr[2]))
&& ((grid[*start + 3] | arr[3]) == (grid[*start + 3] ^ arr[3])))
return (1);
else
{
move_by_one(arr, -1);
counter = counter + 1;
}
}
}
int within_borders(unsigned short *counter, unsigned short min_sqr,
unsigned short *arr, unsigned short *start)
{
if ((*counter + arr[5]) > min_sqr)
{
move_by_one(arr, *counter);
*start = *start + 1;
*counter = 0;
if ((*start + arr[4]) > min_sqr)
{
move_by_one(arr, *counter);
*start = 0;
return (-1);
}
}
if ((*start + arr[4]) > min_sqr)
{
move_by_one(arr, *counter);
*start = 0;
return (-1);
}
return (0);
}