Added ifdef for debuggin mms.h

This commit is contained in:
Fernando 2024-11-13 13:53:41 -06:00
parent e79dc43240
commit f4e82b4be4
3 changed files with 77 additions and 28 deletions

View File

@ -17,6 +17,7 @@ struct fill {
}; };
void flood(struct maze*, struct mmstats*); void flood(struct maze*, struct mmstats*);
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);
@ -79,6 +80,43 @@ void flood(struct maze* maze, struct mmstats* mmstats) {
} }
} }
void flood_start(struct maze* maze, struct mmstats* mmstats) {
int i, j;
int prevNum;
int marked[16][16];
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);
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 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
@ -93,10 +131,25 @@ 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() || !API::wallFront() && API::wallLeft() && API::wallRight()) { 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()) {
API::turnRight();
mms->turn(right);
log("right");
return;
}
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);
@ -233,13 +286,13 @@ int main(int argc, char* argv[]) {
stats.x = 0; stats.x = 0;
stats.y = 0; stats.y = 0;
while (true) { for(;;) {
locateWall(&maze, &stats); locateWall(&maze, &stats);
flood(&maze, &stats); 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");
return 0; break;
} }
//log(stats.x); //log(stats.x);
//log(stats.y); //log(stats.y);
@ -247,27 +300,20 @@ int main(int argc, char* argv[]) {
updatepos(&stats); updatepos(&stats);
//log(maze.hwalls[stats.x][stats.y]); //log(maze.hwalls[stats.x][stats.y]);
} }
/* for(;;){
while (true) { log("returning start");
if (!API::wallLeft()) { flood_start(&maze, &stats);
locateWall(&maze, &stats); locateWall(&maze, &stats);
API::turnLeft(); follow(maze, &stats);
stats.turn(left); if(maze.mhtn[stats.x][stats.y] == 0){
flood(&maze, &stats); log("start");
break;
} }
while (API::wallFront()) { //log(stats.x);
locateWall(&maze, &stats); //log(stats.y);
API::turnRight();
stats.turn(right);
flood(&maze, &stats);
}
locateWall(&maze, &stats);
API::moveForward(); API::moveForward();
updatepos(&stats); updatepos(&stats);
flood(&maze, &stats); //log(maze.hwalls[stats.x][stats.y]);
log(stats.x);
log(stats.y);
log("");
} }
*/ return 0;
} }

View File

@ -6,5 +6,8 @@ ARGS = -g -Wall
compile: ${SRC} compile: ${SRC}
${CC} ${ARGS} $^ -o a.out ${CC} ${ARGS} $^ -o a.out
run: compile dir: ${SRC}
${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
} }
}; };