コード例 #1
0
ファイル: SparseGridRunner.java プロジェクト: daix6/GridWorld
 public static void main(String[] args) {
   ActorWorld world = new ActorWorld();
   world.addGridClass("SparseBoundedGrid");
   world.addGridClass("SparseBoundedGrid1");
   world.addGridClass("SparseBoundedGrid2");
   world.addGridClass("SparseBoundedGrid3");
   world.addGridClass("UnboundedGrid2");
   world.setGrid(new SparseBoundedGrid3<Actor>(10, 10));
   world.add(new Location(5, 6), new Bug());
   world.add(new Location(4, 3), new Critter());
   world.show();
 }
コード例 #2
0
  /**
   * Generates the next generation based on the rules of the Game of Life and updates the grid
   * associated with the world
   *
   * @pre the game has been initialized
   * @post the world has been populated with a new grid containing the next generation
   */
  public void createNextGeneration() {
    /**
     * You will need to read the documentation for the World, Grid, and Location classes in order to
     * implement the Game of Life algorithm and leverage the GridWorld framework.
     */

    // create the grid, of the specified size, that contains Actors
    Grid<Actor> grid = world.getGrid();

    // insert magic here...
    BoundedGrid<Actor> newgrid = new BoundedGrid<Actor>(ROWS, COLS);

    int col = getNumCols();
    int row = getNumRows();
    for (int column = 0; column < col; column++) {
      for (int rows = 0; rows < row; rows++) {
        Actor cell = getActor(rows, column);
        Location location = new Location(rows, column);
        if (cell != null) {
          ArrayList<Actor> Neighbors = grid.getNeighbors(location);
          int Neighborscount = Neighbors.size();
          if (Neighborscount == 3) {
            // lives
            Rock newrock1 = new Rock();
            Location newloc1 = new Location(rows, column);
            newgrid.put(newloc1, newrock1);
          } else if (Neighborscount == 2) {
            // lives
            Rock newrock1 = new Rock();
            Location newloc1 = new Location(rows, column);
            newgrid.put(newloc1, newrock1);
          } else if (Neighborscount < 3) {
            // dies
          } else if (Neighborscount > 3) {
            // dies
          }
        } else if (cell == null) {
          ArrayList<Actor> Neighbors = grid.getNeighbors(location);
          int Neighborscount = Neighbors.size();
          if (Neighborscount == 3) {
            // reborn
            Rock newrock1 = new Rock();
            Location newloc1 = new Location(rows, column);
            newgrid.put(newloc1, newrock1);
          }
        }
      }
    }
    world.setGrid(newgrid);
    world.show();
  }
コード例 #3
0
  /**
   * Generates the next generation based on the rules of the Game of Life and updates the grid
   * associated with the world
   *
   * @pre the game has been initialized
   * @post the world has been populated with a new grid containing the next generation
   */
  public void createNextGeneration() {
    /**
     * You will need to read the documentation for the World, Grid, and Location classes in order to
     * implement the Game of Life algorithm and leverage the GridWorld framework.
     */

    // create the grid, of the specified size, that contains Actors

    Grid<Actor> grid = world.getGrid();

    UnboundedGrid<Actor> grid1 = new UnboundedGrid<Actor>(); // new Grid

    Actor actor = new Actor(); // Actor objects
    int neighbor; // A number to account for the number of neighbors each actor has
    /**
     * A for loop for determining the number of neighbors each actor has, and then it decides
     * whether the actor lives or dies, or if a dead actor comes to life, and it puts the new lay
     * out on a new grid that replaces the previous grid after the for loop ends
     */
    // For loop goes through each grid space with the for loop
    for (int i = 0; i <= 1000; i++) {
      for (int a = 0; a <= 1000; a++) {
        neighbor = 0;
        // if-else checks if there is an alive actor at coordinates (a,i)
        if (getActor(i, a) != null) {
          if (getActor(i + 1, a) != null) {
            neighbor += 1;
          }

          if (getActor(i, a + 1) != null) {
            neighbor += 1;
          }
          if (getActor(i, a - 1) != null) {
            neighbor += 1;
          }
          if (getActor(i + 1, a + 1) != null) {
            neighbor += 1;
          }
          if (getActor(i + 1, a - 1) != null) {
            neighbor += 1;
          }
          if (getActor(i - 1, a + 1) != null) {
            neighbor += 1;
          }
          if (getActor(i - 1, a - 1) != null) {
            neighbor += 1;
          }

          if (getActor(i - 1, a) != null) {
            neighbor += 1;
          }

          // if an actor has 2 or 3 neighbors, it comes to life
          if (neighbor == 2 || neighbor == 3) {
            Location loc = new Location(i, a);
            grid1.put(loc, actor);
          }
        } else {
          if (getActor(i + 1, a) != null) {
            neighbor += 1;
          }
          if (getActor(i - 1, a) != null) {
            neighbor += 1;
          }
          if (getActor(i, a + 1) != null) {
            neighbor += 1;
          }
          if (getActor(i, a - 1) != null) {
            neighbor += 1;
          }
          if (getActor(i + 1, a + 1) != null) {
            neighbor += 1;
          }
          if (getActor(i + 1, a - 1) != null) {
            neighbor += 1;
          }
          if (getActor(i - 1, a + 1) != null) {
            neighbor += 1;
          }
          if (getActor(i - 1, a - 1) != null) {
            neighbor += 1;
          }

          // If a dead actor has 3 neighbors, it comes to life
          if (neighbor == 3) {
            Location loc2 = new Location(i, a);
            grid1.put(loc2, actor);
          }
        }
      }
    }
    world.setGrid(grid1);
  }