您好有帮助的开发人员,
作为家庭作业,我需要用老师已经编写的一些代码来开发著名的《人生游戏》,我们必须使用他们给我们的功能来构建它。读取Universe没问题,但是程序无法正确计算邻居数量。下面,我将发布我的代码,如果有人可以帮助我,我将非常感激!我一直在盯着代码思考可能导致问题的原因,并且尝试输出所有程序检查的坐标,包括居住的邻居数量。我发现这就是问题所在。例如,当我尝试输入“滑翔机”(我们从老师那里获得的zip文件中包含文本文件)时,顶部不算是活着的,但中间的单元格是活着的。我应该改变什么?还是哪里有错? Cell是包含{DEAD,LIVE}的已导入zip文件中其他Java文件之一的枚举。 编辑:函数updatescreen()也在另一个Java文件中
package nl.ru.ai.exercise6;
import nl.ru.ai.gameoflife.Cell;
import static nl.ru.ai.gameoflife.Universe.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class GameOfLife
{
public static void main(String[] args) throws IOException
{
System.out.print("What is the universe name?");
Scanner scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
Cell[][] universe = readUniverseFile(fileName);
Cell[][] nextGenWorld = Arrays.copyOf(universe, universe.length);
System.out.print("How many generations do you want to evolve?");
int totalGen = scanner.nextInt();
int currentGen = 0;
do
{
showUniverse(universe);
nextGenWorld = nextGeneration(nextGenWorld);
universe = Arrays.copyOf(nextGenWorld, nextGenWorld.length);
currentGen++;
sleep(1000);
}
while (currentGen <= --totalGen);
scanner.close();
}
/**
* Reads the file for a universe to use in the Game of Life. Also checks the universe if it follows the rules of the game.
* @param fileName
* @return the universe that is read from the file, as long as it follows the rules.
*/
static Cell[][] readUniverseFile(String fileName) throws IOException
{
assert (fileName != null) : " there is no file name!";
final int maxRow = 40;
final int maxCol = 60;
Cell[][] universe = new Cell[maxRow][maxCol];
try
{
BufferedReader input = new BufferedReader(new FileReader(fileName));
for (int row = 0; row < maxRow; row++)
{
String universeLine = input.readLine();
if (universeLine == null)
{
input.close();
throw new IllegalArgumentException("The universe contains less than 40 lines!");
}
if (universeLine.length() != maxCol)
{
input.close();
throw new IllegalArgumentException("The universe has a line which does not contain 60 characters");
}
if (row == 0 || row == maxRow-1)
{
for (int col = 0; col < maxCol; col++)
{
if (universeLine.charAt(col) != '.')
{
input.close();
throw new IllegalArgumentException("The universe requires a border of dead characters!");
}
}
}
else
{
if (universeLine.charAt(0) != '.' || universeLine.charAt(maxCol-1) != '.')
{
input.close();
throw new IllegalArgumentException("The universe 'requires a border of dead characters!");
}
}
for (int col = 0; col < maxCol; col++)
{
if (universeLine.charAt(col) == '.')
{
universe[row][col] = Cell.DEAD;
}
else if (universeLine.charAt(col) == '*')
{
universe[row][col] = Cell.LIVE;
}
else
{
input.close();
throw new IllegalArgumentException("The universe has an invalid character");
}
}
}
String universeLine = input.readLine();
if (universeLine != null)
{
input.close();
throw new IllegalArgumentException("The universe contains more than 40 lines!");
}
input.close();
}
catch (IOException e)
{
e.printStackTrace();
throw new FileNotFoundException("The universe does not exist");
}
return universe;
}
/**
* Shows the universe on the screen.
* @param universe
*/
private static void showUniverse(Cell[][] universe)
{
assert(universe != null) : "There is no universe!";
for (int row = 0; row < universe.length; row++)
{
for (int col = 0; col < universe[row].length; col++)
{
updateScreen(row, col, universe[row][col]);
}
}
}
/**
* Compiles the next generation of the universe.
* @param universe
* @return the next universe
*/
private static Cell[][] nextGeneration(Cell[][] universe)
{
assert(universe != null) : "There is no universe!";
final int maxRow = 40;
final int maxCol = 60;
Cell[][] nextGenWorld = universe;
for (int row = 1; row < maxRow-1; row++)
{
for (int col = 1; col < maxCol-1; col++)
{
int livingNeighbours = nrLivingNeighbours(universe, row, col);
if (universe[row][col] == Cell.LIVE)
{
livingNeighbours--;
if (livingNeighbours == 2 || livingNeighbours == 3)
{
nextGenWorld[row][col] = Cell.LIVE;
}
else
{
nextGenWorld[row][col] = Cell.DEAD;
}
}
else
{
if (livingNeighbours == 3)
{
nextGenWorld[row][col] = Cell.LIVE;
}
}
}
}
return nextGenWorld;
}
/**
* Counts the number of living neighbours of a cell (including cell itself if alive).
* @param universe
* @param row
* @param col
* @return the number of neighbours as int.
*/
private static int nrLivingNeighbours(Cell[][] universe, int row, int col)
{
assert(row>=1 && row<39) : "Invalid row specified";
assert(col>=1 && col<59) : "Invalid column specified";
assert(universe != null) : "There is no universe!";
int liveNeighbours = 0;
for(int rowCounter = -1; rowCounter <= 1; rowCounter++)
{
for(int colCounter = -1; colCounter <= 1; colCounter++)
{
if (universe[row+rowCounter][col+colCounter] == Cell.LIVE)
{
liveNeighbours++;
}
}
}
return liveNeighbours;
}
}
答案 0 :(得分:0)
您的问题可能是由以下原因引起的:
universe = Arrays.copyOf(nextGenWorld, nextGenWorld.length);
如果我没记错的话,由Arrays.copyOf返回的带有对象的数组与提供的数组相同。这是因为您使用的数组的类型为Cell。由于Cell不是原始元素,因此不会将其值复制到新数组中,而是将对象复制到新数组中。
在这种情况下,您可能想要:
1)定义一个新的单元格数组,该数组具有与旧数组相同的尺寸。
2)遍历单元格数组,并在每个索引处创建一个新的Cell(新数组中的每个单元格将存储与第一个数组中相同的信息)。
例如:
Cell[][] nextGen = new Cell[universe.length][universe[0].length];
for(int r = 0; r < nextGen.length; r++)
{
for(int c = 0; c < nextGen[0].length; c++)
{
nextGen[r][c] = new Cell(/*provide arguments such that this cell stores the same info as universe[r][c]*/);
}
}