Jede Zelle hat 8 Nachbarzellen.
Implementierung: |
Programmcode:
// GameOfLife.java import ch.aplu.jgamegrid.*; import java.awt.Color; import java.util.ArrayList; public class GameOfLife extends GameGrid { private static final int nb = 20; // Number of cells in each direction private static final int nbCreatures = nb * nb; private static final int popSize = 200; // Size of population at start private boolean[][] pop = new boolean[nb][nb]; private Creature[] creatures = new Creature[nbCreatures]; public GameOfLife() { super(nb, nb, 25, Color.red, "sprites/snowwindow.gif"); setSimulationPeriod(1); int k = 0; // Create creature in every cell for (int x = 0; x < nb; x++) { for (int y = 0; y < nb; y++) { creatures[k] = new Creature(); Location loc = new Location(x, y); addActor(creatures[k], loc); creatures[k].hide(); k++; } } reset(); show(); } public void reset() { // All actors are dead for (int i = 0; i < nbCreatures; i++) creatures[i].isAlive = false; // Create the living population randomly for (int i = 0; i < popSize; i++) { Creature creature = (Creature)getOneActorAt(getRandomLocation()); creature.isAlive = true; } act(); } public void act() { // Show the population for (int i = 0; i < nbCreatures; i++) if (creatures[i].isAlive) creatures[i].show(); else creatures[i].hide(); } public static void main(String[] args) { new GameOfLife(); } } // class Creature extends Actor { protected boolean isAlive = false; public Creature() { super("sprites/creature.gif"); } // Every actor applies the population rules to himself public void act() { // Get number of (living) neighbours ArrayList<Actor> neighbours = getNeighbours(1); int nbNeighbours = 0; for (Actor neighbour : neighbours) if (neighbour.isVisible()) nbNeighbours++; // Generation rule: if (isVisible()) // alive { if (!(nbNeighbours == 2 || nbNeighbours == 3)) isAlive = false; // dying } else // dead { if (nbNeighbours == 3) isAlive = true; // become alive } } } |
|
Programmcode:
// GameOfLife2.java import ch.aplu.jgamegrid.*; import java.awt.Color; import java.util.ArrayList; import java.awt.*; public class GameOfLife2 extends GameGrid implements GGMouseListener { private static final int nb = 20; // Number of cells in each direction private static final int nbCreatures = nb * nb; private static final int popSize = 200; // Size of population at start private boolean[][] pop = new boolean[nb][nb]; private Creature[] creatures = new Creature[nbCreatures]; public boolean mouseEvent(GGMouse mouse) { Location location = toLocationInGrid(mouse.getX(), mouse.getY()); Creature creature = (Creature)getOneActorAt(location); if (mouse.getEvent() == GGMouse.lClick) { creature.isAlive = !creature.isVisible(); if (creature.isVisible()) creature.hide(); else creature.show(); } else // Drag { creature.isAlive = true; creature.show(); } if (!isRunning()) refresh(); return true; } public GameOfLife2() { super(nb, nb, 25, Color.red, "sprites/snowwindow.gif"); setSimulationPeriod(1); addMouseListener(this, GGMouse.lPress); int k = 0; // Create creature in every cell for (int x = 0; x < nb; x++) { for (int y = 0; y < nb; y++) { creatures[k] = new Creature(); Location loc = new Location(x, y); addActor(creatures[k], loc); creatures[k].hide(); k++; } } reset(); addMouseListener(this, GGMouse.lPress); show(); } public void reset() { // All actors are dead for (int i = 0; i < nbCreatures; i++) creatures[i].isAlive = false; act(); } public void act() { // Show the population for (int i = 0; i < nbCreatures; i++) if (creatures[i].isAlive) creatures[i].show(); else creatures[i].hide(); } public static void main(String[] args) { new GameOfLife2(); } } // ------------------class Creature ---------------------------------- class Creature extends Actor { protected boolean isAlive = false; public Creature() { super("sprites/creature.gif"); } // Every actor applies the population rules to himself public void act() { // Get number of (living) neighbours ArrayList<Actor> neighbours = getNeighbours(1); int nbNeighbours = 0; for (Actor neighbour : neighbours) if (neighbour.isVisible()) nbNeighbours++; // Generation rule: if (isVisible()) // alive { if (!(nbNeighbours == 2 || nbNeighbours == 3)) isAlive = false; // dying } else // dead { if (nbNeighbours == 3) isAlive = true; // become alive } } } |
|
Programmcode:
// GameOfLife3.java import ch.aplu.jgamegrid.*; import java.awt.Color; public class GameOfLife3 extends GameGrid { private static final int s = 80; // Number of cells in each direction private static final int z = 2000; // Size of population at start private boolean[][] a = new boolean[s][s]; public GameOfLife3() { super(s, s, 10, Color.darkGray); setTitle("Conway's Game Of Life"); reset(); show(); } public void reset() { for (int x = 0; x < s; x++) for (int y = 0; y < s; y++) a[x][y] = false; // All cells dead Location loc; for (int n = 0; n < z; n++) { loc = getRandomEmptyLocation(); a[loc.x][loc.y] = true; } showPopulation(); } private void showPopulation() { Location loc; for (int x = 0; x < s; x++) { for (int y = 0; y < s; y++) { loc = new Location(x, y); if (a[x][y]) getBg().fillCell(loc, Color.green, false); else getBg().fillCell(loc, Color.black, false); } } } private int getNumberOfNeighbours(int x, int y) { int nb = 0; for (int i = Math.max(0, x - 1); i < Math.min(s, x + 2); i++) { for (int k = Math.max(0, y - 1); k < Math.min(s, y + 2); k++) { if (!(i == x && k == y)) { if (a[i][k]) nb++; } } } return nb; } public void act() { boolean[][] b = new boolean[s][s]; for (int x = 0; x < s; x++) { for (int y = 0; y < s; y++) { int nb = getNumberOfNeighbours(x, y); if (a[x][y]) // living cell { if (nb < 2) b[x][y] = false; else if (nb > 3) b[x][y] = false; else b[x][y] = true; } else // dead cell { if (nb == 3) b[x][y] = true; else b[x][y] = false; } } } a = b; showPopulation(); } public static void main(String[] args) { new GameOfLife3(); } } |