Das hier ist nur eine Hilfe... Wer einfach nur diesen perfekten Code den ich mit eigen erbrachter Denkleistung verfasst habe kopiert... kackt sich selbst am bein... Es dient nur zur Hilfe, falls ihr kleine Fraqumente nicht selber Lösen könnt weil euer Intellekt das nicht zulässt. Also einfach mal die Ziege bei den Eiern packen und selber EASY CODEN....
ciao
Alles anzeigen
ciao
Quellcode
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Formatter;
- import java.util.Scanner;
- /**
- *
- */
- /**
- * @author Marcel Hentschel
- * @licence Marcel Hentschel
- * @copy Dont copy that Floppy
- */
- public class SpielDesLebens {
- static void allZero(int [][] Feld){
- for (int i = 0; i < Feld.length; i++){
- for (int j = 0; j < Feld[0].length; j++){
- Feld[i][j] = 0;
- }
- }
- }
- static void ausgabe(int [][] Feld, int generation){
- System.out.printf("Generation:" + generation +"\n");
- for (int i = 0; i < Feld.length; i++){
- for (int j = 0; j < Feld[0].length; j++){
- System.out.printf(Feld[i][j]+" ");
- }
- System.out.println();
- }
- }
- static int freundeZaehlen(int [][] Feld, int x, int y){
- int count = 0;
- //x=Feld.length;
- //y=Feld[0].length;
- if (x+1 >= Feld.length){
- if (y-1 < 0)count= count + Feld[x-Feld.length+1][y+Feld.length-1];
- else count= count + Feld[x-Feld.length+1][y-1];
- if (y+1 >= Feld[0].length) count= count + Feld[x-Feld.length+1][y-Feld.length+1];
- else count= count + Feld[x-Feld.length+1][y+1];
- count= count + Feld[x-Feld.length+1][y];
- }
- else{
- if (y-1 < 0) count= count + Feld[x+1][y+Feld.length-1];
- else count= count + Feld[x+1][y-1];
- if (y+1 >= Feld[0].length) count= count + Feld[x+1][y-Feld.length+1];
- else count= count + Feld[x+1][y+1];
- count= count + Feld[x+1][y];
- }
- if (x-1 < 0){
- if (y-1 < 0) count= count + Feld[x+Feld.length-1][y+Feld.length-1];
- else count= count + Feld[x+Feld.length-1][y-1];
- if (y+1 >= Feld[0].length) count= count + Feld[x+Feld.length-1][y-Feld.length+1];
- else count= count + Feld[x+Feld.length-1][y+1];
- count= count + Feld[x+Feld.length-1][y];
- }
- else{
- if (y+1 >= Feld[0].length) count= count + Feld[x-1][y-Feld.length+1];
- else count= count + Feld[x-1][y+1];
- if (y-1 < 0) count= count + Feld[x-1][y+Feld.length-1];
- else count= count + Feld[x-1][y-1];
- count= count + Feld[x-1][y];
- }
- if (y+1 >= Feld[0].length) count= count + Feld[x][y-Feld.length+1];
- else count= count + Feld[x][y+1];
- if (y-1 < 0) count= count + Feld[x][y+Feld.length-1];
- else count= count + Feld[x][y-1];
- return count;
- }
- static int deadOrAlive (int [][] Feld, int x, int y){
- int count = freundeZaehlen(Feld, x, y);
- if (count == 3)
- return 1;
- else
- return 0;
- }
- static int aliveOrDead (int [][] Feld, int x, int y){
- int count = freundeZaehlen(Feld, x, y);
- if (count == 3 || count == 2)
- return 1;
- else
- return 0;
- }
- static int[][] myGeneration(int [][] Feld, int zeit, int max){
- if (zeit == 0) return Feld;
- int [][] newFeld = new int [Feld.length][Feld[0].length];
- for (int i = 0; i < Feld.length; i++){
- for (int j = 0; j < Feld[0].length; j++){
- if (Feld[i][j] == 0)
- newFeld[i][j] = deadOrAlive(Feld, i, j);
- else
- newFeld[i][j] = aliveOrDead(Feld, i, j);
- }
- }
- if (zeit>1)
- ausgabe(newFeld, max-zeit);
- return myGeneration(newFeld, zeit-1, max);
- }
- static int[][] dateiEinlesen(String filename){
- int [][] Feld = new int[0][0];
- try {
- File file = new File(filename);
- Scanner sc = new Scanner(file);
- String Zeile = sc.next();
- int xsize = Integer.parseInt(Zeile);
- Zeile = sc.next();
- int ysize = Integer.parseInt(Zeile);
- char [] a = new char [ysize];
- Feld = new int [xsize][ysize];
- for (int i = 0; i < Feld.length; i++){
- Zeile = sc.next();
- a = Zeile.toCharArray();
- for (int j = 0; j < Feld[0].length; j++){
- Feld[i][j] = a[j] - 48;
- }
- }
- sc.close();
- }
- // Falls die Datei nicht gefunden wurde, gibt er eine Fehlermeldung aus
- catch (FileNotFoundException e) {
- System.err.println("Benutzung:" +
- "\n [Programmname] schreibt die Zahlen" +
- "\n aus der Quelldatei (\"zahlen.dat\") in die Zieldatei (\"zahlen_new.dat\") und sortiert sie aufsteigend." +
- "\n [Programmname] [-auf/-ab] schreibt die Zahlen" +
- "\n Je nach dem Parameter -auf oder -ab auf-oder absteigend in die Zieldatei.");
- System.exit(1);
- }
- return Feld;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- ausgabe(myGeneration(dateiEinlesen(args[0] + ".dat"), Integer.parseInt(args[1]), Integer.parseInt(args[1])), Integer.parseInt(args[1])-1);
- }
- }
WTF?