Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: Maze/maze_solver.cpp
- Revision:
- 28:600932142201
- Parent:
- 27:b980fce784ea
- Child:
- 30:daf286ac049f
--- a/Maze/maze_solver.cpp Fri May 26 05:45:02 2017 +0000 +++ b/Maze/maze_solver.cpp Sat May 27 07:21:02 2017 +0000 @@ -1,6 +1,7 @@ #include "maze_solver.h" - DriveControl * driver; +int prev_mouse_dir_print = 5; +int prev_mouse_dir = NORTH; Maze:: Maze() { unsigned char goal1 = MAZE_SIZE / 2; @@ -28,7 +29,6 @@ return abs(x1 - x2) + abs(y1 - y2); } - // Function that takes the minimum of the four given distances unsigned char Maze:: min4(unsigned char a, unsigned char b, unsigned char c, unsigned char d) { unsigned char min; @@ -82,14 +82,12 @@ else if(direction == EAST){ if(mouse_y == MAZE_SIZE-1) return 1; - // TODO: has_wall = reference_maze->maze[mouse_x][mouse_y]->right_wall; has_wall = driver->has_front_wall(); detected_maze->maze[mouse_x][mouse_y]->right_wall = has_wall; } else{ //WEST if(mouse_y == 0) return 1; - // TODO: has_wall = reference_maze->maze[mouse_x][mouse_y-1]->right_wall; has_wall = driver->has_front_wall(); detected_maze->maze[mouse_x][mouse_y-1]->right_wall = has_wall; } @@ -364,66 +362,80 @@ return detected_maze->is_center(mouse_x, mouse_y); } +int Mouse::get_next_cell_direction() { + return direction; +} + +int Mouse::get_prev_direction() { + return prev_mouse_dir_print; +} + int Mouse:: solve_maze() { const unsigned char STRAIGHT = 0, LEFT = 1, RIGHT = 2, UTURN = 3; - prev_mouse_dir = NORTH; + + prev_mouse_dir_print = prev_mouse_dir; + + // //while the mouse has not find the center of the Maze - // while(!detected_maze->is_center(mouse_x, mouse_y)) { - move_one_cell(); - unsigned char state = 5; - if (prev_mouse_dir == direction) { - state = STRAIGHT; - } - // Mouse is facing south - else if (prev_mouse_dir == SOUTH && direction == NORTH) { - state = UTURN; - prev_mouse_dir = NORTH; - } else if (prev_mouse_dir == SOUTH && direction == EAST) { - state = LEFT; - prev_mouse_dir = EAST; - } else if (prev_mouse_dir == SOUTH && direction == WEST) { - state = RIGHT; - prev_mouse_dir = WEST; - } - // Mouse is facing north - else if (prev_mouse_dir == NORTH && direction == EAST) { - state = RIGHT; - prev_mouse_dir = EAST; - } else if (prev_mouse_dir == NORTH && direction == WEST) { - state = LEFT; - prev_mouse_dir = WEST; - } else if (prev_mouse_dir == NORTH && direction == SOUTH) { - state = UTURN; - prev_mouse_dir = SOUTH; - } - // Mouse is facing west - else if (prev_mouse_dir == WEST && direction == NORTH) { - state = RIGHT; - prev_mouse_dir = NORTH; - } else if (prev_mouse_dir == WEST && direction == SOUTH) { - state = LEFT; - prev_mouse_dir = SOUTH; - } else if (prev_mouse_dir == WEST && direction == EAST) { - state = UTURN; - prev_mouse_dir = EAST; - } - // Mouse is facing west - else if (prev_mouse_dir == EAST && direction == NORTH) { - state = LEFT; - prev_mouse_dir = NORTH; - } else if (prev_mouse_dir == EAST && direction == SOUTH) { - state = RIGHT; - prev_mouse_dir = SOUTH; - } else if (prev_mouse_dir == EAST && direction == WEST) { - state = UTURN; - prev_mouse_dir = WEST; - } - - return state; - // } + move_one_cell(); + unsigned char state = 5; + + + if (prev_mouse_dir == direction) { + state = STRAIGHT; + } + // Mouse is facing south + else if (prev_mouse_dir == SOUTH && direction == NORTH) { + state = UTURN; + prev_mouse_dir = NORTH; + } else if (prev_mouse_dir == SOUTH && direction == EAST) { + state = LEFT; + prev_mouse_dir = EAST; + } else if (prev_mouse_dir == SOUTH && direction == WEST) { + state = RIGHT; + prev_mouse_dir = WEST; + } + // Mouse is facing north + else if (prev_mouse_dir == NORTH && direction == EAST) { + state = RIGHT; + prev_mouse_dir = EAST; + } else if (prev_mouse_dir == NORTH && direction == WEST) { + state = LEFT; + prev_mouse_dir = WEST; + } else if (prev_mouse_dir == NORTH && direction == SOUTH) { + state = UTURN; + prev_mouse_dir = SOUTH; + } + // Mouse is facing west + else if (prev_mouse_dir == WEST && direction == NORTH) { + state = RIGHT; + prev_mouse_dir = NORTH; + } else if (prev_mouse_dir == WEST && direction == SOUTH) { + state = LEFT; + prev_mouse_dir = SOUTH; + } else if (prev_mouse_dir == WEST && direction == EAST) { + state = UTURN; + prev_mouse_dir = EAST; + } + // Mouse is facing west + else if (prev_mouse_dir == EAST && direction == NORTH) { + state = LEFT; + prev_mouse_dir = NORTH; + } else if (prev_mouse_dir == EAST && direction == SOUTH) { + state = RIGHT; + prev_mouse_dir = SOUTH; + } else if (prev_mouse_dir == EAST && direction == WEST) { + state = UTURN; + prev_mouse_dir = WEST; + } + + + return state; } + + Mouse:: Mouse(DriveControl * prev_driver) { driver = prev_driver; detected_maze = new Maze(); @@ -431,25 +443,4 @@ direction = NORTH; mouse_x = mouse_y = 0; north_open = south_open = east_open = west_open = 0; -} - -//void solve_maze() { -// Mouse * my_mouse = new Mouse(); -// printf("Maze Format: [Row, Col]. Size %d by %d\n", MAZE_SIZE, MAZE_SIZE); -// cout << "Start at the bottom leftmost corner [0,0]." << endl; -// -// my_mouse->load_maze("apec2016.txt"); -// printf("Here is the inital Maze:\n"); -// -// my_mouse->print_reference_maze(); -// printf("Let's see how the Mouse is going to solve it:\n"); -// -// for (unsigned char i = 0; i < MAZE_SIZE; i++) { -// for (unsigned char j = 0; j < MAZE_SIZE; j++) { -// if(my_mouse->reference_maze->maze[i][j]->x != i || my_mouse->reference_maze->maze[i][j]->y != j) { -// printf("Supposed to be <%d,%d> but is <%d,%d>\n", i, j, my_mouse->reference_maze->maze[i][j]->x, my_mouse->reference_maze->maze[i][j]->y); -// } -// } -// } -// my_mouse->solve_maze(); -//} \ No newline at end of file +} \ No newline at end of file