Ajustar algoritmo para regresar al inicio #3

Merged
FisionX merged 4 commits from follow into main 2025-02-06 14:10:21 -06:00
3 changed files with 127 additions and 169 deletions
Showing only changes of commit 488a751790 - Show all commits

1
.gitignore vendored
View File

@ -1 +1,2 @@
a.out a.out
.clang-format

View File

@ -8,7 +8,7 @@ https://github.com/mackorone/mms
## Configuración en mms ## Configuración en mms
1. Clonar el repositorio 1. Clonar el repositorio
```git clone git.fisionx.mooo.com/FisionX/mms-sim``` ```git clone git.fisionx.win/FisionX/mms-sim```
2. Seleccionar el directorio del repo 2. Seleccionar el directorio del repo
3. Usar el comando de compilado 3. Usar el comando de compilado

151
main.cpp
View File

@ -1,8 +1,8 @@
#include <string>
#include <sys/types.h>
#include <queue>
#include "api.h" #include "api.h"
#include "mms.h" #include "mms.h"
#include <queue>
#include <string>
#include <sys/types.h>
#define W 15 /* ancho y alto del laberinto -1 */ #define W 15 /* ancho y alto del laberinto -1 */
@ -16,36 +16,19 @@ struct fill {
int x, y, z; int x, y, z;
}; };
void flood(struct maze*, struct mmstats*); void flood(struct maze *, struct mmstats *, int coordinates[2]);
void flood_start(struct maze*, struct mmstats*);
void follow(struct maze maze, class mmstats mms); void follow(struct maze maze, class mmstats mms);
int *nextcell(int i, int j, int dir, int ndir[2]); int *nextcell(int i, int j, int dir, int ndir[2]);
void updatepos(class mmstats *mms); void updatepos(class mmstats *mms);
void locateWall(struct maze &maze); void locateWall(struct maze &maze);
void flood(struct maze* maze, struct mmstats* mmstats) { void flood(struct maze *maze, struct mmstats *mmstats, int coordinates[2]) {
int i, j; int i, j;
int prevNum; int prevNum;
int marked[16][16] = {0}; int marked[16][16] = {0};
std::queue<fill> myqueue; std::queue<fill> myqueue;
myqueue.push({7,7,-1}); myqueue.push({coordinates[0], coordinates[1], -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);
API::setText(8, 7, 0);
API::setText(8, 8, 0);
/*
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
marked[x][y] = 0;
}
}
*/
while (myqueue.size()) { while (myqueue.size()) {
i = myqueue.front().x; i = myqueue.front().x;
@ -53,48 +36,9 @@ void flood(struct maze* maze, struct mmstats* mmstats) {
prevNum = myqueue.front().z; prevNum = myqueue.front().z;
myqueue.pop(); myqueue.pop();
if (i < 0 || i > W || j < 0 || j > W if (i < 0 || i > W || j < 0 || j > W || marked[i][j] == 1) {
|| marked[i][j]==1) {
continue; continue;
} } else {
else {
API::setText(i, j, prevNum+1);
maze->mhtn[i][j] = prevNum+1;
marked[i][j] = 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});
}
}
}
void flood_start(struct maze* maze, struct mmstats* mmstats) {
int i, j;
int prevNum;
int marked[16][16] = {0};
std::queue<fill> myqueue;
myqueue.push({0,0,-1});
while (myqueue.size()) {
i = myqueue.front().x;
j = myqueue.front().y;
prevNum = myqueue.front().z;
myqueue.pop();
if (i < 0 || i > W || j < 0 || j > W
|| marked[i][j]==1) {
continue;
}
else {
API::setText(i, j, prevNum + 1); API::setText(i, j, prevNum + 1);
maze->mhtn[i][j] = prevNum + 1; maze->mhtn[i][j] = prevNum + 1;
marked[i][j] = 1; marked[i][j] = 1;
@ -127,52 +71,52 @@ 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->prev->value, next_L);
nextcell(mms->x, mms->y, mms->head->next->value, next_R); nextcell(mms->x, mms->y, mms->head->next->value, next_R);
nextcell(mms->x, mms->y, mms->head->next->next->value, next_B); nextcell(mms->x, mms->y, mms->head->next->next->value, next_B);
if(maze.mhtn[next[0]][next[1]] == nextmht && !API::wallFront() if (maze.mhtn[next[0]][next[1]] == nextmht && !API::wallFront() ||
|| !API::wallFront() && API::wallLeft() && API::wallRight()) { !API::wallFront() && API::wallLeft() && API::wallRight()) {
if(maze.mhtn[next_L[0]][next_L[1]] <= nextmht && !API::wallLeft()) { /* Detectar si hay atajos */ if (maze.mhtn[next_L[0]][next_L[1]] <= nextmht &&
!API::wallLeft()) { /* Detectar si hay atajos */
API::turnLeft(); API::turnLeft();
mms->turn(left); mms->turn(left);
log("left"); log("left");
return; return;
} } else if (maze.mhtn[next_R[0]][next_R[1]] <= nextmht &&
else if(maze.mhtn[next_R[0]][next_R[1]] <= nextmht && !API::wallRight()) { !API::wallRight()) {
API::turnRight(); API::turnRight();
mms->turn(right); mms->turn(right);
log("right"); log("right");
return; return;
} } else {
else{
log("forward"); log("forward");
return; 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(); API::turnLeft();
mms->turn(left); mms->turn(left);
log("left"); log("left");
return; return;
} } else if (maze.mhtn[next_R[0]][next_R[1]] <= nextmht &&
else if(maze.mhtn[next_R[0]][next_R[1]] <= nextmht && !API::wallRight()) { !API::wallRight()) {
API::turnRight(); API::turnRight();
mms->turn(right); mms->turn(right);
log("right"); log("right");
return; 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::wallFront() && API::wallLeft() && API::wallRight()) {
API::turnRight(); API::turnRight();
mms->turn(right); mms->turn(right);
API::turnRight(); API::turnRight();
mms->turn(right); mms->turn(right);
log("180"); log("180");
return; return;
} } else if (maze.mhtn[next_R[0]][next_R[1]] <=
else if(maze.mhtn[next_R[0]][next_R[1]] <= maze.mhtn[next_B[0]][next_B[1]] && !API::wallRight()){ maze.mhtn[next_B[0]][next_B[1]] &&
!API::wallRight()) {
API::turnRight(); API::turnRight();
mms->turn(right); mms->turn(right);
log("bRight"); log("bRight");
} } else if (maze.mhtn[next_L[0]][next_L[1]] <=
else if(maze.mhtn[next_L[0]][next_L[1]] <= maze.mhtn[next_B[0]][next_B[1]] && !API::wallLeft()){ maze.mhtn[next_B[0]][next_B[1]] &&
!API::wallLeft()) {
API::turnLeft(); API::turnLeft();
mms->turn(left); mms->turn(left);
log("bLeft"); log("bLeft");
@ -182,13 +126,17 @@ void follow(struct maze maze, class mmstats* mms) {
int *nextcell(int i, int j, int dir, int ndir[2]) { int *nextcell(int i, int j, int dir, int ndir[2]) {
switch (dir) { switch (dir) {
case norte: j++; case norte:
j++;
break; break;
case este: i++; case este:
i++;
break; break;
case sur: j--; case sur:
j--;
break; break;
case oeste: i--; case oeste:
i--;
break; break;
} }
ndir[0] = i; ndir[0] = i;
@ -201,13 +149,17 @@ void updatepos(class mmstats* mms) {
* la posicion del micromouse en el valor correspondiente * la posicion del micromouse en el valor correspondiente
*/ */
switch (mms->dir) { switch (mms->dir) {
case norte: mms->y++; case norte:
mms->y++;
break; break;
case este: mms->x++; case este:
mms->x++;
break; break;
case sur: mms->y--; case sur:
mms->y--;
break; break;
case oeste: mms->x--; case oeste:
mms->x--;
break; break;
} }
} }
@ -273,20 +225,19 @@ void locateWall(struct maze* maze, class mmstats *mms) {
} }
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
log("Running..."); log("Running...");
API::setColor(0, 0, 'G'); API::setColor(0, 0, 'G');
// API::setText(0, 0, "abc");
mmstats stats; mmstats stats;
struct maze maze2;
stats.x = 0; stats.x = 0;
stats.y = 0; stats.y = 0;
int next_pos[2] = {7, 7};
for (;;) { for (;;) {
locateWall(&maze, &stats); locateWall(&maze, &stats);
flood(&maze, &stats); flood(&maze, &stats, next_pos);
follow(maze, &stats); follow(maze, &stats);
if (maze.mhtn[stats.x][stats.y] == 0) { if (maze.mhtn[stats.x][stats.y] == 0) {
log("Finish"); log("Finish");
@ -298,9 +249,12 @@ int main(int argc, char* argv[]) {
updatepos(&stats); updatepos(&stats);
// log(maze.hwalls[stats.x][stats.y]); // log(maze.hwalls[stats.x][stats.y]);
} }
next_pos[0] = 0;
next_pos[1] = 0;
for (;;) { for (;;) {
locateWall(&maze, &stats); locateWall(&maze, &stats);
flood_start(&maze, &stats); flood(&maze, &stats, next_pos);
follow(maze, &stats); follow(maze, &stats);
if (stats.x == 0 && stats.y == 0) { if (stats.x == 0 && stats.y == 0) {
log("start"); log("start");
@ -312,9 +266,12 @@ int main(int argc, char* argv[]) {
updatepos(&stats); updatepos(&stats);
// log(maze.hwalls[stats.x][stats.y]); // log(maze.hwalls[stats.x][stats.y]);
} }
next_pos[0] = 7;
next_pos[1] = 7;
for (;;) { for (;;) {
locateWall(&maze, &stats); locateWall(&maze, &stats);
flood(&maze, &stats); flood(&maze, &stats, next_pos);
follow(maze, &stats); follow(maze, &stats);
if (maze.mhtn[stats.x][stats.y] == 0) { if (maze.mhtn[stats.x][stats.y] == 0) {
log("Finish"); log("Finish");
@ -324,7 +281,7 @@ int main(int argc, char* argv[]) {
// log(stats.y); // log(stats.y);
API::moveForward(); API::moveForward();
updatepos(&stats); updatepos(&stats);
//log(maze.hwalls[stats.x][stats.y]);
} }
// log(maze.hwalls[stats.x][stats.y]);
return 0; return 0;
} }