From b9e87379b9ee16d64aacc6dc6f72776caa547d80 Mon Sep 17 00:00:00 2001 From: fernando Date: Mon, 5 Aug 2024 21:47:54 -0600 Subject: [PATCH 01/15] Renombrado de archivos --- API.cpp => api.cpp | 2 +- API.h => api.h | 0 Main.cpp => main.cpp | 10 +++++++--- 3 files changed, 8 insertions(+), 4 deletions(-) rename API.cpp => api.cpp (99%) rename API.h => api.h (100%) rename Main.cpp => main.cpp (66%) diff --git a/API.cpp b/api.cpp similarity index 99% rename from API.cpp rename to api.cpp index 0f788ae..37e308a 100644 --- a/API.cpp +++ b/api.cpp @@ -1,4 +1,4 @@ -#include "API.h" +#include "api.h" #include #include diff --git a/API.h b/api.h similarity index 100% rename from API.h rename to api.h diff --git a/Main.cpp b/main.cpp similarity index 66% rename from Main.cpp rename to main.cpp index 025123b..5ca6628 100644 --- a/Main.cpp +++ b/main.cpp @@ -1,19 +1,23 @@ #include #include -#include "API.h" - +#include "api.h" void log(const std::string& text) { std::cerr << text << std::endl; } +void log(const int& num) { + std::cerr << num << std::endl; +} int main(int argc, char* argv[]) { log("Running..."); API::setColor(0, 0, 'G'); - API::setText(0, 0, "abc"); +// API::setText(0, 0, "abc"); + API::setWall(1, 0, 'e'); while (true) { if (!API::wallLeft()) { API::turnLeft(); + //std::cerr << API::mazeWidth() << std::endl; } while (API::wallFront()) { API::turnRight(); From 7f4a3ae4142d70faf021826a8615365cdd7677df Mon Sep 17 00:00:00 2001 From: fernando Date: Tue, 6 Aug 2024 04:05:01 -0600 Subject: [PATCH 02/15] Implemente el algoritmo floodfill --- api.cpp | 9 ++++++ api.h | 4 +++ main.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++++++++------- makefile | 10 ++++++ 4 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 makefile diff --git a/api.cpp b/api.cpp index 37e308a..00547ff 100644 --- a/api.cpp +++ b/api.cpp @@ -89,6 +89,9 @@ void API::clearAllColor() { void API::setText(int x, int y, const std::string& text) { std::cout << "setText " << x << " " << y << " " << text << std::endl; } +void API::setText(int x, int y, const int& text) { + std::cout << "setText " << x << " " << y << " " << text << std::endl; +} void API::clearText(int x, int y) { std::cout << "clearText " << x << " " << y << std::endl; @@ -110,3 +113,9 @@ void API::ackReset() { std::string ack; std::cin >> ack; } +void log(const std::string& text) { + std::cerr << text << std::endl; +} +void log(const int& num) { + std::cerr << num << std::endl; +} diff --git a/api.h b/api.h index 691a1ff..64d1a61 100644 --- a/api.h +++ b/api.h @@ -25,6 +25,7 @@ public: static void clearAllColor(); static void setText(int x, int y, const std::string& text); + static void setText(int x, int y, const int& text); static void clearText(int x, int y); static void clearAllText(); @@ -32,3 +33,6 @@ public: static void ackReset(); }; + +void log(const std::string&); +void log(const int&); diff --git a/main.cpp b/main.cpp index 5ca6628..34f89e7 100644 --- a/main.cpp +++ b/main.cpp @@ -1,27 +1,97 @@ -#include #include - +#include #include "api.h" -void log(const std::string& text) { - std::cerr << text << std::endl; -} -void log(const int& num) { - std::cerr << num << std::endl; +#include + +struct mmstats { + int y; + int x; +}stats; + +struct fill { + int x, y, z; +}; + +struct maze { /* Distancias de Manhattan */ + public: + int vwalls[15][15]; + int hwalls[15][15]; + int mhtn[16][16]; + maze(){ + mhtn[7][7] = 0; /* las celdas del centro tienen */ + mhtn[7][8] = 0; /* un valor de cero porque son */ + mhtn[8][7] = 0; /* la meta. */ + mhtn[8][8] = 0; + } +}maze; + +void flood(struct maze*, struct mmstats*); + +void flood(struct maze* maze, struct mmstats* mmstats) { + const int w = 15; /* X lenght*/ + int i, j; + int prevNum; + int marked[16][16]; + std::queue myqueue; + + myqueue.push({6,7,0}); + myqueue.push({6,8,0}); + myqueue.push({7,6,0}); + myqueue.push({8,6,0}); + + myqueue.push({9,7,0}); + myqueue.push({9,8,0}); + myqueue.push({7,9,0}); + myqueue.push({8,9,0}); + + + marked[7][7] = 1; /* las celdas del centro tienen */ + marked[7][8] = 1; /* un valor de cero porque son */ + marked[8][7] = 1; /* la meta. */ + marked[8][8] = 1; + + while (myqueue.size()) { + i = myqueue.front().x; + j = myqueue.front().y; + prevNum = myqueue.front().z; + myqueue.pop(); + + if (mmstats->x == i && mmstats->y == j) { + log("flood end"); + break; + } + + if (i < 0 || i > w || j < 0 || j > w + || marked[i][j]==1) { + continue; + } + else { + API::setText(i, j, prevNum+1); + maze->mhtn[i][j] = prevNum+1; + marked[i][j] = 1; + myqueue.push({i+1, j, prevNum +1}); + myqueue.push({i-1, j, prevNum +1}); + myqueue.push({i, j+1, prevNum +1}); + myqueue.push({i, j-1, prevNum +1}); + } + } } int main(int argc, char* argv[]) { log("Running..."); API::setColor(0, 0, 'G'); -// API::setText(0, 0, "abc"); +// API::setText(0, 0, "abc"); API::setWall(1, 0, 'e'); - while (true) { + stats = {0,0}; + flood(&maze, &stats); + + while (false) { if (!API::wallLeft()) { API::turnLeft(); - //std::cerr << API::mazeWidth() << std::endl; } while (API::wallFront()) { API::turnRight(); } API::moveForward(); - } + } } diff --git a/makefile b/makefile new file mode 100644 index 0000000..e26bda9 --- /dev/null +++ b/makefile @@ -0,0 +1,10 @@ +SRC = *.cpp +CC = g++ +#ARGS = -lstdc++ -g -Wall +ARGS = -g -Wall + +compile: ${SRC} + ${CC} ${ARGS} $^ -o a.out + +run: compile + @./a.out From d73b71756e02da95b2c2748bc39656fafad8d6ab Mon Sep 17 00:00:00 2001 From: fernando Date: Tue, 6 Aug 2024 04:10:40 -0600 Subject: [PATCH 03/15] comentario --- main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.cpp b/main.cpp index 34f89e7..572d100 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,8 @@ #include "api.h" #include +// floodfill + struct mmstats { int y; int x; From 32a582af3478835bdebc3a7c5169d27917d6aa59 Mon Sep 17 00:00:00 2001 From: fernando Date: Tue, 6 Aug 2024 23:36:35 -0600 Subject: [PATCH 04/15] Valores de cero en el centro --- main.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 572d100..54bbcf8 100644 --- a/main.cpp +++ b/main.cpp @@ -46,12 +46,16 @@ void flood(struct maze* maze, struct mmstats* mmstats) { myqueue.push({7,9,0}); myqueue.push({8,9,0}); - marked[7][7] = 1; /* las celdas del centro tienen */ marked[7][8] = 1; /* un valor de cero porque son */ marked[8][7] = 1; /* la meta. */ marked[8][8] = 1; + API::setText(7, 7, 0); + API::setText(7, 8, 0); + API::setText(8, 7, 0); + API::setText(8, 8, 0); + while (myqueue.size()) { i = myqueue.front().x; j = myqueue.front().y; From 6636cff7ff91b7eade438a077ba84c53508699de Mon Sep 17 00:00:00 2001 From: fernando Date: Wed, 7 Aug 2024 23:05:28 -0600 Subject: [PATCH 05/15] Micromouse consciente de su direccion --- api.cpp | 3 +++ main.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/api.cpp b/api.cpp index 00547ff..47b47a0 100644 --- a/api.cpp +++ b/api.cpp @@ -116,6 +116,9 @@ void API::ackReset() { void log(const std::string& text) { std::cerr << text << std::endl; } +void log(const char& text) { + std::cerr << text << std::endl; +} void log(const int& num) { std::cerr << num << std::endl; } diff --git a/main.cpp b/main.cpp index 54bbcf8..ac8ca6b 100644 --- a/main.cpp +++ b/main.cpp @@ -3,19 +3,19 @@ #include "api.h" #include -// floodfill - -struct mmstats { - int y; - int x; -}stats; - +enum turn{left = 0, right =1}; struct fill { int x, y, z; }; +struct dirnode { + char value; + dirnode* next; + dirnode* prev; + dirnode(char val): value(val){}; +}; + struct maze { /* Distancias de Manhattan */ - public: int vwalls[15][15]; int hwalls[15][15]; int mhtn[16][16]; @@ -27,6 +27,55 @@ struct maze { /* Distancias de Manhattan */ } }maze; +class mmstats { +private: + dirnode* tail; + dirnode* head; + void addnode(char val) { + dirnode* temp = new dirnode(val); + if (head != nullptr) { + head->next = temp; + temp->prev = head; + temp->next = tail; + head = temp; + tail->prev = head; + } + + if (head == nullptr) { + head = temp; + tail = temp; + } + } +public: + int y; + int x; + char dir; + mmstats(): tail(nullptr), head(nullptr) { + addnode('e'); + addnode('s'); + addnode('w'); + addnode('n'); + dir = 'n'; + } + void turn(int opt) { + if (opt) + head = head->next; + else head = head->prev; + dir = head->value; + switch (dir) { + case 'n': log("Norte"); + break; + case 'e': log("Este"); + break; + case 's': log("Sur"); + break; + case 'w': log("Oeste"); + break; + } + } + +}stats; + void flood(struct maze*, struct mmstats*); void flood(struct maze* maze, struct mmstats* mmstats) { @@ -88,15 +137,18 @@ int main(int argc, char* argv[]) { API::setColor(0, 0, 'G'); // API::setText(0, 0, "abc"); API::setWall(1, 0, 'e'); - stats = {0,0}; + stats.x = 0; + stats.y = 0; flood(&maze, &stats); - while (false) { + while (true) { if (!API::wallLeft()) { API::turnLeft(); + stats.turn(left); } while (API::wallFront()) { API::turnRight(); + stats.turn(right); } API::moveForward(); } From 03ff9c4ce6c4c5532c6a5380ea4f1ea4b0b6e6db Mon Sep 17 00:00:00 2001 From: fernando Date: Thu, 8 Aug 2024 19:34:02 -0600 Subject: [PATCH 06/15] Nuevo archivo header --- main.cpp | 69 +++++++------------------------------------------------- mms.h | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 61 deletions(-) create mode 100644 mms.h diff --git a/main.cpp b/main.cpp index ac8ca6b..29b26ef 100644 --- a/main.cpp +++ b/main.cpp @@ -2,18 +2,10 @@ #include #include "api.h" #include +#include "mms.h" -enum turn{left = 0, right =1}; -struct fill { - int x, y, z; -}; +#define W 15 /* ancho y alto del laberinto -1 */ -struct dirnode { - char value; - dirnode* next; - dirnode* prev; - dirnode(char val): value(val){}; -}; struct maze { /* Distancias de Manhattan */ int vwalls[15][15]; @@ -27,59 +19,12 @@ struct maze { /* Distancias de Manhattan */ } }maze; -class mmstats { -private: - dirnode* tail; - dirnode* head; - void addnode(char val) { - dirnode* temp = new dirnode(val); - if (head != nullptr) { - head->next = temp; - temp->prev = head; - temp->next = tail; - head = temp; - tail->prev = head; - } - - if (head == nullptr) { - head = temp; - tail = temp; - } - } -public: - int y; - int x; - char dir; - mmstats(): tail(nullptr), head(nullptr) { - addnode('e'); - addnode('s'); - addnode('w'); - addnode('n'); - dir = 'n'; - } - void turn(int opt) { - if (opt) - head = head->next; - else head = head->prev; - dir = head->value; - switch (dir) { - case 'n': log("Norte"); - break; - case 'e': log("Este"); - break; - case 's': log("Sur"); - break; - case 'w': log("Oeste"); - break; - } - } - -}stats; +struct fill { + int x, y, z; +}; void flood(struct maze*, struct mmstats*); - void flood(struct maze* maze, struct mmstats* mmstats) { - const int w = 15; /* X lenght*/ int i, j; int prevNum; int marked[16][16]; @@ -116,7 +61,7 @@ void flood(struct maze* maze, struct mmstats* mmstats) { break; } - if (i < 0 || i > w || j < 0 || j > w + if (i < 0 || i > W || j < 0 || j > W || marked[i][j]==1) { continue; } @@ -132,11 +77,13 @@ void flood(struct maze* maze, struct mmstats* mmstats) { } } + int main(int argc, char* argv[]) { log("Running..."); API::setColor(0, 0, 'G'); // API::setText(0, 0, "abc"); API::setWall(1, 0, 'e'); + mmstats stats; stats.x = 0; stats.y = 0; flood(&maze, &stats); diff --git a/mms.h b/mms.h new file mode 100644 index 0000000..47fc718 --- /dev/null +++ b/mms.h @@ -0,0 +1,57 @@ +#include "api.h" +enum turn{left, right}; +enum compass{norte, sur, este, oeste}; +struct dirnode { + int value; + dirnode* next; + dirnode* prev; + dirnode(int val): value(val){}; +}; +class mmstats { +private: + dirnode* tail; + dirnode* head; + void addnode(int val) { + dirnode* temp = new dirnode(val); + if (head != nullptr) { + head->next = temp; + temp->prev = head; + temp->next = tail; + head = temp; + tail->prev = head; + } + + if (head == nullptr) { + head = temp; + tail = temp; + } + } +public: + int y; + int x; + char dir; + mmstats(): tail(nullptr), head(nullptr) { + addnode(este); + addnode(sur); + addnode(oeste); + addnode(norte); + dir = norte; + } + void turn(int opt) { + if (opt) + head = head->next; + else head = head->prev; + dir = head->value; + switch (dir) { + case norte: log("Norte"); + break; + case este: log("Este"); + break; + case sur: log("Sur"); + break; + case oeste: log("Oeste"); + break; + } + } + +}; From d7a4f0cd7ae68092da02250b148bc1b18fa3bb08 Mon Sep 17 00:00:00 2001 From: fernando Date: Thu, 8 Aug 2024 20:18:50 -0600 Subject: [PATCH 07/15] El micromouse sabe donde esta en todo momento --- main.cpp | 30 ++++++++++++++++++++++++++++++ mms.h | 4 +++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 29b26ef..fe29661 100644 --- a/main.cpp +++ b/main.cpp @@ -77,6 +77,35 @@ void flood(struct maze* maze, struct mmstats* mmstats) { } } +void follow(struct maze maze, class mmstats mms) { + /* para que el micromouse siga el camino descendente + * se tienen que dar 16 estados, esta funcion lo reduce a 4 + */ + int x = mms.x; + int y = mms.y; + int mht = maze.mhtn[x][y]; + +} + +void updatepos(class mmstats* mms) { + /* cada que se ejecuta moveForward la funcion actualiza + * la posicion del micromouse en el valor correspondiente + */ + switch (mms->dir) { + case norte: mms->y++; + break; + case este: mms->x++; + break; + case sur: mms->y--; + break; + case oeste: mms->x--; + break; + } + log(mms->x); + log(mms->y); + log(""); +} + int main(int argc, char* argv[]) { log("Running..."); @@ -98,5 +127,6 @@ int main(int argc, char* argv[]) { stats.turn(right); } API::moveForward(); + updatepos(&stats); } } diff --git a/mms.h b/mms.h index 47fc718..fa18f5c 100644 --- a/mms.h +++ b/mms.h @@ -10,7 +10,6 @@ struct dirnode { class mmstats { private: dirnode* tail; - dirnode* head; void addnode(int val) { dirnode* temp = new dirnode(val); if (head != nullptr) { @@ -27,6 +26,7 @@ private: } } public: + dirnode* head; int y; int x; char dir; @@ -42,6 +42,7 @@ public: head = head->next; else head = head->prev; dir = head->value; + /* switch (dir) { case norte: log("Norte"); break; @@ -52,6 +53,7 @@ public: case oeste: log("Oeste"); break; } + */ } }; From 0ba516d684f5390f7bca7fc821cde22b9a572869 Mon Sep 17 00:00:00 2001 From: fernando Date: Thu, 8 Aug 2024 22:11:48 -0600 Subject: [PATCH 08/15] Funcion para predecir siguiente casilla --- main.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index fe29661..b50c2c8 100644 --- a/main.cpp +++ b/main.cpp @@ -84,7 +84,25 @@ void follow(struct maze maze, class mmstats mms) { int x = mms.x; int y = mms.y; int mht = maze.mhtn[x][y]; + int next[2]; + if (mht > maze.mhtn[x][y+1]) { + next[0] = x; + } +} +void nextcell(int i, int j, int dir, int& nextx, int& nexty) { + switch (dir) { + case norte: j++; + break; + case este: i++; + break; + case sur: j--; + break; + case oeste: i--; + break; + } + nextx = i; + nexty = j; } void updatepos(class mmstats* mms) { @@ -101,9 +119,6 @@ void updatepos(class mmstats* mms) { case oeste: mms->x--; break; } - log(mms->x); - log(mms->y); - log(""); } @@ -115,6 +130,7 @@ int main(int argc, char* argv[]) { mmstats stats; stats.x = 0; stats.y = 0; + flood(&maze, &stats); while (true) { From 4ddb70286277f44467d1e41ea2f6a416b81186e0 Mon Sep 17 00:00:00 2001 From: fernando Date: Fri, 9 Aug 2024 01:03:00 -0600 Subject: [PATCH 09/15] Funcion para girar el mouse a la direccion correcta --- main.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++---------- mms.h | 2 +- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index b50c2c8..18000b3 100644 --- a/main.cpp +++ b/main.cpp @@ -24,6 +24,9 @@ struct fill { }; void flood(struct maze*, struct mmstats*); +void follow(struct maze maze, class mmstats mms); +int* nextcell(int i, int j, int dir, int ndir[2]); + void flood(struct maze* maze, struct mmstats* mmstats) { int i, j; int prevNum; @@ -58,6 +61,8 @@ void flood(struct maze* maze, struct mmstats* mmstats) { if (mmstats->x == i && mmstats->y == j) { log("flood end"); + API::setText(i, j, prevNum+1); + maze->mhtn[i][j] = prevNum+1; break; } @@ -77,20 +82,40 @@ void flood(struct maze* maze, struct mmstats* mmstats) { } } -void follow(struct maze maze, class mmstats mms) { +void follow(struct maze maze, class mmstats* mms) { /* para que el micromouse siga el camino descendente * se tienen que dar 16 estados, esta funcion lo reduce a 4 */ - int x = mms.x; - int y = mms.y; - int mht = maze.mhtn[x][y]; + int mht = maze.mhtn[mms->x][mms->y]; + int nextmht = mht-1; int next[2]; - if (mht > maze.mhtn[x][y+1]) { - next[0] = x; + nextcell(mms->x, mms->y, mms->dir, next); + if(maze.mhtn[next[0]][next[1]] == nextmht) { + log("forward"); + return; } + nextcell(mms->x, mms->y, mms->head->prev->value, next); + if(maze.mhtn[next[0]][next[1]] == nextmht) { + API::turnLeft(); + mms->turn(left); + log("left"); + return; + } + nextcell(mms->x, mms->y, mms->head->next->value, next); + if(maze.mhtn[next[0]][next[1]] == nextmht) { + API::turnRight(); + mms->turn(right); + log("right"); + return; + } + API::turnRight(); + mms->turn(right); + API::turnRight(); + mms->turn(right); + log("180"); } -void nextcell(int i, int j, int dir, int& nextx, int& nexty) { +int* nextcell(int i, int j, int dir, int ndir[2]) { switch (dir) { case norte: j++; break; @@ -101,8 +126,9 @@ void nextcell(int i, int j, int dir, int& nextx, int& nexty) { case oeste: i--; break; } - nextx = i; - nexty = j; + ndir[0] = i; + ndir[1] = j; + return ndir; } void updatepos(class mmstats* mms) { @@ -126,13 +152,24 @@ int main(int argc, char* argv[]) { log("Running..."); API::setColor(0, 0, 'G'); // API::setText(0, 0, "abc"); - API::setWall(1, 0, 'e'); mmstats stats; stats.x = 0; stats.y = 0; flood(&maze, &stats); - + while (true) { + follow(maze, &stats); + API::moveForward(); + updatepos(&stats); + if(maze.mhtn[stats.x][stats.y] == 0){ + log("Finish"); + return 0; + } + log(stats.x); + log(stats.y); + log(""); + } + /* while (true) { if (!API::wallLeft()) { API::turnLeft(); @@ -144,5 +181,9 @@ int main(int argc, char* argv[]) { } API::moveForward(); updatepos(&stats); + log(stats.x); + log(stats.y); + log(""); } + */ } diff --git a/mms.h b/mms.h index fa18f5c..b445127 100644 --- a/mms.h +++ b/mms.h @@ -53,7 +53,7 @@ public: case oeste: log("Oeste"); break; } - */ + */ } }; From 9201c4e75d375ee2d0cd1e8b602daaa216d373d5 Mon Sep 17 00:00:00 2001 From: fernando Date: Fri, 9 Aug 2024 01:59:09 -0600 Subject: [PATCH 10/15] Muros descubiertos mostrados en el simulador --- main.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 18000b3..437e8c2 100644 --- a/main.cpp +++ b/main.cpp @@ -26,6 +26,8 @@ struct fill { void flood(struct maze*, struct mmstats*); void follow(struct maze maze, class mmstats mms); int* nextcell(int i, int j, int dir, int ndir[2]); +void updatepos(class mmstats* mms); +void locateWall(struct maze& maze); void flood(struct maze* maze, struct mmstats* mmstats) { int i, j; @@ -147,6 +149,67 @@ void updatepos(class mmstats* mms) { } } +void locateWall(struct maze* maze, class mmstats *mms) { + switch (mms->dir) { + case norte: + if(API::wallFront() && mms->y != 15) { + maze->hwalls[mms->x][mms->y] = 1; + API::setWall(mms->x, mms->y, 'n'); + } + if(API::wallLeft() && mms->x != 0){ + maze->vwalls[mms->x -1][mms->y] = 1; + API::setWall(mms->x -1, mms->y, 'e'); + } + if(API::wallRight() && mms->x != 15){ + maze->vwalls[mms->x][mms->y] = 1; + API::setWall(mms->x, mms->y, 'e'); + } + break; + case este: + if(API::wallFront() && mms->x != 15) { + maze->vwalls[mms->x][mms->y] = 1; + API::setWall(mms->x, mms->y, 'e'); + } + if(API::wallLeft() && mms->y != 15){ + maze->hwalls[mms->x][mms->y] = 1; + API::setWall(mms->x, mms->y, 'n'); + } + if(API::wallRight() && mms->y != 0){ + maze->hwalls[mms->x][mms->y -1] = 1; + API::setWall(mms->x, mms->y -1, 'n'); + } + break; + case sur: + if(API::wallFront() && mms->y != 0) { + maze->vwalls[mms->x][mms->y-1] = 1; + API::setWall(mms->x, mms->y-1, 'n'); + } + if(API::wallLeft() && mms->x != 15){ + maze->hwalls[mms->x][mms->y] = 1; + API::setWall(mms->x, mms->y, 'e'); + } + if(API::wallRight() && mms->x != 0){ + maze->hwalls[mms->x -1][mms->y] = 1; + API::setWall(mms->x -1, mms->y, 'e'); + } + break; + case oeste: + if(API::wallFront() && mms->x != 0) { + maze->vwalls[mms->x -1][mms->y] = 1; + API::setWall(mms->x -1, mms->y, 'e'); + } + if(API::wallLeft() && mms->y != 0){ + maze->hwalls[mms->x][mms->y -1] = 1; + API::setWall(mms->x, mms->y -1, 'n'); + } + if(API::wallRight() && mms->y != 0){ + maze->hwalls[mms->x][mms->y] = 1; + API::setWall(mms->x, mms->y, 'n'); + } + break; + } +} + int main(int argc, char* argv[]) { log("Running..."); @@ -157,6 +220,7 @@ int main(int argc, char* argv[]) { stats.y = 0; flood(&maze, &stats); + /* while (true) { follow(maze, &stats); API::moveForward(); @@ -169,21 +233,23 @@ int main(int argc, char* argv[]) { log(stats.y); log(""); } - /* + */ while (true) { if (!API::wallLeft()) { + locateWall(&maze, &stats); API::turnLeft(); stats.turn(left); } while (API::wallFront()) { + locateWall(&maze, &stats); API::turnRight(); stats.turn(right); } + locateWall(&maze, &stats); API::moveForward(); updatepos(&stats); log(stats.x); log(stats.y); log(""); } - */ } From fe5cdcede14b8874760829376dbf0d56b1e42e65 Mon Sep 17 00:00:00 2001 From: fernando Date: Fri, 9 Aug 2024 03:04:13 -0600 Subject: [PATCH 11/15] primer intento de resolver el laberinto wip --- main.cpp | 68 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/main.cpp b/main.cpp index 437e8c2..acf21ba 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ #include #include -#include "api.h" #include +#include "api.h" #include "mms.h" #define W 15 /* ancho y alto del laberinto -1 */ @@ -62,7 +62,7 @@ void flood(struct maze* maze, struct mmstats* mmstats) { myqueue.pop(); if (mmstats->x == i && mmstats->y == j) { - log("flood end"); + /*log("flood end");*/ API::setText(i, j, prevNum+1); maze->mhtn[i][j] = prevNum+1; break; @@ -76,10 +76,17 @@ void flood(struct maze* maze, struct mmstats* mmstats) { API::setText(i, j, prevNum+1); maze->mhtn[i][j] = prevNum+1; marked[i][j] = 1; - myqueue.push({i+1, j, prevNum +1}); - myqueue.push({i-1, j, prevNum +1}); - myqueue.push({i, j+1, prevNum +1}); - myqueue.push({i, j-1, prevNum +1}); + if (!maze->vwalls[i][j]) + myqueue.push({i+1, j, prevNum +1}); + + if (!maze->vwalls[i-1][j]) + myqueue.push({i-1, j, prevNum +1}); + + if (!maze->hwalls[i][j]) + myqueue.push({i, j+1, prevNum +1}); + + if (!maze->hwalls[i][j-1]) + myqueue.push({i, j-1, prevNum +1}); } } } @@ -91,30 +98,37 @@ void follow(struct maze maze, class mmstats* mms) { int mht = maze.mhtn[mms->x][mms->y]; int nextmht = mht-1; int next[2]; + int next_L[2]; + int next_R[2]; + int next_B[2]; nextcell(mms->x, mms->y, mms->dir, next); - if(maze.mhtn[next[0]][next[1]] == nextmht) { + nextcell(mms->x, mms->y, mms->head->prev->value, next_L); + nextcell(mms->x, mms->y, mms->head->next->value, next_R); + nextcell(mms->x, mms->y, mms->head->next->next->value, next_B); + if(maze.mhtn[next[0]][next[1]] == nextmht && !API::wallFront() || API::wallLeft() && API::wallRight()) { log("forward"); return; } - nextcell(mms->x, mms->y, mms->head->prev->value, next); - if(maze.mhtn[next[0]][next[1]] == nextmht) { + else if(maze.mhtn[next_L[0]][next_L[1]] == nextmht && !API::wallLeft()) { API::turnLeft(); mms->turn(left); log("left"); return; } - nextcell(mms->x, mms->y, mms->head->next->value, next); - if(maze.mhtn[next[0]][next[1]] == nextmht) { + else if(maze.mhtn[next_R[0]][next_R[1]] == nextmht && !API::wallRight()) { API::turnRight(); mms->turn(right); log("right"); return; } - API::turnRight(); - mms->turn(right); - API::turnRight(); - mms->turn(right); - log("180"); + else if(maze.mhtn[next_B[0]][next_B[1]] == nextmht) { + API::turnRight(); + mms->turn(right); + API::turnRight(); + mms->turn(right); + log("180"); + return; + } } int* nextcell(int i, int j, int dir, int ndir[2]) { @@ -218,38 +232,42 @@ int main(int argc, char* argv[]) { mmstats stats; stats.x = 0; stats.y = 0; - - flood(&maze, &stats); - /* + while (true) { + locateWall(&maze, &stats); + flood(&maze, &stats); follow(maze, &stats); - API::moveForward(); - updatepos(&stats); if(maze.mhtn[stats.x][stats.y] == 0){ log("Finish"); return 0; } - log(stats.x); - log(stats.y); - log(""); + //log(stats.x); + //log(stats.y); + API::moveForward(); + updatepos(&stats); + //log(maze.hwalls[stats.x][stats.y]); } - */ + /* while (true) { if (!API::wallLeft()) { locateWall(&maze, &stats); API::turnLeft(); stats.turn(left); + flood(&maze, &stats); } while (API::wallFront()) { locateWall(&maze, &stats); API::turnRight(); stats.turn(right); + flood(&maze, &stats); } locateWall(&maze, &stats); API::moveForward(); updatepos(&stats); + flood(&maze, &stats); log(stats.x); log(stats.y); log(""); } + */ } From 7a8f8b75e19904528c3fe6a4861780ae8babe494 Mon Sep 17 00:00:00 2001 From: fernando Date: Fri, 9 Aug 2024 13:26:55 -0600 Subject: [PATCH 12/15] correccion de funciones --- main.cpp | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/main.cpp b/main.cpp index acf21ba..e170e5d 100644 --- a/main.cpp +++ b/main.cpp @@ -11,12 +11,6 @@ struct maze { /* Distancias de Manhattan */ int vwalls[15][15]; int hwalls[15][15]; int mhtn[16][16]; - maze(){ - mhtn[7][7] = 0; /* las celdas del centro tienen */ - mhtn[7][8] = 0; /* un valor de cero porque son */ - mhtn[8][7] = 0; /* la meta. */ - mhtn[8][8] = 0; - } }maze; struct fill { @@ -35,6 +29,7 @@ void flood(struct maze* maze, struct mmstats* mmstats) { int marked[16][16]; std::queue myqueue; + /* myqueue.push({6,7,0}); myqueue.push({6,8,0}); myqueue.push({7,6,0}); @@ -44,11 +39,12 @@ void flood(struct maze* maze, struct mmstats* mmstats) { myqueue.push({9,8,0}); myqueue.push({7,9,0}); myqueue.push({8,9,0}); - - marked[7][7] = 1; /* las celdas del centro tienen */ - marked[7][8] = 1; /* un valor de cero porque son */ - marked[8][7] = 1; /* la meta. */ - marked[8][8] = 1; + */ + + myqueue.push({7,7,-1}); + myqueue.push({7,8,-1}); + myqueue.push({8,7,-1}); + myqueue.push({8,8,-1}); API::setText(7, 7, 0); API::setText(7, 8, 0); @@ -61,13 +57,6 @@ void flood(struct maze* maze, struct mmstats* mmstats) { prevNum = myqueue.front().z; myqueue.pop(); - if (mmstats->x == i && mmstats->y == j) { - /*log("flood end");*/ - API::setText(i, j, prevNum+1); - maze->mhtn[i][j] = prevNum+1; - break; - } - if (i < 0 || i > W || j < 0 || j > W || marked[i][j]==1) { continue; @@ -195,15 +184,15 @@ void locateWall(struct maze* maze, class mmstats *mms) { break; case sur: if(API::wallFront() && mms->y != 0) { - maze->vwalls[mms->x][mms->y-1] = 1; + maze->hwalls[mms->x][mms->y-1] = 1; API::setWall(mms->x, mms->y-1, 'n'); } if(API::wallLeft() && mms->x != 15){ - maze->hwalls[mms->x][mms->y] = 1; + maze->vwalls[mms->x][mms->y] = 1; API::setWall(mms->x, mms->y, 'e'); } if(API::wallRight() && mms->x != 0){ - maze->hwalls[mms->x -1][mms->y] = 1; + maze->vwalls[mms->x -1][mms->y] = 1; API::setWall(mms->x -1, mms->y, 'e'); } break; From 540d3135d2d40506b7e20f02bb5d9a987f15bf7e Mon Sep 17 00:00:00 2001 From: fernando Date: Fri, 9 Aug 2024 15:19:44 -0600 Subject: [PATCH 13/15] Casi resuelve el primer laberinto --- main.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index e170e5d..81eb7b8 100644 --- a/main.cpp +++ b/main.cpp @@ -94,23 +94,24 @@ void follow(struct maze maze, class mmstats* mms) { nextcell(mms->x, mms->y, mms->head->prev->value, next_L); nextcell(mms->x, mms->y, mms->head->next->value, next_R); nextcell(mms->x, mms->y, mms->head->next->next->value, next_B); - if(maze.mhtn[next[0]][next[1]] == nextmht && !API::wallFront() || API::wallLeft() && API::wallRight()) { + if(maze.mhtn[next[0]][next[1]] == nextmht && !API::wallFront() || !API::wallFront() && API::wallLeft() && API::wallRight()) { log("forward"); return; } - else if(maze.mhtn[next_L[0]][next_L[1]] == nextmht && !API::wallLeft()) { + else if(maze.mhtn[next_L[0]][next_L[1]] <= nextmht && !API::wallLeft()) { API::turnLeft(); mms->turn(left); log("left"); return; } - else if(maze.mhtn[next_R[0]][next_R[1]] == nextmht && !API::wallRight()) { + else if(maze.mhtn[next_R[0]][next_R[1]] <= nextmht && !API::wallRight()) { API::turnRight(); mms->turn(right); log("right"); return; } - else if(maze.mhtn[next_B[0]][next_B[1]] == nextmht) { + else if(maze.mhtn[next_B[0]][next_B[1]] == nextmht + || API::wallFront() && API::wallLeft() && API::wallRight()) { API::turnRight(); mms->turn(right); API::turnRight(); @@ -118,6 +119,15 @@ void follow(struct maze maze, class mmstats* mms) { log("180"); return; } + else if(maze.mhtn[next_R[0]][next_R[1]] <= maze.mhtn[next_B[0]][next_B[1]] && (API::wallFront() && API::wallLeft())){ + API::turnRight(); + mms->turn(right); + } + else if(maze.mhtn[next_L[0]][next_L[1]] <= maze.mhtn[next_B[0]][next_B[1]] && (API::wallFront() && API::wallRight())){ + API::turnLeft(); + mms->turn(left); + } + log("error en la matri"); } int* nextcell(int i, int j, int dir, int ndir[2]) { From efa8485a4cd8587e53f62e3b618e570cc08cc93a Mon Sep 17 00:00:00 2001 From: fernando Date: Fri, 9 Aug 2024 03:04:13 -0600 Subject: [PATCH 14/15] primer intento de resolver el laberinto wip --- main.cpp | 111 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 46 deletions(-) diff --git a/main.cpp b/main.cpp index 437e8c2..2617502 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ #include #include -#include "api.h" #include +#include "api.h" #include "mms.h" #define W 15 /* ancho y alto del laberinto -1 */ @@ -11,12 +11,6 @@ struct maze { /* Distancias de Manhattan */ int vwalls[15][15]; int hwalls[15][15]; int mhtn[16][16]; - maze(){ - mhtn[7][7] = 0; /* las celdas del centro tienen */ - mhtn[7][8] = 0; /* un valor de cero porque son */ - mhtn[8][7] = 0; /* la meta. */ - mhtn[8][8] = 0; - } }maze; struct fill { @@ -35,6 +29,7 @@ void flood(struct maze* maze, struct mmstats* mmstats) { int marked[16][16]; std::queue myqueue; + /* myqueue.push({6,7,0}); myqueue.push({6,8,0}); myqueue.push({7,6,0}); @@ -44,11 +39,12 @@ void flood(struct maze* maze, struct mmstats* mmstats) { myqueue.push({9,8,0}); myqueue.push({7,9,0}); myqueue.push({8,9,0}); - - marked[7][7] = 1; /* las celdas del centro tienen */ - marked[7][8] = 1; /* un valor de cero porque son */ - marked[8][7] = 1; /* la meta. */ - marked[8][8] = 1; + */ + + myqueue.push({7,7,-1}); + myqueue.push({7,8,-1}); + myqueue.push({8,7,-1}); + myqueue.push({8,8,-1}); API::setText(7, 7, 0); API::setText(7, 8, 0); @@ -61,13 +57,6 @@ void flood(struct maze* maze, struct mmstats* mmstats) { prevNum = myqueue.front().z; myqueue.pop(); - if (mmstats->x == i && mmstats->y == j) { - log("flood end"); - API::setText(i, j, prevNum+1); - maze->mhtn[i][j] = prevNum+1; - break; - } - if (i < 0 || i > W || j < 0 || j > W || marked[i][j]==1) { continue; @@ -76,10 +65,17 @@ void flood(struct maze* maze, struct mmstats* mmstats) { API::setText(i, j, prevNum+1); maze->mhtn[i][j] = prevNum+1; marked[i][j] = 1; - myqueue.push({i+1, j, prevNum +1}); - myqueue.push({i-1, j, prevNum +1}); - myqueue.push({i, j+1, prevNum +1}); - myqueue.push({i, j-1, prevNum +1}); + if (!maze->vwalls[i][j]) + myqueue.push({i+1, j, prevNum +1}); + + if (!maze->vwalls[i-1][j]) + myqueue.push({i-1, j, prevNum +1}); + + if (!maze->hwalls[i][j]) + myqueue.push({i, j+1, prevNum +1}); + + if (!maze->hwalls[i][j-1]) + myqueue.push({i, j-1, prevNum +1}); } } } @@ -91,30 +87,49 @@ void follow(struct maze maze, class mmstats* mms) { int mht = maze.mhtn[mms->x][mms->y]; int nextmht = mht-1; int next[2]; + int next_L[2]; + int next_R[2]; + int next_B[2]; nextcell(mms->x, mms->y, mms->dir, next); - if(maze.mhtn[next[0]][next[1]] == nextmht) { + nextcell(mms->x, mms->y, mms->head->prev->value, next_L); + nextcell(mms->x, mms->y, mms->head->next->value, next_R); + nextcell(mms->x, mms->y, mms->head->next->next->value, next_B); + if(maze.mhtn[next[0]][next[1]] == nextmht && !API::wallFront() || !API::wallFront() && API::wallLeft() && API::wallRight()) { log("forward"); return; } - nextcell(mms->x, mms->y, mms->head->prev->value, next); - if(maze.mhtn[next[0]][next[1]] == nextmht) { + else if(maze.mhtn[next_L[0]][next_L[1]] <= nextmht && !API::wallLeft()) { API::turnLeft(); mms->turn(left); log("left"); return; } - nextcell(mms->x, mms->y, mms->head->next->value, next); - if(maze.mhtn[next[0]][next[1]] == nextmht) { + else if(maze.mhtn[next_R[0]][next_R[1]] <= nextmht && !API::wallRight()) { API::turnRight(); mms->turn(right); log("right"); return; } - API::turnRight(); - mms->turn(right); - API::turnRight(); - mms->turn(right); - log("180"); + else if(maze.mhtn[next_B[0]][next_B[1]] == nextmht + || API::wallFront() && API::wallLeft() && API::wallRight()) { + API::turnRight(); + mms->turn(right); + API::turnRight(); + mms->turn(right); + log("180"); + return; + } + else if(maze.mhtn[next_R[0]][next_R[1]] <= maze.mhtn[next_B[0]][next_B[1]] && !API::wallRight()){ + API::turnRight(); + mms->turn(right); + log("bRight"); + } + else if(maze.mhtn[next_L[0]][next_L[1]] <= maze.mhtn[next_B[0]][next_B[1]] && !API::wallLeft()){ + API::turnLeft(); + mms->turn(left); + log("bLeft"); + } + log("error en la matri"); } int* nextcell(int i, int j, int dir, int ndir[2]) { @@ -181,15 +196,15 @@ void locateWall(struct maze* maze, class mmstats *mms) { break; case sur: if(API::wallFront() && mms->y != 0) { - maze->vwalls[mms->x][mms->y-1] = 1; + maze->hwalls[mms->x][mms->y-1] = 1; API::setWall(mms->x, mms->y-1, 'n'); } if(API::wallLeft() && mms->x != 15){ - maze->hwalls[mms->x][mms->y] = 1; + maze->vwalls[mms->x][mms->y] = 1; API::setWall(mms->x, mms->y, 'e'); } if(API::wallRight() && mms->x != 0){ - maze->hwalls[mms->x -1][mms->y] = 1; + maze->vwalls[mms->x -1][mms->y] = 1; API::setWall(mms->x -1, mms->y, 'e'); } break; @@ -202,7 +217,7 @@ void locateWall(struct maze* maze, class mmstats *mms) { maze->hwalls[mms->x][mms->y -1] = 1; API::setWall(mms->x, mms->y -1, 'n'); } - if(API::wallRight() && mms->y != 0){ + if(API::wallRight() && mms->y != 15){ maze->hwalls[mms->x][mms->y] = 1; API::setWall(mms->x, mms->y, 'n'); } @@ -218,38 +233,42 @@ int main(int argc, char* argv[]) { mmstats stats; stats.x = 0; stats.y = 0; - - flood(&maze, &stats); - /* + while (true) { + locateWall(&maze, &stats); + flood(&maze, &stats); follow(maze, &stats); - API::moveForward(); - updatepos(&stats); if(maze.mhtn[stats.x][stats.y] == 0){ log("Finish"); return 0; } - log(stats.x); - log(stats.y); - log(""); + //log(stats.x); + //log(stats.y); + API::moveForward(); + updatepos(&stats); + //log(maze.hwalls[stats.x][stats.y]); } - */ + /* while (true) { if (!API::wallLeft()) { locateWall(&maze, &stats); API::turnLeft(); stats.turn(left); + flood(&maze, &stats); } while (API::wallFront()) { locateWall(&maze, &stats); API::turnRight(); stats.turn(right); + flood(&maze, &stats); } locateWall(&maze, &stats); API::moveForward(); updatepos(&stats); + flood(&maze, &stats); log(stats.x); log(stats.y); log(""); } + */ } From a77e10e983d76b0f9aca643dc226aac4bbea273b Mon Sep 17 00:00:00 2001 From: fernando Date: Fri, 9 Aug 2024 18:18:25 -0600 Subject: [PATCH 15/15] El micromouse sabe llegar al centro --- main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/main.cpp b/main.cpp index 2617502..edb5773 100644 --- a/main.cpp +++ b/main.cpp @@ -6,7 +6,6 @@ #define W 15 /* ancho y alto del laberinto -1 */ - struct maze { /* Distancias de Manhattan */ int vwalls[15][15]; int hwalls[15][15];