1
0

first commit

This commit is contained in:
Adam Joly
2023-08-03 23:16:27 +02:00
parent 7f3254f1c5
commit a80c4d61d7
133 changed files with 4293 additions and 0 deletions

49
BSQ/Makefile Normal file
View File

@ -0,0 +1,49 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: axdubois <axdubois@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/08/02 19:37:01 by axdubois #+# #+# #
# Updated: 2023/08/02 19:37:06 by axdubois ### ########.fr #
# #
# **************************************************************************** #
NAME = bsq
CC = clang
SRCS = srcs/Display-c/write.c \
srcs/parsing-c/parsing.c \
srcs/utils-c/free.c \
srcs/utils-c/utils.c \
srcs/parsing-c/charobj.c \
srcs/main.c \
srcs/parsing-c/fst_line.c \
srcs/algo/algo.c \
srcs/algo/controller.c \
srcs/parsing-c/map_error.c
OBJS = $(SRCS:.c=.o)
FLAGS = -Wall -Wextra -Werror
$(NAME): $(OBJS)
$(CC) $(FLAGS) -o $(NAME) $(OBJS)
.c.o:
$(CC) $(FLAGS) -c $< -o $@
all: $(NAME)
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: clean all re fclean

20
BSQ/assets/gen.pl Executable file
View File

@ -0,0 +1,20 @@
#!/usr/bin/perl
use warnings;
use strict;
die "program x y density" unless (scalar(@ARGV) == 3);
my ($x, $y, $density) = @ARGV;
print "$y.ox\n";
for (my $i = 0; $i < $y; $i++) {
for (my $j = 0; $j < $x; $j++) {
if (int(rand($y) * 2) < $density) {
print "o";
}
else {
print ".";
}
}
print "\n";
}

BIN
BSQ/bsq Executable file

Binary file not shown.

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* write.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: axdubois <axdubois@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/31 23:17:55 by axdubois #+# #+# */
/* Updated: 2023/08/02 19:21:49 by axdubois ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef WRITE_H
# define WRITE_H
#include "../../includes/utils-h/utils.h"
#include "../../includes/utils-h/charobj.h"
#include "../../includes/algo-h/algo.h"
void ft_putchar(char c);
void ft_print(char *str);
void ft_print_result(t_square sq,t_charobj c,t_coords *listobj,int file);
#endif

View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* algo.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: axdubois <axdubois@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/01 22:11:53 by adjoly #+# #+# */
/* Updated: 2023/08/02 19:31:12 by axdubois ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ALGO_H
# define ALGO_H
#include "../../includes/utils-h/utils.h"
#include "../../includes/utils-h/charobj.h"
#include "../../includes/algo-h/algo.h"
typedef struct s_square
{
int x;
int y;
int side;
} t_square;
t_square ft_comb(t_coords *listobjsx, t_coords *listobjsy);
#endif

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* charobj.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: axdubois <axdubois@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/02 11:01:47 by axdubois #+# #+# */
/* Updated: 2023/08/02 19:10:38 by axdubois ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CHAROBJ_H
# define CHAROBJ_H
typedef struct s_charobj
{
char o;
char x;
char n;
} t_charobj;
t_charobj ft_get_charobj(char *ftline,int sizeline);
#endif

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fst_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/01 18:25:47 by axdubois #+# #+# */
/* Updated: 2023/08/02 20:30:35 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FST_LINE_H
# define FST_LINE_H
int ft_len_line(int file, int n);
int ft_len_colum(int file);
char *ft_get_first_line(int file);
#endif

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/31 23:33:05 by axdubois #+# #+# */
/* Updated: 2023/08/02 20:37:40 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef UTILS_H
# define UTILS_H
# include <unistd.h>
# include <stdlib.h>
# include <fcntl.h>
typedef struct s_coords
{
int x;
int y;
} t_coords;
int ft_strlen(char *str);
void ft_swap(int *a, int *b);
void ft_sort_int_tab(int *tab, int size);
void ft_free_coords(t_coords *coords);
void ft_free_arr(char *arr);
void ft_free_d_arr(char **arr);
int ft_check_map(int file);
int ft_write_error(int file);
int ft_controller(int file);
t_coords *ft_sort_x(t_coords *coords, int size);
t_coords *ft_sort_y(t_coords *coords, int size);
t_coords *ft_get_list_obj(int file, char o);
int ft_get_nobj(int file, char o);
#endif

View File

@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* write.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/31 23:11:14 by axdubois #+# #+# */
/* Updated: 2023/08/02 20:34:57 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Display-h/write.h"
#include "../../includes/utils-h/utils.h"
#include "../../includes/algo-h/algo.h"
#include "../../includes/utils-h/charobj.h"
#include "../../includes/utils-h/fst_line.h"
#include "../../includes/utils-h/utils.h"
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_print(char *str)
{
int i;
i = 0;
while (str[i])
{
ft_putchar(str[i]);
i++;
}
}
void ft_print_linesq(int side,char x)
{
int i;
i = 0;
while(i < side)
{
ft_putchar(x);
i++;
}
}
void ft_print_result(t_square sq,t_charobj c,t_coords *listobj,int file)
{
int x;
int y;
int ysq;
y = 1;
ysq = 0;
while (y < ft_len_colum(file))
{
x = 0;
while (x < ft_len_line(file, y))
{
if ((listobj[x].x == x) && (listobj[y].y == y))
ft_putchar(c.o);
else if ((sq.x == x) && (ysq < sq.side))
{
ft_print_linesq(sq.side,c.x);
x += sq.side -1;
ysq++;
}
else
ft_putchar(c.n);
x++;
}
ft_putchar('\n');
y++;
}
}

BIN
BSQ/srcs/Display-c/write.o Normal file

Binary file not shown.

55
BSQ/srcs/algo/algo.c Normal file
View File

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* algo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/01 22:03:23 by axdubois #+# #+# */
/* Updated: 2023/08/02 20:32:48 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/algo-h/algo.h"
#include "../../includes/utils-h/utils.h"
int ft_is_side_max(int x, int y, int mx, int my)
{
int l;
l = (mx - x) - (my - y);
if (l > 0)
return (mx - x);
else if (l < 0)
return (my - y);
else
return (0);
}
t_square ft_comb(t_coords *listobjsx, t_coords *listobjsy)
{
int i;
int j;
t_square square_max;
square_max.x = 0;
square_max.y = 0;
square_max.side = 0;
i = -1;
j = -1;
while (listobjsx[++i].x > -1)
{
while (listobjsy[++j].y > -1)
{
if ((square_max.x - listobjsx[i].x) < 0
&& (square_max.y - listobjsy[j].y) < 0)
{
square_max.x = listobjsx[i].x;
square_max.y = listobjsy[i].y;
square_max.side = ft_is_side_max(listobjsx[i].x,
listobjsy[j].y, square_max.x, square_max.y);
}
}
}
return (square_max);
}

BIN
BSQ/srcs/algo/algo.o Normal file

Binary file not shown.

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* controller.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/02 14:57:07 by axdubois #+# #+# */
/* Updated: 2023/08/02 20:39:52 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Display-h/write.h"
#include "../../includes/utils-h/utils.h"
#include "../../includes/algo-h/algo.h"
#include "../../includes/utils-h/charobj.h"
#include "../../includes/utils-h/fst_line.h"
int ft_controller(int file)
{
t_charobj charobj;
charobj = ft_get_charobj(ft_get_first_line(file), ft_len_line(file, 0));
ft_print_result(ft_comb(ft_sort_x(ft_get_list_obj(file, charobj.o),
ft_get_nobj(file, charobj.o)),
ft_sort_y(ft_get_list_obj(file, charobj.o),
ft_get_nobj(file, charobj.o))), charobj,
ft_get_list_obj(file, charobj.o),
file);
return (0);
}

BIN
BSQ/srcs/algo/controller.o Normal file

Binary file not shown.

47
BSQ/srcs/main.c Normal file
View File

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/31 23:14:41 by axdubois #+# #+# */
/* Updated: 2023/08/02 20:37:09 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/utils-h/utils.h"
int ft_while_file(int file, char **argv)
{
int i;
i = 0;
while (argv[i])
{
if (ft_check_map(file) == 1)
return (1);
ft_controller(file);
i++;
}
return (0);
}
int main(int argc, char **argv)
{
int file;
int i;
i = 1;
if (argc == 1)
{
file = 0;
ft_controller(file);
}
else
{
file = open(argv[i], O_RDONLY);
ft_while_file(file, argv);
}
}

BIN
BSQ/srcs/main.o Normal file

Binary file not shown.

View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* charobj.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: axdubois <axdubois@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/02 15:09:36 by axdubois #+# #+# */
/* Updated: 2023/08/02 19:39:22 by axdubois ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/utils-h/charobj.h"
t_charobj ft_get_charobj(char *ftline,int sizeline)
{
t_charobj charobj;
charobj.n = ftline[sizeline -3];
charobj.o = ftline[sizeline -2];
charobj.x = ftline[sizeline -1];
return (charobj);
}

Binary file not shown.

View File

@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fst_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/01 18:23:55 by axdubois #+# #+# */
/* Updated: 2023/08/02 11:28:35 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/utils-h/utils.h"
int ft_len_line(int file, int n)
{
int count;
char *buf;
int i;
i = 0;
while (i < n)
{
read(file, &buf, 1);
i++;
}
count = 0;
buf = malloc(1);
while (*buf != '\n' && read(file, &buf, 1) > 0)
count++;
free(buf);
close(file);
return (count);
}
int ft_len_colum(int file)
{
int count;
char *buf;
int i;
i = 0;
count = 0;
buf = malloc(1);
while (read(file, &buf, 1) > 0)
{
if (*buf == '\n')
count++;
}
free(buf);
close(file);
return (count);
}
char *ft_get_first_line(int file)
{
char *buf;
int i;
char *fst_line;
int len_line;
//if (file == -1) main
//return (-1);
len_line = ft_len_line(file, 0);
fst_line = malloc(len_line + 1);
if (fst_line == NULL)
return (NULL);
i = 0;
while (i < len_line)
{
read(file, &buf, 1);
fst_line[i] = *buf;
i++;
}
fst_line[i] = 0;
return (fst_line);
}

Binary file not shown.

View File

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: axdubois <axdubois@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/02 10:06:20 by adjoly #+# #+# */
/* Updated: 2023/08/02 19:46:23 by axdubois ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Display-h/write.h"
#include "../../includes/utils-h/utils.h"
#include "../../includes/algo-h/algo.h"
#include "../../includes/utils-h/charobj.h"
#include "../../includes/utils-h/fst_line.h"
int ft_write_error(int file)
{
write(2, "Map Error", 1);
close(file);
return (1);
}
int ft_atoi(char *str)
{
int i;
int result;
i = 0;
result = 0;
while (str[i] >= '0' && str[i] <= '9')
{
result = (result * 10) + str[i] - 48;
i++;
}
return (result);
}
int ft_check_map(int file)
{
char *first_line;
int line_nb;
int i_line_nb;
int i;
i = 1;
line_nb = ft_len_colum(file);
first_line = ft_get_first_line(file);
i_line_nb = ft_atoi(first_line);
if (i_line_nb != line_nb)
return (ft_write_error(file));
while (i < line_nb)
{
if (ft_len_line(file, i) != i_line_nb)
return (ft_write_error(file));
i++;
}
return (0);
}

Binary file not shown.

View File

@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: axdubois <axdubois@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/31 23:10:14 by axdubois #+# #+# */
/* Updated: 2023/08/02 19:39:26 by axdubois ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/utils-h/utils.h"
#include "../../includes/utils-h/fst_line.h"
int ft_get_nobj(int file, char o)
{
int count;
char *buf;
count = 0;
buf = malloc(1);
while (read(file, &buf, 1) > 0)
{
read(file, &buf, 1);
if (*buf == o)
count++;
}
free(buf);
close(file);
return (count);
}
int ft_get_y(int file, int obj_c, char o)
{
int count_obj;
int y_obj;
char *buf;
y_obj = 0;
count_obj = 0;
buf = malloc(1);
while (*buf)
{
read(file, &buf, 1);
if (*buf == '\n')
y_obj++;
if (*buf == o)
{
count_obj++;
if (obj_c == count_obj)
return (y_obj);
}
}
free(buf);
return(0);
}
int ft_get_x(int file, int obj_c, char o)
{
int count_obj;
int x_obj;
char *buf;
x_obj = 0;
count_obj = 0;
buf = malloc(1);
while (*buf)
{
read(file, &buf, 1);
if (*buf == '\n')
x_obj = 0;
if (*buf == o)
{
count_obj++;
if (obj_c == count_obj)
return (x_obj);
}
x_obj++;
}
free(buf);
return (0);
}
t_coords *ft_get_list_obj(int file, char o)
{
t_coords *list_obj;
int i;
int *buf;
int obj_c;
list_obj = malloc(sizeof(t_coords) * ft_get_nobj(file, o));
if (list_obj == NULL)
return (NULL);
i = 0;
obj_c = 0;
buf = malloc(1);
while (i < ft_get_nobj(file, o))
{
read(file, &buf, 1);
if (*buf == o)
{
obj_c++;
list_obj[i].x = ft_get_x(file, obj_c, o);
list_obj[i].y = ft_get_y(file, obj_c, o);
}
i++;
}
list_obj[i].x = ft_len_line(file, 1);
list_obj[i].y = ft_len_colum(file);
i++;
list_obj[i].x = -1;
list_obj[i].y = -1;
free(buf);
return (list_obj);
}

Binary file not shown.

37
BSQ/srcs/utils-c/free.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/01 17:51:39 by adjoly #+# #+# */
/* Updated: 2023/08/01 18:16:58 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Display-h/write.h"
#include "../../includes/utils-h/utils.h"
void ft_free_coords(t_coords *coords)
{
free(coords);
}
void ft_free_arr(char *arr)
{
free(arr);
}
void ft_free_d_arr(char **arr)
{
int i;
i = -1;
while (arr[i])
{
free(arr[i]);
i++;
}
free(*arr);
}

BIN
BSQ/srcs/utils-c/free.o Normal file

Binary file not shown.

80
BSQ/srcs/utils-c/utils.c Normal file
View File

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: axdubois <axdubois@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/31 23:27:23 by axdubois #+# #+# */
/* Updated: 2023/08/02 19:28:42 by axdubois ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/Display-h/write.h"
#include "../../includes/utils-h/utils.h"
int ft_strlen(char *str)
{
int i;
i = 0;
while (*str != 0)
{
i++;
str++;
}
return (i);
}
void ft_swap(int *a, int *b)
{
int tmp;
tmp = *b;
*b = *a;
*a = tmp;
}
t_coords *ft_sort_x(t_coords *coords, int size)
{
t_coords *arr;
int i;
i = 0;
arr = coords;
while (i != size - 1)
{
if (arr[i].x > arr[i + 1].x)
{
ft_swap(&arr[i].x, &arr[i + 1].x);
i = 0;
}
else
{
i++;
}
}
return (arr);
}
t_coords *ft_sort_y(t_coords *coords, int size)
{
t_coords *arr;
int i;
i = 0;
arr = coords;
while (i != size - 1)
{
if (arr[i].y > arr[i + 1].y)
{
ft_swap(&arr[i].y, &arr[i + 1].y);
i = 0;
}
else
{
i++;
}
}
return (arr);
}

BIN
BSQ/srcs/utils-c/utils.o Normal file

Binary file not shown.

11
BSQ/test Normal file
View File

@ -0,0 +1,11 @@
10.ox
...o.....o
......o..o
.oo...oo..
....o..ooo
o.....o..o
...o.....o
oo......o.
o..oo.....
..oo..o.o.
o..o......