A simple Game of Life implementation in Java
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

25 行
767 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(), false);
  9. for (int i = 0; i < g.getWidth(); i++) {
  10. for (int j = 0; j < g.getHeight(); j++) {
  11. Patch orig = g.getPatch(i, j);
  12. int numAlive = orig.get8Neighbors(1, 4);
  13. Patch p = orig.clone(newGrid);
  14. if (numAlive == 3)
  15. p.setState(1); // Born with 3 neighbors
  16. newGrid.setPatch(i, j, p);
  17. }
  18. }
  19. return newGrid;
  20. }
  21. }