请帮助我完成康威生命游戏的基本 java 实现
我花了相当长的时间尝试编写一个程序来实现康威的生命游戏 - 包含更多信息的链接。 。我正在遵循一些在线指南,并获得了大部分功能。我编写了如下所示的“next”和“neighbours”方法。谁能告诉我这些是否是好的实现,以及如何做得更好?
练习的重点是不要修改或更改任何其他方法,而只编写下一个方法! :)
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;
}
}
I have spent quite a while trying to write a program to implement Conway's game of life - Link with more info. . I am following some online guides and was given the majority of the functions. I wrote the "next" and "neighbours" methods shown below. Could anyone tell me if these are good implementations, and how they could be made better please ?
The point of the exercise was to not modify or change any of the other methods and just write the next method ! :)
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无需将
cells
的内容复制到tempCells
(next
中的第一个嵌套循环)。相反,您可以在下一个循环中的if
-else
中添加一个额外的子句。此外,存储来自邻居的结果对于速度和清晰度来说可能是一个好主意。(除此之外,看起来不错,但我还没有运行和测试你的代码。)
You don't need to copy the contents of
cells
totempCells
(the first nested loop innext
). Instead, you can add one extra clause to theif
-else
in the next loop. Also, storing the result fromneighbours
may be a good idea for both speed and clarity.(Apart from that, looks fine, but I haven't run and tested your code.)
不要使用 ArrayIndexOutOfBoundException 来计算越界 (OOB) 条件。它会降低性能。最好使用环绕机制将数组视为球体,这样您就根本不会遇到 OOB。您可以尝试这样的操作:
}
然后您可以循环返回的数组并检查其中有多少是活动的并返回该计数。
Don't use ArrayIndexOutOfBoundException to compute out-of-boundary (OOB) conditions. It kills performance. Better use the wrap-around mechanism to treat your array like a sphere so that you don't encounter OOBs at all. You could try something like this:
}
Then you can loop through the returned array and check how many of those are alive and return that count.