Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import uk.ac.warwick.dcs.maze.logic.IRobot;
- import java.util.Stack;
- public class Explorer2 {
- private RobotData2 robotData;
- private int pollRun = 0;
- private int explorerMode; //1 = explore, 0 = backtrack
- private Stack<RobotData2> junctionRecorder = new Stack<RobotData2>();
- public void controlRobot(IRobot robot) {
- //If the robot is surrounded by paths it has been to before it should backtrack
- if (beenBeforeExits(robot) == nonWallExits(robot))
- explorerMode = 0;
- //If it can go down a new path set the robot to explore
- else
- explorerMode = 1;
- if (junctionRecorder.isEmpty() == false)
- //System.out.println(robotData.getX(junctionRecorder.peek()) + ", " + robotData.getY(junctionRecorder.peek()));
- System.out.println(junctionRecorder);
- if (explorerMode == 1)
- exploreControl(robot);
- else
- backtrackControl(robot);
- }
- private void exploreControl(IRobot robot) {
- int exits = nonWallExits(robot);
- int direction;
- //Read how many exits there are and pick the corresponding method depending on how many there are
- switch (exits) {
- case 1:
- direction = deadEnd(robot);
- break;
- case 2:
- direction = twoWalls(robot);
- break;
- default:
- direction = junction(robot);
- //Record the junction the robot is at an the direction it has come from
- if (beenBeforeExits(robot) == 1)
- robotData = new RobotData2(robot.getLocation().x, robot.getLocation().y, robot.getHeading());
- junctionRecorder.push(robotData);
- break;
- }
- robot.face(direction);
- }
- private void backtrackControl(IRobot robot) {
- int exits = nonWallExits(robot);
- int direction;
- //Read how many exits there are and pick the corresponding method depending on ho many there are
- switch (exits) {
- case 1:
- direction = deadEnd(robot);
- break;
- case 2:
- direction = twoWalls(robot);
- break;
- default:
- //The robot should go back down the path it originally arrived from and remove the junction from the memory
- robot.setHeading(searchJunction(robot));
- RobotData2 junc = junctionRecorder.pop();
- direction = IRobot.BEHIND;
- break;
- }
- robot.face(direction);
- }
- private int searchJunction(IRobot robot) {
- //Return the heading that the robot had when it arrived at the junction
- return robotData.getHeading(junctionRecorder.peek());
- }
- //Method for calculating how many exits there are around the robot
- private int nonWallExits(IRobot robot) {
- int exits = 0;
- //Read how many exits there are around the robot and add them to the variable 'exits'
- if (robot.look(robot.AHEAD) != IRobot.WALL)
- exits = exits + 1;
- if (robot.look(robot.BEHIND) != IRobot.WALL)
- exits = exits + 1;
- if (robot.look(robot.LEFT) != IRobot.WALL)
- exits = exits + 1;
- if (robot.look(robot.RIGHT) != IRobot.WALL)
- exits = exits + 1;
- return exits;
- }
- //Method for calculating how many passages are around the robot
- private int passageExits(IRobot robot) {
- int passages = 0;
- //Read how many passage exits there are around the robot and add them to the variable 'passages'
- if (robot.look(robot.AHEAD) == IRobot.PASSAGE)
- passages = passages + 1;
- if (robot.look(robot.BEHIND) == IRobot.PASSAGE)
- passages = passages + 1;
- if (robot.look(robot.LEFT) == IRobot.PASSAGE)
- passages = passages + 1;
- if (robot.look(robot.RIGHT) == IRobot.PASSAGE)
- passages = passages + 1;
- return passages;
- }
- //Method for calculating how many exits are around the robot that have already been explored
- private int beenBeforeExits(IRobot robot) {
- int been = 0;
- //Read how many been before exits there are around the robot and add them to the variable 'been'
- if (robot.look(robot.AHEAD) == IRobot.BEENBEFORE)
- been = been + 1;
- if (robot.look(robot.BEHIND) == IRobot.BEENBEFORE)
- been = been + 1;
- if (robot.look(robot.LEFT) == IRobot.BEENBEFORE)
- been = been + 1;
- if (robot.look(robot.RIGHT) == IRobot.BEENBEFORE)
- been = been + 1;
- return been;
- }
- //Method for picking a direction at a dead end
- private int deadEnd(IRobot robot) {
- int direction;
- //Pick the only direction the robot can go
- direction = randomDirection(robot);
- return direction;
- }
- //Method for picking a direction in a corridor or at a corner
- private int twoWalls(IRobot robot) {
- int direction;
- //If all paths have previously been explored, pick a random direction
- if (passageExits(robot) == 0)
- direction = randomDirection(robot);
- //If there is a passage available, go down the passage'
- else {
- do {
- direction = randomDirection(robot);
- } while(robot.look(direction) == IRobot.BEENBEFORE);
- }
- return direction;
- }
- //Method for picking a direction at a junction and crossroads
- private int junction(IRobot robot) {
- int direction;
- //If all paths have previously been explored, pick a random direction
- if (passageExits(robot) == 0)
- direction = randomDirection(robot);
- //If there are 1 or more passages available, pick randomly between the available passages
- else {
- do {
- direction = randomDirection(robot);
- } while(robot.look(direction) == IRobot.BEENBEFORE);
- }
- return direction;
- }
- //Method for picking a direction at random with equal probability
- private int randomDirection(IRobot robot) {
- int direction;
- double randno;
- do {
- // Select a random number with equal probability
- randno = (Math.random()*4);
- if (randno >= 0 && randno <= 1)
- direction = IRobot.LEFT;
- else if (randno > 1 && randno <= 2)
- direction = IRobot.RIGHT;
- //Only pick the direction behind if the robot is at a dead end
- else if (randno > 2 && randno <= 3 && nonWallExits(robot) == 1)
- direction = IRobot.BEHIND;
- else
- direction = IRobot.AHEAD;
- } while(robot.look(direction) == IRobot.WALL); //Keep repeating until a direction is found that doesn't lead to a wall
- return direction;
- }
- }
- class RobotData2 {
- int x;
- int y;
- int heading;
- public RobotData2(int x, int y, int heading) {
- this.x = x;
- this.y = y;
- this.heading = heading;
- }
- //Gets the x coordinate of the object
- public int getX(RobotData2 junction) {
- return junction.x;
- }
- //Gets the y coordinate of the object
- public int getY(RobotData2 junction) {
- return junction.y;
- }
- //Gets the heading of the object
- public int getHeading(RobotData2 junction) {
- return junction.heading;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement