Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
eserebry authored Dec 5, 2017
1 parent a05988c commit 4af449c
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 108 deletions.
61 changes: 18 additions & 43 deletions srcs/display_management.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@

void display_error(t_lemin *lemin)
{
ft_printf("{STC:GR}Look at what we've got here:\n" NONE);
ft_printf("{STC:LG}{SET:UL}{SET:RB}Look at what we've got here:\n" NN);
if (lemin->ants_num == 0)
ft_printf("{STC:RD}No ants\n" NONE);
ft_printf("{STC:RD}{SET:RB}*No ants*\n" NN);
else
ft_printf("{STC:DG}%d ants\n" NONE, lemin->ants_num);
ft_printf("{STC:DG}{SET:UL}{SET:RB}*%d ants*\n" NN, lemin->ants_num);
if (lemin->rooms_num == 0)
ft_printf("{STC:RD}No rooms\n" NONE);
ft_printf("{STC:RD}{SET:RB}*No rooms*\n" NN);
else
ft_printf("{STC:LB}%d rooms\n" NONE, lemin->rooms_num);
ft_printf("{STC:LB}{SET:UL}{SET:RB}*%d rooms*\n" NN, lemin->rooms_num);
if (lemin->links_num == 0)
ft_printf("{STC:RD}No doors\n" NONE);
ft_printf("{STC:RD}{SET:RB}*No doors*\n" NN);
else
ft_printf("{STC:LY}%d doors\n" NONE, lemin->links_num);
ft_printf("{STC:LY}{SET:UL}{SET:RB}*%d doors*\n" NN, lemin->links_num);
}

/*
Expand All @@ -35,63 +35,36 @@ void display_error(t_lemin *lemin)

int validate_input(t_lemin *lemin)
{
int check;
int check;
t_room *tmp;

check = 1;
if (check == 1)
display_error(lemin);
if (lemin->ants_num == 0 || lemin->rooms_num == 0 || lemin->links_num == 0)
check = 0;
if (!display_rooms(lemin))
if (!display_rooms(lemin, tmp))
check = 0;
if (check == 0)
return (0);
if (!validate_path(lemin, lemin->rooms))
{
check = 0;
ft_printf("{STC:RD}No, there is no way.\n" NONE);
ft_printf("{STC:RD}{SET:UL}{SET:RB}No, there is no way.\n" NN);
}
else
ft_printf("{STC:LR}Yes, there is a way!\n" NONE);
ft_printf("{STC:LG}{SET:UL}{SET:RB}Yes, there is a way!\n" NN);
return (check);
}

/*
**looking for a possible path, if it's exist
*/

int validate_path(t_lemin *lemin, t_room *rooms)
{
t_room *tmp;
int x;
int y;

tmp = start_search(rooms);
y = 0;
tmp->checked = 1;
tmp->pos = y;
tmp->path = 1;
if (tmp->doors_num == 0)
return (0);
x = -1;
while (tmp->door_names[++x])
{
if (searching_path(lemin, tmp->door_names[x], y))
return (1);
}
return (0);
}

/*
**validating if exit and start are exist and displpay the result
*/

int display_rooms(t_lemin *lemin)
int display_rooms(t_lemin *lemin, t_room *tmp)
{
int count;
int start;
int end;
t_room *tmp;

tmp = lemin->rooms;
start = 0;
Expand All @@ -106,10 +79,12 @@ int display_rooms(t_lemin *lemin)
}
if (start == 1 && end == 1)
{
ft_printf("{STC:LG}##start and ##end are exist\n" NONE);
ft_printf("{STC:GR}##start is exist\n{STC:BU}##end is exist\n" NN);
return (1);
}
else
ft_printf("{STC:RD}Missing start and/or end \n" NONE);
else if (start == 0 && end == 1)
ft_printf("{STC:BU}##end is exist\n"NN"{STC:RD}##start is missing\n"NN);
else if (start == 1 && end == 0)
ft_printf("{STC:BU}##start is exist\n"NN"{STC:RD}Missing ##end \n" NN);
return (0);
}
8 changes: 8 additions & 0 deletions srcs/ft_ants.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

#include "../includes/lem_in.h"

/*
**checking if ants are represented by number
*/

int check_ants(char *str)
{
int x;
Expand All @@ -25,6 +29,10 @@ int check_ants(char *str)
return (1);
}

/*
**getting number of ants
*/

int get_ants(t_lemin *lemin, char *line)
{
if (!check_ants(line))
Expand Down
65 changes: 65 additions & 0 deletions srcs/ft_free.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free_all.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eserebry <eserebry@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 06:48:54 by eserebry #+# #+# */
/* Updated: 2017/11/26 03:11:38 by eserebry ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/lem_in.h"

void delete_links(t_link *links)
{
if (links)
{
delete_links(links->next);
free(links);
}
}

void delete_rooms(t_room *rooms)
{
int x;

x = -1;
if (!rooms->door_names)
return ;
while (rooms->door_names[++x])
ft_strdel(&rooms->door_names[x]);
ft_memdel((void*)&rooms->door_names);
}

void delete_path(t_lemin *lemin)
{
t_room *tmp;

tmp = lemin->rooms->next;
while (tmp)
{
if (lemin->rooms->door_names)
delete_rooms(lemin->rooms);
if (lemin->rooms->name)
ft_strdel(&lemin->rooms->name);
ft_memdel((void*)&lemin->rooms);
lemin->rooms = tmp;
tmp = tmp->next;
}
if (lemin->rooms->name)
ft_strdel(&lemin->rooms->name);
delete_rooms(lemin->rooms);
ft_memdel((void*)&lemin->rooms);
}

void free_all(t_lemin *lemin)
{
if (lemin->links)
delete_links(lemin->links);
if (lemin->rooms)
delete_path(lemin);
ft_strdel(&lemin->input);
ft_memdel((void*)&lemin);
}
45 changes: 6 additions & 39 deletions srcs/ft_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,20 @@

#include "../includes/lem_in.h"

int validate_spaces(char *line)
{
int spaces;
int x;

spaces = 0;
x = -1;
while (line[++x])
{
if (line[x] == ' ')
spaces++;
}
return (spaces);
}

int check_for_room(char *line)
{
if (line[0] == '#' || line[0] == 'L')
return (0);
else if ((validate_spaces(line)) == 2)
return (1);
return (0);
}

int get_rooms(t_lemin *lemin, char *line)
{
if (!check_for_room(line))
return (0);
add_room(lemin, line);
lemin->rooms_num++;
return (1);
}

/*
** checking if ##start and ##end are found
*/

int get_start_end(t_lemin *lemin, char *line)
int check_start_end(t_lemin *lemin, char *line)
{
if ((ft_strcmp(line, "##start")) == 0)
{
lemin->start_on = 1;
lemin->check_start = 1;
return (1);
}
else if ((ft_strcmp(line, "##end")) == 0)
{
lemin->end_on = 1;
lemin->check_end = 1;
return (1);
}
return (0);
Expand All @@ -75,11 +42,11 @@ int get_input(t_lemin *lemin)
{
if (get_ants(lemin, line))
;
else if (get_rooms(lemin, line))
else if (add_rooms(lemin, line))
;
else if (get_links(lemin, line))
else if (add_links(lemin, line))
;
else if (get_start_end(lemin, line))
else if (check_start_end(lemin, line))
;
lemin->input = ft_strjoin_free(lemin->input, line, 1);
lemin->input = ft_strjoin_free(lemin->input, "\n", 1);
Expand Down
10 changes: 5 additions & 5 deletions srcs/ft_links.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int check_for_links(char *line)
** allocating memory for links
*/

t_link *new(t_lemin *lemin, t_link *tmp)
t_link *new_link(t_lemin *lemin, t_link *tmp)
{
if (!lemin->links)
{
Expand All @@ -60,14 +60,14 @@ t_link *new(t_lemin *lemin, t_link *tmp)
** adding a link
*/

void add_link(t_lemin *lemin, char *line)
void parse_link(t_lemin *lemin, char *line)
{
t_link *tmp;
int x;
int y;

tmp = NULL;
tmp = new(lemin, tmp);
tmp = new_link(lemin, tmp);
x = -1;
while (line[++x] != '-')
;
Expand All @@ -79,11 +79,11 @@ void add_link(t_lemin *lemin, char *line)
tmp->next = NULL;
}

int get_links(t_lemin *lemin, char *line)
int add_links(t_lemin *lemin, char *line)
{
if (!check_for_links(line))
return (0);
add_link(lemin, line);
parse_link(lemin, line);
lemin->links_num++;
return (1);
}
84 changes: 84 additions & 0 deletions srcs/ft_path.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_for_end.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eserebry <eserebry@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 07:08:09 by eserebry #+# #+# */
/* Updated: 2017/11/25 02:22:34 by eserebry ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/lem_in.h"

int room_end(t_lemin *lemin, t_room *tmp, int y)
{
tmp->path = 1;
tmp->pos = y + 1;
return (1);
}

int recursion(t_lemin *lemin, t_room *tmp, int y)
{
int x;

x = -1;
while (tmp->door_names[++x])
{
if ((searching_path(lemin, tmp->door_names[x], y + 1)) == 1)
return (room_end(lemin, tmp, y));
}
return (0);
}

int searching_path(t_lemin *lemin, char *door_name, int y)
{
t_room *tmp;

tmp = lemin->rooms;
while (tmp)
{
if (ft_strcmp(tmp->name, door_name) == 0)
{
if (tmp->checked == 1)
return (0);
tmp->checked = 1;
if (tmp->end == 1)
return (room_end(lemin, tmp, y));
else
{
if (recursion(lemin, tmp, y))
return (1);
}
}
tmp = tmp->next;
}
return (0);
}

/*
**looking for a possible path, if it's exist
*/

int validate_path(t_lemin *lemin, t_room *rooms)
{
t_room *tmp;
int x;
int y;

tmp = start_search(rooms);
y = 0;
tmp->checked = 1;
tmp->pos = y;
tmp->path = 1;
if (tmp->doors_num == 0)
return (0);
x = -1;
while (tmp->door_names[++x])
{
if (searching_path(lemin, tmp->door_names[x], y))
return (1);
}
return (0);
}
Loading

0 comments on commit 4af449c

Please sign in to comment.