'Tis an autonomous (independent, self-operation, automatic, whatevz) robot. It can so far (not very far... it's quite simple) detect when it's about to collide with a wall and evaluate which way to go. That's basically it so far. So it's already autonomous, but stupid, and slow (it spins around 360 before it actually makes a decision where to go).
I call it AutoBot. Here is the code:
(There's no turnRight, that's why turnLeft is repeated so much)
Code: Select all
import becker.robots.*;
/**
* This is an autonomous robot. He can operate on his own, though he does not exhibit free will.
*
* @Author Edutainment */
public class AutoBot extends Robot
{
/**
* Construct a new AutoBot.
*
* @param aCity city name
* @param avenue x coordinate of initial position
* @param street y coordinate of initial position
* @param aDirection initial direction: NORTH, EAST, SOUTH, or WEST
*/
public AutoBot(City aCity, int avenue, int street, Direction aDirection)
{ super(aCity, avenue, street, aDirection);
}
/**
* When AutoBot senses a wall, he will check if his left, and then right, sides are clear.
* These booleans will store than info. */
boolean right;
boolean left;
/**
* Surveys surroundings. Stores info in above booleans.
*/
public void survey(){
this.turnLeft();
if(this.frontIsClear()==true){
left = true;
} else {
left = false;
}
this.turnLeft();
this.turnLeft();
if(this.frontIsClear()==true){
right = true;
} else {
right = false;
}
this.turnLeft();
this.decide();
}
/**
* This service takes said booleans, and uses the information to decide which way to go.
*/
public void decide(){
if(right==true && left==false){
this.turnLeft();
this.turnLeft();
this.turnLeft();
} else if(left==true && right==false){
this.turnLeft();
} else {
this.turnLeft();
this.turnLeft();
}
}
/*
* This is the service called in the main program to run AutoBot autonomously.
*/
public void run(){
while(true){
if(this.frontIsClear()==true){
this.move();
} else {
this.survey();
}
}
}
}
Ideas?
BTW, if you wanna try it, you can get the Robots library at http://www.learningwithrobots.com and lots of example robots.