- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试Game of Life - 我所坚持的是根据空间周围的区域来确定空间是否应该是活的还是死的。我现在无法判断自己做错了什么 - 希望另一双眼睛可以提供帮助。
这只是一个片段,完整代码在这里:http://pastebin.com/ucYe653p
public static void updateMatrix(boolean[][] board, int[] birthLive) {
//clone the board so modified values don't affect the results of upcoming spaces
boolean[][] boardCopy = board.clone();
for (int i = 0; i < board.length; i++)
boardCopy[i] = board[i].clone();
int count = 0;
for (int i = 1; i < board.length - 1; i++) {
for (int j = 1; j < board[i].length - 1; j++) {
//different requirements for dead or living pieces' living status
if (board[i][j] == false) {
//check the nine-by-nine square, starting at (-1, 1) and ending at (1, -1) where 0 is the affected location
// * * *
// * 0 *
// * * *
for (int ii = board[i - 1].length; ii < board[i + 1].length; ii++) {
for (int jj = board[j - 1].length; ii < board[j + 1].length; jj++) {
if (boardCopy[i][j] == true)
count++;
}
}
//check to see what high and low amt of required adjacent lifeforms is
if (count >= birthLive[0] && count <= birthLive[1])
board[i][j] = true;
else
board[i][j] = false;
count = 0;
}
else {
for (int ii = board[i - 1].length; ii < board[i + 1].length; ii++) {
for (int jj = board[j - 1].length; ii < board[j + 1].length; jj++) {
if (boardCopy[i][j] == true)
count++;
}
}
count -= 1; //For board[i][j] is always true
if (count >= birthLive[2] && count <= birthLive[3])
board[i][j] = true;
else
board[i][j] = false;
count = 0;
}
}
}
}
最佳答案
你是完整的代码:
import java.util.*;
public class Update {
public final static int ITERATIONS = 5;
public static void main(String[] args) {
System.out.println("This program will simulate the game of Life.");
Scanner console = new Scanner(System.in);
System.out.println("Please input the size of your board.");
System.out.println("Rows:");
final int rows = console.nextInt();
System.out.println("Columns:");
final int columns = console.nextInt();
System.out.println("Please enter a seed:");
final long seed = console.nextLong();
int[] birthLive = new int[4];
boolean[][] board = new boolean[rows][columns];
createMatrix(board, seed);
birthAndLive(birthLive);
for (int i = 0; i < ITERATIONS; i++) {
printMatrix(board);
updateMatrix(board, birthLive);
}
}
public static void createMatrix(boolean[][] board, long seed) {
Random seedBool = new Random(seed);
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = false;
}
}
for (int i = 1; i < board.length - 1; i++) {
for (int j = 1; j < board[i].length - 1; j++) {
board[i][j] = seedBool.nextBoolean();
}
}
}
public static void birthAndLive(int[] birthLive) {
Scanner console = new Scanner(System.in);
System.out.println("Please input the birth range and the live range:");
System.out.println("Birth (Low):");
birthLive[0] = console.nextInt();
System.out.println("Birth (High):");
birthLive[1] = console.nextInt();
System.out.println("Live (Low):");
birthLive[2] = console.nextInt();
System.out.println("Live (High):");
birthLive[3] = console.nextInt();
}
public static void printMatrix(boolean[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == false)
System.out.print(" - ");
else
System.out.print(" # ");
}
System.out.println();
}
System.out.println();
}
public static void updateMatrix(boolean[][] board, int[] birthLive) {
// clone the board so modified values don't affect the results of upcoming
// spaces
boolean[][] boardCopy = board.clone();
for (int i = 0; i < board.length; i++)
boardCopy[i] = board[i].clone();
int count = 0;
for (int i = 1; i < board.length - 1; i++) {
for (int j = 1; j < board[i].length - 1; j++) {
// different requirements for dead or living pieces' living status
if (board[i][j] == false) {
// check the nine-by-nine square, starting at (-1, 1) and ending
// at (1, -1) where 0 is the affected location
// * * *
// * 0 *
// * * *
for (int ii = board[i - 1].length; ii < board[i + 1].length; ii++) {
for (int jj = board[j - 1].length; ii < board[j + 1].length; jj++) {
if (boardCopy[i][j] == true)
count++;
}
}
// check to see what high and low amt of required adjacent
// lifeforms is
if (count >= birthLive[0] && count <= birthLive[1])
board[i][j] = true;
else
board[i][j] = false;
count = 0;
}
else {
for (int ii = board[i - 1].length; ii < board[i + 1].length; ii++) {
for (int jj = board[j - 1].length; ii < board[j + 1].length; jj++) {
if (boardCopy[i][j] == true)
count++;
}
}
count -= 1; // For board[i][j] is always true
if (count >= birthLive[2] && count <= birthLive[3])
board[i][j] = true;
else
board[i][j] = false;
count = 0;
}
}
}
}
}
您正在 updateMatrix 方法的主体中设置 board[...] 值,不应该这样做,因为它会过早影响相邻单元格的计算。相反,仅在此方法的主体中设置克隆数组的单元格。然后在该方法的末尾要么迭代克隆板,将其结果复制到原始板中,要么返回克隆板,并在调用该方法时使用它将其结果设置到板中。 .即,
board = updateMatrix(board, birthLive);
关于Java——生命游戏;生命体检测的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9170201/
我正在开发一个使用多个 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
我是一名优秀的程序员,十分优秀!