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
maze_solver.h
00001 #include <string> 00002 #include <vector> 00003 #include <iostream> 00004 #include <fstream> 00005 #include "drivecontrol.h" 00006 00007 const unsigned char MAZE_SIZE = 16; 00008 const unsigned char NORTH = 0; 00009 const unsigned char SOUTH = 1; 00010 const unsigned char WEST = 2; 00011 const unsigned char EAST = 3; 00012 const int MAX = 256; 00013 00014 using namespace std; 00015 00016 class Cell { 00017 public: 00018 unsigned char x, y; 00019 unsigned char dist; 00020 bool top_wall, right_wall; 00021 Cell(unsigned char x, unsigned char y, unsigned char dist) : 00022 x(x), y(y), dist(dist) { 00023 top_wall = right_wall = false; 00024 } 00025 Cell(unsigned char x, unsigned char y, bool top_wall, bool right_wall) 00026 : x(x), y(y), top_wall(top_wall), right_wall(right_wall) {} 00027 }; 00028 00029 class Maze { 00030 public: 00031 Maze(); 00032 00033 ~Maze() {}; 00034 00035 unsigned char manhattan_dist(unsigned char x1, unsigned char x2, unsigned char y1, unsigned char y2); 00036 00037 unsigned char min4(unsigned char a, unsigned char b, unsigned char c, unsigned char d); 00038 00039 bool is_center(unsigned char x, unsigned char y); 00040 00041 void print_maze(int x, int y, int dir); 00042 00043 void print_maze(); 00044 00045 void print_mouse(int dir); 00046 00047 void move(); 00048 00049 void load_maze(string file_name); 00050 00051 Cell * maze[MAZE_SIZE][MAZE_SIZE]; 00052 }; 00053 00054 class Mouse { 00055 public: 00056 unsigned char direction, next_cell_dir, prev_mouse_dir; 00057 //the current position of Mouse, should be identical to the Cell'x and y 00058 unsigned char mouse_x, mouse_y; 00059 // the previous direction, the previous direction is open by default 00060 unsigned char prev; 00061 bool north_open, south_open, east_open, west_open; 00062 bool front_sensor, left_sensor, right_sensor; 00063 Maze * detected_maze; 00064 std::vector <Cell*> stk; //vc was used as a stack to update the distance 00065 00066 Mouse( DriveControl * driver); 00067 00068 ~Mouse() { 00069 for (int i = 0; i < MAZE_SIZE; i++) { 00070 // delete [] *maze[i]; 00071 for (int j = 0; j < MAZE_SIZE; j++) { 00072 delete detected_maze->maze[i][j]; 00073 } 00074 } 00075 delete detected_maze; 00076 }; 00077 00078 void set_direction(unsigned char dir); 00079 00080 unsigned char get_direction(); 00081 00082 bool get_front_sensor_value(); //read the value from the front sensor and will update the wall properly 00083 00084 bool get_left_sensor_value(); //read the value from the left sensor and will update the wall properly 00085 00086 bool get_right_sensor_value(); //read the value from the right sensor and will update the wall properly 00087 00088 bool can_move(); 00089 00090 void update_distance(); 00091 00092 unsigned char min_open_neighbor(vector<Cell*> cells); 00093 00094 void check_open_neighbor(); 00095 00096 int solve_maze(); 00097 00098 bool center_found(); 00099 00100 void clear_stack(); 00101 00102 bool can_reset_mouse; 00103 00104 bool is_center; 00105 00106 void move_one_cell(); 00107 00108 int get_prev_direction(); 00109 00110 int get_next_cell_direction(); 00111 00112 };
Generated on Thu Jul 14 2022 05:30:38 by
