Skip to content

Instantly share code, notes, and snippets.

@chopmann
Created May 6, 2013 14:45
Show Gist options
  • Save chopmann/5525602 to your computer and use it in GitHub Desktop.
Save chopmann/5525602 to your computer and use it in GitHub Desktop.
package de.gdplabor.aufgabe7;
import java.util.Locale;
import java.util.Scanner;
public class GDP7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
in.useLocale(Locale.UK);
System.out.println("Wie groß soll das Spielfeld sein?");
int length = in.nextInt();
int[][] population = new int[length][length];
int[][] result = new int[length][length];
System.out.println("Lengh of: " + population.length);
System.out
.println("Bitte geben sie die Anfangspopulation. 1 für Lebendig -- 0 für Tot");
for (int i = 0; i < length; i++) {
for (int k = 0; k < length; k++) {
System.out.println("Feld " + i + "x" + k);
population[i][k] = in.nextInt();
}
}
printMatrix(population);
result = gameOfLife(population);
System.out.println("Result");
printMatrix(result);
}
public static int[][] gameOfLife(int[][] population) {
// System.out.println("Population");
// System.out.println(printMatrix(population));
// System.out.println("Game of Life");
int[][] nextGeneration = new int[population.length][population.length];
for (int i = 0; i < population.length; i++) {
// System.out.println("Computing Row: "+i);
for (int k = 0; k < population.length; k++) {
// Nachbarberrechnung Vertikal
// System.out.println("Computing Column: "+k);
int currentStatus = population[i][k];
int[] nachbarn = new int[8];
// System.out.println("Oben");
nachbarn[0] = getStatus(population, i - 1, k); // Oben
// System.out.println("Unten");
nachbarn[1] = getStatus(population, i + 1, k); // Unten -- check
// System.out.println("Links");
nachbarn[2] = getStatus(population, i, k - 1); // Links
// System.out.println("Rechts");
nachbarn[3] = getStatus(population, i, k + 1); // Rechts -- check
// System.out.println("ObenLinks");
nachbarn[4] = getStatus(population, i - 1, k - 1); // ObenLinks
// System.out.println("ObenRechts");
nachbarn[5] = getStatus(population, i - 1, k + 1); // ObenRechts
// System.out.println("UntenLinks");
nachbarn[6] = getStatus(population, i + 1, k - 1); // UntenLinks - check
// System.out.println("UntenRechts");
nachbarn[7] = getStatus(population, i + 1, k + 1); // UntenRechts -- check
int alive = countAlive(nachbarn);
if (currentStatus == 0) {
if (alive == 3) {
nextGeneration[i][k] = 1;
}
} else {
if (alive == 2 || alive == 3) {
nextGeneration[i][k] = 1;
}else {
nextGeneration[i][k] = 0;
}
}
// System.out.println("Row "+i+" column "+k+" status "+population[i][k]+" new status "+nextGeneration[i][k] + " alive Nachbarn "+alive);
// System.out.println("------------------------------------------------");
}
}
// System.out.println(printMatrix(nextGeneration));
return nextGeneration;
}
public static int getStatus(int[][] population, int x, int y) {
int realX = mirror(x, population.length);
int realY = mirror(y,population.length);
// System.out.println("X: "+x+" Mirror: "+realX);
// System.out.println("Y: "+y+" Mirror: "+realY);
int status = population[realX][realY];
// System.out.println("Status of "+ realX+"x"+realY+" : "+status);
return status;
}
/***
* Randregel
* @param position
* @param length of the Array
* @return
*/
public static int mirror(int position, int length) {
if (position < 0) {
// System.out.println("Out of Bounds LEFT");
int tmp = length + position;
return tmp;
} else if (position >= length) {
// System.out.println("Out of Bounds RIGHT");
int tmp = position - length;
return tmp;
} else {
// System.out.println("Unchanged");
return position;
}
}
public static String printMatrix(int[][] matrix) {
String result = "";
for (int[] is : matrix) {
for (int i : is) {
result = result + i;
}
result = result + "\n";
}
return result;
}
public static int countAlive(int[] cells) {
int counter = 0;
String alive = "";
for (int i = 0; i < cells.length; i++) {
int status = cells[i];
if (status == 1) {
counter++;
alive = alive + i + " ";
}
}
// System.out.println("Whos was alive: "+alive);
return counter;
}
}
@chopmann
Copy link
Author

chopmann commented May 6, 2013

Die Methode um mehrere Populationen zu berechnen

public static void Evolve(int[][] population,int generations) {
    int[][][] speicher = new int[generations][population.length][population.length];
    for (int i = 0 ; i <generations; i++) {
        speicher[i] = gameOfLife(population);
        population = speicher[i];
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment