“Java 2048”中的矩阵随机OutOfBoundException

时间:2014-04-13 19:12:20

标签: java matrix

我正在努力学习Java,我正在挑战自己重写“2048”游戏(如果你不知道它是如何工作的那样看看here

所以我有一个名为'mat'的4x4矩阵,其中包含4个方向移动数字的游戏编号和4种方法:

private int[][] mat = new int[4][4];

和movingUp方法:

    private boolean MoveUp() {
    System.out.println("Moving Up");

    boolean nextRow;
    boolean moved = false;

    for(int j = 0; j < 4; j++){
        boolean[] collapsed = new boolean[3];
        do{
            nextRow = true;
            for(int i = 0; i < 3; i++){
                if((mat[i][j] == 0) && (mat[i+1][j] != 0)){
                    mat[i][j] = mat[i+1][j];
                    mat[i+1][j] = 0;
                    nextRow = false;
                    moved = true;
                } else if((mat[i][j] != 0) && (mat[i][j] == mat[i+1][j]) && !collapsed[i] && !collapsed[i+1]){
                    mat[i][j] += mat[i+1][j];
                    mat[i+1][j] = 0;
                    collapsed[i] = true;
                    nextRow = false;
                    moved = true;
                }
            }
        } while(!nextRow);
    }

    return moved;
}

但是经过一些比赛后我得到了这个输出:

---------------------
|    |    |    |    |
|    |    |    |   4|
|    |    |    |    |
|----|----|----|----|
|    |    |    |    |
|    |   2|   8|  64|
|    |    |    |    |
|----|----|----|----|
|    |    |    |    |
|    |    |   4|  32|
|    |    |    |    |
|----|----|----|----|
|    |    |    |    |
|    |    |   8|  32|
|    |    |    |    |
---------------------
w
Moving Up
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at Game.MoveUp(Game.java:91)
    at Game.Move(Game.java:35)
    at Game.<init>(Game.java:14)
    at Game.main(Game.java:210)

我真的不明白为什么,任何人都可以帮助我? :)


以下是完整代码:

import java.util.Scanner;

public class Game {

private int[][] mat = new int[4][4];

private boolean gameover = false;

public Game(){
    GenRand();
    GenRand();
    do{
        PrintMat();
        Move();
        gameover = CheckGameover();
        if(!gameover) GenRand();
    } while(!gameover);
    System.out.println("Game Over!");
}

private void Move(){
    boolean done = false;
    do{
        Scanner in = new Scanner(System.in);
        char input = in.next().charAt(0);

        switch(input){
            case ('a'):
                done = MoveLeft();
                break;
            case ('d'):
                done = MoveRight();
                break;
            case ('w'):
                done = MoveUp();
                break;
            case ('s'):
                done = MoveDown();
                break;
            default: System.out.println("Wrong Input");
        }
    } while(!done);
}

private boolean MoveDown() {
    System.out.println("Moving Down");

    boolean nextRow;
    boolean moved = false;

    for(int j = 0; j < 4; j++){
        boolean[] collapsed = new boolean[4];
        do{
            nextRow = true;
            for(int i = 3; i > 0; i--){
                if((mat[i][j] == 0) && (mat[i-1][j] != 0)){
                    mat[i][j] = mat[i-1][j];
                    mat[i-1][j] = 0;
                    nextRow = false;
                    moved = true;
                } else if((mat[i][j] != 0) && (mat[i][j] == mat[i-1][j]) && !collapsed[i] && !collapsed[i-1]){
                    mat[i][j] += mat[i-1][j];
                    mat[i-1][j] = 0;
                    collapsed[i] = true;
                    nextRow = false;
                    moved = true;
                }
            }
        } while(!nextRow);
    }

    return moved;
}

private boolean MoveUp() {
    System.out.println("Moving Up");

    boolean nextRow;
    boolean moved = false;

    for(int j = 0; j < 4; j++){
        boolean[] collapsed = new boolean[3];
        do{
            nextRow = true;
            for(int i = 0; i < 3; i++){
                if((mat[i][j] == 0) && (mat[i+1][j] != 0)){
                    mat[i][j] = mat[i+1][j];
                    mat[i+1][j] = 0;
                    nextRow = false;
                    moved = true;
                } else if((mat[i][j] != 0) && (mat[i][j] == mat[i+1][j]) && !collapsed[i] && !collapsed[i+1]){
                    mat[i][j] += mat[i+1][j];
                    mat[i+1][j] = 0;
                    collapsed[i] = true;
                    nextRow = false;
                    moved = true;
                }
            }
        } while(!nextRow);
    }

    return moved;
}

private boolean MoveRight() {
    System.out.println("Moving Right");

    boolean nextRow;
    boolean moved = false;

    for(int i = 0; i < 4; i++){
        boolean[] collapsed = new boolean[4];
        do{
            nextRow = true;
            for(int j = 3; j > 0; j--){
                if((mat[i][j] == 0) && (mat[i][j-1] != 0)){
                    mat[i][j] = mat[i][j-1];
                    mat[i][j-1] = 0;
                    nextRow = false;
                    moved = true;
                } else if((mat[i][j] != 0) && (mat[i][j] == mat[i][j-1]) && !collapsed[j] && !collapsed[j-1]){
                    mat[i][j] += mat[i][j-1];
                    mat[i][j-1] = 0;
                    collapsed[j] = true;
                    nextRow = false;
                    moved = true;
                }
            }
        } while(!nextRow);
    }

    return moved;
}

public boolean MoveLeft(){
    System.out.println("Moving Left");

    boolean nextRow;
    boolean moved = false;

    for(int i = 0; i < 4; i++){
        boolean[] collapsed = new boolean[3];
        do{
            nextRow = true;
            for(int j = 0; j < 3; j++){
                if((mat[i][j] == 0) && (mat[i][j+1] != 0)){
                    mat[i][j] = mat[i][j+1];
                    mat[i][j+1] = 0;
                    nextRow = false;
                    moved = true;
                } else if((mat[i][j] != 0) && (mat[i][j] == mat[i][j+1]) && !collapsed[j] && !collapsed[j+1]){
                    mat[i][j] += mat[i][j+1];
                    mat[i][j+1] = 0;
                    collapsed[j] = true;
                    nextRow = false;
                    moved = true;
                }
            }
        } while(!nextRow);
    }

    return moved;
}

private boolean CheckGameover(){
    System.out.println("Checking Gameover");
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            if(mat[i][j] == 0) return false;
            else if((i != 0) && (mat[i][j] == mat[i-1][j])) return false;
            else if((i != 3) && (mat[i][j] == mat[i+1][j])) return false;
            else if((j != 0) && (mat[i][j] == mat[i][j-1])) return false;
            else if((j != 3) && (mat[i][j] == mat[i][j+1])) return false;
        }
    }
    return true;
}

private void GenRand(){
    boolean done = false;
    while(!done){
        int n = (int)(Math.random() * 4);
        int m = (int)(Math.random() * 4);
        if(mat[n][m] == 0){
            mat[n][m] = ((int)((Math.random() *2) + 1) *2); //Random 2 or 4
            done = true;
        }
    }
}

private void PrintMat() {
    System.out.println("---------------------");
    System.out.println("|    |    |    |    |");
    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 4; j++) {
            if(mat[i][j] != 0) System.out.print(String.format("|%4d", mat[i][j]));
            else System.out.print("|    ");
        }
        System.out.println("|\n|    |    |    |    |");
        if(i != 3) {
            System.out.println("|----|----|----|----|");
            System.out.println("|    |    |    |    |");
        } else {
            System.out.println("---------------------");
        }
    }
}

public static void main(String[] args) {
    Game newGame = new Game();
}
}

更新: 我有另一个问题:我从未进入“游戏结束!”,可能checkGameover()方法不起作用,但我找不到原因......

1 个答案:

答案 0 :(得分:1)

看起来问题是:

boolean[] collapsed = new boolean[3];

的尺寸应为4,而不是3。