Compare commits

..

No commits in common. "5236a776fb8c1ff381acc902c379ff835e33e3a8" and "e79dc43240903a331b2eaa89c8730502c2089c67" have entirely different histories.

5 changed files with 141 additions and 159 deletions

1
.gitignore vendored
View File

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

View File

@ -8,11 +8,11 @@ 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.win/FisionX/mms-sim``` ```git clone git.fisionx.mooo.com/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
```c++ API.cpp Main.cpp``` ```c++ API.cpp Main.cpp```
4. Seleccionar el binario ```a.out``` 4. Seleccionar el binario ```a.out```

286
main.cpp
View File

@ -1,8 +1,8 @@
#include "api.h"
#include "mms.h"
#include <queue>
#include <string> #include <string>
#include <sys/types.h> #include <sys/types.h>
#include <queue>
#include "api.h"
#include "mms.h"
#define W 15 /* ancho y alto del laberinto -1 */ #define W 15 /* ancho y alto del laberinto -1 */
@ -10,25 +10,45 @@ struct maze { /* Distancias de Manhattan */
int vwalls[15][15]; int vwalls[15][15];
int hwalls[15][15]; int hwalls[15][15];
int mhtn[16][16]; int mhtn[16][16];
} maze; }maze;
struct fill { struct fill {
int x, y, z; int x, y, z;
}; };
void flood(struct maze *, struct mmstats *, int coordinates[2]); void flood(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, int coordinates[2]) { void flood(struct maze* maze, struct mmstats* mmstats) {
int i, j; int i, j;
int prevNum; int prevNum;
int marked[16][16] = {0}; int marked[16][16];
std::queue<fill> myqueue; std::queue<fill> myqueue;
myqueue.push({coordinates[0], coordinates[1], -1}); /*
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);
while (myqueue.size()) { while (myqueue.size()) {
i = myqueue.front().x; i = myqueue.front().x;
@ -36,33 +56,35 @@ void flood(struct maze *maze, struct mmstats *mmstats, int coordinates[2]) {
prevNum = myqueue.front().z; prevNum = myqueue.front().z;
myqueue.pop(); 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; continue;
} else { }
API::setText(i, j, prevNum + 1); else {
maze->mhtn[i][j] = prevNum + 1; API::setText(i, j, prevNum+1);
maze->mhtn[i][j] = prevNum+1;
marked[i][j] = 1; marked[i][j] = 1;
if (!maze->vwalls[i][j]) if (!maze->vwalls[i][j])
myqueue.push({i + 1, j, prevNum + 1}); myqueue.push({i+1, j, prevNum +1});
if (!maze->vwalls[i - 1][j]) if (!maze->vwalls[i-1][j])
myqueue.push({i - 1, j, prevNum + 1}); myqueue.push({i-1, j, prevNum +1});
if (!maze->hwalls[i][j]) if (!maze->hwalls[i][j])
myqueue.push({i, j + 1, prevNum + 1}); myqueue.push({i, j+1, prevNum +1});
if (!maze->hwalls[i][j - 1]) if (!maze->hwalls[i][j-1])
myqueue.push({i, j - 1, prevNum + 1}); myqueue.push({i, j-1, prevNum +1});
} }
} }
} }
void follow(struct maze maze, class mmstats *mms) { void follow(struct maze maze, class mmstats* mms) {
/* para que el micromouse siga el camino descendente /* para que el micromouse siga el camino descendente
* se tienen que dar 16 estados, esta funcion lo reduce a 4 * se tienen que dar 16 estados, esta funcion lo reduce a 4
*/ */
int mht = maze.mhtn[mms->x][mms->y]; int mht = maze.mhtn[mms->x][mms->y];
int nextmht = mht - 1; int nextmht = mht-1;
int next[2]; int next[2];
int next_L[2]; int next_L[2];
int next_R[2]; int next_R[2];
@ -71,153 +93,130 @@ 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()) { log("forward");
if (maze.mhtn[next_L[0]][next_L[1]] <= nextmht && return;
!API::wallLeft()) { /* Detectar si hay atajos */ }
API::turnLeft(); else if(maze.mhtn[next_L[0]][next_L[1]] <= nextmht && !API::wallLeft()) {
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 {
log("forward");
return;
}
} 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 && }
!API::wallRight()) { else if(maze.mhtn[next_R[0]][next_R[1]] <= nextmht && !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 || }
API::wallFront() && API::wallLeft() && API::wallRight()) { else if(maze.mhtn[next_B[0]][next_B[1]] == nextmht
|| 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]] <= }
maze.mhtn[next_B[0]][next_B[1]] && else if(maze.mhtn[next_R[0]][next_R[1]] <= maze.mhtn[next_B[0]][next_B[1]] && !API::wallRight()){
!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]] <= }
maze.mhtn[next_B[0]][next_B[1]] && else if(maze.mhtn[next_L[0]][next_L[1]] <= maze.mhtn[next_B[0]][next_B[1]] && !API::wallLeft()){
!API::wallLeft()) {
API::turnLeft(); API::turnLeft();
mms->turn(left); mms->turn(left);
log("bLeft"); log("bLeft");
} }
// log("error en la matri"); log("error en la matri");
} }
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: case norte: j++;
j++; break;
break; case este: i++;
case este: break;
i++; case sur: j--;
break; break;
case sur: case oeste: i--;
j--; break;
break;
case oeste:
i--;
break;
} }
ndir[0] = i; ndir[0] = i;
ndir[1] = j; ndir[1] = j;
return ndir; return ndir;
} }
void updatepos(class mmstats *mms) { void updatepos(class mmstats* mms) {
/* cada que se ejecuta moveForward la funcion actualiza /* cada que se ejecuta moveForward la funcion actualiza
* la posicion del micromouse en el valor correspondiente * la posicion del micromouse en el valor correspondiente
*/ */
switch (mms->dir) { switch (mms->dir) {
case norte: case norte: mms->y++;
mms->y++; break;
break; case este: mms->x++;
case este: break;
mms->x++; case sur: mms->y--;
break; break;
case sur: case oeste: mms->x--;
mms->y--; break;
break;
case oeste:
mms->x--;
break;
} }
} }
void locateWall(struct maze *maze, class mmstats *mms) { void locateWall(struct maze* maze, class mmstats *mms) {
switch (mms->dir) { switch (mms->dir) {
case norte: case norte:
if (API::wallFront() && mms->y != 15) { if(API::wallFront() && mms->y != 15) {
maze->hwalls[mms->x][mms->y] = 1; maze->hwalls[mms->x][mms->y] = 1;
API::setWall(mms->x, mms->y, 'n'); API::setWall(mms->x, mms->y, 'n');
} }
if (API::wallLeft() && mms->x != 0) { if(API::wallLeft() && mms->x != 0){
maze->vwalls[mms->x - 1][mms->y] = 1; maze->vwalls[mms->x -1][mms->y] = 1;
API::setWall(mms->x - 1, mms->y, 'e'); API::setWall(mms->x -1, mms->y, 'e');
} }
if (API::wallRight() && mms->x != 15) { if(API::wallRight() && mms->x != 15){
maze->vwalls[mms->x][mms->y] = 1; maze->vwalls[mms->x][mms->y] = 1;
API::setWall(mms->x, mms->y, 'e'); API::setWall(mms->x, mms->y, 'e');
} }
break; break;
case este: case este:
if (API::wallFront() && mms->x != 15) { if(API::wallFront() && mms->x != 15) {
maze->vwalls[mms->x][mms->y] = 1; maze->vwalls[mms->x][mms->y] = 1;
API::setWall(mms->x, mms->y, 'e'); API::setWall(mms->x, mms->y, 'e');
} }
if (API::wallLeft() && mms->y != 15) { if(API::wallLeft() && mms->y != 15){
maze->hwalls[mms->x][mms->y] = 1; maze->hwalls[mms->x][mms->y] = 1;
API::setWall(mms->x, mms->y, 'n'); API::setWall(mms->x, mms->y, 'n');
} }
if (API::wallRight() && mms->y != 0) { if(API::wallRight() && mms->y != 0){
maze->hwalls[mms->x][mms->y - 1] = 1; maze->hwalls[mms->x][mms->y -1] = 1;
API::setWall(mms->x, mms->y - 1, 'n'); API::setWall(mms->x, mms->y -1, 'n');
} }
break; break;
case sur: case sur:
if (API::wallFront() && mms->y != 0) { if(API::wallFront() && mms->y != 0) {
maze->hwalls[mms->x][mms->y - 1] = 1; maze->hwalls[mms->x][mms->y-1] = 1;
API::setWall(mms->x, mms->y - 1, 'n'); API::setWall(mms->x, mms->y-1, 'n');
} }
if (API::wallLeft() && mms->x != 15) { if(API::wallLeft() && mms->x != 15){
maze->vwalls[mms->x][mms->y] = 1; maze->vwalls[mms->x][mms->y] = 1;
API::setWall(mms->x, mms->y, 'e'); API::setWall(mms->x, mms->y, 'e');
} }
if (API::wallRight() && mms->x != 0) { if(API::wallRight() && mms->x != 0){
maze->vwalls[mms->x - 1][mms->y] = 1; maze->vwalls[mms->x -1][mms->y] = 1;
API::setWall(mms->x - 1, mms->y, 'e'); API::setWall(mms->x -1, mms->y, 'e');
} }
break; break;
case oeste: case oeste:
if (API::wallFront() && mms->x != 0) { if(API::wallFront() && mms->x != 0) {
maze->vwalls[mms->x - 1][mms->y] = 1; maze->vwalls[mms->x -1][mms->y] = 1;
API::setWall(mms->x - 1, mms->y, 'e'); API::setWall(mms->x -1, mms->y, 'e');
} }
if (API::wallLeft() && mms->y != 0) { if(API::wallLeft() && mms->y != 0){
maze->hwalls[mms->x][mms->y - 1] = 1; maze->hwalls[mms->x][mms->y -1] = 1;
API::setWall(mms->x, mms->y - 1, 'n'); API::setWall(mms->x, mms->y -1, 'n');
} }
if (API::wallRight() && mms->y != 15) { if(API::wallRight() && mms->y != 15){
maze->hwalls[mms->x][mms->y] = 1; maze->hwalls[mms->x][mms->y] = 1;
API::setWall(mms->x, mms->y, 'n'); API::setWall(mms->x, mms->y, 'n');
} }
@ -225,63 +224,50 @@ 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;
stats.x = 0; stats.x = 0;
stats.y = 0; stats.y = 0;
int next_pos[2] = {7, 7}; while (true) {
for (;;) {
locateWall(&maze, &stats); locateWall(&maze, &stats);
flood(&maze, &stats, next_pos); flood(&maze, &stats);
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");
break; return 0;
} }
// log(stats.x); //log(stats.x);
// 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]);
} }
/*
next_pos[0] = 0; while (true) {
next_pos[1] = 0; if (!API::wallLeft()) {
for (;;) { 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); locateWall(&maze, &stats);
flood(&maze, &stats, next_pos);
follow(maze, &stats);
if (stats.x == 0 && stats.y == 0) {
log("start");
break;
}
// log(stats.x);
// log(stats.y);
API::moveForward(); API::moveForward();
updatepos(&stats); updatepos(&stats);
// log(maze.hwalls[stats.x][stats.y]); flood(&maze, &stats);
} log(stats.x);
log(stats.y);
next_pos[0] = 7; log("");
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,8 +6,5 @@ ARGS = -g -Wall
compile: ${SRC} compile: ${SRC}
${CC} ${ARGS} $^ -o a.out ${CC} ${ARGS} $^ -o a.out
dir: ${SRC} run: compile
${CC} ${ARGS} -DDIR $^ -o a.out
run: a.out
@./a.out @./a.out

4
mms.h
View File

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