Spiel des Lebens (Game of Life)

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Spiel des Lebens (Game of Life)

    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


    Quellcode

    1. import java.io.File;
    2. import java.io.FileNotFoundException;
    3. import java.util.Formatter;
    4. import java.util.Scanner;
    5. /**
    6. *
    7. */
    8. /**
    9. * @author Marcel Hentschel
    10. * @licence Marcel Hentschel
    11. * @copy Dont copy that Floppy
    12. */
    13. public class SpielDesLebens {
    14. static void allZero(int [][] Feld){
    15. for (int i = 0; i < Feld.length; i++){
    16. for (int j = 0; j < Feld[0].length; j++){
    17. Feld[i][j] = 0;
    18. }
    19. }
    20. }
    21. static void ausgabe(int [][] Feld, int generation){
    22. System.out.printf("Generation:" + generation +"\n");
    23. for (int i = 0; i < Feld.length; i++){
    24. for (int j = 0; j < Feld[0].length; j++){
    25. System.out.printf(Feld[i][j]+" ");
    26. }
    27. System.out.println();
    28. }
    29. }
    30. static int freundeZaehlen(int [][] Feld, int x, int y){
    31. int count = 0;
    32. //x=Feld.length;
    33. //y=Feld[0].length;
    34. if (x+1 >= Feld.length){
    35. if (y-1 < 0)count= count + Feld[x-Feld.length+1][y+Feld.length-1];
    36. else count= count + Feld[x-Feld.length+1][y-1];
    37. if (y+1 >= Feld[0].length) count= count + Feld[x-Feld.length+1][y-Feld.length+1];
    38. else count= count + Feld[x-Feld.length+1][y+1];
    39. count= count + Feld[x-Feld.length+1][y];
    40. }
    41. else{
    42. if (y-1 < 0) count= count + Feld[x+1][y+Feld.length-1];
    43. else count= count + Feld[x+1][y-1];
    44. if (y+1 >= Feld[0].length) count= count + Feld[x+1][y-Feld.length+1];
    45. else count= count + Feld[x+1][y+1];
    46. count= count + Feld[x+1][y];
    47. }
    48. if (x-1 < 0){
    49. if (y-1 < 0) count= count + Feld[x+Feld.length-1][y+Feld.length-1];
    50. else count= count + Feld[x+Feld.length-1][y-1];
    51. if (y+1 >= Feld[0].length) count= count + Feld[x+Feld.length-1][y-Feld.length+1];
    52. else count= count + Feld[x+Feld.length-1][y+1];
    53. count= count + Feld[x+Feld.length-1][y];
    54. }
    55. else{
    56. if (y+1 >= Feld[0].length) count= count + Feld[x-1][y-Feld.length+1];
    57. else count= count + Feld[x-1][y+1];
    58. if (y-1 < 0) count= count + Feld[x-1][y+Feld.length-1];
    59. else count= count + Feld[x-1][y-1];
    60. count= count + Feld[x-1][y];
    61. }
    62. if (y+1 >= Feld[0].length) count= count + Feld[x][y-Feld.length+1];
    63. else count= count + Feld[x][y+1];
    64. if (y-1 < 0) count= count + Feld[x][y+Feld.length-1];
    65. else count= count + Feld[x][y-1];
    66. return count;
    67. }
    68. static int deadOrAlive (int [][] Feld, int x, int y){
    69. int count = freundeZaehlen(Feld, x, y);
    70. if (count == 3)
    71. return 1;
    72. else
    73. return 0;
    74. }
    75. static int aliveOrDead (int [][] Feld, int x, int y){
    76. int count = freundeZaehlen(Feld, x, y);
    77. if (count == 3 || count == 2)
    78. return 1;
    79. else
    80. return 0;
    81. }
    82. static int[][] myGeneration(int [][] Feld, int zeit, int max){
    83. if (zeit == 0) return Feld;
    84. int [][] newFeld = new int [Feld.length][Feld[0].length];
    85. for (int i = 0; i < Feld.length; i++){
    86. for (int j = 0; j < Feld[0].length; j++){
    87. if (Feld[i][j] == 0)
    88. newFeld[i][j] = deadOrAlive(Feld, i, j);
    89. else
    90. newFeld[i][j] = aliveOrDead(Feld, i, j);
    91. }
    92. }
    93. if (zeit>1)
    94. ausgabe(newFeld, max-zeit);
    95. return myGeneration(newFeld, zeit-1, max);
    96. }
    97. static int[][] dateiEinlesen(String filename){
    98. int [][] Feld = new int[0][0];
    99. try {
    100. File file = new File(filename);
    101. Scanner sc = new Scanner(file);
    102. String Zeile = sc.next();
    103. int xsize = Integer.parseInt(Zeile);
    104. Zeile = sc.next();
    105. int ysize = Integer.parseInt(Zeile);
    106. char [] a = new char [ysize];
    107. Feld = new int [xsize][ysize];
    108. for (int i = 0; i < Feld.length; i++){
    109. Zeile = sc.next();
    110. a = Zeile.toCharArray();
    111. for (int j = 0; j < Feld[0].length; j++){
    112. Feld[i][j] = a[j] - 48;
    113. }
    114. }
    115. sc.close();
    116. }
    117. // Falls die Datei nicht gefunden wurde, gibt er eine Fehlermeldung aus
    118. catch (FileNotFoundException e) {
    119. System.err.println("Benutzung:" +
    120. "\n [Programmname] schreibt die Zahlen" +
    121. "\n aus der Quelldatei (\"zahlen.dat\") in die Zieldatei (\"zahlen_new.dat\") und sortiert sie aufsteigend." +
    122. "\n [Programmname] [-auf/-ab] schreibt die Zahlen" +
    123. "\n Je nach dem Parameter -auf oder -ab auf-oder absteigend in die Zieldatei.");
    124. System.exit(1);
    125. }
    126. return Feld;
    127. }
    128. /**
    129. * @param args
    130. */
    131. public static void main(String[] args) {
    132. ausgabe(myGeneration(dateiEinlesen(args[0] + ".dat"), Integer.parseInt(args[1]), Integer.parseInt(args[1])), Integer.parseInt(args[1])-1);
    133. }
    134. }
    Alles anzeigen
    WTF?
  • Conways Spiel des Lebens

    Conways Spiel des Lebens ist ein von dem Mathematiker John Horton Conway 1970 entworfenes System zweidimensional angeordneter zellulärer Automaten. Es ist eine einfache und bis heute populäre Umsetzung der Automaten-Theorie von Stanisław Marcin Ulam.

    Die Regeln:
    Die Folgegeneration wird für alle Zellen gleichzeitig berechnet und ersetzt die aktuelle Generation. Der Zustand einer Zelle, lebendig oder tot in der Folgegeneration hängt nur vom Zustand der acht Nachbarzellen dieser Zelle in der aktuellen Generation ab.

    Die von Conway zu Anfang verwendeten Regeln sind:
    * Eine Zelle mit genau drei lebenden Nachbarn wird in der Folgegeneration neu geboren.
    * Lebende Zellen mit weniger als zwei lebenden Nachbarn sterben in der Folgegeneration an Einsamkeit.
    * Lebende Zellen mit mehr als drei lebenden Nachbarn sterben in der Folgegeneration an Überbevölkerung

    Mit diesen drei einfachen Regeln entsteht aus bestimmten Anfangsmustern im Laufe des Spiels eine Vielfalt komplexer Strukturen. Einige bleiben unverändert, andere oszillieren und wieder andere wachsen oder vergehen. Manche Strukturen, sogenannte Gleiter, bewegen sich auf dem Spielfeld fort.
  • Hi, freut mich, dass du dich hierher verirrst hast.

    Schöner Code, auch wenn unser Prof das JavaDoc vermissen wird..
    Aber funktioniert das Programm bei dir?

    Habe die blinker.dat wie beschrieben erstellt (siehe Anhang) und dein Programm mit den Argumenten "blinker 2" aufgerufen

    als ergebnis erhalte ich nur eine arrayOutOfBoundException
    in dieser schleife

    Quellcode

    1. for (int j = 0; j < Feld[0].length; j++){
    2. Feld[i][j] = a[j] - 48;
    3. }
  • ich habe mich hier nicht verirrt....
    ich studiere doch mit dir...

    javadoc comments sind noch nicht drin weil wenns dann jemand kopiert muss er sich wenigstens noch etwas gedanken machen....
    vielleicht hast du die datei nicht richtig gespeichert...
    So muss die datei blinker.dat aussehen
    -----------------------------------------------------------------
    4 5
    00100
    00100
    00100
    00000
    -----------------------------------------------------------------
    ich hab mir die arbeit gemacht noch ein paar andere schöne sachen aufzuschreiben in ne datei....
    -----------------------------------------------------------------
    12 30
    000000000000000000000000000000
    001000000000000000000000000000
    001000000000000000000000000000
    001000000000000000000000000000
    000000000000000110000000000000
    000000000000000100000000000000
    000000000000000000100000000000
    000000000000000001100000000000
    000000000000000000000000000000
    000000000000000000000000000000
    000000000000000000000000000000
    000000000000000000000000000000
    -----------------------------------------------------------------
    try again...
    ciao
    Dateien
    • blinker.dat

      (31 Byte, 805 mal heruntergeladen, zuletzt: )
    WTF?
  • Meine Version

    Das ist meine Version :)

    Quellcode

    1. import java.io.File;
    2. import java.io.FileNotFoundException;
    3. import java.util.*;
    4. public class Aufgabe4 {
    5. public static boolean [][] makeArray( String dateiName, File myFile )
    6. throws FileNotFoundException{
    7. int zeile, spalte;
    8. Scanner input = new Scanner( myFile );
    9. spalte = input.nextInt();
    10. zeile = input.nextInt();
    11. boolean gameOfLife [][] = new boolean[spalte][zeile];
    12. for( int x = 0; x < spalte; x += 1)
    13. for( int y = 0; y < zeile; y += 1 ){
    14. if( input.nextInt() == 1)
    15. gameOfLife[x][y] = true;
    16. else
    17. gameOfLife[x][y] = false;
    18. }
    19. return gameOfLife;
    20. }
    21. public static void printCell( boolean [][] cell, int gen ){
    22. System.out.printf("Generation: %d\n", gen);
    23. for( int i = 0; i < cell.length; i += 1){
    24. for( int x = 0; x < cell[i].length; x += 1){
    25. if ( cell[i][x] ){
    26. System.out.printf("1 ");
    27. }
    28. else{
    29. System.out.printf("0 ");
    30. }
    31. }
    32. System.out.printf("\n");
    33. }
    34. }
    35. public static boolean zelleAmLeben(int x, int y, boolean [][] cell){
    36. boolean amLeben = false;
    37. if ( ( x >= 0 ) && ( x < cell.length ) && (y >= 0) && (y < cell[0].length ) ){
    38. amLeben = cell[x][y];
    39. return amLeben;
    40. }
    41. else if( ( x < 0 ) && ( y >= 0 ) && ( y < cell[0].length ) ){
    42. amLeben = cell[cell.length-1][y];
    43. return amLeben;
    44. }
    45. else if( ( x < cell.length ) && ( y >= 0 ) && ( y < cell[0].length ) ){
    46. amLeben = cell[0][y];
    47. return amLeben;
    48. }
    49. else if( ( y < 0 ) && ( x >= 0 ) && (x < cell.length ) ){
    50. amLeben = cell[x][cell[0].length-1];
    51. return amLeben;
    52. }
    53. else if( ( y <= cell[0].length ) && ( x >= 0 ) && ( x < cell.length ) ){
    54. amLeben = cell[x][0];
    55. return amLeben;
    56. }
    57. else if( ( x < 0 ) && ( y < 0 ) ){
    58. amLeben = cell[cell.length-1][cell[0].length-1];
    59. return amLeben;
    60. }
    61. else if( ( x > cell.length ) && ( y > cell[0].length ) ){
    62. amLeben = cell[0][0];
    63. return amLeben;
    64. }
    65. else {
    66. amLeben = false;
    67. return amLeben;
    68. }
    69. }
    70. public static int anzahlNachbarn( int x, int y, boolean [][] cell ){
    71. int count = 0;
    72. if ( zelleAmLeben( x-1, y-1, cell ) )
    73. count += 1;
    74. if ( zelleAmLeben( x-1, y, cell ) )
    75. count += 1;
    76. if ( zelleAmLeben( x-1, y+1, cell ) )
    77. count += 1;
    78. if ( zelleAmLeben( x, y-1, cell ) )
    79. count += 1;
    80. if ( zelleAmLeben( x, y+1, cell ) )
    81. count += 1;
    82. if ( zelleAmLeben( x+1, y-1, cell ) )
    83. count += 1;
    84. if ( zelleAmLeben( x+1, y, cell ) )
    85. count += 1;
    86. if ( zelleAmLeben( x+1, y+1, cell ) )
    87. count += 1;
    88. return count;
    89. }
    90. public static boolean [][] nextGen ( boolean [][] cell){
    91. boolean [][] tempCell = new boolean [cell.length][cell[0].length];
    92. for( int x = 0; x < cell.length; x += 1){
    93. for( int y = 0; y < cell[x].length; y += 1){
    94. boolean alive_now = cell[x][y], alive_then = false;
    95. int anzahl_nachbarn = anzahlNachbarn( x, y, cell );
    96. if (( alive_now ) && ( anzahl_nachbarn < 2 )){
    97. alive_then = false;
    98. }
    99. else if (( alive_now ) && ( anzahl_nachbarn > 3 )){
    100. alive_then = false;
    101. }
    102. else if (( !alive_now ) && ( anzahl_nachbarn == 3 )){
    103. alive_then = true;
    104. }
    105. else if (( alive_now ) && (( anzahl_nachbarn == 2 ) || ( anzahl_nachbarn == 3 ))){
    106. alive_then = true;
    107. }
    108. tempCell[x][y] = alive_then;
    109. }
    110. }
    111. for( int x = 0; x < cell.length; x += 1)
    112. for( int y = 0; y < cell[x].length; y += 1){
    113. cell[x][y] = tempCell[x][y];
    114. }
    115. return cell;
    116. }
    117. public static void spielDesLebens( String [] parameter ){
    118. String dateiName = parameter[0];
    119. int anzahlGen = Integer.parseInt( parameter[1] );
    120. File myFile = new File( dateiName+".dat" );
    121. try {
    122. boolean [][] cell = makeArray( dateiName, myFile );
    123. int gen = 1;
    124. while( anzahlGen > 0){
    125. cell = nextGen( cell );
    126. printCell( cell, gen );
    127. anzahlGen -= 1;
    128. gen += 1;
    129. }
    130. } catch (FileNotFoundException e) {
    131. System.out.printf("Fehler bei der Array Erstellung!!");
    132. }
    133. }
    134. public static void main(String[] args) {
    135. switch( args.length){
    136. case 2:
    137. spielDesLebens( args );
    138. break;
    139. default:
    140. System.out.printf("usage: Dateiname | anzahlGen ");
    141. }
    142. }
    143. }
    Alles anzeigen
  • und hier noch meine Lösung.. knapp 100 Zeilen..

    Quellcode

    1. import java.io.File;
    2. import java.io.IOException;
    3. import java.util.Scanner;
    4. /**
    5. *
    6. * @author Torben Brodt
    7. * @version 1.0
    8. *
    9. * <p />Game of Life (John Cornway)
    10. */
    11. public class A4_Game_of_Life {
    12. /**
    13. * @param args
    14. */
    15. public static void main(String[] args) throws IOException {
    16. // TODO Auto-generated method stub
    17. String source = "blinker.dat";
    18. int dimensionen = 5;
    19. try {
    20. source = args[0];
    21. dimensionen = Integer.parseInt(args[1]);
    22. } catch(Exception e) {
    23. System.err.println("Keine Werte übergeben -> Standardwerte werden genutzt!");
    24. }
    25. Scanner sc = new Scanner(new File(source));
    26. int rows = sc.nextInt();
    27. int columns = sc.nextInt();
    28. boolean[][] cell = new boolean[rows][columns];
    29. for(int x = 0; x < rows; x++) {
    30. int y=0;
    31. for(char digit : sc.next().toCharArray()) {
    32. if(digit == '1')
    33. cell[x][y] = true;
    34. else
    35. cell[x][y] = false;
    36. y++;
    37. }
    38. }
    39. System.out.println("Generation 0");
    40. show(cell);
    41. for(int i=1; i<dimensionen; i++) {
    42. System.out.println("Generation "+i);
    43. cell = nextGen(cell);
    44. }
    45. }
    46. /**
    47. * Ausgabefunktion
    48. * @param cell
    49. */
    50. static void show(boolean [][] cell) {
    51. for(boolean[] arrZeile : cell) {
    52. for(boolean arrSpalte : arrZeile) {
    53. int ausgabe = arrSpalte == true ? 1 : 0;
    54. System.out.print(ausgabe);
    55. }
    56. System.out.println();
    57. }
    58. System.out.println();
    59. }
    60. /**
    61. * Lebt die Zelle?
    62. * @param column -> Spalte
    63. * @param row -> Zeile
    64. * @param cell -> Array
    65. * @return -> true / false
    66. */
    67. static boolean cellAlive(int column, int row, boolean [][] cell){
    68. int right = (column+1 > cell[0].length-1) ? 0 : column+1;
    69. int left = (column-1 < 0) ? cell[0].length-1 : column-1;
    70. int bottom = (row+1 > cell.length-1) ? 0 : row+1;
    71. int top = (row-1 < 0) ? cell.length-1 : row-1;
    72. int treffer=0;
    73. //die 8 Nachbarn im Uhrzeigersinn
    74. treffer += cell[top][column] ? 1 : 0;
    75. treffer += cell[top][right] ? 1 : 0;
    76. treffer += cell[row][right] ? 1 : 0;
    77. treffer += cell[bottom][right] ? 1 : 0;
    78. treffer += cell[bottom][column] ? 1 : 0;
    79. treffer += cell[bottom][left] ? 1 : 0;
    80. treffer += cell[row][left] ? 1 : 0;
    81. treffer += cell[top][left] ? 1 : 0;
    82. if(cell[row][column] == true && (treffer == 2 || treffer == 3)) //lebende Zelle bleibt lebendig
    83. return true;
    84. else if(cell[row][column] == false && (treffer == 3)) //tote Zelle wird lebendig
    85. return true;
    86. else
    87. return false;
    88. }
    89. /**
    90. * Schickt alle Array Elemente durch den cellAlive Check
    91. * @param cell -> die alte Generation
    92. * @return -> das temporäre Array (die neue Generation)
    93. */
    94. static boolean [][] nextGen ( boolean[][] cell) {
    95. boolean [][] tmp = new boolean [cell.length][cell[0].length];
    96. for(int y=0; y<cell.length; y++)
    97. for(int x=0; x<cell[0].length; x++)
    98. tmp[y][x] = cellAlive(x, y, cell);
    99. show(tmp);
    100. return tmp;
    101. }
    102. }
    Alles anzeigen
  • Jo, servus... hier meins...

    Quellcode

    1. import java.io.File;
    2. import java.io.FileNotFoundException;
    3. import java.util.*;
    4. /**
    5. * <p>
    6. * <b>Organisation:</b> FH-Wiesbaden<br>
    7. * <b>Homepage:</b> <a href="http://www.fh-wiesbaden.de">www.fh-wiesbaden.de</a><br>
    8. * <b>Fachbereich:</b> 06 - Meieninformatik<br>
    9. * </p>
    10. * <p>
    11. * <b>Entwickler:</b> Berntheisel, Stefan<br>
    12. * <p>
    13. * <b>Programm:</b><i> Game of Life </i><br>
    14. * </p>
    15. *
    16. * @author sbern001 (Username)
    17. * @version 1.0
    18. * @docRoot .
    19. */
    20. public class U11A04 {
    21. /**
    22. * Haupprogramm
    23. * @param args Kommandozeilenparameter
    24. * arg[0] = Zellen-Datei
    25. * arg[1] = Generationen
    26. */
    27. public static void main (String args[])
    28. {
    29. String filename = "";
    30. int generation = 0;
    31. try
    32. {
    33. filename = args[0];
    34. generation = Integer.parseInt(args[1]);
    35. if(generation < 1) {
    36. System.err.printf("Keine negative Anzahl an Generaionen moeglich!\n");
    37. System.exit(-1);
    38. }
    39. }
    40. catch (Exception e)
    41. {
    42. System.out.printf("Bitte ueberpruefen Sie die Kommandozeilenparameter!\n\n");
    43. System.out.printf(" <Programm> [Dateiname] [Anzahl_Gernerationen]\n");
    44. System.exit(-1);
    45. }
    46. int integer_cell[][] = readCellFile(filename);
    47. if(integer_cell.length == 0)
    48. {
    49. System.err.printf("Die Datei \"%s\" kann nicht gelesen werden!\n",filename);
    50. System.exit(-1);
    51. }
    52. boolean cell[][] = intToBool(integer_cell);
    53. System.out.printf("Generation: 0\n");
    54. drawCell(cell);
    55. for(int i = 0; i < generation; i++)
    56. cell = nextGen(cell);
    57. System.out.printf("Generation: %d\n",generation);
    58. drawCell(cell);
    59. }
    60. /**
    61. * Methode: nextGen
    62. * @param cell Ausgangs-Generation der Zellen
    63. * @return Nexte Generatiation der Zellen
    64. */
    65. static boolean[][] nextGen(boolean cell[][])
    66. {
    67. cell = expansionCell(cell);
    68. boolean life[][] = new boolean[cell.length][cell[0].length];
    69. for(int x = 1; x < cell.length - 1; x++)
    70. for(int y = 1; y < cell[0].length - 1; y++)
    71. {
    72. if(cell[x][y] && life(cell, x, y, 2) || life(cell, x, y, 3)) life[x][y] = true;
    73. else if(!cell[x][y] && life(cell, x, y, 3)) life[x][y] = true;
    74. else life[x][y] = false;
    75. }
    76. return reduceCell(life);
    77. }
    78. /**
    79. * Methode: expansionCell
    80. * @param cell Ausgangs-Zellenmatrix
    81. * @return Alle Seiten und Ecken der Ausgangs-Zellenmatrix wurden
    82. * an den gegenueberliegenden Punkten angehaengt (erweitert)
    83. */
    84. static boolean[][] expansionCell (boolean cell[][])
    85. {
    86. boolean expansion[][] = new boolean[cell.length + 2][cell[0].length + 2];
    87. int cx = cell.length - 1;
    88. int cy = cell[0].length - 1;
    89. int ex = expansion.length - 1;
    90. int ey = expansion[0].length - 1;
    91. // Kopieren von "cell" in die mitte von "expansion"
    92. for(int x = 0; x <= cx; x++)
    93. for(int y = 0; y <= cy; y++)
    94. expansion[x+1][y+1] = cell[x][y];
    95. // Ecken verspiegelt kopieren
    96. expansion[0 ][0 ] = cell[cx][cy];
    97. expansion[ex][ey] = cell[0 ][0 ];
    98. expansion[0 ][ey] = cell[cx][0 ];
    99. expansion[ex][0 ] = cell[0 ][cy];
    100. // Obere- und Untere Seite verspiegelt kopieren
    101. for(int x = 0; x <= cx; x++) {
    102. expansion[x+1][0] = cell[x][cy];
    103. expansion[x+1][ey] = cell[x][0]; }
    104. // Linke- und Rechte Seite verspiegelt kopieren
    105. for(int y = 0; y <= cy; y++) {
    106. expansion[0][y+1] = cell[cx][y];
    107. expansion[ex][y+1] = cell[0][y]; }
    108. return expansion;
    109. }
    110. /**
    111. * Methode: reduceCell
    112. * @param cell Erweiterte Zellenmatrix
    113. * @return Zuruecksetzen in die Ursprungs-Zellenmatrix
    114. */
    115. static boolean[][] reduceCell (boolean cell[][])
    116. {
    117. boolean reduce[][] = new boolean[cell.length - 2][cell[0].length - 2];
    118. int rx = reduce.length - 1;
    119. int ry = reduce[0].length - 1;
    120. // Verkleinern von "cell" durch kopieren nach "reduce"
    121. for(int x = 0; x <= rx; x++)
    122. for(int y = 0; y <= ry; y++)
    123. reduce[x][y] = cell[x+1][y+1];
    124. return reduce;
    125. }
    126. /**
    127. * Methode: life
    128. * @param cell Zellenmatrix
    129. * @param x X-Koordinate in der Matrix
    130. * @param y Y-Koordinate in der Matrix
    131. * @param life_value Anzahl der zusuchenden lebenden Zellen
    132. * @return
    133. */
    134. static boolean life(boolean cell[][], int x, int y, int life_value)
    135. {
    136. int count = 0;
    137. // Prueft die umliegenden Zellen auf Leben
    138. // Läuft im Uhrzeiger, start bei 3 Uhr
    139. if(cell[x][y+1]) count++;
    140. if(cell[x+1][y+1]) count++;
    141. if(cell[x+1][y]) count++;
    142. if(cell[x+1][y-1]) count++;
    143. if(cell[x][y-1]) count++;
    144. if(cell[x-1][y-1]) count++;
    145. if(cell[x-1][y]) count++;
    146. if(cell[x-1][y+1]) count++;
    147. if(count >= life_value) return true;
    148. else return false;
    149. }
    150. /**
    151. * Methode: readCellFile
    152. * @param filename Dateiname der Zellenmatrix
    153. * @return Zellenmatrix
    154. */
    155. static int[][] readCellFile(String filename)
    156. {
    157. int x,y;
    158. int null_cell[][] = new int[0][0];
    159. try
    160. {
    161. File file = new File(filename);
    162. if(!file.exists() || !file.canRead()) return null_cell;
    163. Scanner sc = new Scanner(file);
    164. x = sc.nextInt();
    165. y = sc.nextInt();
    166. if(x < 3 && y < 3) { return null_cell; }
    167. int cell[][] = new int[x][y];
    168. for(int i = 0; i < x; i++)
    169. for(int j = 0; j < y; j++)
    170. cell[i][j] = sc.nextInt();
    171. return cell;
    172. }
    173. catch(FileNotFoundException e)
    174. {
    175. return null_cell;
    176. }
    177. }
    178. /**
    179. * Methode: intToBool
    180. * @param cell Zellenmatrix als Integer
    181. * @return Zellenmatrix als Boolean
    182. */
    183. public static boolean[][] intToBool(int cell[][])
    184. {
    185. boolean bool_cell[][] = new boolean[cell.length][cell[0].length];
    186. for(int i = 0; i < cell.length; i++)
    187. for(int j = 0; j < cell[0].length; j++)
    188. if(cell[i][j] < 1) bool_cell[i][j] = false;
    189. else bool_cell[i][j] = true;
    190. return bool_cell;
    191. }
    192. /**
    193. * Methode: drawCell
    194. * @param cell Zellenmatrix
    195. */
    196. public static void drawCell (boolean cell[][])
    197. {
    198. for(int i = 0; i < cell.length; i++) {
    199. for(int j = 0; j < cell[0].length; j++) {
    200. if(cell[i][j]) System.out.print("1");
    201. else System.out.print("0");
    202. if(j < cell[0].length - 1) System.out.print(" ");
    203. }
    204. System.out.println();
    205. }
    206. }
    207. }
    Alles anzeigen