gpt4 book ai didi

java - 在用 Java 编写康威生命游戏的邻居数量方法时遇到困难。格式化为 2D 数组,10 x 10 矩阵

转载 作者:行者123 更新时间:2023-12-02 05:43:50 25 4
gpt4 key购买 nike

我正在用 Java 编写 Conway 的《生命游戏》的类作业。要求是使用 10 x 10 2D 矩阵在 Java 中编写矩阵。

我在下面编写了以下代码。 NumberOfNeighbors(int[][] board, int row, int col)方法不起作用,我不知道为什么。我已经使用 boolean 二维数组矩阵解决了这个问题,并且正在研究 int[][] 矩阵的解决方案。在编写引用 NumberOfNeighbors() 的 DeadOrAlive 方法时,我感到很困惑方法。

我不明白为什么 NumberOfNeighbors(int[][] board, int row, int col)方法失败。

package com.cis2151.proj1;

import java.util.Scanner;
import java.io.*;

public class Life
{

private static final int ROWS = 10;
private static final int COLS = 10;
private static Scanner inputFile;

public static void main(String[] args) throws FileNotFoundException
{
int[][] board;
board = new int[ROWS][COLS];
ReadFile(board);
PrintArray(board);
System.out.println();

for (int i = 1; i <= 10; i++)
{
board = NextGeneration(board);
PrintArray(board);
System.out.println();
}

} // end main

public static void ReadFile(int[][] board) throws FileNotFoundException
{
File file = new File("numbers");
inputFile = new Scanner(file);
String line;

for(int r = 0; r < ROWS; r++)
{
line = inputFile.nextLine();
for(int c = 0; c < COLS; c++)
{
String temp = String.valueOf(line.charAt(c));
board[r][c] = Integer.parseInt(temp);
} // end c
} // end r
} // end ReadFile()

public static void PrintArray(int[][] board)
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}


public static int[][] NextGeneration(int[][] board)
{
int[][] newBoard = new int[ROWS][COLS];
for (int i = 0; i < ROWS ; i++)
{
for (int j = 0; j < COLS ; j++)
{
newBoard[i][j] = DeadOrAlive(board, i, j);
}
}
return newBoard;
}



public static int DeadOrAlive(int[][] board, int r, int c)
{
int neighbors = NumberOfNeighbors(board, r, c);
int deadOrAlive = 0;


if (board[r][c] == 1 && (neighbors == 2 || neighbors == 3))
deadOrAlive = 1;
else if (board[r][c] == 0 && neighbors == 3)
deadOrAlive = 1;
else
deadOrAlive = 0;

return deadOrAlive;
}


private static int NumberOfNeighbors(int[][] board, int row, int col)
{
int neighbors = 0;
for(int i = row - 1; i <= row + 1; i++)
{
for(int j = col - 1; j <= col + 1; j++)
{
if ( !(row == i && col == j))
{
if ( insideMatrix(board, i, j))
{
neighbors++;
}
}
}
}

return neighbors;
}

private static boolean insideMatrix(int[][] board, int row, int col)
{
return row >= 0 && row < ROWS && col >= 0 &&
col < COLS;
}
}

控制台输出:

0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

1 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 1

1 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 1

1 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0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 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 1

1 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 1

1 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 1

正确的解决方案-

package com.cis2151.project1;

import java.io.*;
import java.util.Scanner;

public class Life
{
private static Scanner inputfile;

public static void main(String[] args) throws FileNotFoundException
{
boolean[][] cells = ReadFile();
PrintBoard(cells);
for (int i = 0; i < 10; i++)
{
cells = nextGeneration(cells);
PrintBoard(cells);
}

}


public static boolean[][] ReadFile() throws FileNotFoundException{
boolean[][] matrix = new boolean[10][10];
File file = new File("Numbers.txt");
inputfile = new Scanner(file);
for(int i = 0; i < matrix.length; i++)
{

for(int j = 0; j < matrix[0].length; j++)
{
if (inputfile.hasNextInt() && inputfile.nextInt() != 0)
{
matrix[i][j] = true;
}
}
}
return matrix;
}


public static void PrintBoard(boolean[][] matrix)
{
String cell = "";
for(boolean[] i : matrix){
for(boolean val : i)
if(val)
cell += "1 ";
else
cell += "0 ";
cell += "\n";
}
System.out.println(cell);
}

public static boolean[][] nextGeneration(boolean[][] cells)
{
boolean[][] nextGenerationOfCells = new boolean[cells.length][cells[0].length];
int newCellGenerated;
for (int i = 0; i < cells.length; i++)
{
for (int j = 0; j < cells[0].length; j++)
{
newCellGenerated = NumberOfNeighbors(cells, i, j);
if (rulesOfLife(newCellGenerated, cells[i][j]))
{
nextGenerationOfCells[i][j] = true;
}
}
}
return nextGenerationOfCells;
}

public static boolean rulesOfLife(int numberOfNeighbors, boolean alive){
if( alive && (numberOfNeighbors == 2 || numberOfNeighbors == 3))
return true;
else if (!alive && numberOfNeighbors == 3)
return true;
else
return false;
}


private static int NumberOfNeighbors(boolean[][] cells, int r, int c) {

int deadOrAlive = cells[r][c] ? -1 : 0;
for(int i = r - 1; i <= r + 1; i++)
{
for(int j = c - 1; j <= c + 1; j++)
{
if( insideMatrix(cells, i, j) && cells[i][j] )
{
deadOrAlive++;
}
}
}

return deadOrAlive;
}


private static boolean insideMatrix(boolean[][] grid, int i, int j)
{
return i >= 0 && i < grid.length && j >= 0 &&
j < grid[0].length;
}

}

控制台输出:

0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

0 0 0 0 0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 0 0 0
0 0 1 0 1 0 1 0 0 0
0 1 1 1 0 1 1 1 0 0
0 0 1 0 1 0 1 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

最佳答案

您需要将其添加到条件行:

            if ( insideMatrix(board, i, j))
{
neighbors++;
}

变成:

        // we also need to make sure the neighbour is alive
if ( insideMatrix(board, i, j) && DeadOrAlive(board,i,j)==1)
{
neighbors++;
}

编辑:

此外,您还应该实现溢出算法(模拟球形世界),以便第 0 行上的元素将检查第 n 行而不是第 -1 行上的邻居

关于java - 在用 Java 编写康威生命游戏的邻居数量方法时遇到困难。格式化为 2D 数组,10 x 10 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24285471/

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