reched end of stack, gonna branch back to board

This commit is contained in:
Jack Halford 2016-09-14 12:58:16 +02:00
parent 07effd02c9
commit c4bf6b9e55
17 changed files with 364 additions and 536 deletions

View file

@ -24,44 +24,31 @@ typedef struct s_ttmn
t_list *ft_get_ttmn(char *filename); t_list *ft_get_ttmn(char *filename);
void ft_map_clean(t_list *list); void ft_map_clean(t_list *list);
void ft_map_switch(t_list *list); void ft_map_switch(t_list *list);
t_list *ft_stack_new_range(int a, int b); t_list *ft_stack_new_range(int a, int b);
void ft_map_stack_stars(t_list **amap, t_list **astack, char c); void ft_map_stack_stars(t_list **amap, t_list **astack, char c);
void ft_map_stack_ttmn(t_list **amap, t_list **astack, int anchor, int pos[4][2], char id, int size);
void ft_put_stack(t_stack *content); void ft_put_stack(t_stack *content);
int ft_stack_cmp_num(t_stack *a, int *b); int ft_stack_cmp_num(t_stack *a, int *b);
int ft_stack_cmp_num2(t_stack *a, t_stack *b); int ft_stack_cmp_num2(t_stack *a, t_stack *b);
void ft_stack_as_board(t_list **stack, int size);
void ft_unstack_char(t_list **amap, t_list **astack, char c);
void ft_unstack_ttmn(t_list **amap, t_list **astack, char c);
void ft_map_unstack_char(t_list **amap, t_list **astack, char c); int ft_check_ttmnfit(t_list **amap, int anchor, int pos[4][2], int size);
void ft_map_unstack_ttmn(t_list **amap, t_list **astack, char c); void ft_stack_ttmn(t_list **amap, t_list **astack, int anchor, int pos[4][2], int size, char id);
char **ft_empty_board(size_t size);
char **ft_copy_board(char **board); int ft_check_waste(t_list **amap, t_list **astack, t_list *lttmn, int space, int size);
void ft_free_board(char ***board);
void ft_fill_board(char **dst, char **src);
void ft_show_ttmn(t_ttmn ttmn); void ft_show_ttmn(t_ttmn ttmn);
void ft_show_board(char **board);
void ft_ttmn_reset(t_list *ttmn); void ft_ttmn_reset(t_list *ttmn);
int ft_board_add(char **board, t_ttmn block, int i); int ft_solver(t_list **amap, t_list **astack, t_list *lttmn, int space, int size);
void ft_board_remove(char **board, char c); int ft_solved(t_list **astack, t_list **amap, int size);
int ft_solver(char **board, t_list **amap, t_list **astack, t_list *lttmn, int space); int ft_fit_blob(t_list **amap, t_list **astack, t_list *lttmn, int blob_size, int space, int size);
int ft_solved(char **board); int ft_blobs(t_list **amap, t_list **astack, t_list *lttmn, int space, int size);
int ft_fit_blob(char **board, t_list **amap, t_list **astack, t_list *lttmn, int i, int blob_size, int space); int ft_flood_fill(t_list *map, int size, int i, char c);
int ft_solve_blobs(char **board, t_list **amap, t_list **astack, t_list *lttmn, int space);
t_list *ft_empty_around(char **board, int size, int i);
t_list *ft_empty_here(char **board, int size, int i);
int ft_empty_around2(t_list *map, int size, int i);
int ft_empty_here2(t_list *map, int size, int i);
void ft_map_delttmn(t_list **amap, int anchor, int pos[4][2], int size);
void ft_map_addttmn(t_list **amap, int anchor, int pos[4][2], int size);
void ft_board_replace(char **board, char a, char b);
int ft_validate_waste(char **board, t_list **amap, t_list *lttmn, int space);
#endif #endif

View file

@ -1,130 +0,0 @@
#include "fillit.h"
char **ft_empty_board(size_t size)
{
char **board;
int i;
int j;
board = (char **)malloc(sizeof(char *) * (size + 1));
i = -1;
while (++i < (int)size)
{
board[i] = ft_strnew(size);
j = -1;
while (++j < (int)size)
board[i][j] = '.';
}
board[i] = NULL;
return (board);
}
char **ft_copy_board(char **board)
{
size_t size;
char **copy;
size = ft_strlen(*board);
copy = ft_empty_board(size);
ft_fill_board(copy, board);
return (copy);
}
void ft_fill_board(char **dst, char **src)
{
int i;
int j;
i = 0;
while (src[i])
{
j = 0;
while (src[i][j])
{
dst[i][j] = src[i][j];
j++;
}
i++;
}
}
void ft_free_board(char ***board)
{
int i;
i = 0;
while ((*board)[i])
{
free((*board)[i]);
i++;
}
free(*board);
*board = NULL;
}
void ft_board_replace(char **board, char a, char b)
{
int i;
int j;
i = -1;
while (board[++i])
{
j = -1;
while (board[i][++j])
{
if (board[i][j] == a)
board[i][j] = b;
}
}
}
void ft_board_remove(char **board, char c)
{
int i;
int j;
i = -1;
while (board[++i])
{
j = -1;
while (board[i][++j])
{
if (board[i][j] == c)
board[i][j] = '.';
}
}
}
int ft_board_add(char **board, t_ttmn block, int i)
{
int x;
int y;
int k;
int size;
size = ft_strlen(*board);
k = 0;
while (k < 4)
{
x = i % size + block.pos[k][1];
y = i / size + block.pos[k][0];
/* printf("adding %c to %i,%i\n", block.id, y, x); */
/* fflush(stdout); */
if (x > size - 1 || y > size - 1 || x < 0 || y < 0)
return (1);
if (board[y][x] != '.' && board[y][x] != '*')
return (1);
k++;
}
k = 0;
while (k < 4)
{
x = i % size + block.pos[k][1];
y = i / size + block.pos[k][0];
board[y][x] = block.id;
/* ft_show_board(board); */
k++;
}
return (0);
}

View file

@ -1,87 +1,22 @@
#include "fillit.h" #include "fillit.h"
int ft_fit_blob(char **board, t_list **amap, t_list **astack, t_list *lttmn, int i, int blob_size, int space) int ft_blobs(t_list **amap, t_list **astack, t_list *lttmn, int space, int size)
{
t_ttmn *ttmn;
t_list *list;
t_list *blob;
t_stack *stack;
int size;
int n;
n = blob_size;
size = ft_strlen(*board);
blob = *astack;
/* ft_putendl("fitting blob:"); */
/* ft_show_board(board); */
/* ft_lst_print(blob, &ft_put_stack); */
while (blob)
{
stack = blob->content;
/* ft_put_stack(stack); */
if (stack->id != '*')
{
/* ft_putendl("iwtbf"); */
break ;
}
stack->id = '.';
/* ft_lst_print(*astack, &ft_put_stack); */
/* ft_put_stack(stack); */
blob = blob->next;
i = stack->num;
list = lttmn;
while (list)
{
ttmn = (t_ttmn *)list->content;
if (ttmn->placed || ft_board_add(board, *ttmn, i))
{
list = list->next;
continue ;
}
/* printf("fitted %c in blob\n", ttmn->id); */
/* fflush(stdout); */
ttmn->placed = 1;
/* ft_map_stack_ttmn(amap, astack, i, ttmn->pos, ttmn->id, size); */
if (ft_solver(board, amap, astack, lttmn, space))
return (1);
/* ft_map_unstack(amap, astack, 4); */
ttmn->placed = 0;
ft_board_remove(board, ttmn->id);
list = list->next;
}
}
/* ft_lst_print(*astack, &ft_put_stack); */
return (0);
}
int ft_solve_blobs(char **board, t_list **amap, t_list **astack, t_list *lttmn, int space)
{ {
t_list *map; t_list *map;
/* static int id = 0;; */
int sup_space = 0; int sup_space = 0;
int size;
int blob_size; int blob_size;
int i; int i;
if (!lttmn) if (!lttmn)
return (ft_solved(board)); return (ft_solved(amap, astack, size));
size = ft_strlen(*board);
map = *amap; map = *amap;
/* ft_board_remove(board, '*'); */
/* ft_board_remove(board, '^'); */
ft_lstiter(*amap, &ft_map_clean); ft_lstiter(*amap, &ft_map_clean);
/* ft_show_board(board); */
while (map) while (map)
{ {
i = ((t_stack *)map->content)->num; i = ((t_stack *)map->content)->num;
ft_board_replace(board, '*', '^');
ft_lstiter(*amap, &ft_map_switch); ft_lstiter(*amap, &ft_map_switch);
blob_size = ft_empty_here2(map, size, i); blob_size = ft_flood_fill(map, size, i, '*');
map = map->next; map = map->next;
if (blob_size == 0) if (blob_size == 0)
continue ; continue ;
@ -90,16 +25,13 @@ int ft_solve_blobs(char **board, t_list **amap, t_list **astack, t_list *lttmn,
/* fflush(stdout); */ /* fflush(stdout); */
/* ft_lst_print(*amap, &ft_put_stack); */ /* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */ /* ft_lst_print(*astack, &ft_put_stack); */
/* fflush(stdout); */
fflush(stdout);
if (blob_size / 4 == 0) if (blob_size / 4 == 0)
{ {
space -= blob_size % 4; space -= blob_size % 4;
if (space + sup_space < 0) if (space + sup_space < 0)
{
ft_board_remove(board, '*');
return (0); return (0);
}
/* ft_putendl("stacking stars:"); */ /* ft_putendl("stacking stars:"); */
/* ft_lst_print(*amap, &ft_put_stack); */ /* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */ /* ft_lst_print(*astack, &ft_put_stack); */
@ -112,10 +44,7 @@ int ft_solve_blobs(char **board, t_list **amap, t_list **astack, t_list *lttmn,
{ {
space -= blob_size % 4; space -= blob_size % 4;
if (space + sup_space < 0) if (space + sup_space < 0)
{
ft_board_remove(board, '*');
return (0); return (0);
}
/* ft_putendl("stacking stars:"); */ /* ft_putendl("stacking stars:"); */
/* ft_lst_print(*amap, &ft_put_stack); */ /* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */ /* ft_lst_print(*astack, &ft_put_stack); */
@ -125,32 +54,21 @@ int ft_solve_blobs(char **board, t_list **amap, t_list **astack, t_list *lttmn,
/* ft_lst_print(*amap, &ft_put_stack); */ /* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */ /* ft_lst_print(*astack, &ft_put_stack); */
if (ft_fit_blob(board, amap, astack, lttmn, i, blob_size, space)) if (ft_fit_blob(amap, astack, lttmn, blob_size, space, size))
{
ft_board_remove(board, '*');
return (1); return (1);
}
else else
{ {
space -= 4; space -= 4;
if (space + sup_space < 0) if (space + sup_space < 0)
{
/* ft_board_remove(board, '*'); */
return (0); return (0);
}
} }
} }
else else
{ {
sup_space -= blob_size % 4; sup_space -= blob_size % 4;
if (space + sup_space < 0) if (space + sup_space < 0)
{
ft_board_remove(board, '*');
return (0); return (0);
}
} }
} }
/* ft_board_remove(board, '*'); */ return (ft_solver(amap, astack, lttmn, space, size));
/* ft_board_remove(board, '^'); */
return (ft_solver(board, amap, astack, lttmn, space));
} }

View file

@ -0,0 +1,43 @@
#include "fillit.h"
int ft_check_waste(t_list **amap, t_list **astack, t_list *lttmn, int space, int size)
{
t_stack *stack;
t_ttmn *ttmn;
t_list *map;
int i;
int blob_size;
if (!lttmn)
return (ft_solved(amap, astack, size));
ttmn = (t_ttmn *)lttmn->content;
if (ttmn->placed)
return (ft_solver(amap, astack, lttmn->next, space, size));
space = 0;
i = -1;
map = *amap;
while (map)
{
stack = (t_stack *)map->content;
map = map->next;
i = stack->num;
if (stack->id != '.')
continue ;
ft_lstiter(*amap, &ft_map_switch);
blob_size = ft_flood_fill(map, size, i, '*');
space -= blob_size % 4;
if (space > size * size - 4 * g_ttmn)
return (0);
/* if (blob_size / 4 == 1) */
/* { */
/* ft_board_remove(board, '*'); */
/* blob = ft_empty_here(board, size, i); */
/* if (ft_fit_blob2(board, amap, lttmn, i, blob_size, space)) */
/* return (1); */
/* space -= 4; */
/* if (space > size * size - 4 * g_ttmn) */
/* return (0); */
/* } */
}
return (ft_solver(amap, astack, lttmn, space, size));
}

View file

@ -0,0 +1,44 @@
#include "fillit.h"
int ft_fit_blob(t_list **amap, t_list **astack, t_list *lttmn, int blob_size, int space, int size)
{
t_ttmn *ttmn;
t_list *list;
t_list *blob;
t_stack *stack;
int n;
int i;
n = blob_size;
blob = *astack;
while (blob)
{
stack = blob->content;
/* ft_put_stack(stack); */
if (stack->id != '*')
{
/* ft_putendl("iwtbf"); */
break ;
}
stack->id = '.';
/* ft_lst_print(*astack, &ft_put_stack); */
/* ft_put_stack(stack); */
blob = blob->next;
i = stack->num;
list = lttmn;
while (list)
{
ttmn = (t_ttmn *)list->content;
list = list->next;
/* if (ttmn->placed */
/* || ft_stack_ttmn(amap, astack, i, ttmn->pos, ttmn->id, size)) */
/* continue ; */
ttmn->placed = 1;
if (ft_solver(amap, astack, lttmn, space, size))
return (1);
ttmn->placed = 0;
}
}
/* ft_lst_print(*astack, &ft_put_stack); */
return (0);
}

View file

@ -1,25 +0,0 @@
#include "fillit.h"
void ft_show_ttmn(t_ttmn ttmn)
{
int i;
if (!ttmn.id)
{
ft_putendl("end of ttmn\n");
return ;
}
printf("%c,%i", ttmn.id, ttmn.placed);
for (i=1; i < 4; i++)
printf("\t%d,%d\n", ttmn.pos[i][0], ttmn.pos[i][1]);
fflush(stdout);
ft_putendl("");
}
void ft_show_board(char **board)
{
/* ft_putendl("-s-"); */
while (*board)
ft_putendl(*board++);
ft_putendl("");
}

View file

@ -1,59 +1,48 @@
#include "fillit.h" #include "fillit.h"
int ft_solved(char **board) int ft_solved(t_list **amap, t_list **astack, int size)
{ {
ft_board_remove(board, '*');
g_sol = ft_copy_board(board);
ft_putendl("found solution:"); ft_putendl("found solution:");
ft_show_board(board);
ft_lst_sort(astack, &ft_stack_cmp_num2);
ft_lst_sorted_merge(astack, *amap, &ft_stack_cmp_num2);
ft_lstiter(*astack, &ft_map_clean);
ft_stack_as_board(astack, size);
return (1); return (1);
} }
int ft_solver(char **board, t_list **amap, t_list **astack, t_list *lttmn, int space) int ft_solver(t_list **amap, t_list **astack, t_list *lttmn, int space, int size)
{ {
int i; int i;
int size;
t_ttmn *ttmn; t_ttmn *ttmn;
t_list *map; t_list *map;
if (!lttmn) if (!lttmn)
return (ft_solved(board)); return (ft_solved(amap, astack, size));
ttmn = (t_ttmn *)lttmn->content; ttmn = (t_ttmn *)lttmn->content;
if (ttmn->placed) if (ttmn->placed)
return (ft_solver(board, amap, astack, lttmn->next, space)); return (ft_solver(amap, astack, lttmn->next, space, size));
size = ft_strlen(*board);
map = *amap; map = *amap;
/* ft_show_board(board); */
/* ft_lst_print(*amap, &ft_put_stack); */ /* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */ /* ft_lst_print(*astack, &ft_put_stack); */
/* ft_stack_as_board(astack, size); */
while (map) while (map)
{ {
i = ((t_stack *)map->content)->num; i = ((t_stack *)map->content)->num;
if (ft_board_add(board, *ttmn, i))
{
map = map->next;
continue ;
}
ft_map_stack_ttmn(amap, astack, i, ttmn->pos, ttmn->id, size);
/* ft_show_board(board); */
/* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */
if (ft_solve_blobs(board, amap, astack, lttmn->next, space))
return (1);
/* ft_putendl("p2"); */
/* ft_show_board(board); */
/* ft_lst_print(*amap, &ft_put_stack); */
/* ft_lst_print(*astack, &ft_put_stack); */
ft_map_unstack_ttmn(amap, astack, ttmn->id);
ft_lstiter(*amap, &ft_map_clean);
ft_board_remove(board, ttmn->id);
map = map->next; map = map->next;
if (ft_check_ttmnfit(amap, i, ttmn->pos, size))
continue ;
ft_stack_ttmn(amap, astack, i, ttmn->pos, size, ttmn->id);
/* ft_lst_print(*amap, &ft_put_stack); */ /* if (ft_check_waste(amap, astack, lttmn->next, space, size)) */
/* ft_lst_print(*astack, &ft_put_stack); */ /* return (1); */
/* if (ft_blobs(amap, astack, lttmn->next, space, size)) */
/* return (1); */
/* if (ft_solver(amap, astack, lttmn->next, space, size)) */
/* return (1); */
ft_unstack_ttmn(amap, astack, ttmn->id);
} }
return (0); return (0);
} }

View file

@ -1,108 +0,0 @@
#include "fillit.h"
/* int ft_fit_blob2(char **board, t_list **amap, t_list *lttmn, int i, int blob_size, int space) */
/* { */
/* t_ttmn *ttmn; */
/* t_list *list; */
/* int y; */
/* int l; */
/* int size; */
/* int n; */
/* /1* printf("going to fit any at %i,%i\n", i, j); *1/ */
/* /1* fflush(stdout); *1/ */
/* /1* ft_show_board(board); *1/ */
/* y = i; */
/* n = blob_size; */
/* size = ft_strlen(*board); */
/* while (y < size * size) */
/* { */
/* if (board[y / size][y % size] == '*') */
/* { */
/* n--; */
/* l = -1; */
/* /1* printf("0 trying all at %i\n", y); *1/ */
/* /1* fflush(stdout); *1/ */
/* list = lttmn; */
/* while (list) */
/* { */
/* ttmn = (t_ttmn *)list->content; */
/* if (ttmn->placed) */
/* { */
/* list = list->next; */
/* continue ; */
/* } */
/* if (ft_board_add(board, *ttmn, i)) */
/* { */
/* list = list->next; */
/* continue ; */
/* } */
/* ttmn->placed = 1; */
/* if (ft_solver(board, amap, lttmn, space)) */
/* return (1); */
/* ttmn->placed = 0; */
/* ft_board_remove(board, ttmn->id); */
/* list = list->next; */
/* } */
/* /1* printf("failed at %i\n", y); *1/ */
/* /1* fflush(stdout); *1/ */
/* } */
/* y++; */
/* } */
/* return (0); */
/* } */
/* int ft_validate_waste(char **board, t_list **amap, t_list *lttmn, int space) */
/* { */
/* t_ttmn *ttmn; */
/* int waste; */
/* int y; */
/* t_list *blob; */
/* int blob_size; */
/* int size; */
/* if (!lttmn) */
/* return (ft_solved(board)); */
/* ttmn = (t_ttmn *)lttmn->content; */
/* if (ttmn->placed) */
/* return (ft_solver(board, amap, lttmn->next, space)); */
/* waste = 0; */
/* size = ft_strlen(*board); */
/* y = -1; */
/* /1* ft_show_board(board); *1/ */
/* while (++y < size * size) */
/* { */
/* if (board[y / size][y % size] != '.') */
/* continue ; */
/* /1* blob = ft_empty_here(board, size, y); *1/ */
/* /1* blob_size = ft_lstsize(blob); *1/ */
/* blob_size = ft_empty_here2(board, size, y); */
/* /1* ft_board_replace(board, '*', '^') *1/ */
/* /1* printf("found blob=%i at %i\n", blob_size, y); *1/ */
/* /1* fflush(stdout); *1/ */
/* /1* ft_show_board(board); *1/ */
/* waste += blob_size % 4; */
/* if (waste > size * size - 4 * g_ttmn) */
/* { */
/* ft_board_remove(board, '*'); */
/* return (0); */
/* } */
/* if (blob_size / 4 == 1) */
/* { */
/* ft_board_remove(board, '*'); */
/* blob = ft_empty_here(board, size, y); */
/* if (ft_fit_blob2(board, amap, lttmn, y, blob_size, space)) */
/* return (1); */
/* } */
/* waste += (blob_size / 4 == 1) ? 4 : 0; */
/* if (waste > size * size - 4 * g_ttmn) */
/* { */
/* ft_board_remove(board, '*'); */
/* return (0); */
/* } */
/* } */
/* ft_board_remove(board, '*'); */
/* return (ft_solver(board, amap, lttmn, space)); */
/* } */

View file

@ -1,73 +0,0 @@
#include "fillit.h"
t_list *ft_empty_here(char **board, int size, int i)
{
t_list *blob;
blob = NULL;
if (board[i / size][i % size] == '.')
{
board[i / size][i % size] = '*';
blob = ft_empty_around(board, size, i);
ft_lstadd(&blob, ft_lstnew(&i, sizeof(int)));
}
/* printf("list at %i: ", i); */
/* fflush(stdout); */
/* ft_lst_print(blob, &ft_putnbr); */
return (blob);
}
t_list *ft_empty_around(char **board, int size, int i)
{
t_list *add;
add = NULL;
i % size < size - 1 ? ft_lsteadd(&add, ft_empty_here(board, size, i + 1)) : 0;
i / size > 0 ? ft_lsteadd(&add, ft_empty_here(board, size, i - size)) : 0;
i % size > 0 ? ft_lsteadd(&add, ft_empty_here(board, size, i - 1)) : 0;
i / size < size - 1 ? ft_lsteadd(&add, ft_empty_here(board, size, i + size)) : 0;
/* ft_lst_print(add, &ft_putnbr); */
return (add);
}
int ft_empty_here2(t_list *map, int size, int i)
{
int n;
char c;
t_list *list;
n = 0;
list = ft_lst_find(map, &i, &ft_stack_cmp_num);
if (list)
{
c = ((t_stack *)list->content)->id;
if (c == '.')
{
/* board[i / size][i % size] = '*'; */
((t_stack *)list->content)->id = '*';
n++;
n += ft_empty_around2(map, size, i);
}
}
/* printf("list at %i: ", i); */
/* fflush(stdout); */
/* ft_lst_print(blob, &ft_putnbr); */
return (n);
}
int ft_empty_around2(t_list *map, int size, int i)
{
int n;
n = 0;
/* n += i % size < size - 1 ? ft_empty_here2(board, size, i + 1) : 0; */
/* n += i / size > 0 ? ft_empty_here2(board, size, i - size) : 0; */
/* n += i % size > 0 ? ft_empty_here2(board, size, i - 1) : 0; */
/* n += i / size < size - 1 ? ft_empty_here2(board, size, i + size) : 0; */
n += i % size < size - 1 ? ft_empty_here2(map, size, i + 1) : 0;
n += i / size > 0 ? ft_empty_here2(map, size, i - size) : 0;
n += i % size > 0 ? ft_empty_here2(map, size, i - 1) : 0;
n += i / size < size - 1 ? ft_empty_here2(map, size, i + size) : 0;
/* ft_lst_print(add, &ft_putnbr); */
return (n);
}

28
fillit/src/flood_fill.c Normal file
View file

@ -0,0 +1,28 @@
#include "fillit.h"
int ft_flood_fill(t_list *map, int size, int i, char c)
{
int n;
char id;
t_list *list;
n = 0;
list = ft_lst_find(map, &i, &ft_stack_cmp_num);
if (list)
{
id = ((t_stack *)list->content)->id;
if (id != c)
{
((t_stack *)list->content)->id = c;
n++;
n += i % size < size - 1 ? ft_flood_fill(map, size, i + 1, c) : 0;
n += i / size > 0 ? ft_flood_fill(map, size, i - size, c) : 0;
n += i % size > 0 ? ft_flood_fill(map, size, i - 1, c) : 0;
n += i / size < size - 1 ? ft_flood_fill(map, size, i + size, c) : 0;
}
}
/* printf("list at %i: ", i); */
/* fflush(stdout); */
/* ft_lst_print(blob, &ft_putnbr); */
return (n);
}

View file

@ -0,0 +1,138 @@
/* #include "fillit.h" */
/* void ft_show_board(char **board) */
/* { */
/* /1* ft_putendl("-s-"); *1/ */
/* while (*board) */
/* ft_putendl(*board++); */
/* ft_putendl(""); */
/* } */
/* char **ft_empty_board(size_t size) */
/* { */
/* char **board; */
/* int i; */
/* int j; */
/* board = (char **)malloc(sizeof(char *) * (size + 1)); */
/* i = -1; */
/* while (++i < (int)size) */
/* { */
/* board[i] = ft_strnew(size); */
/* j = -1; */
/* while (++j < (int)size) */
/* board[i][j] = '.'; */
/* } */
/* board[i] = NULL; */
/* return (board); */
/* } */
/* char **ft_copy_board(char **board) */
/* { */
/* size_t size; */
/* char **copy; */
/* size = ft_strlen(*board); */
/* copy = ft_empty_board(size); */
/* ft_fill_board(copy, board); */
/* return (copy); */
/* } */
/* void ft_fill_board(char **dst, char **src) */
/* { */
/* int i; */
/* int j; */
/* i = 0; */
/* while (src[i]) */
/* { */
/* j = 0; */
/* while (src[i][j]) */
/* { */
/* dst[i][j] = src[i][j]; */
/* j++; */
/* } */
/* i++; */
/* } */
/* } */
/* void ft_free_board(char ***board) */
/* { */
/* int i; */
/* i = 0; */
/* while ((*board)[i]) */
/* { */
/* free((*board)[i]); */
/* i++; */
/* } */
/* free(*board); */
/* *board = NULL; */
/* } */
/* void ft_board_replace(char **board, char a, char b) */
/* { */
/* int i; */
/* int j; */
/* i = -1; */
/* while (board[++i]) */
/* { */
/* j = -1; */
/* while (board[i][++j]) */
/* { */
/* if (board[i][j] == a) */
/* board[i][j] = b; */
/* } */
/* } */
/* } */
/* void ft_board_remove(char **board, char c) */
/* { */
/* int i; */
/* int j; */
/* i = -1; */
/* while (board[++i]) */
/* { */
/* j = -1; */
/* while (board[i][++j]) */
/* { */
/* if (board[i][j] == c) */
/* board[i][j] = '.'; */
/* } */
/* } */
/* } */
/* int ft_board_add(char **board, t_ttmn block, int i) */
/* { */
/* int x; */
/* int y; */
/* int k; */
/* int size; */
/* size = ft_strlen(*board); */
/* k = 0; */
/* while (k < 4) */
/* { */
/* x = i % size + block.pos[k][1]; */
/* y = i / size + block.pos[k][0]; */
/* /1* printf("adding %c to %i,%i\n", block.id, y, x); *1/ */
/* /1* fflush(stdout); *1/ */
/* if (x > size - 1 || y > size - 1 || x < 0 || y < 0) */
/* return (1); */
/* if (board[y][x] != '.' && board[y][x] != '*') */
/* return (1); */
/* k++; */
/* } */
/* k = 0; */
/* while (k < 4) */
/* { */
/* x = i % size + block.pos[k][1]; */
/* y = i / size + block.pos[k][0]; */
/* board[y][x] = block.id; */
/* /1* ft_show_board(board); *1/ */
/* k++; */
/* } */
/* return (0); */
/* } */

View file

@ -12,7 +12,6 @@ int main(int ac, char **av)
t_list *lttmn; t_list *lttmn;
t_list *map; t_list *map;
t_list *stack; t_list *stack;
char **board;
int size; int size;
if (ac != 2 || !(lttmn = ft_get_ttmn(av[1]))) if (ac != 2 || !(lttmn = ft_get_ttmn(av[1])))
@ -25,12 +24,9 @@ int main(int ac, char **av)
{ {
map = ft_stack_new_range(0, size * size); map = ft_stack_new_range(0, size * size);
stack = NULL; stack = NULL;
board = ft_empty_board(size);
ft_lstiter(lttmn, &ft_ttmn_reset); ft_lstiter(lttmn, &ft_ttmn_reset);
ft_solver(board, &map, &stack, lttmn, size * size - 4 * g_ttmn); ft_solver(&map, &stack, lttmn, size * size - 4 * g_ttmn, size);
ft_free_board(&board);
size--; size--;
} }
ft_show_board(g_sol);
return (0); return (0);
} }

View file

@ -1,5 +1,30 @@
#include "fillit.h" #include "fillit.h"
void ft_put_stack_id(t_list *list)
{
t_stack *stack;
stack = list->content;
ft_putchar(stack->id);
}
void ft_stack_as_board(t_list **astack, int size)
{
int i;
t_list *list;
list = *astack;
i = 1;
while (list)
{
ft_put_stack_id(list);
list = list->next;
if (i++ % size == 0)
ft_putendl("");
}
}
void ft_put_stack(t_stack *content) void ft_put_stack(t_stack *content)
{ {
ft_putnbr(content->num); ft_putnbr(content->num);
@ -50,7 +75,8 @@ void ft_map_clean(t_list *list)
t_stack *stack; t_stack *stack;
stack = (t_stack *)list->content; stack = (t_stack *)list->content;
stack->id = '.'; if (stack->id == '^')
stack->id = '.';
} }
void ft_map_switch(t_list *list) void ft_map_switch(t_list *list)
@ -77,19 +103,38 @@ void ft_map_stack_stars(t_list **amap, t_list **astack, char c)
} }
} }
void ft_map_stack_ttmn(t_list **amap, t_list **astack, int anchor, int pos[4][2], char id, int size) int ft_check_ttmnfit(t_list **amap, int anchor, int pos[4][2], int size)
{
int i;
int j;
int x;
int y;
i = -1;
while (++i < 4)
{
x = anchor % size + pos[i][1];
y = anchor / size + pos[i][0];
j = anchor + size * pos[i][0] + pos[i][1];
if (x > size - 1 || y > size - 1 || x < 0 || y < 0)
return (1);
if (!ft_lst_find(*amap, &j, &ft_stack_cmp_num))
return (1);
}
return (0);
}
void ft_stack_ttmn(t_list **amap, t_list **astack, int anchor, int pos[4][2], int size, char id)
{ {
int i; int i;
int j; int j;
t_list *link;
t_stack *stack; t_stack *stack;
t_list *link;
i = -1; i = -1;
while (++i < 4) while (++i < 4)
{ {
j = anchor + size * pos[i][0] + pos[i][1]; j = anchor + size * pos[i][0] + pos[i][1];
/* printf("moving: %i\n", j); */
/* fflush(stdout); */
link = ft_lst_removeif(amap, &j, &ft_stack_cmp_num); link = ft_lst_removeif(amap, &j, &ft_stack_cmp_num);
stack = (t_stack *)link->content; stack = (t_stack *)link->content;
stack->id = id; stack->id = id;

View file

@ -1,44 +1,22 @@
#include "fillit.h" #include "fillit.h"
int cmp_ttmn(t_ttmn a, t_ttmn b) void ft_show_ttmn(t_ttmn ttmn)
{ {
return (ft_strcmp(&a.id, &b.id)); int i;
if (!ttmn.id)
{
ft_putendl("end of ttmn\n");
return ;
}
printf("%c,%i", ttmn.id, ttmn.placed);
for (i=1; i < 4; i++)
printf("\t%d,%d\n", ttmn.pos[i][0], ttmn.pos[i][1]);
fflush(stdout);
ft_putendl("");
} }
void ft_ttmn_reset(t_list *ttmn) void ft_ttmn_reset(t_list *ttmn)
{ {
((t_ttmn *)ttmn->content)->placed = 0; ((t_ttmn *)ttmn->content)->placed = 0;
} }
void ft_map_delttmn(t_list **amap, int anchor, int pos[4][2], int size)
{
int i;
int j;
i = -1;
fflush(stdout);
while (++i < 4)
{
/* printf("deleting: %i\n", anchor + size * pos[i][0] + pos[i][1]); */
/* fflush(stdout); */
j = anchor + size * pos[i][0] + pos[i][1];
ft_lst_delif(amap, &j, &ft_diff, &ft_lst_cfree);
}
}
void ft_map_addttmn(t_list **amap, int anchor, int pos[4][2], int size)
{
int i;
int j;
t_list *link;
i = -1;
while (++i < 4)
{
j = (anchor + size * pos[i][0] + pos[i][1]);
/* printf("adding %i to map\n", j); */
/* fflush(stdout); */
link = ft_lstnew(&j, sizeof(int));
ft_lst_sorted_insert(amap, link, &ft_diff);
}
}

View file

@ -1,6 +1,6 @@
#include "fillit.h" #include "fillit.h"
void ft_map_unstack_char(t_list **amap, t_list **astack, char c) void ft_unstack_char(t_list **amap, t_list **astack, char c)
{ {
t_list *link; t_list *link;
t_stack *stack; t_stack *stack;
@ -9,12 +9,10 @@ void ft_map_unstack_char(t_list **amap, t_list **astack, char c)
stack = link->content; stack = link->content;
while (stack->id == c) while (stack->id == c)
{ {
/* printf("stack->num = %i\n", stack->num); */
/* fflush(stdout); */
stack->id = '.';
*astack = (*astack)->next; *astack = (*astack)->next;
ft_lst_sorted_insert(amap, link, &ft_stack_cmp_num2); ft_lst_sorted_insert(amap, link, &ft_stack_cmp_num2);
/* stack->id = '.'; */
link = *astack; link = *astack;
if (!link) if (!link)
break ; break ;
@ -22,8 +20,8 @@ void ft_map_unstack_char(t_list **amap, t_list **astack, char c)
} }
} }
void ft_map_unstack_ttmn(t_list **amap, t_list **astack, char c) void ft_unstack_ttmn(t_list **amap, t_list **astack, char c)
{ {
ft_map_unstack_char(amap, astack, '.'); ft_unstack_char(amap, astack, '.');
ft_map_unstack_char(amap, astack, c); ft_unstack_char(amap, astack, c);
} }