/// <summary> /// Determines if the end is in the eastern vision of the walker and if so, sends the walker directly in that direction. /// </summary> /// <param name="walker">The walker.</param> /// <returns> /// A boolean value if the end is in the vicinity and also the walker with updated values. /// </returns> private bool is_eastfinish(MazeWalker walker) { bool is_finish = false; int y = walker.Y; int x = walker.X; if (y < 0 || x < 0 || y >= m_maze_to_solve.Count) { return(false); } List <string> pixel_row = m_maze_to_solve[y]; if (x >= pixel_row.Count) { return(false); } while (pixel_row[x] != "1") { if (x >= pixel_row.Count) { break; } if (pixel_row[x] == "E") { is_finish = true; break; } else { x++; } } if (is_finish) { //finish the job x = walker.X; walker.Dir = Direction.East; while (pixel_row[x] != "1" && pixel_row[x] != "E") { if (x >= pixel_row.Count) { break; } pixel_row[x] = "G"; x++; walker.X = walker.X + 1; MazeParser.color(m_maze_to_solve, walker); } } return(is_finish); }
/// <summary> /// Determines if the end is in the southern vision of the walker and if so, sends the walker directly in that direction. /// </summary> /// <param name="walker">The walker.</param> /// <returns> /// A boolean value if the end is in the vicinity and also the walker with updated values. /// </returns> private bool is_southfinish(MazeWalker walker) { bool is_finish = false; int y = walker.Y; int x = walker.X; if (y < 0 || x < 0 || y >= m_maze_to_solve.Count) { return(false); } for (; y < m_maze_to_solve.Count; y++) { List <string> pixel_row = m_maze_to_solve[y]; if (pixel_row[x] == "1") { break; } if (pixel_row[x] == "E") { is_finish = true; break; } } if (is_finish) { //finish the job walker.Dir = Direction.South; for (y = walker.Y; y < m_maze_to_solve.Count; y++) { List <string> pixel_row = m_maze_to_solve[y]; if (pixel_row[x] == "1") { break; } else if (pixel_row[x] == "E") { break; } else { pixel_row[x] = "X"; } walker.Y = walker.Y + 1; MazeParser.color(m_maze_to_solve, walker); } } return(is_finish); }
/// <summary> /// Solves the given maze using the right-hand wall following method. /// NOTE: this algorithm will not work for mazes that are not simply connected. /// </summary> /// <returns> /// The parsed solution to the maze. /// </returns> private List <List <string> > solve_maze_RH() { bool pathFound = false; MazeWalker walker = new MazeWalker(); walker = findstart(walker); while (!pathFound) { //for each direction, test to see which directions are valid //we always prioritize the current direction //then the directions are prioritized according to the right-hand following method if (walker.Dir == Direction.North) { bool move_success = false; if (is_northvalid(walker.X, walker.Y)) { move_success = walker.move_north(m_maze_to_solve); } else if (is_eastvalid(walker.X, walker.Y)) { move_success = walker.move_east(m_maze_to_solve); } else if (is_westvalid(walker.X, walker.Y)) { move_success = walker.move_west(m_maze_to_solve); } else { //turning back move_success = walker.move_south(m_maze_to_solve); } if (!move_success) { return(null); //something went wrong somewhere } else { walker = turncorner(walker, walker.Dir); } } else if (walker.Dir == Direction.South) { bool move_success = false; if (is_southvalid(walker.X, walker.Y)) { move_success = walker.move_south(m_maze_to_solve); } else if (is_westvalid(walker.X, walker.Y)) { move_success = walker.move_west(m_maze_to_solve); } else if (is_eastvalid(walker.X, walker.Y)) { move_success = walker.move_east(m_maze_to_solve); } else { //turn around move_success = walker.move_north(m_maze_to_solve); } if (!move_success) { return(null); } else { walker = turncorner(walker, walker.Dir); } } else if (walker.Dir == Direction.East) { bool move_success = false; if (is_eastvalid(walker.X, walker.Y)) { move_success = walker.move_east(m_maze_to_solve); } else if (is_southvalid(walker.X, walker.Y)) { move_success = walker.move_south(m_maze_to_solve); } else if (is_northvalid(walker.X, walker.Y)) { move_success = walker.move_north(m_maze_to_solve); } else { //turn around move_success = walker.move_west(m_maze_to_solve); } if (!move_success) { return(null); } else { walker = turncorner(walker, walker.Dir); } } else if (walker.Dir == Direction.West) { bool move_success = false; if (is_westvalid(walker.X, walker.Y)) { move_success = walker.move_west(m_maze_to_solve); } else if (is_northvalid(walker.X, walker.Y)) { move_success = walker.move_north(m_maze_to_solve); } else if (is_southvalid(walker.X, walker.Y)) { move_success = walker.move_south(m_maze_to_solve); } else { //turn around move_success = walker.move_east(m_maze_to_solve); } if (!move_success) { return(null); } else { walker = turncorner(walker, walker.Dir); } } else { //this should never be executed, but in case something goes wrong here, we will arbitrarily place the walker into a direction walker.Dir = Direction.North; } MazeParser.color(m_maze_to_solve, walker); pathFound = is_finish(walker); MazeParser.color(m_maze_to_solve, walker); }//end while //MazeParser.output_parse(m_maze_to_solve); //for debugging return(m_maze_to_solve); }