简体   繁体   English

用Java实现Conway的生活游戏

[英]Implement Conway's Game of Life in Java

The way I have to implement this game is by reading in a file of ones and zeros and then determining the next generation and updating the board accordingly. 我要实现此游戏的方法是读取文件“ 1”和“ 0”,然后确定下一代产品并相应地更新开发板。 This is what I have so far. 到目前为止,这就是我所拥有的。 I am required to use the five methods given, so I cant change that, I can add some if necessary but I can't get rid of any. 我必须使用给定的五种方法,所以我无法更改它,可以在必要时添加一些方法,但是我无法摆脱任何方法。 I don't know how I'm supposed to update the existing integer array in the update array by only passing it one 2d integer array. 我不知道如何仅通过传递一个2d整数数组来更新update数组中的现有整数数组。 I tried writing the conditionals in the main method to solve this but I'm completely stuck. 我试图在main方法中编写条件语句来解决此问题,但是我完全陷入了困境。 Any advice would be much appreciated. 任何建议将不胜感激。

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);
            }
        }
    }
}
}

the life4.txt file looks like this: life4.txt文件如下所示:

25 77 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000100000000000000000000000000000000000000000000000000000 00000000000000000000001010000000000000000000000000000000000000000000000000000 00000000000000000000011111000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000010001000000000000000000000000000000000000000000000000000 00000000000000000000110001100000000000000000000000000000000000000000000000000 00000000000000000001010001010000000000000000000000000000000000000000000000000 00000000000000000000110001100000000000000000000000000000000000000000000000000 0000000000000000000001000100000000000000000000000000000000 25 77 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000100000000000000000000000000000000000000000000000000000 00000000000000000000001010000000000000000000000000000000000000000000000000000 00000000000000000000011111000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000010001000000000000000000000000000000000000000000000000000 00000000000000000000110001100000000000000000000000000000000000000000000000000 00000000000000000001010001010000000000000000000000000000000000000000000000000 00000000000000000000110001100000000000000000000000000000000000000000000000000 0000000000000000000001000100000000000000000000000000000000 0000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000011111000000000000000000000000000000000000000000000000000 00000000000000000000001010000000000000000000000000000000000000000000000000000 00000000000000000000000100000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000011111000000000000000000000000000000000000000000000000000 00000000000000000000001010000000000000000000000000000000000000000000000000000 00000000000000000000000100000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000

Your update method needs to receive an array, make a temp array, set that temp array's data based on the received array, and return the temp array. 您的update方法需要接收一个数组,创建一个临时数组,根据接收到的数组设置该临时数组的数据,然后返回该临时数组。 So move the code that updates the array out of your main method, and put it into update. 因此,将更新数组的代码移出main方法,然后将其更新。

Then, in your main method, read the original grid exactly as you do already, and then call update repeatedly: 然后,在您的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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM