《生活的Java游戏》';不要遵循宇宙法则

《生活的Java游戏》';不要遵循宇宙法则,java,Java,你好,有帮助的开发人员 作为家庭作业,我需要开发著名的生活游戏,老师已经编写了一些代码,我们必须用他们给我们的函数来构建它。读取宇宙不是问题,但程序无法正确计算邻居的数量。下面我将发布我的代码,如果有人能帮助我,我将非常感激!我一直在盯着代码思考是什么导致了这个问题,我试着输出程序检查的所有坐标,包括活邻居的数量。我发现这就是问题所在。例如,当我尝试“滑翔机”(包括在从老师那里得到的ZIP文件中包含的文本文件)时,顶端部分不被计算为活的,但是中间的单元是。我应该换什么?或者哪里会出错?Cell是

你好,有帮助的开发人员

作为家庭作业,我需要开发著名的生活游戏,老师已经编写了一些代码,我们必须用他们给我们的函数来构建它。读取宇宙不是问题,但程序无法正确计算邻居的数量。下面我将发布我的代码,如果有人能帮助我,我将非常感激!我一直在盯着代码思考是什么导致了这个问题,我试着输出程序检查的所有坐标,包括活邻居的数量。我发现这就是问题所在。例如,当我尝试“滑翔机”(包括在从老师那里得到的ZIP文件中包含的文本文件)时,顶端部分不被计算为活的,但是中间的单元是。我应该换什么?或者哪里会出错?Cell是导入的zip文件中包含{DEAD,LIVE}的另一个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;
    }
}

包nl.ru.ai.exercise6;
导入nl.ru.ai.gameoflife.Cell;
导入静态nl.ru.ai.gameoflife.Universe.*;
导入java.io.BufferedReader;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.array;
导入java.util.Scanner;
公共生活类游戏
{
公共静态void main(字符串[]args)引发IOException
{
System.out.print(“宇宙名称是什么?”);
扫描仪=新的扫描仪(System.in);
字符串文件名=scanner.nextLine();
单元格[][]universe=readUniverseFile(文件名);
Cell[][]nextGenWorld=Arrays.copyOf(universe,universe.length);
System.out.print(“您希望进化多少代?”);
int totalGen=scanner.nextInt();
int currentGen=0;
做
{
展示宇宙(宇宙);
nextGenWorld=nextGeneration(nextGenWorld);
universe=Arrays.copyOf(nextGenWorld,nextGenWorld.length);
currentGen++;
睡眠(1000);
}

而(currentGen=1&&row=1&&col您的问题可能是由以下原因引起的:

universe = Arrays.copyOf(nextGenWorld, nextGenWorld.length);
如果我没记错的话,Arrays.copyOf with objects返回的数组与提供的数组相同。这是因为您使用的数组类型为Cell。因为Cell不是基元,而不是将其值复制到新数组中,所以将对象复制到新数组中

在这种情况下,您可能希望:

1) 定义与旧单元格具有相同维度的新单元格数组

2) 遍历单元格数组并在每个索引处创建一个新单元格(新数组中的每个单元格将存储与第一个数组中相同的信息)

例如:

Cell[][]nextGen=新单元格[universe.length][universe[0.length];
for(int r=0;r
欢迎来到SO!作为提问的一部分,最好解释一下您已经尝试调试过的内容,并且只发布MCVE()-这可能就是为什么有人立即否决了你的问题。换句话说,如果你已经调试了你的应用程序并确定了问题所在,只发布该函数,这样人们就可以帮助进一步调试,而不必查看整个应用程序。我的第一个建议是因为你已经确定了
nrLivingNeightbours
返回的计数不正确,在那里添加一些断点,然后逐步通过函数分析周围的单元格,找出可能出错的地方。@WOUNDEDStevenJones谢谢!由于我是新来的,我不知道如何正确提问,希望尽可能包含在内。我将尝试使用您的关于断点的提示,看看能给我什么信息!非常感谢!问题确实是当前宇宙和下一代显然没有正确复制。改变这一点很有帮助!@LethalLadders
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]*/);
   }
}