FIX MERGElsls

This commit is contained in:
Gregory Tertyshny 2016-12-20 19:03:03 +02:00
commit 9cd3acb0d1
9 changed files with 316 additions and 92 deletions

3
.gitignore vendored
View file

@ -34,3 +34,6 @@
# Vim temp
*.swp
# Sublime files
*.sublime*

View file

@ -17,12 +17,13 @@ SRCDIR = ./src/
OBJDIR = ./obj/
SRC_FILES = main.c \
fillit.c \
glue_figure.c \
move_up_left.c \
read_file.c \
string_to_table.c \
to_strct_array.c \
to_letters.c \
build_matrix.c \
test!_check.c \
test!_print_one_string.c \
test!_print_table.c \

View file

@ -6,7 +6,7 @@
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/14 16:42:06 by gtertysh #+# #+# */
/* Updated: 2016/12/14 16:45:34 by gtertysh ### ########.fr */
/* Updated: 2016/12/20 18:59:06 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,32 +17,19 @@
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h> // warning!
# define BUF_S 8192
# define HOW_MUCH 10000
typedef struct s_tetrominoes
typedef struct s_ttrmn
{
uint16_t line[16];
int offset_y;
int height;
} t_tetrominoes;
typedef struct s_map
{
uint16_t line[16];
unsigned int size;
t_tetrominoes *figure[26];
unsigned int figure_amount;
} t_map;
typedef struct s_tetrominoes_templates
{
char figure[17];
} t_tetrominoes_templates;
void convert_tetramino(char *s, t_map *map, int index);
void map_initialization(char **tetramino_table, t_map *map);
char t[17];
int x;
int y;
int coord[4];
} t_ttrmn;
// reads from file
char *read_file(char *path);
@ -63,13 +50,16 @@ void move_up_left(char **table);
void print_one_string(char **glued);
// compare 19 templates with each tetromino in *ttr table
void test_check(char **ttr, char **tamplates);
void test_check(char **ttr, t_ttrmn *tamplates);
// change hashes ('#') with letters
// obsolete, rewrite for structs array
void to_letters(char **ttr);
// проверка считаной строки
int check_raw_string(char *raw_string);
// create array of ttr structures
t_ttrmn **to_strct_array(char **ttr, t_ttrmn *templates);
// build matrix for algorythm X
int **build_matrix(t_ttrmn **ttr);
#endif

106
src/build_matrix.c Normal file
View file

@ -0,0 +1,106 @@
#include "fillit.h"
unsigned int ft_sqrt_ceil(unsigned int num)
{
unsigned int i;
i = 1;
while (i * i < num)
i++;
return (i);
}
void put_figure(char **char_matrix, t_ttrmn t, int y, int x)
{
int i;
int j;
int t_x;
int runner;
t_x = x;
i = 0;
runner = 0;
while (i < t.y)
{
x = t_x;
j = 0;
while (j < t.x)
{
char_matrix[y][x] = t.t[runner];
j++;
x++;
runner++;
}
}
}
void print_char_matrix(char **matrix, int size)
{
int y;
y = 0;
while (y < size)
{
printf("%s\n", matrix[y]);
y++;
}
}
int **build_matrix(t_ttrmn **ttr)
{
int **matrix;
char **char_matrix;
int i;
int j;
int amount;
int size;
int k;
int curr_line;
amount = 0;
i = 0;
matrix = malloc(sizeof(int*) * HOW_MUCH);
while (i < HOW_MUCH)
{
j = 0;
matrix[i] = malloc(sizeof(int) * HOW_MUCH);
while (j < HOW_MUCH)
matrix[i][j++] = -1;
i++;
}
while(ttr[amount])
amount++;
size = ft_sqrt_ceil(amount * 4);
if (size < 4)
size = 4;
char_matrix = malloc(sizeof(char *) * size + 1);
i = 0;
while (i < size)
{
char_matrix[i] = malloc(sizeof(char) * size + 1);
char_matrix[i][size] = 0;
i++;
}
char_matrix[size] = 0;
i = 0;
curr_line = 0;
while (i < amount)
{
k = 0;
while (k < size - ttr[i]->y)
{
j = 0;
while (j < size - ttr[i]->x)
{
put_figure(char_matrix, *ttr[i], k, j);
//print_char_matrix(char_matrix, size);
//count_cover(matrix, char_matrix, curr_line++);
//clear_char_matrix(&char_matrix, size);
j++;
}
k++;
}
i++;
}
return (matrix);
}

View file

@ -5,7 +5,6 @@ unsigned int ft_sqrt_ceil(unsigned int num)
unsigned int i;
i = 1;
// int and unsigned int comparsion
while (i * i < num)
i++;
return (i);
@ -57,6 +56,8 @@ void map_initialization(char **tetramino_table, t_map *map)
convert_tetramino(*tetramino_table++, map, i++);
map->figure_amount = i;
map->size = ft_sqrt_ceil(i * 4);
map->mask = 1;
map->mask <<= 16 - map->size;
clear_map(map);
}
@ -66,9 +67,9 @@ void insert_tetramino(t_map *map, int index)
int k;
t_tetrominoes *temp;
temp = map->figure[index];
temp = &map->figure[index];
i = temp->offset_y;
k = i + 4;
k = i + temp->height;
while (i < k)
{
map->line[i] |= temp->line[i];
@ -83,37 +84,59 @@ void erase_tetramino(t_map *map, int index)
int k;
t_tetrominoes *temp;
temp = map->figure[index];
temp = &map->figure[index];
i = temp->offset_y;
k = 0;
while (k < 4)
while (k < temp->height)
{
map->line[i + k] ^= temp->line[i + k];
k++;
}
}
// void shift_tetramino(t_map *map, int index)
// {
// //сделаю сдвиг фигуры вправо, если дошли до границы, то перемещу в начало следующего ряд и т.д.
// }
int can_insert(t_map *map, int index)
{
int i;
int k;
t_tetrominoes *temp;
// void algorithm(t_map *map)
// {
// //тут у меня сейчас трабл с рекурсией
// //не могу пока написать что-то годное
// }
temp = &map->figure[index];
i = temp->offset_y;
k = i + temp->height;
while (i < k)
{
if (map->line[i] & temp->line[i])
return (0);
i++;
}
return (1);
}
int shift_tetramino(t_map *map, int index)
{
int left_shift;
int i;
int k;
t_tetrominoes *temp;
// мейн в отдельный файл кину, знаю что ты не любишь когда много файлов)
// int main()
// {
// t_map map;
// char** table;
// table = read_from_file();
// map_initialization(table, &map);
// algoritm(&map);
// print_result(&map);
// return (0);
// }
temp = &map->figure[index];
i = temp->offset_y;
k = i + temp->height;
left_shift = 1;
while (i < k)
if (temp->line[i++] & map->mask)
left_shift = 0;
i = temp->offset_y;
if (left_shift)
while (i < k)
temp->line[i++] >>= 1;
else if (k < map->size)
while(k-- > i)
{
while (!(temp->line[k] & 0x8000))
temp->line[k] <<= 1;
temp->line[k + 1] = temp->line[k];
}
else
return (0);
return (1);
}

View file

@ -6,32 +6,90 @@
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/10 16:07:25 by gtertysh #+# #+# */
/* Updated: 2016/12/14 16:45:31 by gtertysh ### ########.fr */
/* Updated: 2016/12/20 18:59:59 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
char *str_templates[19] =
{ "#...#...#...#...",
"####............",
"#...#...##......",
"..#.###.........",
"##...#...#......",
"###.#...........",
".#...#..##......",
"###...#.........",
"##..#...#.......",
"#...###.........",
"###..#..........",
"#...##..#.......",
".#..###.........",
".#..##...#......",
"##..##..........",
"##...##.........",
".#..##..#.......",
".##.##..........",
"#...##...#......"
// bad global variable
t_ttrmn templates[19] =
{
{ .t = "#...#...#...#...",
.x = 1,
.y = 4},
{ .t = "####............",
.x = 4,
.y = 1},
{ .t = "#...#...##......",
.x = 2,
.y = 3},
{ .t = "..#.###.........",
.x = 3,
.y = 2},
{ .t = "##...#...#......",
.x = 2,
.y = 3},
{ .t = "###.#...........",
.x = 3,
.y = 2},
{ .t = ".#...#..##......",
.x = 2,
.y = 3},
{ .t = "###...#.........",
.x = 3,
.y = 2},
{ .t = "##..#...#.......",
.x = 2,
.y = 3},
{ .t = "#...###.........",
.x = 3,
.y = 2},
{ .t = "###..#..........",
.x = 3,
.y = 2},
{ .t = "#...##..#.......",
.x = 2,
.y = 3},
{ .t = ".#..###.........",
.x = 3,
.y = 2},
{ .t = ".#..##...#......",
.x = 2,
.y = 3},
{ .t = "##..##..........",
.x = 2,
.y = 2},
{ .t = "##...##.........",
.x = 3,
.y = 2},
{ .t = ".#..##..#.......",
.x = 2,
.y = 3},
{ .t = ".##.##..........",
.x = 3,
.y = 2},
{ .t = "#...##...#......",
.x = 2,
.y = 3}
};
int main(int argc, char **argv)
@ -40,6 +98,8 @@ int main(int argc, char **argv)
char **table;
char **ttr;
int i;
t_ttrmn **strct_ttr;
int **matrix;
i = 0;
if (argc != 2)
@ -52,22 +112,30 @@ int main(int argc, char **argv)
ft_putstr("error\n");
return (1);
}
if (check_raw_string(string))
{
ft_putstr("error\n");
return (1);
}
//printf("string:\n\n\n%s\n", string);
table = to_table(&string);
// if (check_string_table(table))
// {
// ft_putstr("error\n");
// return (1);
// }
//printf("print_table:\n\n\n");
//print_table(table);
ttr = glue_figure(table);
//printf("glued figures:\n\n\n");
//print_one_string(ttr);
move_up_left(ttr);
//printf("move to up left:\n\n\n");
//print_one_string(ttr);
test_check(ttr, str_templates);
//printf("test check:\n\n");
//test_check(ttr, templates);
strct_ttr = to_strct_array(ttr, templates);
matrix = build_matrix(strct_ttr);
//printf("%s\n %d, %d", strct_ttr[0]->t, strct_ttr[0]->x, strct_ttr[0]->y);
//to_letters(ttr);
//printf("to latters:\n\n");
//print_one_string(ttr);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/14 17:26:13 by gtertysh #+# #+# */
/* Updated: 2016/12/14 18:47:16 by gtertysh ### ########.fr */
/* Updated: 2016/12/20 19:02:34 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
// Test func. Compare figures from file
// with templates
void test_check(char **ttr, char **tamplates)
void test_check(char **ttr, t_ttrmn *tamplates)
{
int i;
int j;
@ -28,8 +28,8 @@ void test_check(char **ttr, char **tamplates)
printf("\n\ninput:\n");
printf("%s\n", ttr[i]);
printf("\n\ntemplate:\n");
printf("%s\n", tamplates[j]);
if ((ft_memcmp(ttr[i], tamplates[j], 17) == 0))
printf("%s\n", tamplates[j].t);
if ((ft_memcmp(ttr[i], tamplates[j].t, 17) == 0))
printf(" match!\n");
j++;
}

33
src/to_strct_array.c Normal file
View file

@ -0,0 +1,33 @@
#include "fillit.h"
t_ttrmn **to_strct_array(char **ttr, t_ttrmn *templates)
{
t_ttrmn **ret;
int amount;
int i;
int j;
int k;
amount = 0;
i = 0;
k = 0;
while (ttr[amount])
amount++;
ret = malloc(sizeof(ret) * (amount + 1));
while (k < amount)
ret[k++] = malloc(sizeof(t_ttrmn));
ret[k] = 0;
while (i < amount)
{
j = 0;
while(ft_strcmp(templates[j].t, ttr[i]) != 0)
j++;
//printf("%d\n", j);
ft_strcpy(ret[i]->t, ttr[i]);
ret[i]->x = templates[j].x;
ret[i]->y = templates[j].y;
i++;
}
return (ret);
}