001package agent.three; 002import agent.Agent; 003import agent.TimeServer; 004import agent.TimeServerLinked; 005import java.util.Observer; 006 007public class Main { 008 public static void main (String[] args) { 009 World w = World.instance; 010 Agent a = new Tiger(); 011 w.enqueue(0,a); 012 w.set(0,0,a); 013 w.run(100); 014 } 015} 016 017class Util { 018 private Util() {} 019 private final static long SEED = 2497; 020 private final static java.util.Random r = new java.util.Random(SEED); 021 static int random(int n) { return r.nextInt(n); } 022 static boolean randomBoolean() { return r.nextBoolean(); } 023} 024 025interface World extends TimeServer { 026 public static final World instance = new WorldObj(); 027 public int maxx(); 028 public int maxy(); 029 public Object get(int i, int j); 030 public void set(int i, int j, Object a); 031} 032 033class WorldObj implements World { 034 private final static int MAXX = 100; 035 private final static int MAXY = 100; 036 private TimeServer time = new TimeServerLinked(); 037 private Object[][] space = new Object[MAXX][MAXY]; 038 039 public int maxx() { return MAXX; } 040 public int maxy() { return MAXY; } 041 public Object get(int x, int y) { return space[x%MAXX][y%MAXY]; } 042 public void set(int x, int y, Object a){ space[x%MAXX][y%MAXY] = a; } 043 public double currentTime() { return time.currentTime(); } 044 public void enqueue(double t, Agent a) { time.enqueue(t,a); } 045 public void run(double d) { time.run(d); } 046 public void addObserver(Observer o) { time.addObserver(o); } 047 public void deleteObserver(Observer o) { time.deleteObserver(o); } 048} 049 050/* BUGS HERE */ 051class Tiger implements Agent { 052 private int x; 053 private int y; 054 private World w = World.instance; 055 private void moveRandom() { 056 w.set(x,y,null); 057 if (Util.randomBoolean()) { x = (x+1)%w.maxx(); } 058 if (Util.randomBoolean()) { y = (y+1)%w.maxy(); } 059 w.set(x,y,this); 060 } 061 public void run() { 062 moveRandom(); 063 System.out.println("It's " + w.currentTime() + " and I'm alive at (" + x + "," + y + ")!"); 064 w.enqueue(10+w.currentTime(), this); 065 } 066 067}