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/maze_solver.h@31:f7a8a9b82bc1, 2017-05-28 (annotated)
- Committer:
- kolanery
- Date:
- Sun May 28 09:54:40 2017 +0000
- Revision:
- 31:f7a8a9b82bc1
- Parent:
- 30:daf286ac049f
Update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
szh66 | 27:b980fce784ea | 1 | #include <string> |
ryan_whr | 25:7155cb993870 | 2 | #include <vector> |
szh66 | 27:b980fce784ea | 3 | #include <iostream> |
szh66 | 27:b980fce784ea | 4 | #include <fstream> |
szh66 | 27:b980fce784ea | 5 | #include "drivecontrol.h" |
ryan_whr | 25:7155cb993870 | 6 | |
szh66 | 27:b980fce784ea | 7 | const unsigned char MAZE_SIZE = 16; |
szh66 | 27:b980fce784ea | 8 | const unsigned char NORTH = 0; |
szh66 | 27:b980fce784ea | 9 | const unsigned char SOUTH = 1; |
szh66 | 27:b980fce784ea | 10 | const unsigned char WEST = 2; |
szh66 | 27:b980fce784ea | 11 | const unsigned char EAST = 3; |
ryan_whr | 25:7155cb993870 | 12 | const int MAX = 256; |
ryan_whr | 25:7155cb993870 | 13 | |
szh66 | 27:b980fce784ea | 14 | using namespace std; |
szh66 | 27:b980fce784ea | 15 | |
ryan_whr | 25:7155cb993870 | 16 | class Cell { |
ryan_whr | 25:7155cb993870 | 17 | public: |
szh66 | 27:b980fce784ea | 18 | unsigned char x, y; |
szh66 | 27:b980fce784ea | 19 | unsigned char dist; |
szh66 | 27:b980fce784ea | 20 | bool top_wall, right_wall; |
szh66 | 27:b980fce784ea | 21 | Cell(unsigned char x, unsigned char y, unsigned char dist) : |
szh66 | 27:b980fce784ea | 22 | x(x), y(y), dist(dist) { |
szh66 | 27:b980fce784ea | 23 | top_wall = right_wall = false; |
szh66 | 27:b980fce784ea | 24 | } |
szh66 | 27:b980fce784ea | 25 | Cell(unsigned char x, unsigned char y, bool top_wall, bool right_wall) |
szh66 | 27:b980fce784ea | 26 | : x(x), y(y), top_wall(top_wall), right_wall(right_wall) {} |
ryan_whr | 25:7155cb993870 | 27 | }; |
ryan_whr | 25:7155cb993870 | 28 | |
szh66 | 27:b980fce784ea | 29 | class Maze { |
szh66 | 27:b980fce784ea | 30 | public: |
szh66 | 27:b980fce784ea | 31 | Maze(); |
ryan_whr | 25:7155cb993870 | 32 | |
szh66 | 27:b980fce784ea | 33 | ~Maze() {}; |
ryan_whr | 25:7155cb993870 | 34 | |
szh66 | 27:b980fce784ea | 35 | unsigned char manhattan_dist(unsigned char x1, unsigned char x2, unsigned char y1, unsigned char y2); |
ryan_whr | 25:7155cb993870 | 36 | |
szh66 | 27:b980fce784ea | 37 | unsigned char min4(unsigned char a, unsigned char b, unsigned char c, unsigned char d); |
ryan_whr | 25:7155cb993870 | 38 | |
szh66 | 27:b980fce784ea | 39 | bool is_center(unsigned char x, unsigned char y); |
ryan_whr | 25:7155cb993870 | 40 | |
szh66 | 27:b980fce784ea | 41 | void print_maze(int x, int y, int dir); |
ryan_whr | 25:7155cb993870 | 42 | |
szh66 | 27:b980fce784ea | 43 | void print_maze(); |
ryan_whr | 25:7155cb993870 | 44 | |
szh66 | 27:b980fce784ea | 45 | void print_mouse(int dir); |
ryan_whr | 25:7155cb993870 | 46 | |
szh66 | 27:b980fce784ea | 47 | void move(); |
ryan_whr | 25:7155cb993870 | 48 | |
szh66 | 27:b980fce784ea | 49 | void load_maze(string file_name); |
ryan_whr | 25:7155cb993870 | 50 | |
szh66 | 27:b980fce784ea | 51 | Cell * maze[MAZE_SIZE][MAZE_SIZE]; |
ryan_whr | 25:7155cb993870 | 52 | }; |
ryan_whr | 25:7155cb993870 | 53 | |
szh66 | 27:b980fce784ea | 54 | class Mouse { |
szh66 | 27:b980fce784ea | 55 | public: |
szh66 | 27:b980fce784ea | 56 | unsigned char direction, next_cell_dir, prev_mouse_dir; |
szh66 | 27:b980fce784ea | 57 | //the current position of Mouse, should be identical to the Cell'x and y |
szh66 | 27:b980fce784ea | 58 | unsigned char mouse_x, mouse_y; |
szh66 | 27:b980fce784ea | 59 | // the previous direction, the previous direction is open by default |
szh66 | 27:b980fce784ea | 60 | unsigned char prev; |
szh66 | 27:b980fce784ea | 61 | bool north_open, south_open, east_open, west_open; |
szh66 | 27:b980fce784ea | 62 | bool front_sensor, left_sensor, right_sensor; |
szh66 | 27:b980fce784ea | 63 | Maze * detected_maze; |
szh66 | 27:b980fce784ea | 64 | std::vector <Cell*> stk; //vc was used as a stack to update the distance |
ryan_whr | 25:7155cb993870 | 65 | |
szh66 | 27:b980fce784ea | 66 | Mouse( DriveControl * driver); |
ryan_whr | 25:7155cb993870 | 67 | |
szh66 | 27:b980fce784ea | 68 | ~Mouse() { |
szh66 | 27:b980fce784ea | 69 | for (int i = 0; i < MAZE_SIZE; i++) { |
szh66 | 27:b980fce784ea | 70 | // delete [] *maze[i]; |
szh66 | 27:b980fce784ea | 71 | for (int j = 0; j < MAZE_SIZE; j++) { |
szh66 | 27:b980fce784ea | 72 | delete detected_maze->maze[i][j]; |
szh66 | 27:b980fce784ea | 73 | } |
szh66 | 27:b980fce784ea | 74 | } |
szh66 | 27:b980fce784ea | 75 | delete detected_maze; |
szh66 | 27:b980fce784ea | 76 | }; |
ryan_whr | 25:7155cb993870 | 77 | |
szh66 | 27:b980fce784ea | 78 | void set_direction(unsigned char dir); |
szh66 | 27:b980fce784ea | 79 | |
szh66 | 27:b980fce784ea | 80 | unsigned char get_direction(); |
szh66 | 27:b980fce784ea | 81 | |
szh66 | 27:b980fce784ea | 82 | bool get_front_sensor_value(); //read the value from the front sensor and will update the wall properly |
ryan_whr | 25:7155cb993870 | 83 | |
szh66 | 27:b980fce784ea | 84 | bool get_left_sensor_value(); //read the value from the left sensor and will update the wall properly |
ryan_whr | 25:7155cb993870 | 85 | |
szh66 | 27:b980fce784ea | 86 | bool get_right_sensor_value(); //read the value from the right sensor and will update the wall properly |
ryan_whr | 25:7155cb993870 | 87 | |
szh66 | 27:b980fce784ea | 88 | bool can_move(); |
ryan_whr | 25:7155cb993870 | 89 | |
szh66 | 27:b980fce784ea | 90 | void update_distance(); |
ryan_whr | 25:7155cb993870 | 91 | |
szh66 | 27:b980fce784ea | 92 | unsigned char min_open_neighbor(vector<Cell*> cells); |
ryan_whr | 25:7155cb993870 | 93 | |
szh66 | 27:b980fce784ea | 94 | void check_open_neighbor(); |
ryan_whr | 25:7155cb993870 | 95 | |
szh66 | 27:b980fce784ea | 96 | int solve_maze(); |
szh66 | 27:b980fce784ea | 97 | |
szh66 | 27:b980fce784ea | 98 | bool center_found(); |
kolanery | 30:daf286ac049f | 99 | |
kolanery | 30:daf286ac049f | 100 | void clear_stack(); |
kolanery | 30:daf286ac049f | 101 | |
kolanery | 30:daf286ac049f | 102 | bool can_reset_mouse; |
kolanery | 31:f7a8a9b82bc1 | 103 | |
kolanery | 31:f7a8a9b82bc1 | 104 | bool is_center; |
ryan_whr | 25:7155cb993870 | 105 | |
szh66 | 27:b980fce784ea | 106 | void move_one_cell(); |
kolanery | 28:600932142201 | 107 | |
kolanery | 28:600932142201 | 108 | int get_prev_direction(); |
kolanery | 28:600932142201 | 109 | |
kolanery | 28:600932142201 | 110 | int get_next_cell_direction(); |
ryan_whr | 25:7155cb993870 | 111 | |
szh66 | 27:b980fce784ea | 112 | }; |