From 9f2916e9795a753bc5d33a8134e7d77edebc60c3 Mon Sep 17 00:00:00 2001 From: Valentin Nasraty Date: Tue, 15 May 2018 00:11:52 +0200 Subject: [PATCH] solver --- solver/Makefile | 41 +++++++++++++ solver/count.c | 31 ++++++++++ solver/main.c | 55 +++++++++++++++++ solver/my_str_to_wordtab.c | 117 +++++++++++++++++++++++++++++++++++++ solver/read.c | 99 +++++++++++++++++++++++++++++++ solver/solver.h | 64 ++++++++++++++++++++ 6 files changed, 407 insertions(+) create mode 100644 solver/Makefile create mode 100644 solver/count.c create mode 100644 solver/main.c create mode 100644 solver/my_str_to_wordtab.c create mode 100644 solver/read.c create mode 100644 solver/solver.h diff --git a/solver/Makefile b/solver/Makefile new file mode 100644 index 0000000..2cce1e1 --- /dev/null +++ b/solver/Makefile @@ -0,0 +1,41 @@ +## +## Makefile for Makefile in /home/nasrat_v/rendu/Initiation_IA/dante +## +## Made by nasrat_v +## Login +## +## 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 diff --git a/solver/count.c b/solver/count.c new file mode 100644 index 0000000..2a6d6a9 --- /dev/null +++ b/solver/count.c @@ -0,0 +1,31 @@ +/* +** count.c for dante in /home/nasrat_v/rendu/Initiation_IA/dante +** +** Made by nasrat_v +** Login +** +** 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); +} diff --git a/solver/main.c b/solver/main.c new file mode 100644 index 0000000..84a23e8 --- /dev/null +++ b/solver/main.c @@ -0,0 +1,55 @@ +/* +** main.c for dante in /home/nasrat_v/rendu/Initiation_IA/dante +** +** Made by nasrat_v +** Login +** +** 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); +} diff --git a/solver/my_str_to_wordtab.c b/solver/my_str_to_wordtab.c new file mode 100644 index 0000000..b09644a --- /dev/null +++ b/solver/my_str_to_wordtab.c @@ -0,0 +1,117 @@ +/* +** my_str_to_wordtab.c for dante in /home/nasrat_v/rendu/Initiation_IA/dante +** +** Made by nasrat_v +** Login +** +** 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)); +} diff --git a/solver/read.c b/solver/read.c new file mode 100644 index 0000000..382a527 --- /dev/null +++ b/solver/read.c @@ -0,0 +1,99 @@ +/* +** read.c for dante in /home/nasrat_v/rendu/Initiation_IA/dante +** +** Made by nasrat_v +** Login +** +** 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); +} diff --git a/solver/solver.h b/solver/solver.h new file mode 100644 index 0000000..7a6fad7 --- /dev/null +++ b/solver/solver.h @@ -0,0 +1,64 @@ +/* +** solver.h for dante in /home/nasrat_v/rendu/Initiation_IA/dante +** +** Made by nasrat_v +** Login +** +** Started on Sun May 29 15:55:29 2016 nasrat_v +** Last update Mon May 14 23:53:50 2018 Valentin Nasraty +*/ + +#ifndef SOLVER_H_ +# define SOLVER_H_ + +# define RED init_pair(1, COLOR_RED, COLOR_RED) +# define YELLOW init_pair(2, COLOR_YELLOW, COLOR_YELLOW) +# define BLUE init_pair(3, COLOR_BLUE, COLOR_BLUE) +/* Time in milliseconds of the slows motion */ +# define UTIME_TO_WAIT 500 + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct s_struct +{ + char **tab; + int a; + int b; + int i; + int n; + int x; + int y; +} t_struct; + +/* !LIBMY.A */ +void my_putchar(char c); +void my_putstr(char *str); +int my_strlen(char *str); +char *get_next_line(const int fd); + +/* !READ.C */ +int resolve(t_struct *st, int l, int c); +int set_begin_end(t_struct *st); +void modif_for_push(t_struct *st); +int read_map(t_struct *st, char *filename); + +/* !MY_STR_TO_WORDTAB */ +int nb_mot(char *str, char sep); +int nb_mot_bis(char *str, char sep1, char sep2); +char **my_str_to_wordtab(char *str, char sep); +char **my_str_to_wordtab_bis(char *str, char sep1, char sep2); +void my_show_wordtab(char **tab); + +/* !COUNT.C */ +int my_count_col(char **tab); +int my_tablen(char **tab); + +#endif /* !SOLVER_H_ */