Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Generations {
- static int anzahlNachbarn(int x, int y, int[][] world) {
- int anzahl = 0;
- int breite = world[0].length;
- int hoehe = world.length;
- anzahl
- = wertZelle(x + (breite - 1), y + (hoehe - 1), world)
- + wertZelle(x, y + (hoehe - 1), world)
- + wertZelle(x + 1, y + (hoehe -1), world)
- + wertZelle(x + 1, y, world)
- + wertZelle(x + 1, y + 1, world)
- + wertZelle(x, y + 1, world)
- + wertZelle(x + (breite - 1), y + 1, world)
- + wertZelle(x + (breite - 1), y, world);
- return anzahl;
- }
- static int wertZelle(int x, int y, int[][] world) {
- x %= world[0].length;
- y %= world.length;
- return world[y][x];
- }
- /**
- * Liefert ein Feld mit der nächsten Generation gemäß der
- * Game-of-Life-Regeln.
- *
- * @param world
- * das aktuelle Feld; 1 entspricht: Zelle ist lebenbidg; 0
- * entspricht: Zelle ist tot
- * @return ein Feld mit der nächsten Generation;1 entspricht: Zelle ist
- * lebenbidg; 0 entspricht: Zelle ist tot
- */
- static int[][] calcNextGeneration(int[][] world) {
- int[][] neueWelt = new int[world.length][world[0].length];
- for(int i = 0; i < world.length; i++) {
- for(int j = 0; j < world[0].length; j++) {
- if(wertZelle(j, i, world) == 0) {
- if(anzahlNachbarn(j, i, world) == 3)
- neueWelt[i][j] = 1;
- } else {
- int anzahl = anzahlNachbarn(j, i, world);
- if(2 > anzahl || anzahl > 3) {
- neueWelt[i][j] = 0;
- } else {
- neueWelt[i][j] = 1;
- }
- }
- }
- }
- return neueWelt;
- }
- }
Add Comment
Please, Sign In to add comment