Merge pull request 'Ajustar algoritmo para regresar al inicio' (#3) from follow into main

Reviewed-on: #3
This commit is contained in:
Fernando 2025-02-06 14:10:21 -06:00
commit 5236a776fb
5 changed files with 159 additions and 141 deletions

1
.gitignore vendored
View File

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

View File

@ -8,7 +8,7 @@ https://github.com/mackorone/mms
## Configuración en mms
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
3. Usar el comando de compilado

166
main.cpp
View File

@ -1,8 +1,8 @@
#include <string>
#include <sys/types.h>
#include <queue>
#include "api.h"
#include "mms.h"
#include <queue>
#include <string>
#include <sys/types.h>
#define W 15 /* ancho y alto del laberinto -1 */
@ -16,39 +16,19 @@ struct fill {
int x, y, z;
};
void flood(struct maze*, struct mmstats*);
void flood(struct maze *, struct mmstats *, int coordinates[2]);
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) {
void flood(struct maze *maze, struct mmstats *mmstats, int coordinates[2]) {
int i, j;
int prevNum;
int marked[16][16];
int marked[16][16] = {0};
std::queue<fill> 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});
*/
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);
API::setText(8, 7, 0);
API::setText(8, 8, 0);
myqueue.push({coordinates[0], coordinates[1], -1});
while (myqueue.size()) {
i = myqueue.front().x;
@ -56,11 +36,9 @@ void flood(struct maze* maze, struct mmstats* mmstats) {
prevNum = myqueue.front().z;
myqueue.pop();
if (i < 0 || i > W || j < 0 || j > W
|| marked[i][j]==1) {
if (i < 0 || i > W || j < 0 || j > W || marked[i][j] == 1) {
continue;
}
else {
} else {
API::setText(i, j, prevNum + 1);
maze->mhtn[i][j] = prevNum + 1;
marked[i][j] = 1;
@ -93,53 +71,72 @@ 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::wallFront() && API::wallLeft() && API::wallRight()) {
log("forward");
return;
}
else if(maze.mhtn[next_L[0]][next_L[1]] <= nextmht && !API::wallLeft()) {
if (maze.mhtn[next[0]][next[1]] == nextmht && !API::wallFront() ||
!API::wallFront() && API::wallLeft() && API::wallRight()) {
if (maze.mhtn[next_L[0]][next_L[1]] <= nextmht &&
!API::wallLeft()) { /* Detectar si hay atajos */
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 {
log("forward");
return;
}
else if(maze.mhtn[next_B[0]][next_B[1]] == nextmht
|| API::wallFront() && API::wallLeft() && API::wallRight()) {
} 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()) {
API::turnRight();
mms->turn(right);
log("right");
return;
} 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()){
} 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()){
} 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");
// log("error en la matri");
}
int *nextcell(int i, int j, int dir, int ndir[2]) {
switch (dir) {
case norte: j++;
case norte:
j++;
break;
case este: i++;
case este:
i++;
break;
case sur: j--;
case sur:
j--;
break;
case oeste: i--;
case oeste:
i--;
break;
}
ndir[0] = i;
@ -152,13 +149,17 @@ void updatepos(class mmstats* mms) {
* la posicion del micromouse en el valor correspondiente
*/
switch (mms->dir) {
case norte: mms->y++;
case norte:
mms->y++;
break;
case este: mms->x++;
case este:
mms->x++;
break;
case sur: mms->y--;
case sur:
mms->y--;
break;
case oeste: mms->x--;
case oeste:
mms->x--;
break;
}
}
@ -224,22 +225,23 @@ void locateWall(struct maze* maze, class mmstats *mms) {
}
}
int main(int argc, char *argv[]) {
log("Running...");
API::setColor(0, 0, 'G');
// API::setText(0, 0, "abc");
mmstats stats;
stats.x = 0;
stats.y = 0;
while (true) {
int next_pos[2] = {7, 7};
for (;;) {
locateWall(&maze, &stats);
flood(&maze, &stats);
flood(&maze, &stats, next_pos);
follow(maze, &stats);
if (maze.mhtn[stats.x][stats.y] == 0) {
log("Finish");
return 0;
break;
}
// log(stats.x);
// log(stats.y);
@ -247,27 +249,39 @@ int main(int argc, char* argv[]) {
updatepos(&stats);
// log(maze.hwalls[stats.x][stats.y]);
}
/*
while (true) {
if (!API::wallLeft()) {
next_pos[0] = 0;
next_pos[1] = 0;
for (;;) {
locateWall(&maze, &stats);
API::turnLeft();
stats.turn(left);
flood(&maze, &stats);
flood(&maze, &stats, next_pos);
follow(maze, &stats);
if (stats.x == 0 && stats.y == 0) {
log("start");
break;
}
while (API::wallFront()) {
locateWall(&maze, &stats);
API::turnRight();
stats.turn(right);
flood(&maze, &stats);
}
locateWall(&maze, &stats);
// log(stats.x);
// log(stats.y);
API::moveForward();
updatepos(&stats);
flood(&maze, &stats);
log(stats.x);
log(stats.y);
log("");
// log(maze.hwalls[stats.x][stats.y]);
}
*/
next_pos[0] = 7;
next_pos[1] = 7;
for (;;) {
locateWall(&maze, &stats);
flood(&maze, &stats, next_pos);
follow(maze, &stats);
if (maze.mhtn[stats.x][stats.y] == 0) {
log("Finish");
break;
}
// log(stats.x);
// log(stats.y);
API::moveForward();
updatepos(&stats);
}
// log(maze.hwalls[stats.x][stats.y]);
return 0;
}

View File

@ -6,5 +6,8 @@ ARGS = -g -Wall
compile: ${SRC}
${CC} ${ARGS} $^ -o a.out
run: compile
dir: ${SRC}
${CC} ${ARGS} -DDIR $^ -o a.out
run: a.out
@./a.out

4
mms.h
View File

@ -42,7 +42,7 @@ public:
head = head->next;
else head = head->prev;
dir = head->value;
/*
#ifdef DIR
switch (dir) {
case norte: log("Norte");
break;
@ -53,7 +53,7 @@ public:
case oeste: log("Oeste");
break;
}
*/
#endif
}
};