ArrayIndexOutOfBounds TicTacToe游戏中的异常

时间:2012-06-05 09:53:10

标签: java swt indexoutofboundsexception

我收到的错误是我不明白为什么。这是一个tictactoe游戏(n x n) 这是表类

public class Table {

private int columns;
private int rows;
private int wincells;
private PieceType[][] table;

public Table() {
}

public Table(int rows, int columns, int wins) {
    this.columns = columns;
    this.rows = rows;
    this.wincells = wins;
    table = new PieceType[rows][columns];
    fillEmpty();
}
public void fillEmpty() {
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < columns; ++j) {
            table[i][j] = PieceType.None;
        }
    }
}

public PieceType getElement(int i, int j) {
    return table[i][j];
}

它的getElement()方法在我调用函数后给出错误  game.move(e.x,e.y);

public void widgetSelected(SelectionEvent e) {
        Button button = (Button) e.getSource();
        MoveResult moveResult = game.move(e.x, e.y);
        if (game.getCurrentPlayer().getPieceType() == PieceType.Cross)
            button.setText("x");
        else
            button.setText("0");
        switch (moveResult) {
        case ValidMove: {
            buttonTable[gridData.horizontalIndent][gridData.verticalIndent]
                    .setText("X");
            game.changePlayer();
        }
        case WinMatch:
            disableButtons();

        case Draw:
            disableButtons();
        }

这是x和Y得到值的地方

   for (int i = 0; i < rows; ++i)
        for (int j = 0; j < cols; ++j) {

            gridData.heightHint = 45;
            gridData.widthHint = 45;

            Button button = new Button(buttonpanel, SWT.PUSH);
            button.setLayoutData(gridData);
            button.setData(new Cell(i,j));
            buttonTable[i][j] = button;
            buttonTable[i][j]
                    .addSelectionListener(new buttSelectionListener());

关于问题可能是什么的任何想法?它来自game.move(e.x,e.y)?我没有正确称呼它吗?

堆栈跟踪:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at backview.Table.getElement(Table.java:42)
at backview.Game.move(Game.java:56)
at frontview.MainGui$buttSelectionListener.widgetSelected(MainGui.java:159)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at frontview.MainGui.<init>(MainGui.java:55)
at main.Main.main(Main.java:18)

这是调用

的方法
if(table.getElement(x, y) != PieceType.None) return MoveResult.InvalidMove;

3 个答案:

答案 0 :(得分:2)

您可能希望从Cell数据中获取Button并使用i,j坐标来调用getElement()

注意:您的Table课程有几条评论。如果您已经知道表的初始大小,则默认构造函数没有意义。如果是这种情况,请制作columnsrowswincells final,以便在游戏过程中无法对其进行修改。另外,检查getElement()中提供的坐标是否在数组范围内:

public PieceType getElement(int i, int j) {
    if ((i < 0) || (i >= rows) ||
        (j < 0) || (j >= columns)) {
        return null;
    }

    return table[i][j];
}

答案 1 :(得分:0)

e.x e.y的{​​{1}}和SelectionEvent是鼠标点击的坐标,而不是网格。您应该从e数据中获取Cell,然后将其用于您的游戏移动。 您可以像这样修复Button方法:

widgetSelected

答案 2 :(得分:0)

import java.util.Scanner;

public class TicTacToe_Game {

    public static void main(String[] args) {
        Scanner Myinput = new Scanner(System.in);
        char[][] board = new char[3][3];

        boolean Gameover;
        Gameover = CalculateWinner(board);
        while(!Gameover){

        displayBoard(board);
        System.out.println("Enter row and column for player X");
        int rowX, columnX;
        rowX=Myinput.nextInt();
        columnX= Myinput.nextInt();
        boolean successX = setMove(rowX , columnX ,board,'X');
        while(!successX){
            System.out.println("INVALID MOVE FOR PLAYER X");
            System.out.println("Re-Enter row and column for player X");
            rowX=Myinput.nextInt();
            columnX= Myinput.nextInt();
            successX = setMove(rowX , columnX ,board,'X');
        }
        Gameover= CalculateWinner(board);

        displayBoard(board);
        System.out.println();
        System.out.println("Enter row and column for player O");
        int rowO, columnO;
        rowO=Myinput.nextInt();
        columnO= Myinput.nextInt();
        boolean successO= setMove(rowO , columnO ,board,'O');
        while(!successO){
            System.out.println("invalid Move");
            System.out.println("Re-Enter row and column for player O");
            rowO=Myinput.nextInt();
            columnO= Myinput.nextInt();
            successO = setMove(rowO , columnO ,board,'0');
        }
        Gameover=CalculateWinner(board);
        }
    }
    public static boolean setMove(int row, int column,char[][] board,char player){
        if(board[row][column]== 'X' || board[row][column]== 'O' ){
            return false;
            }
            else if (player =='X'){
            board[row][column] = 'X';
            return true;
            }
            else{
            board[row][column] = 'O';
    return true;
            }
    }
    public static void displayBoard(char[][]board){
        System.out.println("-------------------");
        System.out.println("|"+board[0][0]+"|"+board[0][1]+"|"+board[0][2]+"|");
        System.out.println("-------------------");
        System.out.println("|"+board[1][0]+"|"+board[1][1]+"|"+board[1][2]+"|");
        System.out.println("-------------------");
        System.out.println("|"+board[2][0]+"|"+board[2][1]+"|"+board[2][2]+"|");
        System.out.println("-------------------");
    }
    public static boolean CalculateWinner(char[][] board){
        boolean filled =false;
        for(int column=0; column<=2; column++){
            for(int row =0; row<=2; row++){
                if (board[column][row]!='X'&& board[column][row]!='O'){
                    filled = false;

                }
                else
                { filled = true;
            }
        }
    }
        if (filled){
            return true;
        }else {
            return false;
        }
}
}