AvoidBorder.java
package prey;
 
import java.util.Random;
 
public class AvoidBorder extends Behavior {
    private Random rand = new Random();
 
    public AvoidBorder(String name, int priority) {
        // Calls the super constructor. NOTE: selfInterruptable=true. 
        super(name, priority, true);
    }
 
    @Override
    public int getMotivation() {
        // Gets and saves the color id from the Sensors class. 
        int colorID = Sensors.getColourSensorValue();
 
        if (colorID == Sensors.BORDERCOLOR || colorID == Sensors.EDGECOLOR) {
            // If the robot is on the border this behavior is more important than anything else. 
            SoundPlayer.playAvoidBorderSound(this);
            return  100;
        } else {
            // If the robot is not on the border it should be self-interruptible. 
            setSelfinterruptible(true);
            // If the robot is not on the border it returns 50 if its running (i.e. avoiding the border) or 0 otherwise. 
            return (isRunning() ? 50 : 0);
        }
 
    }
 
    @Override
    public void action() throws InterruptBehaviorException {
        /* Make sure the behavior does not interrupt itself while on the border. 
         * Otherwise the robot would newer get to the move forward part before it is interrupted by itself. */
        setSelfinterruptible(false);
 
        if (Mover.isMovingBackward()) {
            // If the robot is backing (e.g. avoiding an object) while detecting the border, it drives forward to stay in the field. 
            while (Sensors.getColourSensorValue() == Sensors.BORDERCOLOR) {
                Mover.forward(70, 70);
                delay(500);
                if (Sensors.getColourSensorValue() == Sensors.EDGECOLOR) {
                    Mover.backward(70, 70);
                    delay(1100);
                }
            }
            setSelfinterruptible(true);
            Mover.forward(70, 70);
            delay(200);
            Mover.turnInRandomdirection(70, false);
            delay(1000 + rand.nextInt(500));
        } else {
            // Backing to stay in the field. 
            while (Sensors.getColourSensorValue() == Sensors.BORDERCOLOR) {
                Mover.backward(70, 70);
                delay(10);
                if (Sensors.getColourSensorValue() == Sensors.EDGECOLOR) {
                    Mover.forward(70, 70);
                    delay(1100);
                }
            }
            setSelfinterruptible(true);
            Mover.backward(70, 70);
            delay(200);
            Mover.turnInRandomdirection(70, true);
            delay(1000 + rand.nextInt(500));
        }
 
        /* Since the maneuver to stay in the field is series of movements and the motivation depends upon 
         * if the robot is performing these maneuvers, the robot tells itself when the maneuvers are done. */ 
 
        actionDone();        
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License