A simple Game of Life implementation in Java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
881 B

  1. package edu.stuy.goldfish.rules;
  2. import edu.stuy.goldfish.Grid;
  3. import edu.stuy.goldfish.Patch;
  4. // Conway, without dying cells
  5. public class LifeWithoutDeath extends RuleSet {
  6. public static int states = 2;
  7. public static Grid run (Grid g) {
  8. Grid newGrid = new Grid(g.getWidth(), g.getHeight());
  9. for (int i = 0; i < g.getWidth(); i++) {
  10. for (int j = 0; j < g.getHeight(); j++) {
  11. Patch[] neighbors = g.getPatch(i, j).get8Neighbors();
  12. int numAlive = 0;
  13. for (Patch neighbor : neighbors)
  14. if (neighbor.getState() == 1) numAlive++;
  15. Patch p = g.getPatch(i,j).clone(newGrid);
  16. if (numAlive == 3)
  17. p.setState(1); //Born with 3 neighbors.
  18. newGrid.setPatch(i,j,p);
  19. }
  20. }
  21. return newGrid;
  22. }
  23. }