MouseMovement nextMovement(unsigned x, unsigned y, const Maze &maze) { const bool frontWall = maze.wallInFront(); const bool leftWall = maze.wallOnLeft(); // Pause at each cell if the user requests it. // It allows for better viewing on command line. if(pause) { std::cout << "Hit enter to continue..., (" << x << "," << y << "), M=" << getManDistance(x,y) << std::endl; std::cin.ignore(10000, '\n'); std::cin.clear(); } std::cout << maze.draw(5) << std::endl << std::endl; // If we somehow miraculously hit the center // of the maze, just terminate and celebrate! if(isAtCenter(x, y)) { std::cout << "Found center! Good enough for the demo, won't try to get back." << std::endl; return Finish; } // If we hit the start of the maze a second time, then // we couldn't find the center and never will... if(x == 0 && y == 0) { if(visitedStart) { std::cout << "Unable to find center, giving up." << std::endl; return Finish; } else { visitedStart = true; } } // If we have just turned left, we should take that path! if(!frontWall && shouldGoForward) { shouldGoForward = false; return MoveForward; } // As long as nothing is in front and we have // a wall to our left, keep going forward! if(!frontWall && leftWall) { shouldGoForward = false; return MoveForward; } // If our forward and left paths are blocked // we should try going to the right! if(frontWall && leftWall) { shouldGoForward = false; return TurnClockwise; } // Lastly, if there is no left wall we should take that path! if(!leftWall) { shouldGoForward = true; return TurnCounterClockwise; } // If we get stuck somehow, just terminate. std::cout << "Got stuck..." << std::endl; return Finish; }
MouseMovement nextMovement(unsigned x, unsigned y, const Maze& maze) { coord block; block.x = x; block.y = y; //todo: CREATE IR (so we can tell if there's walls in front or not) bool front = maze.wallInFront(); bool left = maze.wallOnLeft(); bool right = maze.wallOnRight(); //todo: reverse the goal and reset the values. if(isAtCenter(x, y)) return Finish; switch(heading) { case NORTH: { if(front) horizontal.set(y+1,x); if(left) vertical.set(y,x); if(right) vertical.set(y,x+1); break; } case EAST: { if(front) vertical.set(y,x+1); if(left) horizontal.set(y+1,x); if(right) horizontal.set(y,x); break; } case WEST: { if(front) vertical.set(y,x); if(left) horizontal.set(y,x); if(right) horizontal.set(y+1,x); break; } case SOUTH: { if(front) horizontal.set(y,x); if(left) vertical.set(y,x+1); if(right) vertical.set(y,x); break; } case INVALID: //cry { } } if((getDistance(x,y) <= getDistance(x,y+1) || horizontal.get(y+1,x)) && (getDistance(x,y) <= getDistance(x,y-1) || horizontal.get(y,x)) && (getDistance(x,y) <= getDistance(x+1,y) || vertical.get(y,x+1)) && (getDistance(x,y) <= getDistance(x-1,y) || vertical.get(y,x)) ){ floodFillFinder(block, Distance, horizontal, vertical); } else if(front && left && right) floodFillFinder(block, Distance, horizontal, vertical); if(maze.wallInFront()){ if(left && right){ heading = opposite(heading); return TurnAround; } else if(left){ heading = clockwise(heading); return TurnClockwise; } else{ heading = counterClockwise(heading); return TurnCounterClockwise; } } if(!coords.get(y,x)){ coords.set(y,x); return MoveForward; } left = maze.wallOnLeft(); right = maze.wallOnRight(); unsigned int shortest; MouseMovement dir = MoveForward; switch(heading){ case NORTH: shortest = Distance[x][y+1]; if(!left){ if(shortest > Distance[x-1][y]){ shortest = Distance[x-1][y]; heading = counterClockwise(heading); dir = TurnCounterClockwise; } } if(!right){ if(shortest > Distance[x+1][y]){ shortest = Distance[x+1][y]; heading = clockwise(heading); dir = TurnClockwise; } } break; case EAST: shortest = Distance[x+1][y]; if(!left){ if(shortest > Distance[x][y+1]){ shortest = Distance[x][y+1]; heading = counterClockwise(heading); dir = TurnCounterClockwise; } } if(!right){ if(shortest > Distance[x][y-1]){ shortest = Distance[x][y-1]; heading = clockwise(heading); dir = TurnClockwise; } } break; case WEST: shortest = Distance[x-1][y]; if(!left){ if(shortest > Distance[x][y-1]){ shortest = Distance[x][y-1]; heading = counterClockwise(heading); dir = TurnCounterClockwise; } } if(!right){ if(shortest > Distance[x][y+1]){ shortest = Distance[x][y+1]; heading = clockwise(heading); dir = TurnClockwise; } } break; case SOUTH: shortest = Distance[x][y-1]; if(!left){ if(shortest > Distance[x+1][y]){ shortest = Distance[x+1][y]; heading = counterClockwise(heading); dir = TurnCounterClockwise; } } if(!right){ if(shortest > Distance[x-1][y]){ shortest = Distance[x-1][y]; heading = clockwise(heading); dir = TurnClockwise; } } break; } return dir; }