A simple Game of Life implementation in Java
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

32 lignes
1.1 KiB

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