コード例 #1
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();
  }