📄 antworld.java
字号:
/* * This code is from the book: * * Winder, R and Roberts, G (2000) Developing Java * Software, second edition, John Wiley & Sons. * * It is copyright (c) 2000 Russel Winder and Graham Roberts. */import java.util.List ;import java.util.Iterator ;import SimFrameWork.* ;/** * AntWorld - a specialized grid representing an ant world. * * @version 2.0 March 1999 * @author Graham Roberts */class AntWorld extends Grid { /** * Create a grid specialized for the ant simulation. * * @param x width of display in patches. * @param y height of display in patches. * @param d display object to display simulation. */ public AntWorld(int x, int y, Display d) { super(x,y,new AntFactory(),d) ; } /** * Iterate through the grid of patches and output a value for * each patch, using the display object. */ public void output() { display.clear() ; for (int i = 0 ; i < sizey ; ++i) { for (int j = 0 ; j < sizex ; ++j) { // Generate a display value for each patch Patch patch = patches[i][j] ; int patchvalue = patch.value() ; List turtles = patch.getTurtles() ; int val = 0 ; if (patchvalue > 0) { val = patchvalue ; } if (turtles.size() > 0) { val = -1 ; for (Iterator iter = turtles.iterator() ; iter.hasNext() ; ) { Ant ant = (Ant)(iter.next()) ; if (ant.isLoaded()) { val = -2 ; break ; } } } // Send the coordinates and value of each patch to the // display object. display.show(j,i,val) ; } } // Tell the display object that the iteration is complete and // that it should go ahead and display the grid. display.showGrid() ; } /** * Call update for one time step on all updatable objects. For * this simulation, only the ants need to be updated. */ public void update() { for (Iterator i = turtles.iterator() ; i.hasNext() ;) { ((Turtle)i.next()).update() ; } } /** * Allocate ants at random around the grid. */ public void createTurtles() { for (int i = 0 ; i < 150 ; ++i) { int px = RandomGen.getNext(sizex) ; int py = RandomGen.getNext(sizey) ; Patch p = patches[py][px] ; Turtle t = factory.createTurtle(p) ; turtles.add(t) ; p.addTurtle(t) ; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -