-1

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

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(ex, ey) 调用函数后给出错误;

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(ex,ey)吗?我没有正确调用它吗?

堆栈跟踪?:

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

3 回答 3

2

您可能想CellButton数据中获取 并使用i,j坐标来调用getElement().

注意:关于您的Table课程的一些评论。如果您已经知道表的初始大小,则默认构造函数没有意义。如果是这种情况,请 make columnsrows因此wincells final它们不能在游戏期间进行修改。另外,检查提供的坐标getElement()是否在数组范围内:

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

    return table[i][j];
}
于 2012-06-05T10:23:05.790 回答
0

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

Cell c = (Cell) button.getData();
// Then use i and j of the cell for the game move
game.move(c.i, c.j); // or something similar
于 2012-06-05T10:00:20.553 回答
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;
        }
}
}
于 2016-03-28T21:15:47.580 回答