我必须实现这个游戏的方法是读入1和0的文件,然后确定下一代并相应地更新电路板。这就是我到目前为止所拥有的。我需要使用给出的五种方法,所以我无法改变它,如果有必要我可以添加一些,但我无法摆脱任何。我不知道如何通过只传递一个2d整数数组来更新更新数组中的现有整数数组。我尝试在main方法中编写条件来解决这个问题,但我完全被卡住了。任何建议都将不胜感激。
import java.util.*;
import java.io.*;
public class Proj5{
public static void main(String[] args)throws FileNotFoundException, NumberFormatException{
int[][] grid = readBoard("life4.txt");
System.out.print(boardDisplay(grid));
System.out.println();
int rows = grid.length;
int cols = grid[0].length;
int[][] temp = new int [rows][cols];
//Any live cell with fewer than two live neighbors dies of loneliness
//Any live cell with more than three live neighbors dies of overcrowding
//Any dead cell with exactly three live neighbors comes to life
//Any live cell with two or three neighbors stays alive
//Any dead cell that does not have exactly three live neighbors remains dead
for(int i = 1; i < rows - 1; i++){
for (int j = 1; j < cols - 1; j++){
int friends = neighbors(grid, rows, cols);
System.out.print(friends);
if(grid[i][j] == 1 && friends < 2){
temp[i][j] = 0;
}
if(grid[i][j] == 1 && friends > 3){
temp[i][j] = 0;
}
/*else if(grid[i][j] == 0 && friends == 3){
grid[i][j] = 1;
}
else if(grid[i][j] == 1 && (friends == 2 || friends == 3)){
grid[i][j] = 1;
}*/
}
}
System.out.print(boardDisplay(temp));
}
//This method should read the specified input file, read it into an int[][] array, and return that array.
public static int[][] readBoard(String filename)throws FileNotFoundException, NumberFormatException{
Scanner inFile = new Scanner(new File(filename));
int rows = Integer.parseInt(inFile.nextLine());
int columns = Integer.parseInt(inFile.nextLine());
int[][] board = new int [rows + 2][columns + 2];
for(int i = 1; i < rows - 1; i++){
String newLine = inFile.nextLine();
String[] elements = newLine.split("");
for(int j = 1; j < columns - 1; j++){
board[i][j] = Integer.parseInt(elements[j]);
}
}
return board;
}
//This method should return the String representing the cells array (so that it would print as a grid if printed).
public static String boardDisplay(int[][] cells){
String st = "";
for(int i = 1; i < cells.length - 1; i++){
for(int j = 1; j < cells[0].length - 1; j++){
if(cells[i][j] == 0){
st += ".";
}
else{
st += "*";
}
}
st += "\n";
}
return st;
}
//This method should return the number of live neighbors that position (row,col) has in the cells array.
public static int neighbors(int[][] cells, int row, int col){
int counter = 0;
for(int i = 1; i < row - 1; i++){
for(int j = 1; j < col - 1; j++){
if(cells[i-1][j-1] == 1){
counter++;
}
if(cells[i-1][j] == 1){
counter++;
}
if(cells[i-1][j+1] == 1){
counter++;
}
if(cells[i][j-1] == 1){
counter++;
}
if(cells[i][j+1] == 1){
counter++;
}
if(cells[i+1][j-1] == 1){
counter++;
}
if(cells[i+1][j] == 1){
counter++;
}
if(cells[i+1][j+1] == 1){
counter++;
}
System.out.print(counter);
}
System.out.println();
}
return counter;
}
//This method should return the next generation of the cells array.
public static int[][] update(int[][] cells){
for(int i = 1; i < rows - 1; i++){
for int(j = 1; j < cols - 1; j++){
if(grid[i][j] == 1 && friends < 2){
grid[i][j] == update(grid);
}
}
}
}
}
life4.txt文件如下所示:
25 77 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000100000000000000000000000000000000000000000000000000000 00000000000000000000001010000000000000000000000000000000000000000000000000000 00000000000000000000011111000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000010001000000000000000000000000000000000000000000000000000 00000000000000000000110001100000000000000000000000000000000000000000000000000 00000000000000000001010001010000000000000000000000000000000000000000000000000 00000000000000000000110001100000000000000000000000000000000000000000000000000 00000000000000000000010001000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000011111000000000000000000000000000000000000000000000000000 00000000000000000000001010000000000000000000000000000000000000000000000000000 00000000000000000000000100000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000
答案 0 :(得分:0)
您的update
方法需要接收数组,生成临时数组,根据接收到的数组设置临时数组的数据,然后返回临时数组。因此,将更新数组的代码移出main方法,然后将其更新。
然后,在您的main方法中,完全按照原样读取原始网格,然后重复调用update:
int[][] grid = readBoard("life4.txt");
// println original grid
System.out.println(boardDisplay(grid));
for(int i=0; i < 10; i++){ // 10 iterations
grid = update(grid);
System.out.println(boardDisplay(grid));
}