From c6dfd8f9da8a1733a6dffda1de532e1a11c96ce0 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 17 Feb 2023 16:29:44 +0100 Subject: [PATCH 01/18] use enum --- TP1/C-Cpp/includes/mysok.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/TP1/C-Cpp/includes/mysok.h b/TP1/C-Cpp/includes/mysok.h index ee0d31c..986c0dc 100644 --- a/TP1/C-Cpp/includes/mysok.h +++ b/TP1/C-Cpp/includes/mysok.h @@ -7,11 +7,7 @@ #define NBC 20 #define MAX_CRATES 20 -#define MOVE_U 0 -#define MOVE_D 1 -#define MOVE_L 2 -#define MOVE_R 3 -#define MOVE_W 4 +enum movement { MOVE_U = 0, MOVE_D, MOVE_L, MOVE_R, MOVE_W }; enum board_str { OUT = ' ', From 59c984d5ea8770a40c9555df8d24b3998eb79e86 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 19 Feb 2023 17:58:59 +0100 Subject: [PATCH 02/18] impl bfs --- TP1/C-Cpp/includes/mysok.h | 17 ++++- TP1/C-Cpp/src/mysok.cpp | 134 +++++++++++++++++++++++++++++++++++-- TP1/C-Cpp/src/r0.cpp | 14 ++++ 3 files changed, 158 insertions(+), 7 deletions(-) diff --git a/TP1/C-Cpp/includes/mysok.h b/TP1/C-Cpp/includes/mysok.h index 986c0dc..2261ada 100644 --- a/TP1/C-Cpp/includes/mysok.h +++ b/TP1/C-Cpp/includes/mysok.h @@ -1,6 +1,8 @@ #ifndef MYSOK_H #define MYSOK_H +#include +#include #include #define NBL 20 @@ -23,7 +25,11 @@ enum board_str { END_OF_LINE = 'a' }; -// const std::string move_str[] = {"Up", "Down", "Left", "Right", "Wait"}; +const std::string move_str[] = {"Up", "Down", "Left", "Right", "Wait"}; + +const int dx[] = {-1, 1, 0, 0, 0}; +const int dy[] = {0, 0, -1, 1, 0}; +const int dsize = 5; struct sok_board_t { int board[NBL][NBC]; @@ -32,10 +38,19 @@ struct sok_board_t { int man1_y; int man2_x; int man2_y; + int num_crates_free; sok_board_t(); void print_board(); void load(char *_file); }; +typedef struct { + sok_board_t state; + int path_len; + std::string path; +} bfsNode; + +int bfs(bfsNode &result); + #endif diff --git a/TP1/C-Cpp/src/mysok.cpp b/TP1/C-Cpp/src/mysok.cpp index 16a4048..801a947 100644 --- a/TP1/C-Cpp/src/mysok.cpp +++ b/TP1/C-Cpp/src/mysok.cpp @@ -1,16 +1,16 @@ #include "../includes/mysok.h" sok_board_t::sok_board_t() { - for (int i = 0; i < NBL; i++) { - for (int j = 0; j < NBC; j++) { + for (int i = 0; i < NBL; ++i) { + for (int j = 0; j < NBC; ++j) { board[i][j] = FREE; } } } void sok_board_t::print_board() { - for (int i = 0; i < board_nbl; i++) { - for (int j = 0; j < NBC; j++) { + for (int i = 0; i < board_nbl; ++i) { + for (int j = 0; j < NBC; ++j) { if (board[i][j] == END_OF_LINE) { break; } @@ -33,11 +33,12 @@ void sok_board_t::load(char *_file) { exit(EXIT_FAILURE); } + num_crates_free = 0; board_nbl = 0; while ((nread = getline(&line, &len, fp)) != -1) { if ((static_cast(nread)) > 0) { bool read_ok = false; - for (int i = 0; i < nread; i++) { + for (int i = 0; i < nread; ++i) { switch (line[i]) { case (FREE): { board[board_nbl][i] = FREE; @@ -54,6 +55,7 @@ void sok_board_t::load(char *_file) { } case (CRATE_ON_FREE): { board[board_nbl][i] = CRATE_ON_FREE; + ++num_crates_free; break; } case (CRATE_ON_TARGET): { @@ -88,7 +90,7 @@ void sok_board_t::load(char *_file) { } if (read_ok) { board[board_nbl][nread - 1] = END_OF_LINE; - board_nbl++; + ++board_nbl; } } } @@ -96,3 +98,123 @@ void sok_board_t::load(char *_file) { free(line); fclose(fp); } + +int bfs(bfsNode &result) { + // Création de la queue de BFS + std::queue q; + + // Ajout de l'état initial à la queue + bfsNode first; + first.state = result.state; + first.path_len = 0; + first.path = ""; + q.push(first); + + // Boucle principale de la recherche BFS + while (!q.empty()) { + // Retrait de l'état en tête de la queue + bfsNode cur = q.front(); + q.pop(); + + // Si l'état actuel est celui souhaité (toutes les caisses sur des cibles) + if (cur.state.num_crates_free <= 0) { + result = cur; + return 0; + } + + // Parcours des déplacements possibles à partir de l'état actuel + for (int i = 0; i < dsize - 1; ++i) { + // Position de la case du joueur + int man1_x = cur.state.man1_x; + int man1_y = cur.state.man1_y; + + // Position du joueur après déplacement + int new_man1_x = cur.state.man1_x + dx[i]; + int new_man1_y = cur.state.man1_y + dy[i]; + + // Si la case devant le joueur est un mur + if (cur.state.board[new_man1_x][new_man1_y] == WALL) { + continue; + } + + // Si la case devant le joueur est libre + if (cur.state.board[new_man1_x][new_man1_y] == FREE || + cur.state.board[new_man1_x][new_man1_y] == TARGET) { + // On déplace le joueur dans le prochain état + sok_board_t next = cur.state; + + // Déplacement joueur, ancienne position + next.board[man1_x][man1_y] = + next.board[man1_x][man1_y] == MAN1_ON_TARGET ? TARGET : FREE; + // Déplacement joueur, nouvelle position + next.board[new_man1_x][new_man1_y] = + next.board[new_man1_x][new_man1_y] == TARGET ? MAN1_ON_TARGET + : MAN1_ON_FREE; + next.man1_x = new_man1_x; + next.man1_y = new_man1_y; + + // On ajoute cet état suivant à la queue + bfsNode next_bfs; + next_bfs.state = next; + next_bfs.path_len = cur.path_len + 1; + next_bfs.path = cur.path + move_str[i] + " "; + q.push(next_bfs); + } + + // Si la case devant le joueur est une caisse + if (cur.state.board[new_man1_x][new_man1_y] == CRATE_ON_FREE || + cur.state.board[new_man1_x][new_man1_y] == CRATE_ON_TARGET) { + // Position de la caisse + int cx = new_man1_x; + int cy = new_man1_y; + // Position de la caisse après déplacement + int new_cx = cx + dx[i]; + int new_cy = cy + dy[i]; + + // Si la case derrière la caisse n'est pas accessible + if (cur.state.board[new_cx][new_cy] != TARGET || + cur.state.board[new_cx][new_cy] != FREE) { + continue; + } + + // On déplace la caisse et le joueur + sok_board_t next = cur.state; + + // Déplacement joueur ancienne position + next.board[man1_x][man1_y] = + next.board[man1_x][man1_y] == MAN1_ON_TARGET ? TARGET : FREE; + + // Déplacement caisse, anciennne position, ie. nouvelle position joueur + next.board[cx][cy] = next.board[cx][cy] == CRATE_ON_TARGET + ? MAN1_ON_TARGET + : MAN1_ON_FREE; + next.man1_x = new_man1_x; + next.man1_y = new_man1_y; + // S'éloigne de l'objectif si on déplace une caisse d'une cible + if (next.board[cx][cy] == MAN1_ON_TARGET) { + ++next.num_crates_free; + } + + // Déplacement caisse, nouvelle position + next.board[new_cx][new_cy] = next.board[new_cx][new_cy] == TARGET + ? CRATE_ON_TARGET + : CRATE_ON_FREE; + // S'approche de l'objectif si on place une caisse sur la cible + if (next.board[new_cx][new_cy] == CRATE_ON_TARGET) { + --next.num_crates_free; + } + + // On ajoute l'état suivant à la queue + bfsNode next_bfs; + next_bfs.state = next; + next_bfs.path_len = cur.path_len + 1; + next_bfs.path = cur.path + move_str[i] + " "; + q.push(next_bfs); + } + } + } + + // Si la queue est vide et qu'on n'a pas trouvé de solution, c'est qu'il n'y + // en a pas + return -1; +} diff --git a/TP1/C-Cpp/src/r0.cpp b/TP1/C-Cpp/src/r0.cpp index 746df83..6a3fc8d 100644 --- a/TP1/C-Cpp/src/r0.cpp +++ b/TP1/C-Cpp/src/r0.cpp @@ -11,5 +11,19 @@ int main(int _ac, char **_av) { S.load(_av[1]); S.print_board(); + bfsNode result; + + result.state = S; + result.path = ""; + result.path_len = 0; + if (bfs(result) == -1) { + std::cout << "Aucune solution trouvée\n"; + } else { + std::cout << "Coups : " << result.path_len << "\n"; + std::cout << "Solution : " << result.path << "\n"; + } + + result.state.print_board(); + return 0; } From e4f1d90c6fde0e391a24f93c399e1f7f127747ba Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 19 Feb 2023 18:21:27 +0100 Subject: [PATCH 03/18] fix cond --- TP1/C-Cpp/src/mysok.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TP1/C-Cpp/src/mysok.cpp b/TP1/C-Cpp/src/mysok.cpp index 801a947..a1c9565 100644 --- a/TP1/C-Cpp/src/mysok.cpp +++ b/TP1/C-Cpp/src/mysok.cpp @@ -172,7 +172,7 @@ int bfs(bfsNode &result) { int new_cy = cy + dy[i]; // Si la case derrière la caisse n'est pas accessible - if (cur.state.board[new_cx][new_cy] != TARGET || + if (cur.state.board[new_cx][new_cy] != TARGET && cur.state.board[new_cx][new_cy] != FREE) { continue; } From 88928aa18c45c690f6899af15b84f63a4b1b417e Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 19 Feb 2023 18:23:59 +0100 Subject: [PATCH 04/18] add dfs --- TP1/C-Cpp/includes/mysok.h | 8 ++- TP1/C-Cpp/src/mysok.cpp | 124 +++++++++++++++++++++++++++++++++++-- TP1/C-Cpp/src/r0.cpp | 13 +++- 3 files changed, 133 insertions(+), 12 deletions(-) diff --git a/TP1/C-Cpp/includes/mysok.h b/TP1/C-Cpp/includes/mysok.h index 2261ada..e82c450 100644 --- a/TP1/C-Cpp/includes/mysok.h +++ b/TP1/C-Cpp/includes/mysok.h @@ -3,11 +3,12 @@ #include #include +#include #include #define NBL 20 #define NBC 20 -#define MAX_CRATES 20 +#define MAX_DEPTH 16 enum movement { MOVE_U = 0, MOVE_D, MOVE_L, MOVE_R, MOVE_W }; @@ -49,8 +50,9 @@ typedef struct { sok_board_t state; int path_len; std::string path; -} bfsNode; +} Node; -int bfs(bfsNode &result); +int bfs(Node &result); +int dfs(Node &result, int depth); #endif diff --git a/TP1/C-Cpp/src/mysok.cpp b/TP1/C-Cpp/src/mysok.cpp index a1c9565..5ad9450 100644 --- a/TP1/C-Cpp/src/mysok.cpp +++ b/TP1/C-Cpp/src/mysok.cpp @@ -99,12 +99,12 @@ void sok_board_t::load(char *_file) { fclose(fp); } -int bfs(bfsNode &result) { +int bfs(Node &result) { // Création de la queue de BFS - std::queue q; + std::queue q; // Ajout de l'état initial à la queue - bfsNode first; + Node first; first.state = result.state; first.path_len = 0; first.path = ""; @@ -113,7 +113,7 @@ int bfs(bfsNode &result) { // Boucle principale de la recherche BFS while (!q.empty()) { // Retrait de l'état en tête de la queue - bfsNode cur = q.front(); + Node cur = q.front(); q.pop(); // Si l'état actuel est celui souhaité (toutes les caisses sur des cibles) @@ -154,7 +154,7 @@ int bfs(bfsNode &result) { next.man1_y = new_man1_y; // On ajoute cet état suivant à la queue - bfsNode next_bfs; + Node next_bfs; next_bfs.state = next; next_bfs.path_len = cur.path_len + 1; next_bfs.path = cur.path + move_str[i] + " "; @@ -205,7 +205,7 @@ int bfs(bfsNode &result) { } // On ajoute l'état suivant à la queue - bfsNode next_bfs; + Node next_bfs; next_bfs.state = next; next_bfs.path_len = cur.path_len + 1; next_bfs.path = cur.path + move_str[i] + " "; @@ -218,3 +218,115 @@ int bfs(bfsNode &result) { // en a pas return -1; } + +int dfs(Node &result, int depth) { + // Si on a atteint la profondeur maximale, on arrête la recherche + if (depth == MAX_DEPTH) { + /* std::cout << "max\n"; */ + return -1; + } + + // Si l'état actuel est celui souhaité (toutes les caisses sur des cibles) + if (result.state.num_crates_free <= 0) { + return 0; + } + + // Parcours des déplacements possibles à partir de l'état actuel + for (int i = 0; i < dsize - 1; ++i) { + // Position de la case du joueur + int man1_x = result.state.man1_x; + int man1_y = result.state.man1_y; + + // Position du joueur après déplacement + int new_man1_x = result.state.man1_x + dx[i]; + int new_man1_y = result.state.man1_y + dy[i]; + + // Si la case devant le joueur est un mur, on ignore ce mouvement + if (result.state.board[new_man1_x][new_man1_y] == WALL) { + continue; + } + + // Si la case devant le joueur est libre ou une cible, on déplace le joueur + if (result.state.board[new_man1_x][new_man1_y] == FREE || + result.state.board[new_man1_x][new_man1_y] == TARGET) { + // On déplace le joueur dans le prochain état + sok_board_t next = result.state; + + // Déplacement joueur, ancienne position + next.board[man1_x][man1_y] = + next.board[man1_x][man1_y] == MAN1_ON_TARGET ? TARGET : FREE; + // Déplacement joueur, nouvelle position + next.board[new_man1_x][new_man1_y] = + next.board[new_man1_x][new_man1_y] == TARGET ? MAN1_ON_TARGET + : MAN1_ON_FREE; + next.man1_x = new_man1_x; + next.man1_y = new_man1_y; + + // On ajoute cet état suivant à la pile + Node next_dfs; + next_dfs.state = next; + next_dfs.path_len = result.path_len + 1; + next_dfs.path = result.path + move_str[i] + " "; + + int res = dfs(next_dfs, depth + 1); + if (res == 0) { + result = next_dfs; + return 0; + } + } + + // Si la case devant le joueur est une caisse, on déplace la caisse et le + // joueur + if (result.state.board[new_man1_x][new_man1_y] == CRATE_ON_FREE || + result.state.board[new_man1_x][new_man1_y] == CRATE_ON_TARGET) { + // Position de la caisse + int cx = new_man1_x; + int cy = new_man1_y; + // Position de la caisse après déplacement + int new_cx = cx + dx[i]; + int new_cy = cy + dy[i]; + + // Si la case derrière la caisse n'est pas accessible, on ignore ce + // mouvement + if (result.state.board[new_cx][new_cy] != TARGET && + result.state.board[new_cx][new_cy] != FREE) { + continue; + } + + // On déplace la caisse et le joueur dans le prochain état + sok_board_t next = result.state; + + // Déplacement caisse, ancienne position + next.board[cx][cy] = + next.board[cx][cy] == CRATE_ON_TARGET ? TARGET : FREE; + // Déplacement caisse, nouvelle position + next.board[new_cx][new_cy] = next.board[new_cx][new_cy] == TARGET + ? CRATE_ON_TARGET + : CRATE_ON_FREE; + // Déplacement joueur, ancienne position + next.board[man1_x][man1_y] = + next.board[man1_x][man1_y] == MAN1_ON_TARGET ? TARGET : FREE; + // Déplacement joueur, nouvelle position + next.board[new_man1_x][new_man1_y] = + next.board[new_man1_x][new_man1_y] == TARGET ? MAN1_ON_TARGET + : MAN1_ON_FREE; + next.man1_x = new_man1_x; + next.man1_y = new_man1_y; + + // On ajoute cet état suivant à la pile + Node next_dfs; + next_dfs.state = next; + next_dfs.path_len = result.path_len + 1; + next_dfs.path = result.path + move_str[i] + " "; + + int res = dfs(next_dfs, depth + 1); + if (res == 0) { + result = next_dfs; + return 0; + } + } + } + + // Aucune solution n'a été trouvée à partir de cet état + return -1; +} diff --git a/TP1/C-Cpp/src/r0.cpp b/TP1/C-Cpp/src/r0.cpp index 6a3fc8d..65eaa35 100644 --- a/TP1/C-Cpp/src/r0.cpp +++ b/TP1/C-Cpp/src/r0.cpp @@ -11,19 +11,26 @@ int main(int _ac, char **_av) { S.load(_av[1]); S.print_board(); - bfsNode result; + Node result; result.state = S; result.path = ""; result.path_len = 0; - if (bfs(result) == -1) { + /* if (bfs(result) == -1) { + std::cout << "Aucune solution trouvée\n"; + } else { + std::cout << "Coups : " << result.path_len << "\n"; + std::cout << "Solution : " << result.path << "\n"; + } */ + + if (dfs(result, 0) == -1) { std::cout << "Aucune solution trouvée\n"; } else { std::cout << "Coups : " << result.path_len << "\n"; std::cout << "Solution : " << result.path << "\n"; } - result.state.print_board(); + // result.state.print_board(); return 0; } From 0500215eda50abf716ce319291bf239f6b334518 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 19 Feb 2023 18:24:29 +0100 Subject: [PATCH 05/18] change depth --- TP1/C-Cpp/includes/mysok.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TP1/C-Cpp/includes/mysok.h b/TP1/C-Cpp/includes/mysok.h index e82c450..34cff32 100644 --- a/TP1/C-Cpp/includes/mysok.h +++ b/TP1/C-Cpp/includes/mysok.h @@ -8,7 +8,7 @@ #define NBL 20 #define NBC 20 -#define MAX_DEPTH 16 +#define MAX_DEPTH 30 enum movement { MOVE_U = 0, MOVE_D, MOVE_L, MOVE_R, MOVE_W }; From d6d2c5def513de75faad0c915d9e442e0cddb24d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 21 Feb 2023 13:24:15 +0100 Subject: [PATCH 06/18] stupid --- TP1/C-Cpp/src/mysok.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/TP1/C-Cpp/src/mysok.cpp b/TP1/C-Cpp/src/mysok.cpp index 5ad9450..62a58ba 100644 --- a/TP1/C-Cpp/src/mysok.cpp +++ b/TP1/C-Cpp/src/mysok.cpp @@ -303,6 +303,9 @@ int dfs(Node &result, int depth) { next.board[new_cx][new_cy] = next.board[new_cx][new_cy] == TARGET ? CRATE_ON_TARGET : CRATE_ON_FREE; + if (next.board[new_cx][new_cy] == CRATE_ON_TARGET) { + --next.num_crates_free; + } // Déplacement joueur, ancienne position next.board[man1_x][man1_y] = next.board[man1_x][man1_y] == MAN1_ON_TARGET ? TARGET : FREE; From fc077eb86b4e5c1b69ed766d82a0b17edad5b67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar?= Date: Tue, 21 Feb 2023 14:09:08 +0100 Subject: [PATCH 07/18] verification des coups passes --- TP1/C-Cpp/Screens-1/scree-test.txt | 0 TP1/C-Cpp/Screens-1/screen-0.txt | 3 +- TP1/C-Cpp/includes/mysok.h | 2 +- TP1/C-Cpp/src/mysok.cpp | 45 ++++++++++++++++++++++++------ TP1/C-Cpp/src/r0.cpp | 4 ++- 5 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 TP1/C-Cpp/Screens-1/scree-test.txt diff --git a/TP1/C-Cpp/Screens-1/scree-test.txt b/TP1/C-Cpp/Screens-1/scree-test.txt new file mode 100644 index 0000000..e69de29 diff --git a/TP1/C-Cpp/Screens-1/screen-0.txt b/TP1/C-Cpp/Screens-1/screen-0.txt index ea941ec..db249cb 100644 --- a/TP1/C-Cpp/Screens-1/screen-0.txt +++ b/TP1/C-Cpp/Screens-1/screen-0.txt @@ -4,5 +4,4 @@ #$$$$$$# #......# # # -######## - +######## \ No newline at end of file diff --git a/TP1/C-Cpp/includes/mysok.h b/TP1/C-Cpp/includes/mysok.h index 34cff32..a0af94c 100644 --- a/TP1/C-Cpp/includes/mysok.h +++ b/TP1/C-Cpp/includes/mysok.h @@ -53,6 +53,6 @@ typedef struct { } Node; int bfs(Node &result); -int dfs(Node &result, int depth); +int dfs(Node &result, int depth, std::vector history); #endif diff --git a/TP1/C-Cpp/src/mysok.cpp b/TP1/C-Cpp/src/mysok.cpp index 62a58ba..4da83aa 100644 --- a/TP1/C-Cpp/src/mysok.cpp +++ b/TP1/C-Cpp/src/mysok.cpp @@ -99,6 +99,22 @@ void sok_board_t::load(char *_file) { fclose(fp); } +bool is_in_history(Node current, std::vector history) { + for (int i = 0; i < history.size(); ++i) { + Node noeud = history[i]; + + int c = 0; + for (int l = 0; l < NBL; ++l) { + for (int m = 0; m < NBC; ++m) { + if (current.state.board[l][m] != current.state.board[l][m]) c++; + } + } + + if (c == 0) return true; + } + return false; +} + int bfs(Node &result) { // Création de la queue de BFS std::queue q; @@ -219,7 +235,8 @@ int bfs(Node &result) { return -1; } -int dfs(Node &result, int depth) { +int dfs(Node &result, int depth, std::vector history) { + // Si on a atteint la profondeur maximale, on arrête la recherche if (depth == MAX_DEPTH) { /* std::cout << "max\n"; */ @@ -268,10 +285,15 @@ int dfs(Node &result, int depth) { next_dfs.path_len = result.path_len + 1; next_dfs.path = result.path + move_str[i] + " "; - int res = dfs(next_dfs, depth + 1); - if (res == 0) { - result = next_dfs; - return 0; + if (!is_in_history(next_dfs, history)) { + history.push_back(next_dfs); + int res = dfs(next_dfs, depth + 1, history); + if (res == 0) { + result = next_dfs; + return 0; + } + } else { + return -1; } } @@ -322,10 +344,15 @@ int dfs(Node &result, int depth) { next_dfs.path_len = result.path_len + 1; next_dfs.path = result.path + move_str[i] + " "; - int res = dfs(next_dfs, depth + 1); - if (res == 0) { - result = next_dfs; - return 0; + if (!is_in_history(next_dfs, history)) { + history.push_back(next_dfs); + int res = dfs(next_dfs, depth + 1, history); + if (res == 0) { + result = next_dfs; + return 0; + } + } else { + return -1; } } } diff --git a/TP1/C-Cpp/src/r0.cpp b/TP1/C-Cpp/src/r0.cpp index 65eaa35..8e6dbe5 100644 --- a/TP1/C-Cpp/src/r0.cpp +++ b/TP1/C-Cpp/src/r0.cpp @@ -23,7 +23,9 @@ int main(int _ac, char **_av) { std::cout << "Solution : " << result.path << "\n"; } */ - if (dfs(result, 0) == -1) { + std::vector history; + + if (dfs(result, 0, history) == -1) { std::cout << "Aucune solution trouvée\n"; } else { std::cout << "Coups : " << result.path_len << "\n"; From 94d9102b64209f03bbcdbbcf7d04058b25bca782 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 21 Feb 2023 14:20:03 +0100 Subject: [PATCH 08/18] hihi --- TP1/C-Cpp/src/mysok.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/TP1/C-Cpp/src/mysok.cpp b/TP1/C-Cpp/src/mysok.cpp index 4da83aa..8b5c9df 100644 --- a/TP1/C-Cpp/src/mysok.cpp +++ b/TP1/C-Cpp/src/mysok.cpp @@ -106,11 +106,13 @@ bool is_in_history(Node current, std::vector history) { int c = 0; for (int l = 0; l < NBL; ++l) { for (int m = 0; m < NBC; ++m) { - if (current.state.board[l][m] != current.state.board[l][m]) c++; + if (current.state.board[l][m] != current.state.board[l][m]) + c++; } } - if (c == 0) return true; + if (c == 0) + return true; } return false; } @@ -319,6 +321,9 @@ int dfs(Node &result, int depth, std::vector history) { sok_board_t next = result.state; // Déplacement caisse, ancienne position + if (next.board[cx][cy] == CRATE_ON_TARGET) { + ++next.num_crates_free; + } next.board[cx][cy] = next.board[cx][cy] == CRATE_ON_TARGET ? TARGET : FREE; // Déplacement caisse, nouvelle position From 243b3ed6414deeecd338b138693ade4bb1213aa1 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 21 Feb 2023 14:22:16 +0100 Subject: [PATCH 09/18] tqt --- TP1/C-Cpp/src/mysok.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TP1/C-Cpp/src/mysok.cpp b/TP1/C-Cpp/src/mysok.cpp index 8b5c9df..65910f1 100644 --- a/TP1/C-Cpp/src/mysok.cpp +++ b/TP1/C-Cpp/src/mysok.cpp @@ -100,13 +100,13 @@ void sok_board_t::load(char *_file) { } bool is_in_history(Node current, std::vector history) { - for (int i = 0; i < history.size(); ++i) { + for (ulong i = 0; i < history.size(); ++i) { Node noeud = history[i]; int c = 0; for (int l = 0; l < NBL; ++l) { for (int m = 0; m < NBC; ++m) { - if (current.state.board[l][m] != current.state.board[l][m]) + if (current.state.board[l][m] != noeud.state.board[l][m]) c++; } } From e6742d54a06c87d117c064fd6cd856d765c799f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar?= Date: Tue, 21 Feb 2023 14:46:03 +0100 Subject: [PATCH 10/18] =?UTF-8?q?=C3=A7a=20marche=20bof?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TP1/C-Cpp/includes/mysok.h | 2 +- TP1/C-Cpp/src/mysok.cpp | 10 ++++------ TP1/C-Cpp/src/r0.cpp | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/TP1/C-Cpp/includes/mysok.h b/TP1/C-Cpp/includes/mysok.h index a0af94c..8b324d2 100644 --- a/TP1/C-Cpp/includes/mysok.h +++ b/TP1/C-Cpp/includes/mysok.h @@ -8,7 +8,7 @@ #define NBL 20 #define NBC 20 -#define MAX_DEPTH 30 +#define MAX_DEPTH 60 enum movement { MOVE_U = 0, MOVE_D, MOVE_L, MOVE_R, MOVE_W }; diff --git a/TP1/C-Cpp/src/mysok.cpp b/TP1/C-Cpp/src/mysok.cpp index 65910f1..b2e4069 100644 --- a/TP1/C-Cpp/src/mysok.cpp +++ b/TP1/C-Cpp/src/mysok.cpp @@ -100,8 +100,10 @@ void sok_board_t::load(char *_file) { } bool is_in_history(Node current, std::vector history) { - for (ulong i = 0; i < history.size(); ++i) { - Node noeud = history[i]; + + //printf("Taille history : %d\n", history.size()); + + for (auto &noeud : history) { int c = 0; for (int l = 0; l < NBL; ++l) { @@ -294,8 +296,6 @@ int dfs(Node &result, int depth, std::vector history) { result = next_dfs; return 0; } - } else { - return -1; } } @@ -356,8 +356,6 @@ int dfs(Node &result, int depth, std::vector history) { result = next_dfs; return 0; } - } else { - return -1; } } } diff --git a/TP1/C-Cpp/src/r0.cpp b/TP1/C-Cpp/src/r0.cpp index 8e6dbe5..33659eb 100644 --- a/TP1/C-Cpp/src/r0.cpp +++ b/TP1/C-Cpp/src/r0.cpp @@ -32,7 +32,7 @@ int main(int _ac, char **_av) { std::cout << "Solution : " << result.path << "\n"; } - // result.state.print_board(); + //result.state.print_board(); return 0; } From 155b81fa0ee8767fbe7da2491924188e9d97d846 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 22 Feb 2023 10:52:30 +0100 Subject: [PATCH 11/18] spaces --- TP1/C-Cpp/src/mysok.cpp | 3 +-- TP1/C-Cpp/src/r0.cpp | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/TP1/C-Cpp/src/mysok.cpp b/TP1/C-Cpp/src/mysok.cpp index b2e4069..d23defa 100644 --- a/TP1/C-Cpp/src/mysok.cpp +++ b/TP1/C-Cpp/src/mysok.cpp @@ -100,8 +100,7 @@ void sok_board_t::load(char *_file) { } bool is_in_history(Node current, std::vector history) { - - //printf("Taille history : %d\n", history.size()); + // printf("Taille history : %d\n", history.size()); for (auto &noeud : history) { diff --git a/TP1/C-Cpp/src/r0.cpp b/TP1/C-Cpp/src/r0.cpp index 33659eb..8e6dbe5 100644 --- a/TP1/C-Cpp/src/r0.cpp +++ b/TP1/C-Cpp/src/r0.cpp @@ -32,7 +32,7 @@ int main(int _ac, char **_av) { std::cout << "Solution : " << result.path << "\n"; } - //result.state.print_board(); + // result.state.print_board(); return 0; } From cee7cb5603d47a9fbdef59d63f184a6447469182 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 22 Feb 2023 11:33:07 +0100 Subject: [PATCH 12/18] remove empty test --- TP1/C-Cpp/Screens-1/scree-test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 TP1/C-Cpp/Screens-1/scree-test.txt diff --git a/TP1/C-Cpp/Screens-1/scree-test.txt b/TP1/C-Cpp/Screens-1/scree-test.txt deleted file mode 100644 index e69de29..0000000 From 2229ec32345c44335a44e230af012e72ac101706 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 22 Feb 2023 11:33:10 +0100 Subject: [PATCH 13/18] update report --- TP1/Rapport/rapport.tex | 54 ++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/TP1/Rapport/rapport.tex b/TP1/Rapport/rapport.tex index abec066..603303f 100644 --- a/TP1/Rapport/rapport.tex +++ b/TP1/Rapport/rapport.tex @@ -17,6 +17,11 @@ % \usepackage{minted} % intégration code % \usemintedstyle{emacs} +% Minimum pour les colonnes des tableaux +\usepackage{array} +\newcolumntype{y}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}} +\newcolumntype{Y}{y{126pt}|y{70pt}|y{66pt}|y{71pt}} + \title{\textbf{TP1 - Sokoban}} \author{Groupe 4\thanks{César PICHON, Florian POSEZ, Omar ANOUAR, Anri KENNEL}\\ \\Intelligence artificielle pour les jeux $\cdot$ Université Paris 8} @@ -28,20 +33,47 @@ \tableofcontents \clearpage -\section{Algorithme} -TODO +\section{Algorithmes} +\subsection{Algorithme de parcours en largeur} +Notre implémentation de l'algorithme de parcours en largeur est, malheureusement, +trop lente pour résoudre un Sokoban, on a décidé de ne pas l'optimiser et +d'utiliser un autre algorithme. + +\subsection{Algorithme de parcours en profondeur} +Notre implémentation de l'algorithme de parcours en profondeur, +elle arrive à résoudre le \texttt{screen-0} en temps raisonnable. + +\subsubsection{Optimisations} +Afin d'éviter les cas répétitifs, on utilises un tableau qui stockes les états +déjà visités. \section{Précalculs} -TODO +Nous n'avons pas utilisés de pré-calculs. \section{Problèmes} -\begin{figure}[h] - \centering - \begin{tabular}{c|c|c|c} - Nombre max caisses déplacées & Solution & Temps calcul & Temps précalculs \\ - \hline - TODO & TODO & TODO & TODO \\ - \end{tabular} -\end{figure} +\subsection{1 joueur} +\begin{enumerate} + \item \texttt{Screen-0} : + \begin{figure}[h] + \centering + \begin{tabular}{Y} + Nombre max caisses déplacées & Solution & Temps calcul & Temps précalculs \\ + \hline + 6 (toutes) & Oui, en 39 coups & 5-10s & Aucun \\ + \end{tabular} + \end{figure} + \item \texttt{Screen-2} : + \begin{figure}[h] + \centering + \begin{tabular}{Y} + Nombre max caisses déplacées & Solution & Temps calcul & Temps précalculs \\ + \hline + 2-3 & Non & Encore en cours & Aucun \\ + \end{tabular} + \end{figure} +\end{enumerate} + +\subsection{2 joueurs} +L'algorithme n'as pas tourné sur les parties à 2 joueurs. \end{document} From 26df71e99ed6d5fc2db5a2482c079e18d6f38121 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 22 Feb 2023 11:49:53 +0100 Subject: [PATCH 14/18] Update report --- TP1/Rapport/rapport.tex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TP1/Rapport/rapport.tex b/TP1/Rapport/rapport.tex index 603303f..c188cff 100644 --- a/TP1/Rapport/rapport.tex +++ b/TP1/Rapport/rapport.tex @@ -22,7 +22,7 @@ \newcolumntype{y}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}} \newcolumntype{Y}{y{126pt}|y{70pt}|y{66pt}|y{71pt}} -\title{\textbf{TP1 - Sokoban}} +\title{\textbf{TP1 (CPP) - Sokoban}} \author{Groupe 4\thanks{César PICHON, Florian POSEZ, Omar ANOUAR, Anri KENNEL}\\ \\Intelligence artificielle pour les jeux $\cdot$ Université Paris 8} From 7f44df915a93349d14fbc0e0814157fe74fd8c5c Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 22 Feb 2023 11:50:06 +0100 Subject: [PATCH 15/18] Add makefile for archives --- .gitignore | 3 +++ TP1/Makefile | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 TP1/Makefile diff --git a/.gitignore b/.gitignore index 29bcf70..495e16a 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,6 @@ Cours/ # IDE related compile_flags.txt + +# Archives +*.tar.gz diff --git a/TP1/Makefile b/TP1/Makefile new file mode 100644 index 0000000..ba97239 --- /dev/null +++ b/TP1/Makefile @@ -0,0 +1,22 @@ +NAME = TP1 - Groupe 4 +CPP_NAME = C++ +PROLOG_NAME = Prolog + +TAR = tar czf +RM = rm + +RAPPORT = Rapport/rapport.pdf +CPP = C-Cpp/ +PROLOG = Prolog/*.pl + +tgz-all: + $(TAR) "$(NAME).tar.gz" $(RAPPORT) $(CPP) $(PROLOG) + +tgz-cpp: + $(TAR) "$(NAME) - $(CPP_NAME).tar.gz" $(RAPPORT) $(CPP) + +tgz-prolog: + $(TAR) "$(NAME) - $(PROLOG_NAME).tar.gz" $(RAPPORT) $(PROLOG) + +clean: + $(RM) *.tar.gz From 81b6126f23fa9cf8943850f5e2170199d2aa0fec Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 28 Feb 2023 17:31:01 +0100 Subject: [PATCH 16/18] move tests --- TP1/{C-Cpp => }/Screens-1/screen-0.txt | 0 TP1/{C-Cpp => }/Screens-1/screen-1.txt | 0 TP1/{C-Cpp => }/Screens-1/screen-2.txt | 0 TP1/{C-Cpp => }/Screens-1/screen-3.txt | 0 TP1/{C-Cpp => }/Screens-1/screen-4.txt | 0 TP1/{C-Cpp => }/Screens-1/screen-5.txt | 0 TP1/{C-Cpp => }/Screens-1/screen-6.txt | 0 TP1/{C-Cpp => }/Screens-1/screen-7.txt | 0 TP1/{C-Cpp => }/Screens-1/screen-8.txt | 0 TP1/{C-Cpp => }/Screens-1/screen-9.txt | 0 TP1/{C-Cpp => }/Screens-2/screen-0.txt | 0 TP1/{C-Cpp => }/Screens-2/screen-1.txt | 0 TP1/{C-Cpp => }/Screens-2/screen-2.txt | 0 TP1/{C-Cpp => }/Screens-2/screen-3.txt | 0 TP1/{C-Cpp => }/Screens-2/screen-4.txt | 0 TP1/{C-Cpp => }/Screens-2/screen-5.txt | 0 TP1/{C-Cpp => }/Screens-2/screen-6.txt | 0 TP1/{C-Cpp => }/Screens-2/screen-7.txt | 0 TP1/{C-Cpp => }/Screens-2/screen-8.txt | 0 TP1/{C-Cpp => }/Screens-2/screen-9.txt | 0 20 files changed, 0 insertions(+), 0 deletions(-) rename TP1/{C-Cpp => }/Screens-1/screen-0.txt (100%) rename TP1/{C-Cpp => }/Screens-1/screen-1.txt (100%) rename TP1/{C-Cpp => }/Screens-1/screen-2.txt (100%) rename TP1/{C-Cpp => }/Screens-1/screen-3.txt (100%) rename TP1/{C-Cpp => }/Screens-1/screen-4.txt (100%) rename TP1/{C-Cpp => }/Screens-1/screen-5.txt (100%) rename TP1/{C-Cpp => }/Screens-1/screen-6.txt (100%) rename TP1/{C-Cpp => }/Screens-1/screen-7.txt (100%) rename TP1/{C-Cpp => }/Screens-1/screen-8.txt (100%) rename TP1/{C-Cpp => }/Screens-1/screen-9.txt (100%) rename TP1/{C-Cpp => }/Screens-2/screen-0.txt (100%) rename TP1/{C-Cpp => }/Screens-2/screen-1.txt (100%) rename TP1/{C-Cpp => }/Screens-2/screen-2.txt (100%) rename TP1/{C-Cpp => }/Screens-2/screen-3.txt (100%) rename TP1/{C-Cpp => }/Screens-2/screen-4.txt (100%) rename TP1/{C-Cpp => }/Screens-2/screen-5.txt (100%) rename TP1/{C-Cpp => }/Screens-2/screen-6.txt (100%) rename TP1/{C-Cpp => }/Screens-2/screen-7.txt (100%) rename TP1/{C-Cpp => }/Screens-2/screen-8.txt (100%) rename TP1/{C-Cpp => }/Screens-2/screen-9.txt (100%) diff --git a/TP1/C-Cpp/Screens-1/screen-0.txt b/TP1/Screens-1/screen-0.txt similarity index 100% rename from TP1/C-Cpp/Screens-1/screen-0.txt rename to TP1/Screens-1/screen-0.txt diff --git a/TP1/C-Cpp/Screens-1/screen-1.txt b/TP1/Screens-1/screen-1.txt similarity index 100% rename from TP1/C-Cpp/Screens-1/screen-1.txt rename to TP1/Screens-1/screen-1.txt diff --git a/TP1/C-Cpp/Screens-1/screen-2.txt b/TP1/Screens-1/screen-2.txt similarity index 100% rename from TP1/C-Cpp/Screens-1/screen-2.txt rename to TP1/Screens-1/screen-2.txt diff --git a/TP1/C-Cpp/Screens-1/screen-3.txt b/TP1/Screens-1/screen-3.txt similarity index 100% rename from TP1/C-Cpp/Screens-1/screen-3.txt rename to TP1/Screens-1/screen-3.txt diff --git a/TP1/C-Cpp/Screens-1/screen-4.txt b/TP1/Screens-1/screen-4.txt similarity index 100% rename from TP1/C-Cpp/Screens-1/screen-4.txt rename to TP1/Screens-1/screen-4.txt diff --git a/TP1/C-Cpp/Screens-1/screen-5.txt b/TP1/Screens-1/screen-5.txt similarity index 100% rename from TP1/C-Cpp/Screens-1/screen-5.txt rename to TP1/Screens-1/screen-5.txt diff --git a/TP1/C-Cpp/Screens-1/screen-6.txt b/TP1/Screens-1/screen-6.txt similarity index 100% rename from TP1/C-Cpp/Screens-1/screen-6.txt rename to TP1/Screens-1/screen-6.txt diff --git a/TP1/C-Cpp/Screens-1/screen-7.txt b/TP1/Screens-1/screen-7.txt similarity index 100% rename from TP1/C-Cpp/Screens-1/screen-7.txt rename to TP1/Screens-1/screen-7.txt diff --git a/TP1/C-Cpp/Screens-1/screen-8.txt b/TP1/Screens-1/screen-8.txt similarity index 100% rename from TP1/C-Cpp/Screens-1/screen-8.txt rename to TP1/Screens-1/screen-8.txt diff --git a/TP1/C-Cpp/Screens-1/screen-9.txt b/TP1/Screens-1/screen-9.txt similarity index 100% rename from TP1/C-Cpp/Screens-1/screen-9.txt rename to TP1/Screens-1/screen-9.txt diff --git a/TP1/C-Cpp/Screens-2/screen-0.txt b/TP1/Screens-2/screen-0.txt similarity index 100% rename from TP1/C-Cpp/Screens-2/screen-0.txt rename to TP1/Screens-2/screen-0.txt diff --git a/TP1/C-Cpp/Screens-2/screen-1.txt b/TP1/Screens-2/screen-1.txt similarity index 100% rename from TP1/C-Cpp/Screens-2/screen-1.txt rename to TP1/Screens-2/screen-1.txt diff --git a/TP1/C-Cpp/Screens-2/screen-2.txt b/TP1/Screens-2/screen-2.txt similarity index 100% rename from TP1/C-Cpp/Screens-2/screen-2.txt rename to TP1/Screens-2/screen-2.txt diff --git a/TP1/C-Cpp/Screens-2/screen-3.txt b/TP1/Screens-2/screen-3.txt similarity index 100% rename from TP1/C-Cpp/Screens-2/screen-3.txt rename to TP1/Screens-2/screen-3.txt diff --git a/TP1/C-Cpp/Screens-2/screen-4.txt b/TP1/Screens-2/screen-4.txt similarity index 100% rename from TP1/C-Cpp/Screens-2/screen-4.txt rename to TP1/Screens-2/screen-4.txt diff --git a/TP1/C-Cpp/Screens-2/screen-5.txt b/TP1/Screens-2/screen-5.txt similarity index 100% rename from TP1/C-Cpp/Screens-2/screen-5.txt rename to TP1/Screens-2/screen-5.txt diff --git a/TP1/C-Cpp/Screens-2/screen-6.txt b/TP1/Screens-2/screen-6.txt similarity index 100% rename from TP1/C-Cpp/Screens-2/screen-6.txt rename to TP1/Screens-2/screen-6.txt diff --git a/TP1/C-Cpp/Screens-2/screen-7.txt b/TP1/Screens-2/screen-7.txt similarity index 100% rename from TP1/C-Cpp/Screens-2/screen-7.txt rename to TP1/Screens-2/screen-7.txt diff --git a/TP1/C-Cpp/Screens-2/screen-8.txt b/TP1/Screens-2/screen-8.txt similarity index 100% rename from TP1/C-Cpp/Screens-2/screen-8.txt rename to TP1/Screens-2/screen-8.txt diff --git a/TP1/C-Cpp/Screens-2/screen-9.txt b/TP1/Screens-2/screen-9.txt similarity index 100% rename from TP1/C-Cpp/Screens-2/screen-9.txt rename to TP1/Screens-2/screen-9.txt From eb172ae62187a37b3897a135a83b06311601aeb7 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 28 Feb 2023 17:40:51 +0100 Subject: [PATCH 17/18] better makefile --- TP1/Makefile | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/TP1/Makefile b/TP1/Makefile index ba97239..51982c4 100644 --- a/TP1/Makefile +++ b/TP1/Makefile @@ -2,21 +2,31 @@ NAME = TP1 - Groupe 4 CPP_NAME = C++ PROLOG_NAME = Prolog -TAR = tar czf -RM = rm +TAR = tar --exclude="*AidesCPP" --exclude="*TODO.md" -czf +CP = cp -r +RM = rm -r RAPPORT = Rapport/rapport.pdf CPP = C-Cpp/ -PROLOG = Prolog/*.pl +TESTS = Screens-* +PROLOG = Prolog/ tgz-all: + echo $(CPP) $(PROLOG) | xargs -n 1 $(CP) $(TESTS) + -make -C $(CPP) clean 2> /dev/null $(TAR) "$(NAME).tar.gz" $(RAPPORT) $(CPP) $(PROLOG) + $(RM) $(CPP)$(TESTS) $(PROLOG)$(TESTS) tgz-cpp: + $(CP) $(TESTS) $(CPP) + -make -C $(CPP) clean 2> /dev/null $(TAR) "$(NAME) - $(CPP_NAME).tar.gz" $(RAPPORT) $(CPP) + $(RM) $(CPP)$(TESTS) tgz-prolog: + $(CP) $(TESTS) $(PROLOG) $(TAR) "$(NAME) - $(PROLOG_NAME).tar.gz" $(RAPPORT) $(PROLOG) + $(RM) $(PROLOG)$(TESTS) clean: $(RM) *.tar.gz From c07605e5762cbe11265f22109966235f0bb878ea Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 28 Feb 2023 17:48:11 +0100 Subject: [PATCH 18/18] use MAKE variable --- TP1/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TP1/Makefile b/TP1/Makefile index 51982c4..f45bce5 100644 --- a/TP1/Makefile +++ b/TP1/Makefile @@ -13,13 +13,13 @@ PROLOG = Prolog/ tgz-all: echo $(CPP) $(PROLOG) | xargs -n 1 $(CP) $(TESTS) - -make -C $(CPP) clean 2> /dev/null + -$(MAKE) -C $(CPP) clean 2> /dev/null $(TAR) "$(NAME).tar.gz" $(RAPPORT) $(CPP) $(PROLOG) $(RM) $(CPP)$(TESTS) $(PROLOG)$(TESTS) tgz-cpp: $(CP) $(TESTS) $(CPP) - -make -C $(CPP) clean 2> /dev/null + -$(MAKE) -C $(CPP) clean 2> /dev/null $(TAR) "$(NAME) - $(CPP_NAME).tar.gz" $(RAPPORT) $(CPP) $(RM) $(CPP)$(TESTS)