- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 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 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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 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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
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 编写康威生命游戏的邻居数量方法时遇到困难。格式化为 2D 数组,10 x 10 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24285471/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!