// Nibbler agent demonstrates how to implement behaviour, check and alter an environment, and communicate with other agents using method calls. // The nibbler will move around randomly and nibbles down any Environmental data. // Note that the spatial x and y details are inherited from 'Animal'. public class Nibbler extends Animal { // Constructor. Sets a random x and y based on current world size, but // otherwise defers to Animal. public Nibbler (Environment worldIn) { super(worldIn); x = (int)(Math.random() * world.getWidth()); y = (int)(Math.random() * world.getHeight()); } public void run () { move(); interactWithEnvironment(); } private void move() { int x = 0; do { x = this.x; double randomNumber = Math.random(); if (randomNumber < 0.33) x--; if (randomNumber > 0.66) x++; } while ((x < 0) || (x > world.getWidth() - 1)); this.x = x; int y = 0; do { y = this.y; double randomNumber = Math.random(); if (randomNumber < 0.33) y--; if (randomNumber > 0.66) y++; } while ((y < 0) || (y > world.getHeight() - 1)); this.y = y; } private void interactWithEnvironment () { if (world.getDataValue(x, y) > 10) { world.setDataValue(x, y, world.getDataValue(x, y) - 10); } else { world.setDataValue(x, y, 0); } } }