简体   繁体   中英

Fix Index out of bounds for array?

I am creating a maze game using console of eclipse. I am trying to move a player, and save the players new position so I can move him in another direction. However, every time I try to move the player, I get Index ___ out of bounds for length 5 (usually -1 or 5). It is skipping over the entire array, and I am unsure how to fix it.

When ran, it prints the following

P. . . .

. . . . .

. . . . .

. . . T .

. . ! ! .

Please select one of the following:

Press 1 to move up.

Press 2 to move down.

Press 3 to move left.

Press 4 to move right.

Press 0 to stop playing the game.

I only listed one movement, but the rest are the same, with interchanging variables. Every option returns Index out of bounds. And I was suggested to create a 'check' system but that doesn't solve the problem. It just runs the program without allowing any movements, because every movement returns out of bounds. I believe it has something to do with the line

mazeStructure[vxTemp][vyTemp] = mazeStructure[i][k-1];

Either that or something to do with the for conditions. If someone could provide a fix that would be greatly appreciated.

Scanner input = new Scanner(System.in);
int rowsTotal = 5;
int columnsTotal = 5;
int vxTemp = 0;
int vyTemp = 0;
String[][] mazeStructure =  {
        {"P",".",".","!","."},
        {".",".",".",".","."},
        {".",".",".","!","."},
        {"!","!",".","T","."},
        {"!",".","!","!","."},
};

 boolean won = false;
while (won == false){
    for (int a = 0; a < rowsTotal; a++){
        for (int b = 0; b < columnsTotal; b++){
            System.out.print(mazeStructure[a][b]);
            System.out.print(" ");
    }
        System.out.print("\n");
  }


    System.out.printf("\n");
    System.out.println("Please select one of the following:");
    System.out.println("Press 1 to move up.");
    System.out.println("Press 2 to move down.");
    System.out.println("Press 3 to move left.");
    System.out.println("Press 4 to move right.");
    System.out.println("Press 0 to stop playing the game.");
    int choice = input.nextInt();
    int a = 0;

   
    if (choice == 1 && a >= 0 && a < columnsTotal){
    for (int i = 0; i < rowsTotal; i++){
        for (int k = 0; k < columnsTotal; k++){
        if (mazeStructure[vxTemp][vyTemp-1].equals("!") == false && mazeStructure[i][k].equals(mazeStructure[vxTemp][vyTemp])){
                    mazeStructure[vxTemp][vyTemp] = ".";
                    mazeStructure[vxTemp][vyTemp-1] = "P";
                    mazeStructure[vxTemp][vyTemp] = mazeStructure[i][k-1];
       }else if (mazeStructure[vxTemp][vyTemp-1] == "!"){
                    System.out.println("Move isn't allowed.");
       }else {
        continue;}

In the line if (mazeStructure[vxTemp][vyTemp-1].equals(".") == false && mazeStructure[i][k].equals(mazeStructure[vxTemp][vyTemp])){ you try to access mazeStructure at [vxTemp][vyTemp-1] . You initialize vyTemp at 0 and it doesnt change before this point. You start that loop with trying vyTemp - 1 which will be -1. That will be out of bounds for your array.

You do that same with variable k within your loop. The first time around k will be zero. So all though k - 1 existing is ok, you can't try to access any array at some -1 index.

I guess the problem is vyTemp-1 in this line

if (mazeStructure[vxTemp][vyTemp-1].equals(".") == false && mazeStructure[i][k].equals(mazeStructure[vxTemp][vyTemp])){

The value of vyTemp is 0 initially

Scanner input = new Scanner(System.in);
int rowsTotal = 5;
int columnsTotal = 5;
int vxTemp = 0;
int vyTemp = 0;
String[][] mazeStructure =  {
        {"P",".",".","!","."},
        {".",".",".",".","."},
        {".",".",".","!","."},
        {"!","!",".","T","."},
        {"!",".","!","!","."},
};

 boolean won = false;
while (won == false){
    for (int a = 0; a < rowsTotal; a++){
        for (int b = 0; b < columnsTotal; b++){
            System.out.print(mazeStructure[a][b]);
            System.out.print(" ");
    }
        System.out.print("\n");
  }


    System.out.printf("\n");
    System.out.println("Please select one of the following:");
    System.out.println("Press 1 to move up.");
    System.out.println("Press 2 to move down.");
    System.out.println("Press 3 to move left.");
    System.out.println("Press 4 to move right.");
    System.out.println("Press 0 to stop playing the game.");
    int choice = input.nextInt();
    int a = 0;

   
    if (choice == 1 && a >= 0 && a < columnsTotal){
    for (int i = 0; i < rowsTotal; i++){
        for (int k = 0; k < columnsTotal; k++){

//There error in if condition. If your choice is 1. It will run this if condition 'mazeStructure[vxTemp][vyTemp-1].equals(".")'. As vyTemp = 0. vyTemp -1 < 0. Which will java.lang.ArrayIndexOutOfBoundsException

 if (mazeStructure[vxTemp][vyTemp-1].equals("!") == false && mazeStructure[i][k].equals(mazeStructure[vxTemp][vyTemp])){
                    mazeStructure[vxTemp][vyTemp] = ".";
                    mazeStructure[vxTemp][vyTemp-1] = "P";
                    mazeStructure[vxTemp][vyTemp] = mazeStructure[i][k-1];
       }else if (mazeStructure[vxTemp][vyTemp-1] == "!"){
                    System.out.println("Move isn't allowed.");
       }else {
        continue;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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