All pastes #2092033 Raw Edit

Anonymous

public text v1 · immutable
#2092033 ·published 2011-10-20 22:28 UTC
rendered paste body
import uk.ac.warwick.dcs.maze.logic.IRobot;

public class DumboController
{
    
    public void controlRobot(IRobot robot) {

	int randno;
	int direction;
	char heading;
	int wallNumber;
	String directionState;
	String surroundingState;
	
	do { // Begin do-while loop
		// Select a random number for randno variable
	
		randno = (int) Math.round(Math.random()*3);

		// Convert this to a direction and set heading variable
	    
		if (randno == 0) {
			direction = IRobot.LEFT;
			heading = 'L';
		}
		else if (randno == 1) {
			direction = IRobot.RIGHT;
			heading = 'R';
		}
		else if (randno == 2) {
			direction = IRobot.BEHIND;
			heading = 'B';
		}
		else {
			direction = IRobot.AHEAD;
			heading = 'F';
		}
	} while (robot.look(direction) == IRobot.WALL) ; // Check if direction chosen will move into wall, if not, terminate loop, else continue looping
     
	// Open a switch to check the heading variable
	
	switch (heading) {
		case 'L' : directionState = "I'm going left"; break;
		case 'R' : directionState = "I'm going right"; break;
		case 'B' : directionState = "I'm going backwards"; break;
		case 'F' : directionState = "I'm going forward"; break;
		default : directionState = "I'm doing nothing"; break;
	} // Check the heading and update the string directionState to log where the robot is going to move, then break out of the switch
	
	robot.face(direction);  /* Face the robot in this direction */   
	
	// Since we now know that the direction the robot is facing in, is clear, we can ignore the possibility of a wall being there
	// so begin checking the other three directions for walls and update the wallNumber variable as appropriate
	
	wallNumber = 0; // Initialise wallNumber as 0 before rest of program runs to make sure that checking the surroundings works correctly
	
	if (robot.look(IRobot.LEFT) == IRobot.WALL) { // Check left of the robot
		wallNumber = wallNumber + 1; // Add 1 to wallNumber if there is a wall to the left
	}
	
	if (robot.look(IRobot.RIGHT) == IRobot.WALL) { // Check right of the robot
		wallNumber = wallNumber +1; // Add 1 to wallNumber if there is a wall to the right
	}
	
	if (robot.look(IRobot.BEHIND) == IRobot.WALL) { // Check behind the robot
		wallNumber = wallNumber +1; // Add 1 to wallNumber if there is a wall behind
	}
	
	// Open a switch to check the wallNumber variable
	
	switch (wallNumber) {
		case 0 : surroundingState = "at a crossroads"; break;
		case 1 : surroundingState = "at a junction"; break;
		case 2 : surroundingState = "down a corridor"; break;
		case 3 : surroundingState = "at a deadend"; break;
		default : surroundingState = "and I don't know where I am"; break;
	} // Check the number of walls surrounding, and enter into the string surroundingState, the state of the robot's surroundings
	
	System.out.println(directionState + " " + surroundingState); // Print how the robot is moving and what the state of it's surroundings is
	wallNumber = 0; // Reset the variable wallNumber for the next run
	
	robot.advance();        /* and move the robot */
    }

}