Hello guys!
Before starting, i'm Brazilian and i don't speak inglish very well, so forgive me if i say something wrong!
well, i'm trying to create a micromouse robot to solve a maze, as college work. I will use the floodfill algortihm to do that, but i'm having much dificult to create this code.
i saw this code here in the forum and i using it like base:
const byte size=16;
const byte Empty=255;
int i = 0;
const byte North=1;
const byte South=2;
const byte East=4;
const byte West=8;
const byte Walls[size][size] PROGMEM=
{
{West|North,North,North,North,North,North,North,North,North,North,North,North,North,North,North,North|East},
{West,0,0,West,0,0,0,0,0,0,0,0,0,0,0,East},
{West,0,0,West,0,0,0,0,0,0,0,0,0,0,0,East},
{West,0,0,West,0,0,0,0,0,0,0,0,0,0,0,East},
{West,0,0,West,0,0,0,0,0,0,0,0,0,0,0,East},
{West,0,0,West|North,North,North,North,North,North,North,North,North,North,North,North,East|North},
{West,0,0,West,0,0,0,0,0,0,0,0,0,0,0,East},
{West,0,0,West,0,0,0,West|North,East,0,0,0,0,0,0,East},
{West,0,0,West,0,0,0,West|South,South,0,0,0,0,0,0,East},
{West,0,0,West,0,0,0,0,0,0,0,0,0,0,0,East},
{West,0,0,West,0,0,0,0,0,0,0,0,0,0,0,East},
{West,0,0,West,0,0,0,0,0,0,0,0,0,0,0,East},
{West,0,0,West|South,South,South,South,South,South,South,South,South,South,South,South,East|South},
{West,0,0,West,0,0,0,0,0,0,0,0,0,0,0,East},
{West,0,0,0,West,0,0,0,0,0,0,0,0,0,0,East},
{West|South,South,South,South,South,South,South,South,South,South,South,South,South,South,South,South|East}
};
typedef struct
{
//byte walls;
byte steps;
}Cell;
Cell Maze[size][size];
void setup()
{
Serial.begin(115200);
ClearMaze();
SetGoalCell(7,7);
SetGoalCell(7,8);
SetGoalCell(8,7);
SetGoalCell(8,8);
PrintWalls();
Serial.println();
PrintMaze();
Serial.println();
FloodFill(7,7);
FloodFill(7,8);
FloodFill(8,7);
FloodFill(8,8);
PrintMaze();
}
void ClearMaze()
{
for(byte row=0;row<size;row++)
for(byte col=0;col<size;col++)
Maze[row][col].steps=Empty;
}
void SetGoalCell(byte col,byte row)
{
Maze[row][col].steps=0;
}
void FloodFill(byte row,byte col)
{
byte Next=Maze[row][col].steps+1;
byte wall = pgm_read_byte_near(&(Walls[row][col]));
if(row && (wall&North)==0)
{
if(Maze[row-1][col].steps>Next)
{
Maze[row-1][col].steps=Next;
FloodFill(row-1,col);
}
}
if(col<size-1 && (wall&East)==0)
{
if(Maze[row][col+1].steps>Next)
{
Maze[row][col+1].steps=Next;
FloodFill(row,col+1);
}
}
if(row<size-1 && (wall&South)==0)
{
if(Maze[row+1][col].steps>Next)
{
Maze[row+1][col].steps=Next;
FloodFill(row+1,col);
}
}
if(col && (wall&West)==0)
{
if(Maze[row][col-1].steps>Next)
{
Maze[row][col-1].steps=Next;
FloodFill(row,col-1);
}
}
}
void PrintMaze()
{
for(byte row=0;row<size;row++)
{
for(byte col=0;col<size;col++)
{
Serial.print(Maze[row][col].steps);
Serial.print("\t");
}
Serial.println();
}
}
void PrintWalls()
{
for(byte row=0;row<size;row++)
{
for(byte col=0;col<size;col++)
{
Serial.print(pgm_read_byte_near(&(Walls[row][col])));
Serial.print(",");
}
Serial.println();
}
}
void loop()
{
}
but, i didn't can understand how the all code works, someone can explain me some functions in the code?
for example, the function "const byte Walls PROGMEM", i know that is about a constant matriz 16x16 that represents the walls of the maze, but my doubt is, if this matriz represents the walls, why the values is seted this way?
I tried to draw the maze with base in this values, in the cell that is "west" i put a wall on west of that cell, and the same for the others cells, and the maze don't maked sense to me.
In the cells that the values is 0, means that there isn't a walls?
And if is this, means that there are more than 1 cells conected without wall, as in case the cells at (1,5),(1,6),(1,7),(2,5),(2,6),(2,7), being (row,col), and so on in the matriz of walls.
other doubt, the values of north, south, east, west, obligatorily should be this values (1,2,4,8) or can i put other values?
i think there is some rule to found this values, can someone explain me why these values are used?