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_start(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);
@ -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) {
/* para que el micromouse siga el camino descendente
* 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->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()) {
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");
return;
}
}
else if(maze.mhtn[next_L[0]][next_L[1]] <= nextmht && !API::wallLeft()) {
API::turnLeft();
mms->turn(left);
@ -233,13 +286,13 @@ int main(int argc, char* argv[]) {
stats.x = 0;
stats.y = 0;
while (true) {
for(;;) {
locateWall(&maze, &stats);
flood(&maze, &stats);
follow(maze, &stats);
if(maze.mhtn[stats.x][stats.y] == 0){
log("Finish");
return 0;
break;
}
//log(stats.x);
//log(stats.y);
@ -247,27 +300,20 @@ int main(int argc, char* argv[]) {
updatepos(&stats);
//log(maze.hwalls[stats.x][stats.y]);
}
/*
while (true) {
if (!API::wallLeft()) {
for(;;){
log("returning start");
flood_start(&maze, &stats);
locateWall(&maze, &stats);
API::turnLeft();
stats.turn(left);
flood(&maze, &stats);
follow(maze, &stats);
if(maze.mhtn[stats.x][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]);
}
*/
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
}
};