-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_solver.c
151 lines (139 loc) · 3.9 KB
/
ft_solver.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
139
140
141
142
143
144
145
146
147
148
149
150
151
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_solver.c :+: :+: */
/* +:+ */
/* By: dsaripap <marvin@codam.nl> +#+ */
/* +#+ */
/* Created: 2019/06/26 18:27:35 by dsaripap #+# #+# */
/* Updated: 2019/06/26 18:27:39 by dsaripap ######## odam.nl */
/* */
/* ************************************************************************** */
#include "fillit.h"
int place_tetrimino(uint64_t tet, uint16_t **board, size_t size, size_t row, size_t col, int check)
{
uint16_t tetbit;
uint64_t tetshift;
size_t tetrow;
size_t last_pos;
check = 0;
tetrow = 0;
last_pos = 0;
(void)size;
// iterate through all the bits of the tetrimino
while (tetrow < 4)
{
tetshift = tet << col;
tetshift = tetshift >> (tetrow * 16);
tetbit = (uint16_t)tetshift;
(*board)[row] |= tetbit;
tetrow++;
row++;
}
return (0);
}
int remove_tetrimino(uint64_t tet, uint16_t **board, size_t size, size_t row, size_t col, int check)
{
uint16_t tetbit;
uint64_t tetshift;
size_t tetrow;
size_t last_pos;
check = 0;
tetrow = 0;
last_pos = 0;
(void)size;
// iterate through all the bits of the tetrimino
while (tetrow < 4)
{
tetshift = tet << col;
tetshift = tetshift >> (tetrow * 16);
tetbit = (uint16_t)tetshift;
(*board)[row] &= ~(tetbit);
tetrow++;
row++;
}
return (0);
}
int can_be_placed(uint64_t tet, uint16_t **board, size_t size, size_t row, size_t col, int check)
{
uint16_t tetbit;
uint64_t tetshift;
size_t tetrow;
// size_t b_pos;
size_t last_pos;
uint16_t size_limit;
uint16_t rightset_bit;
check = 0;
tetrow = 0;
last_pos = 0;
//size = 0;
rightset_bit = 0;
// iterate through all the bits of the tetrimino
while (tetrow < 4)
{
tetshift = tet << col;
tetshift = tetshift >> (tetrow * 16);
tetbit = (uint16_t)tetshift;
if (!tetbit)
break;
//checking if it goes beyond the size limit
size_limit = ((*board)[row] | tetbit);
if (size == 8 && col == 0 && row == 2 && tet == 196611)
{
printf(ANSI_COLOR_MAGENTA "Current State of the board \n" ANSI_COLOR_RESET);
print_board(*board);
}
if ((((*board)[row] & tetbit) != 0) || ((size_limit >> (size)) != 0) || (row > (size - 1)))
{
printf(ANSI_COLOR_RED " Cannot be placed in board[%zu][%zu], size = %lu \n" ANSI_COLOR_RESET, row, col, size);
return (1);
}
tetrow++;
row++;
}
return (0);
}
int fill_board(t_list *tetr_lst, uint16_t **board, size_t size)
{
t_list *temp;
uint64_t tet;
size_t row;
size_t col;
temp = tetr_lst;
row = 0;
col = 0;
while (row < size)
{
col = 0;
while (col < size)
{
tet = *(uint64_t *)(temp->content);
if ( (can_be_placed(tet, board, size, row, col , 1) == 0))
{
printf(ANSI_COLOR_GREEN " It is safe to place the tetrimino \n" ANSI_COLOR_RESET);
place_tetrimino(tet, board, size, row, col, 0);
if ((temp->next == NULL) || (fill_board(tetr_lst->next, board, size) == 1))
{
printf(" RECURSION Run Successfully for tet %llu @ (%lu, %lu)\n", *(uint64_t *)(temp->content), row, col);
return (1);
}
printf(ANSI_COLOR_RED " Removing the tetrimino \n" ANSI_COLOR_RESET);
remove_tetrimino(tet, board, size, row, col, 0);
}
col++;
}
row++;
}
return (0);
}
int solver(t_list *tetr_lst, uint16_t **board, size_t size)
{
while (size < 16)
{
if (fill_board(tetr_lst, board, size) == 0)
size++;
else
break ;
}
return (0);
}