FloodFill #2

Merged
FisionX merged 16 commits from floodfill into main 2024-08-09 18:29:49 -06:00
2 changed files with 53 additions and 12 deletions
Showing only changes of commit 4ddb702862 - Show all commits

View File

@ -24,6 +24,9 @@ struct fill {
}; };
void flood(struct maze*, struct mmstats*); void flood(struct maze*, struct mmstats*);
void follow(struct maze maze, class mmstats mms);
int* nextcell(int i, int j, int dir, int ndir[2]);
void flood(struct maze* maze, struct mmstats* mmstats) { void flood(struct maze* maze, struct mmstats* mmstats) {
int i, j; int i, j;
int prevNum; int prevNum;
@ -58,6 +61,8 @@ void flood(struct maze* maze, struct mmstats* mmstats) {
if (mmstats->x == i && mmstats->y == j) { if (mmstats->x == i && mmstats->y == j) {
log("flood end"); log("flood end");
API::setText(i, j, prevNum+1);
maze->mhtn[i][j] = prevNum+1;
break; break;
} }
@ -77,20 +82,40 @@ void flood(struct maze* maze, struct mmstats* mmstats) {
} }
} }
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 x = mms.x; int mht = maze.mhtn[mms->x][mms->y];
int y = mms.y; int nextmht = mht-1;
int mht = maze.mhtn[x][y];
int next[2]; int next[2];
if (mht > maze.mhtn[x][y+1]) { nextcell(mms->x, mms->y, mms->dir, next);
next[0] = x; if(maze.mhtn[next[0]][next[1]] == nextmht) {
log("forward");
return;
} }
nextcell(mms->x, mms->y, mms->head->prev->value, next);
if(maze.mhtn[next[0]][next[1]] == nextmht) {
API::turnLeft();
mms->turn(left);
log("left");
return;
}
nextcell(mms->x, mms->y, mms->head->next->value, next);
if(maze.mhtn[next[0]][next[1]] == nextmht) {
API::turnRight();
mms->turn(right);
log("right");
return;
}
API::turnRight();
mms->turn(right);
API::turnRight();
mms->turn(right);
log("180");
} }
void nextcell(int i, int j, int dir, int& nextx, int& nexty) { int* nextcell(int i, int j, int dir, int ndir[2]) {
switch (dir) { switch (dir) {
case norte: j++; case norte: j++;
break; break;
@ -101,8 +126,9 @@ void nextcell(int i, int j, int dir, int& nextx, int& nexty) {
case oeste: i--; case oeste: i--;
break; break;
} }
nextx = i; ndir[0] = i;
nexty = j; ndir[1] = j;
return ndir;
} }
void updatepos(class mmstats* mms) { void updatepos(class mmstats* mms) {
@ -126,13 +152,24 @@ 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"); // API::setText(0, 0, "abc");
API::setWall(1, 0, 'e');
mmstats stats; mmstats stats;
stats.x = 0; stats.x = 0;
stats.y = 0; stats.y = 0;
flood(&maze, &stats); flood(&maze, &stats);
while (true) {
follow(maze, &stats);
API::moveForward();
updatepos(&stats);
if(maze.mhtn[stats.x][stats.y] == 0){
log("Finish");
return 0;
}
log(stats.x);
log(stats.y);
log("");
}
/*
while (true) { while (true) {
if (!API::wallLeft()) { if (!API::wallLeft()) {
API::turnLeft(); API::turnLeft();
@ -144,5 +181,9 @@ int main(int argc, char* argv[]) {
} }
API::moveForward(); API::moveForward();
updatepos(&stats); updatepos(&stats);
log(stats.x);
log(stats.y);
log("");
} }
*/
} }

2
mms.h
View File

@ -53,7 +53,7 @@ public:
case oeste: log("Oeste"); case oeste: log("Oeste");
break; break;
} }
*/ */
} }
}; };