0

首先,感谢您的到来并花时间帮助解决问题。

我是一个完整的菜鸟(第 3 天编程),这解释了为什么我花了无数令人头疼的时间试图解决这个问题,但仍然没有弄清楚:

问题:如何扫描由字符串(* 和 _)组成的 .txt 文件并将其转换为布尔数组(即 * = true 和 _ = false)(以及如何打印出来?我是猜测我们也需要一个双循环)?扫描仪似乎无法在主要方法中工作,并且出现“没有此类文件”错误。

额外问题:您如何迭代网格(例如使用 for 循环)以使新网格替换旧网格并且最新网格不间断地成为新网格?我不熟悉“show()”方法,但我有点让它工作。

这是我的不完整代码,可让您了解问题:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class GameOfLife {

    public static boolean[][] gen() throws IOException {

        Scanner scanner = new Scanner(new File("seed.txt"));
        int rows = 0, columns = 0;
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            if (s.length() > columns)
                columns = s.length();
            rows++;
        }

        boolean[][] grid = new boolean[rows][columns + 1];
        Scanner scanner1 = new Scanner(new File("seed.txt"));
        String line;        
        for (int r = 0; r < rows; r++) {
            line = scanner1.nextLine();
            System.out.println(line);
            for (int c = 0; c <= line.length(); c++) {
                if (r == line.length()) {
                    break;
                }
                if(line.charAt(r) == '*') {
                grid[r][c] = true;
                }
            }
        }
        return grid;
    }

    public static boolean[][] nextGen(boolean[][] cells){
        boolean[][] newCells = new boolean[cells.length][cells[0].length];
        int num;
        for(int r = 0; r < cells.length; r++){
            for(int c = 0; c < cells[0].length; c++){
                num = numNeighbours(cells, r, c);
                if (occupiedNext(num, cells[r][c]))
                    newCells[r][c] = true;
            }
        }
        return newCells;
    }

    public static void main(String[] args) throws IOException{      
        boolean[][] cells = gen();
        show(cells);
        cells = nextGen(cells);
        show(cells);
        Scanner scanner2 = new Scanner(new File("seed.txt"));
        while(scanner2.nextLine().length() != 0){
            cells = nextGen(cells);
            show(cells);
        }
    }

    private static boolean inbounds(boolean[][] cells, int r, int c) {
        return r >= 0 && r < cells.length && c >= 0 &&
        c < cells[0].length;
    }

    private static int numNeighbours(boolean[][] cells, int row, int col) {
        int num = cells[row][col] ? -1 : 0;
        for (int r = row - 1; r <= row + 1; r++)
            for(int c = col - 1; c <= col + 1; c++)
                if (inbounds(cells, r, c) && cells[r][c] )
                    num++;
        return num;
    }

    public static void show(boolean[][] grid){
        String s = "";
        for(boolean[] row : grid){
            for(boolean val : row) 
                if(val)
                    s += "*";
                else
                    s += "_";
            s += "\n";
        }
        System.out.println(s);
    }

    public static boolean occupiedNext(int numNeighbours, boolean occupied){
        if (occupied && (numNeighbours == 2 || numNeighbours == 3))
            return true;
        else if (!occupied && numNeighbours == 3)
            return true;
        else
            return false;
    } 
}
4

1 回答 1

0

我只是想提出一个可能对您有所帮助的解决方案。尝试读取文件内容并分配给字符串变量。现在使用string.replaceAll方法(* and _)to * = true and _ = false。比将更新的字符串写入文件。

于 2014-11-23T15:19:38.543 回答