الرجاء المساعدة في تنفيذ Java الأساسي لـ Conway's Game of Life

StackOverflow https://stackoverflow.com/questions/4058292

  •  27-09-2019
  •  | 
  •  

سؤال

لقد أمضيت بعض الوقت في محاولة لكتابة برنامج لتنفيذ لعبة حياة كونواي - ارتباط بمزيد من المعلومات. . أنا أتابع بعض المرشدين عبر الإنترنت وتم منح غالبية الوظائف. كتبت أساليب "التالي" و "الجيران" الموضحة أدناه. هل يمكن لأي شخص أن يخبرني ما إذا كانت هذه تطبيقات جيدة ، وكيف يمكن تحسينها من فضلك؟

كانت الهدف من التمرين هو عدم تعديل أو تغيير أي من الأساليب الأخرى واكتب الطريقة التالية! قون

import java.io.*;
import java.util.Random;

public class Life {

private boolean[][] cells;

public static void main( String[] args ) {
  Life generation = new Life( );
  for (int i = 0; i != 10; i++) {
    System.out.println( generation );
    generation.next( );
  }
}
// Constructors

public void next (){

  int SIZE;
  SIZE=cells.length;
  boolean[][] tempCells = new boolean [SIZE] [SIZE]; 

  for( int i=0; i<SIZE; i++ ) {
 for( int j=0; j<SIZE; j++ ) {
  tempCells[i][j] = cells[i][j];
 }
  } 
  for (int row = 0; row < cells.length ; row++)
  {
    for (int col = 0 ; col < cells[row].length ; col++)
    {
      if ( neighbours(row, col) > 3  ||  neighbours(row, col) < 2 )
      {
        tempCells[row][col] = false;
      }
      else if (neighbours(row, col) == 3 )
      {
        tempCells[row][col] = true;
      }      

    }

  }
  cells = tempCells;

}


public int neighbours (int row, int col) {
  int acc=0;
  for ( int i = row -1; i <= row + 1 ; i++)
    {
     for (int j = col -1 ; j <= col + 1 ; j++)
       {
       try {
         if (cells[i][j]==true && (i != row || j!=col))
         {
           acc++;
         }          
       } catch ( ArrayIndexOutOfBoundsException f)
       {continue;}
     }
  }
  return acc;
}


// Initialises 6 * 6 grid with Glider pattern.
public Life( ) {
final int SIZE = 8;
// Arguably, this should have been a class (static) array.
final int[][] pairs = {{2,4},{3,3},{1,2},{2,2},{3,2}};
cells = new boolean[ SIZE ][ ];
for (int row = 0; row < SIZE; row ++) {
cells[ row ] = new boolean[ SIZE ];
}
for (int pair = 0; pair < pairs.length; pair ++) {
final int row = pairs[ pair ][ 0 ];
final int col = pairs[ pair ][ 1 ];
cells[ row ][ col ] = true;
}
}
 // Initialise size * size grid with random cells.
//public Life( int size ) {
//final Random rand = new Random( );
//cells = new boolean[ size ][ ];
//for (int row = 0; row < size; row ++) {
//cells[ row ] = new boolean[ size ];
//for (int col = 0; col < size; col ++) {
//cells[ row ][ col ] = (rand.nextInt( 2 ) == 0);
//}
//}
//}
// Public methods and helper methods.

@Override
public String toString( ) {
String result = "";
for (int row = 0; row < cells.length; row ++) {
final boolean[] column = cells[ row ];
for (int col = 0; col < column.length; col ++) {
result = result + (column[ col ] ? "x" : ".");
}
result = result + "\n";
}
return result;
}
}
هل كانت مفيدة؟

المحلول

لا تحتاج إلى نسخ محتويات cells إلى tempCells (أول حلقة متداخلة في next). بدلاً من ذلك ، يمكنك إضافة جملة إضافية إلى if-else في الحلقة التالية. أيضا ، تخزين النتيجة من neighbours قد تكون فكرة جيدة لكل من السرعة والوضوح.

for (int row = 0; row < cells.length ; row++)
    for (int col = 0 ; col < cells[row].length ; col++) {
       int n = neighbours(row,col);

       if (n > 3  ||  n < 2)
           tempCells[row][col] = false;
       else if (n == 3)
           tempCells[row][col] = true;
       else
           tempCells[row][col] = cells[row][col];
    }

(بصرف النظر عن ذلك ، تبدو جيدة ، لكنني لم أركض واختبرت التعليمات البرمجية الخاصة بك.)

نصائح أخرى

لا تستخدم ArrayIndExoutofBoundException لحساب شروط خارج الحدود (OOB). يقتل الأداء. من الأفضل استخدام آلية التفاف لعلاج صفيفك مثل كرة حتى لا تواجه OOBs على الإطلاق. يمكنك تجربة شيء مثل هذا:

public Cell[] getNeighbours(int i, int j) {
int i2 = i - 1, i3 = i + 1, j2 = j - 1, j3 = j + 1;
if (i2 == -1) i2 = board.length - 1;
if (i3 == (board.length)) i3 = 0;
if (j2 == -1) j2 = board[i].length - 1;
if (j3 == (board[i].length)) j3 = 0;
return new Cell[]{board[i2][j2], board[i2][j], board[i2][j3], board[i][j2], board[i][j3], board[i3][j2], board[i3][j], board[i3][j3]};

}

ثم يمكنك حلقة الصفيف الذي تم إرجاعه والتحقق من عدد هؤلاء على قيد الحياة وإعادة هذا العد.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top