/* * Lab 05. * * Created on 22 March 2007, 11:54 * Modified on 5 April 2011 * * @author Chuong Cong Vo * vocongchuong@yahoo.com */ import java.util.Random; agent VacuumAgent extends Agent implements GenericVacuum{ #handles event VacuumEvent; #uses plan VacuumPlan; #posts event VacuumEvent ev; private static final byte NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3; private int x; private int y; private int direction; private boolean[][] room; public VacuumAgent(String name, boolean[][] _room){ super(name); this.room = _room; displayRoom(); this.x = this.y = 0; //set the initial possition for the Vacuum this.direction = EAST; //set the initial direction for the Vacuum } private void displayRoom() { for (byte i = 0; i < 3; i++) { for (byte j = 0; j < 3; j++) { if (isDirt(i, j)) { System.out.print("(" + i + "," + j + ") D) "); // "D" means dirt } else { System.out.print("(" + i + "," + j + ") _) "); // "_" means no dirt } } System.out.println(); } } public void startAgent(){// this inherited method will be called automatically once an instance of this agent is created. So, you don't need to explicitly call this method System.out.println("The agent starts..."); super.startAgent(); postEvent(ev.nextEvent()); } public boolean isIn(int _x, int _y){ return (this.x == _x && this.y == _y); } public boolean isFacing(int d){ return (this.direction == d); } public boolean isDirt(int _x, int _y){ return this.room[_x][_y]; } public void doTurn(){ this.direction = (this.direction + 1) % 4; //modulo System.out.println("Turn"); } public void doForward(){ if (this.isFacing(NORTH)) { this.x--; } else if (this.isFacing(EAST)) { this.y++; } else if (this.isFacing(SOUTH)) { this.x++; } else { // that is, facing WEST this.y--; } System.out.println("Forward To(" + this.x + "," + this.y + ")"); } public void doSuck(){ room[this.x][this.y] = false; System.out.println("Suck(" + this.x + "," + this.y + ")"); } public int getX(){ return this.x; } public int getY(){ return this.y; } public int getDirection(){ return this.direction; } public void die(){ System.out.print("The agent is going to die... "); this.finish(); // the "finish" method is from the Agent super class System.out.println("The agent died"); System.exit(0); } public static void main(String[] args){ boolean[][] room = new boolean[3][3]; // true = dirt; false = no dirt // generating the room with some dirt for (byte i = 0; i < 3; i++) { for (byte j = 0; j < 3; j++) { room[i][j] = new Random().nextBoolean(); // random dirt in a cell } } VacuumAgent vacuumAgent = new VacuumAgent("My Vacuum Agent", room); } }