简体   繁体   English

数组索引超出Java游戏的生命范围?

[英]Array index out of bounds Java game of life?

I'm trying to do the game of life project for my java programming class, and I think i've got everything right, but for some reason i keep getting an out of bounds exception even though i don't have any numbers greater than the indexes. 我正在尝试为我的Java编程类做一些“生活游戏”项目,我认为我做对了所有事情,但是由于某种原因,即使我没有任何数字大于该值,我仍然会遇到异常索引。 Wondering why it is throwing this error? 想知道为什么会引发此错误?

Code: 码:

Project 4: 专案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]);
        }

    }

}

To expand on Anton's post, while you need to count the neighbors on the corners still, you have to account for the fact that you are in the corner and can only count certain sides: 要扩展Anton的职位,虽然您需要仍然指望拐角处的邻居,但必须考虑到您在拐角处并且只能指望某些边的事实:

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

You call count_neighbors on (0,0) in your evolve method. 你叫count_neighbors在您的(0,0) evolve方法。 By the way, I am pretty sure, that compiler gives you the line where this exception was thrown. 顺便说一下,我很确定,该编译器为您提供了抛出此异常的行。

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

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