数组索引越界Java生活游戏?

时间:2015-10-22 00:18:24

标签: java arrays indexoutofboundsexception conways-game-of-life

我正在尝试为我的java编程课程做生命游戏项目,我认为我已经把一切都搞定了,但出于某种原因,我仍然会遇到一个异常的例外,即使我不知道#39; t的数字大于索引。想知道为什么会抛出这个错误?

代码:

项目4:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Project4 {

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in); // Created a scanner
        System.out.println("Enter the file name you would like to use");
        File file = new File(input.nextLine()); // Takes file name to find file
        Scanner inputFromFile = new Scanner(file);
        FileInputStream fileInput = new FileInputStream(file); // reads file
        int r;
        int y = 0;
        int i = 0;
        while ((r = fileInput.read()) != -1) { // goes through each character in
                                                // file, char by char
            char c = (char) r;
            GameOfLife.grid[i][y] = c;
            y++;
            if (y == 75) {
                y = 0;
                i++;
                if (i == 25) {
                    break;
                }
            }
        }
        // Prints the initial environment
        System.out.println("Initial set: ");
        for (int j = 0; j < GameOfLife.grid.length; j++) {
            System.out.print(GameOfLife.grid[j]);
        }

        System.out.println("Do you want to see the next generation? Y/N?");
        String q = input.nextLine();
        if (q.equalsIgnoreCase("y")) {
            GameOfLife.evolve();
        } else {
            System.exit(0);
        }
    }
}

GameOfLife:

import java.util.Arrays;

public class GameOfLife {

    static final int m = 25; // number of rows
    static final int n = 75; // number of columns
    static char[][] grid = new char[m][n]; // Creates an empty (no dots or
                                            // X's)grid of rows and columns.

    static int count_neighbors(int i, int j) {
        int nn = 0; // number of neighbors of cell(i,j)

        if (grid[i - 1][j - 1] == 'X') {
            nn++;
        }
        ;
        if (grid[i - 1][j] == 'X') {
            nn++;
        }
        ;
        if (grid[i - 1][j + 1] == 'X') {
            nn++;
        }
        ;
        if (grid[i][j - 1] == 'X') {
            nn++;
        }
        ;
        if (grid[i][j + 1] == 'X') {
            nn++;
        }
        ;
        if (grid[i + 1][j - 1] == 'X') {
            nn++;
        }
        ;
        if (grid[i + 1][j] == 'X') {
            nn++;
        }
        ;
        if (grid[i + 1][j + 1] == 'X') {
            nn++;
        }

        return nn;
    }

    static void evolve() {
        for (int i = 0; i < 23; i++) {
            for (int j = 0; j < 73; j++) {
                int s = count_neighbors(i, j);
                if (s < 2) {
                    grid[i][j] = '.';
                }
                if (s == 2 || s == 3) {
                    grid[i][j] = 'X';
                }
                if (s > 3) {
                    grid[i][j] = '.';
                }
            }
            for (int j = 0; j < GameOfLife.grid.length; j++)
                System.out.print(GameOfLife.grid[j]);
        }

    }

}

2 个答案:

答案 0 :(得分:1)

要扩展安东的帖子,虽然你需要计算角落里的邻居,你必须考虑到你在角落的事实并且只能计算某些方面:

static int count_neighbors(int i, int j) {
    int nn = 0; // number of neighbors of cell(i,j)

    if (i > 0 && j > 0 && grid[i - 1][j - 1] == 'X') {
        nn++;
    }
    ;
    if (i > 0 && grid[i - 1][j] == 'X') {
        nn++;
    }
    ;
    if (i > 0 && j < 72 && grid[i - 1][j + 1] == 'X') {
        nn++;
    }
    ;
    if (j > 0 && grid[i][j - 1] == 'X') {
        nn++;
    }
    ;
    if (j < 72 && grid[i][j + 1] == 'X') {
        nn++;
    }
    ;
    if (j > 0 && i < 22 && grid[i + 1][j - 1] == 'X') {
        nn++;
    }
    ;
    if (i < 22 && grid[i + 1][j] == 'X') {
        nn++;
    }
    ;
    if (i < 22 && j < 72 && grid[i + 1][j + 1] == 'X') {
        nn++;
    }

    return nn;
}

答案 1 :(得分:0)

您在count_neighbors方法中拨打{0}的evolve。顺便说一句,我很确定,编译器会为您提供抛出此异常的行。