gpt4 book ai didi

java - 请帮助我对康威生命游戏的基本 Java 实现

转载 作者:行者123 更新时间:2023-12-04 03:14:50 25 4
gpt4 key购买 nike

我花了很长时间尝试编写一个程序来实现康威的生命游戏 - Link with more info. .我遵循一些在线指南并获得了大部分功能。我编写了如下所示的“next”和“neighbors”方法。谁能告诉我这些是否是好的实现,以及如何改进它们?

练习的重点是不要修改或更改任何其他方法,只需编写下一个方法! :)

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 添加一个额外的子句。此外,存储来自 neighbors 的结果对于速度和清晰度来说可能是个好主意。

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];
}

(除此之外,看起来不错,但我还没有运行和测试您的代码。)

关于java - 请帮助我对康威生命游戏的基本 Java 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4058292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com