示例#1
0
 public static void main(String[] args) {
   ActorWorld world = new ActorWorld();
   ZBug alice = new ZBug(6);
   alice.setColor(Color.ORANGE);
   ZBug bob = new ZBug(3);
   // world.add(new Location(7, 8), alice);
   // world.add(new Location(5, 5), bob);
   bob.putSelfInGrid(world.getGrid(), new Location(0, 0));
   bob.removeSelfFromGrid();
   // bob.removeSelfFromGrid();
   bob.putSelfInGrid(world.getGrid(), new Location(0, 2));
   world.show();
 }
  /**
   * 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();
  }
  /**
   * Creates the actors and inserts them into their initial starting positions in the grid
   *
   * @pre the grid has been created
   * @post all actors that comprise the initial state of the game have been added to the grid
   */
  private void populateGame(boolean test) {
    // constants for the location of the three cells initially alive

    // the grid of Actors that maintains the state of the game
    //  (alive cells contains actors; dead cells do not)
    Grid<Actor> grid = world.getGrid();

    Random rand = new Random();

    int n; // Nunmber variable for random number

    // Runs for regular program
    if (test == false) {
      // Randomly places an actor at coordinates (a, i)
      for (int i = 0; i <= 1000; i++) {
        for (int a = 0; a <= 1000; a++) {
          n = rand.nextInt(10);
          // Randomly chooses where an actor is initially placed on a grid
          if (n == 2 || n == 5 || n == 7 || n == 9 || n == 1) {
            Actor rock1 = new Actor();
            Location loc1 = new Location(i, a);
            grid.put(loc1, rock1);
          }
        }
      }
    }

    // Tested version (Works if this is placed as code for populateGame)
    if (test == true) {
      for (int i = 0; i <= 4; i++) {
        for (int a = 0; a <= 4; a++) {
          if (i == 0 && a == 0
              || i == 0 && a == 4
              || i == 1 && a == 1
              || i == 1 && a == 3
              || i == 2 && a == 2
              || i == 3 && a == 1
              || i == 3 && a == 3
              || i == 4 && a == 0
              || i == 4 && a == 4) {
            Actor rock1 = new Actor();
            Location loc1 = new Location(i, a);
            grid.put(loc1, rock1);
          }
        }
      }
    }
  }
  /**
   * Creates the actors and inserts them into their initial starting positions in the grid
   *
   * @pre the grid has been created
   * @post all actors that comprise the initial state of the game have been added to the grid
   */
  private void populateGame() {
    // constants for the location of the three cells initially alive
    final int X1 = 0, Y1 = 4;
    final int X2 = 1, Y2 = 3;
    final int X3 = 1, Y3 = 5;
    final int X4 = 2, Y4 = 2;
    final int X5 = 2, Y5 = 6;
    final int X6 = 3, Y6 = 1;
    final int X7 = 3, Y7 = 3;
    final int X8 = 3, Y8 = 5;
    final int X9 = 3, Y9 = 7;
    final int X10 = 4, Y10 = 0;
    final int X11 = 4, Y11 = 4;
    final int X12 = 4, Y12 = 8;
    final int X13 = 5, Y13 = 1;
    final int X14 = 5, Y14 = 3;
    final int X15 = 5, Y15 = 5;
    final int X16 = 5, Y16 = 7;
    final int X17 = 6, Y17 = 2;
    final int X18 = 6, Y18 = 6;
    final int X19 = 7, Y19 = 3;
    final int X20 = 7, Y20 = 5;
    final int X21 = 8, Y21 = 4;

    // the grid of Actors that maintains the state of the game
    //  (alive cells contains actors; dead cells do not)
    Grid<Actor> grid = world.getGrid();

    // create and add rocks (a type of Actor) to the three intial locations
    Rock rock1 = new Rock();
    Location loc1 = new Location(Y1, X1);
    grid.put(loc1, rock1);

    Rock rock2 = new Rock();
    Location loc2 = new Location(Y2, X2);
    grid.put(loc2, rock2);

    Rock rock3 = new Rock();
    Location loc3 = new Location(Y3, X3);
    grid.put(loc3, rock3);

    Rock rock4 = new Rock();
    Location loc4 = new Location(Y4, X4);
    grid.put(loc4, rock4);

    Rock rock5 = new Rock();
    Location loc5 = new Location(Y5, X5);
    grid.put(loc5, rock5);

    Rock rock6 = new Rock();
    Location loc6 = new Location(Y6, X6);
    grid.put(loc6, rock6);

    Rock rock7 = new Rock();
    Location loc7 = new Location(Y7, X7);
    grid.put(loc7, rock7);

    Rock rock8 = new Rock();
    Location loc8 = new Location(Y8, X8);
    grid.put(loc8, rock8);

    Rock rock9 = new Rock();
    Location loc9 = new Location(Y9, X9);
    grid.put(loc9, rock9);

    Rock rock10 = new Rock();
    Location loc10 = new Location(Y10, X10);
    grid.put(loc10, rock10);

    Rock rock11 = new Rock();
    Location loc11 = new Location(Y11, X11);
    grid.put(loc11, rock11);

    Rock rock12 = new Rock();
    Location loc12 = new Location(Y12, X12);
    grid.put(loc12, rock12);

    Rock rock13 = new Rock();
    Location loc13 = new Location(Y13, X13);
    grid.put(loc13, rock13);

    Rock rock14 = new Rock();
    Location loc14 = new Location(Y14, X14);
    grid.put(loc14, rock14);

    Rock rock15 = new Rock();
    Location loc15 = new Location(Y15, X15);
    grid.put(loc15, rock15);

    Rock rock16 = new Rock();
    Location loc16 = new Location(Y16, X16);
    grid.put(loc16, rock16);

    Rock rock17 = new Rock();
    Location loc17 = new Location(Y17, X17);
    grid.put(loc17, rock17);

    Rock rock18 = new Rock();
    Location loc18 = new Location(Y18, X18);
    grid.put(loc18, rock18);

    Rock rock19 = new Rock();
    Location loc19 = new Location(Y19, X19);
    grid.put(loc19, rock19);

    Rock rock20 = new Rock();
    Location loc20 = new Location(Y20, X20);
    grid.put(loc20, rock20);

    Rock rock21 = new Rock();
    Location loc21 = new Location(Y21, X21);
    grid.put(loc21, rock21);
  }
 /**
  * Returns the actor at the specified row and column. Intended to be used for unit testing.
  *
  * @param row the row (zero-based index) of the actor to return
  * @param col the column (zero-based index) of the actor to return
  * @pre the grid has been created
  * @return the actor at the specified row and column
  */
 public Actor getActor(int row, int col) {
   Location loc = new Location(row, col);
   Actor actor = world.getGrid().get(loc);
   return actor;
 }
  /**
   * 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);
  }