Peace of shit

This commit is contained in:
Yaroslav Kolomiets 2016-12-15 22:20:46 +02:00
parent 9ae40bfa6e
commit bb28ece7b6
14 changed files with 103 additions and 74 deletions

4
.gitignore vendored
View file

@ -34,3 +34,7 @@
# Vim temp
*.swp
CMakeLists.txt
.idea/
**/cmake-build-debug/*

View file

@ -6,7 +6,7 @@
# By: gtertysh <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2016/12/14 16:54:48 by gtertysh #+# #+# #
# Updated: 2016/12/14 18:41:33 by gtertysh ### ########.fr #
# Updated: 2016/12/15 22:08:42 by ykolomie ### ########.fr #
# #
# **************************************************************************** #
@ -43,12 +43,14 @@ LIBFOLDER = ./libft/
FLAGS = -Werror -Wextra -Wall
STACK = -Wl,-stack_size -Wl,0x10000000000
CC = clang
all: $(NAME)
$(NAME): $(OBJ) $(LIBFOLDER)$(LIB)
$(CC) $(FLAGS) $(OBJ) $(LIBFLAGS) -o $(NAME)
$(CC) $(FLAGS) $(STACK) $(OBJ) $(LIBFLAGS) -o $(NAME)
$(OBJDIR)%.o : $(SRCDIR)%.c
$(CC) $(FLAGS) $(INC) -c $< -o $@

View file

@ -1,4 +1,4 @@
#include "fillit.h"
#include "../inc/fillit.h"
unsigned int ft_sqrt_ceil(unsigned int num)
{
@ -26,7 +26,7 @@ void convert_tetramino(char *s, t_map *map, int index)
int y;
int sharp_count;
map->figure[index]->offset_y = 0;
map->figure[index].offset_y = 0;
sharp_count = 0;
i = 0;
y = 1;
@ -41,10 +41,10 @@ void convert_tetramino(char *s, t_map *map, int index)
}
temp = temp << 12;
map->figure[index]->line[y - 1] = temp;
map->figure[index].line[y - 1] = temp;
y++;
}
map->figure[index]->height = y - 1;
map->figure[index].height = y - 1;
}
void map_initialization(char **tetramino_table, t_map *map)
@ -58,6 +58,7 @@ void map_initialization(char **tetramino_table, t_map *map)
map->size = ft_sqrt_ceil(i * 4);
map->mask = 1;
map->mask <<= 16 - map->size;
map->ok = 0;
clear_map(map);
}
@ -111,32 +112,60 @@ int can_insert(t_map *map, int index)
}
return (1);
}
int shift_tetramino(t_map *map, int index)
{
int left_shift;
int i;
int k;
int right_shift;
unsigned int i;
unsigned int k;
t_tetrominoes *temp;
temp = &map->figure[index];
i = temp->offset_y;
k = i + temp->height;
left_shift = 1;
right_shift = 1;
while (i < k)
if (temp->line[i++] & map->mask)
left_shift = 0;
right_shift = 0;
i = temp->offset_y;
if (left_shift)
if (right_shift && ++(temp->offset_x))
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 if (k < map->size && ++(temp->offset_y)) {
while (k-- > i) {
temp->line[k] <<= temp->offset_x;
temp->line[k + 1] = temp->line[k];
}
temp->offset_x = 0;
}
else
return (0);
return (1);
}
}
t_map algorithm(t_map map, unsigned int index)
{
int ins = 0;
t_map copy;
if (index == map.figure_amount) {
map.ok = 1;
return map;
}
while (!(ins = can_insert(&map, index)))
if (!shift_tetramino(&map, index))
break;
if (ins)
{
copy = map;
insert_tetramino(&map, index);
map = algorithm(map, index + 1);
if (!map.ok)
{
shift_tetramino(&copy, index);
map = algorithm(copy, index);
}
}
return map;
}

View file

@ -10,7 +10,7 @@
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "../inc/fillit.h"
// This function creates string table in which one line represent one figure
// by "glueing" evry 4 strings from table

View file

@ -10,7 +10,7 @@
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "../inc/fillit.h"
// bad global variable
t_tetrominoes_templates templates[19] =
@ -113,42 +113,40 @@ t_tetrominoes_templates templates[19] =
int main(int argc, char **argv)
{
char *string;
char **table;
char **ttr;
int i;
char *string;
char **table;
char **ttr;
int i;
t_map map;
t_map copy;
unsigned int size_map;
i = 0;
if (argc != 2)
{
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);
table = to_table(&string);
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);
printf("test check:\n\n");
test_check(ttr, templates);
to_letters(ttr);
printf("to latters:\n\n");
print_one_string(ttr);
map_initialization(ttr, &map);
size_map = map.size;
copy = map;
while (!map.ok)
{
map = copy;
map.size = size_map++;
map = algorithm(map, 0);
}
return (0);
}

View file

@ -10,7 +10,7 @@
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "../inc/fillit.h"
// Moves figures to top left corner.
// table contains strings each represent one figure

View file

@ -10,7 +10,7 @@
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "../inc/fillit.h"
// read from file. Returns string.
char *read_file(char *path)
@ -32,7 +32,7 @@ char *read_file(char *path)
string = ft_memalloc(sizeof(char) * BUF_S);
while ((readed = read(fd, buf, BUF_S)))
{
buf[BUF_S] = '\0';
buf[readed] = '\0';
old_size = ft_strlen(string);
string = ft_realloc(string, old_size + readed, old_size);
ft_strcat(string, buf);

View file

@ -10,7 +10,7 @@
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "../inc/fillit.h"
// Split string to table by newline character.
// Each string contain one line of the file

View file

@ -10,7 +10,7 @@
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "../inc/fillit.h"
// Test func. Compare figures from file
// with templates

View file

@ -10,7 +10,7 @@
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "../inc/fillit.h"
// Test func. Print table but one string contain whole figure.
void print_one_string(char **glued)

View file

@ -10,7 +10,7 @@
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "../inc/fillit.h"
// Test func. Print string table,
// each line - line of tetromino figure.

View file

@ -10,7 +10,7 @@
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "../inc/fillit.h"
// convert "#" symbols to letters
// rewrite for new method with structures

View file

@ -18,27 +18,4 @@
.##.
....
....
..#.
.##.
.#..
....
....
.##.
##..
....
....
..##
.##.
....
...#
...#
..##
....
....
...#
.###

19
tests/sample1.fillit Normal file
View file

@ -0,0 +1,19 @@
####
....
....
....
....
##..
.#..
.#..
..#.
###.
....
....
....
##..
##..
....