2 new functions: to_strct_array and build matrix

This commit is contained in:
Gregory 2016-12-20 05:47:48 +02:00
parent 9ae40bfa6e
commit 32d0500048
8 changed files with 241 additions and 119 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

@ -17,33 +17,18 @@
#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;
// obsolete, rewrite for unit16
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;
} t_ttrmn;
// reads from file
char *read_file(char *path);
@ -64,10 +49,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, t_tetrominoes_templates *tamplates);
void test_check(char **ttr, t_ttrmn *tamplates);
// change hashes ('#') with letters
// obsolete, rewrite for structs array
void to_letters(char **ttr);
// 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

@ -14,7 +14,7 @@
// This function creates string table in which one line represent one figure
// by "glueing" evry 4 strings from table
char **glue_figure(char **table)
char **glue_figure(char **table)
{
int i;
int j;

View file

@ -13,102 +13,83 @@
#include "fillit.h"
// bad global variable
t_tetrominoes_templates templates[19] =
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)
@ -117,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)
@ -124,31 +107,36 @@ int main(int argc, char **argv)
ft_putstr("error\n");
return (1);
}
string = read_file(argv[1]);
if (!(string = read_file(argv[1])))
{
ft_putstr("error\n");
return (1);
}
printf("string:\n\n\n%s\n", string);
//printf("string:\n\n\n%s\n", string);
table = to_table(&string);
printf("print_table:\n\n\n");
print_table(table);
//printf("print_table:\n\n\n");
//print_table(table);
ttr = glue_figure(table);
printf("glued figures:\n\n\n");
print_one_string(ttr);
//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);
//printf("move to up left:\n\n\n");
//print_one_string(ttr);
printf("test check:\n\n");
test_check(ttr, templates);
//printf("test check:\n\n");
//test_check(ttr, templates);
to_letters(ttr);
printf("to latters:\n\n");
print_one_string(ttr);
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

@ -14,7 +14,7 @@
// Test func. Compare figures from file
// with templates
void test_check(char **ttr, t_tetrominoes_templates *tamplates)
void test_check(char **ttr, t_ttrmn *tamplates)
{
int i;
int j;
@ -28,8 +28,8 @@ void test_check(char **ttr, t_tetrominoes_templates *tamplates)
printf("\n\ninput:\n");
printf("%s\n", ttr[i]);
printf("\n\ntemplate:\n");
printf("%s\n", tamplates[j].figure);
if ((ft_memcmp(ttr[i], tamplates[j].figure, 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);
}