Skip to content

Commit

Permalink
solver
Browse files Browse the repository at this point in the history
  • Loading branch information
nasrat-v authored May 14, 2018
1 parent 45fd1b5 commit 9f2916e
Show file tree
Hide file tree
Showing 6 changed files with 407 additions and 0 deletions.
41 changes: 41 additions & 0 deletions solver/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
##
## Makefile for Makefile in /home/nasrat_v/rendu/Initiation_IA/dante
##
## Made by nasrat_v
## Login <[email protected]>
##
## Started on Sun May 29 15:56:10 2016 nasrat_v
## Last update Mon May 14 23:46:20 2018 Valentin Nasraty
##

RM = rm -f

NAME = solver

SRC = main.c \
read.c \
count.c \
my_str_to_wordtab.c

OBJS = $(SRC:.c=.o)

CC = gcc

CFLAGS = -W -Wall -Wextra -Wundef -Wpointer-arith -Wcast-align -Wcast-qual -Wunreachable-code

NCURSES = -lncurses -ltinfo

LIB = ../lib/libmy.a

all: $(NAME) $(NAME2) $(NAME3) $(NAME4)

$(NAME): $(OBJS)
$(CC) -o $(NAME) $(OBJS) $(LIB) $(NCURSES) -no-pie

clean:
$(RM) $(OBJS)

fclean: clean
$(RM) $(NAME) $(NAME2) $(NAME3) $(NAME4)

re: fclean all
31 changes: 31 additions & 0 deletions solver/count.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
** count.c for dante in /home/nasrat_v/rendu/Initiation_IA/dante
**
** Made by nasrat_v
** Login <[email protected]>
**
** Started on Sun May 29 15:55:08 2016 nasrat_v
** Last update Fri Oct 27 13:34:18 2017 Valentin Nasraty
*/

#include "solver.h"

int my_count_col(char **tab)
{
int c;

c = 0;
while (tab[0][c])
c += 1;
return (c);
}

int my_tablen(char **tab)
{
int i;

i = 0;
while (tab && tab[i])
i += 1;
return (i);
}
55 changes: 55 additions & 0 deletions solver/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
** main.c for dante in /home/nasrat_v/rendu/Initiation_IA/dante
**
** Made by nasrat_v
** Login <[email protected]>
**
** Started on Sun May 29 15:55:19 2016 nasrat_v
** Last update Fri Oct 27 13:41:07 2017 Valentin Nasraty
*/

#include "solver.h"

void init_ncurses()
{
SCREEN *win;

win = newterm(NULL, stderr, stdin);
set_term(win);
refresh();
keypad(stdscr, 1);
curs_set(0);
start_color();
noecho();
}

int main(int ac, char **av)
{
t_struct st;

if (ac == 2)
{
if ((read_map(&st, av[1])) == 1)
return (0);
st.y = my_tablen(st.tab);
st.x = my_count_col(st.tab);
init_ncurses();
my_show_wordtab(st.tab);
if ((set_begin_end(&st)) == 0)
{
write(1, "No solution found\n", my_strlen("No solution found\n"));
return (0);
}
if ((resolve(&st, 0, 0)) == 0)
{
write(1, "No solution found\n", my_strlen("No solution found\n"));
return (0);
}
modif_for_push(&st);
my_putchar('\n');
}
else
return (1);
endwin();
return (0);
}
117 changes: 117 additions & 0 deletions solver/my_str_to_wordtab.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
** my_str_to_wordtab.c for dante in /home/nasrat_v/rendu/Initiation_IA/dante
**
** Made by nasrat_v
** Login <[email protected]>
**
** Started on Sun May 29 15:56:21 2016 nasrat_v
** Last update Fri Oct 27 13:34:46 2017 Valentin Nasraty
*/

#include "solver.h"

int nb_mot(char *str, char sep)
{
int i;
int nb;

i = 0;
nb = 1;
while (str[i] != 0)
{
if (str[i] == sep)
nb = nb + 1;
i = i + 1;
}
return (nb);
}

int nb_mot_bis(char *str, char sep1, char sep2)
{
int i;
int nb;

i = 0;
nb = 1;
while (str[i] != 0)
{
if (str[i] == sep1 && str[i + 1] == sep2)
nb = nb + 1;
i = i + 1;
}
return (nb);
}

char **my_str_to_wordtab(char *str, char sep)
{
char **tab;
int i;
int j;

if ((tab = malloc((nb_mot(str, sep) + 1) * sizeof(char*))) == NULL)
return (NULL);
tab[nb_mot(str, sep)] = NULL;
i = 0;
j = 1;
tab[0] = str;
while (str[i] != 0)
{
if (str[i] == sep)
{
str[i] = 0;
i = i + 1;
tab[j] = &str[i];
j = j + 1;
}
i = i + 1;
}
return (tab);
}

char **my_str_to_wordtab_bis(char *str, char sep1, char sep2)
{
char **tab;
int i;
int j;

if ((tab = malloc((nb_mot_bis(str, sep1, sep2) + 1) * sizeof(char*))) == NULL)
return (NULL);
tab[nb_mot_bis(str, sep1, sep2)] = NULL;
i = 0;
j = 1;
tab[0] = str;
while (str[i] != 0)
{
if (str[i] == sep1 && str[i + 1] == sep2)
{
str[i] = 0;
i = i + 1;
tab[j] = &str[i + 1];
j = j + 1;
}
i = i + 1;
}
return (tab);
}

void my_show_wordtab(char **tab)
{
int i;
int c;

i = 0;
BLUE;
attron(COLOR_PAIR(3));
while (tab[i] != NULL)
{
c = 0;
while (tab[i][c])
{
if (tab[i][c] == 'X')
mvprintw(c, i, "N");
c += 1;
}
i += 1;
}
attroff(COLOR_PAIR(3));
}
99 changes: 99 additions & 0 deletions solver/read.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
** read.c for dante in /home/nasrat_v/rendu/Initiation_IA/dante
**
** Made by nasrat_v
** Login <[email protected]>
**
** Started on Sun May 29 15:55:40 2016 nasrat_v
** Last update Fri Oct 27 13:40:25 2017 Valentin Nasraty
*/

#include "solver.h"

int resolve(t_struct *st, int l, int c)
{
usleep(UTIME_TO_WAIT);
timeout(1);
getch();
if (c < 0 || (c > st->x - 1) || l < 0 || (l > st->y - 1))
return (0);
else if (st->tab[l][c] == 'F')
return (1);
else if (st->tab[l][c] != '*' && st->tab[l][c] != 'D')
return (0);
RED;
attron(COLOR_PAIR(1));
mvprintw(c, l, "o");
attroff(COLOR_PAIR(1));
st->tab[l][c] = 'o';
if (resolve(st, l, (c + 1)) == 1)
return (1);
else if (resolve(st, (l + 1), c) == 1)
return (1);
else if (resolve(st, l, (c - 1)) == 1)
return (1);
else if (resolve(st, (l - 1), c) == 1)
return (1);
YELLOW;
attron(COLOR_PAIR(2));
mvprintw(c, l, "C");
attroff(COLOR_PAIR(2));
st->tab[l][c] = 'C';
return (0);
}

int set_begin_end(t_struct *st)
{
if (st->tab[0][0] == 'X' || st->tab[st->y - 1][st->x - 1] == 'X')
return (0);
st->tab[0][0] = 'D';
st->tab[st->y - 1][st->x - 1] = 'F';
st->tab[st->y - 1][st->x] = 0;
return (1);
}

void modif_for_push(t_struct *st)
{
int l;
int c;

l = 0;
while (st->tab[l])
{
c = 0;
while (st->tab[l][c])
{
if (st->tab[l][c] == 'C')
st->tab[l][c] = '*';
else if (st->tab[l][c] == 'F')
st->tab[l][c] = 'o';
c += 1;
}
l += 1;
}
}

int read_map(t_struct *st, char *filename)
{
struct stat sb;
int fd;
int l;
char *buffer;

if (stat(filename, &sb) == -1)
return (1);
if ((fd = open(filename, O_RDWR)) == -1)
return (1);
if ((buffer = malloc(sizeof(char) * (sb.st_size + 1))) == NULL)
return (1);
if (read(fd, buffer, sb.st_size) <= 0)
return (1);
buffer[sb.st_size - 1] = '\0';
l = sb.st_size - 1;
while (buffer[--l] == '\n');
buffer[l + 1] = 0;
close(fd);
st->tab = my_str_to_wordtab(buffer, '\n');
my_show_wordtab(st->tab);
return (0);
}
Loading

0 comments on commit 9f2916e

Please sign in to comment.