📄 grid.java
字号:
package SimFrameWork;
import java.util.List;
import java.util.ArrayList;
/**
*Abstract Grid class representing a generic world.This class is
*written so that it does not depend on any particular Simulation;
*Note that the class denpendent on abstract nethods that must be
*overridden by subclasses to plug in the specific behaviour for a
*particular simulation.
*/
public abstract class Grid{
public Grid(int x,int y,Factory f,Display d){
sizex=x;
sizey=y;
factory=f;
display=d;
turtles=new ArrayList();
patches=new Patch[y][x];
for(int i=0;i<sizey;++i){
for(int j=0;j<sizex;++j){
patches[i][j]=factory.createPatch();
}
}
linkPatches();
createTurtles();
}
/**
*Move the simulated world through 100 times steps,to allow the
*simulation to move forward at a reasonable speed. At some
*point this should be modified to allow the number of steps to
*be changed by the users.
*/
public final void step(){
for(int i=0;i<100;++i){
count++;
update();
}
output();
}
/**
*Return number of timesteps conpleted.
*/
public int getTimeCount(){
return count;
}
/**
*Output grid representation to display. Must be
*overridden as this class has no knowledge fo how grids are
*actually displayed.
*/
public abstract void output();
/**
*Update grid one time step.
*/
public abstract void update();
/**
*Creat turtles and add to patches on grid. This version
*completely delegates to a subclass.
*/
public abstract void createTurtles();
/**
*Link patches to their neighbours.
*/
private void linkPatches(){
for(int i=0;i<sizey;++i){
for(int j=0;j<sizex;++j){
Patch patch=patches[i][j];
if(i>0){
patch.setNeighbour(Patch.S,patches[i-1][j]);
if(j>0){
patch.setNeighbour(Patch.SW,patches[i-1][j-1]);
}
if(j<(sizex-1)){
patch.setNeighbour(Patch.SE,patches[i-1][j+1]);
}
}
if(j>0){
patch.setNeighbour(Patch.W,patches[i][j-1]);
}
if(j<(sizex-1)){
patch.setNeighbour(Patch.E,patches[i][j+1]);
}
if(i<(sizey-1)){
patch.setNeighbour(Patch.N,patches[i+1][j]);
if(j>0){
patch.setNeighbour(Patch.NW,patches[i+1][j-1]);
}
if(j<(sizex-1)){
patch.setNeighbour(Patch.NE,patches[i+1][j+1]);
}
}
}
}
}
protected int sizex;
protected int sizey;
protected int count=0;
protected Factory factory;
protected Patch[][] patches;
protected List turtles;
protected Display display;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -