Closed. This question needs debugging details。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
我正在为Conway的Java生活游戏进行课堂作业。要求是使用10 x 10 2D矩阵在Java中编写矩阵。
我在下面编写了以下代码。
我看不到为什么
控制台输出:
正确的解决方案-
控制台输出:
变成:
编辑:
另外,您应该实现溢出算法(模拟球形世界),以便第0行的元素将检查第n行而不是第-1行的邻居
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
我正在为Conway的Java生活游戏进行课堂作业。要求是使用10 x 10 2D矩阵在Java中编写矩阵。
我在下面编写了以下代码。
NumberOfNeighbors(int[][] board, int row, int col)
方法不起作用,我不确定为什么。我已经使用布尔2D数组矩阵解决了该问题,并且正在研究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 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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 0 0 0 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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 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 1 0 0 0 0 0
0 0 0 1 1 1 0 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编写Conway的《人生游戏》的邻居数方法时遇到麻烦。在2D数组中以10 x 10矩阵格式化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24285471/
10-09 15:58