⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grid.java

📁 一个TSP问题的图形演示程序
💻 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. */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 is dependent on abstract methods that must be *  overridden by subclasses to plug in the specific behaviour for a *  particular simulation. * *  @version 2.0 March 1999 *  @author Graham Roberts */public abstract class Grid {  /**   * Create a grid of given size using patches   * supplied by a factory object.   *   * @param x width of grid in patches   * @param y height of grid in patches   * @param f factory object to be used to create patches   * @param d display object used to display 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] ;    // Create all the patches    for (int i = 0 ; i < sizey ; ++i) {       for (int j = 0 ; j < sizex ; ++j) {         // Use the factory to create patches without         // having to specify the concrete patch type         // used.         patches[i][j] = factory.createPatch() ;       }    }    // Connect all the patches together.    linkPatches() ;    // Now add turtles to grid.    createTurtles() ;  }  /**   *  Move the simulated world through 100 time 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() ;    }    // When the update is completed, redisplay the grid.    output() ;  }  /**   *  Return number of timesteps completed.   */  public int getTimeCount() {    return count ;  }  /**   *  Output grid representation to display.  Must be   *  overridden as this class has no knowledge of how grids are   *  actually displayed.   */  public abstract void output() ;  /**   *  Update grid one time step.   */  public abstract void update() ;  /**   *  Create 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]) ;          }        }      }    }  }  /**   *  Width of grid in patches.   */  protected int sizex ;  /**   *  Height of grid in patches.   */  protected int sizey ;  /**   * Count of the number of time steps.   */  protected int count = 0 ;  /**   *  Factory to be used to create turtles and patches.   */  protected Factory factory ;  /**   *  2D array of patches in the grid.  Using an array as once   *  connected, patches remain fixed in position.   */  protected Patch[][] patches ;  /**   *  Collection of all turtles on the grid.   */  protected List turtles ;  /**   *  Display object used to render grid on.   */  protected Display display ;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -