2012-06-05 51 views
-1

我收到一个错误,我并不真正理解为什么。这是一个井字游戏(N×n)个 这里是表类TicTacToe游戏中的ArrayIndexOutOfBounds异常

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; 
+0

在哪里/你是如何调用'getElement'方法? – npinti

+0

请发布堆栈跟踪。它显示了问题发生在哪一行。此外,使用默认构造函数可能会导致问题,因为您的字段不会被初始化。提供默认值或不提供默认构造函数是个好主意。 – toniedzwiedz

回答

2

你可能想从Button数据Cell并使用i,j坐标调用getElement()

注意:关于您的Table类的一些评论。如果您已经知道表的初始大小,那么默认的构造函数没有意义。如果是这种情况,请制作columnsrowswincellsfinal,以便在游戏过程中不能进行修改。此外,检查中getElement()提供的坐标是数组边界内:

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

    return table[i][j]; 
} 
+0

整理好了,谢谢。 –

0

SelectionEventee.xe.y鼠标点击的是坐标的方法,而不是电网。您应该从Button数据中获得Cell,然后将其用于您的游戏移动。 您可以修复你的widgetSelected方式类似:

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 
+0

我试过你的方式,可能有些工作,因为现在我得到相同的例外,但2号相同的方法。 –

+0

排序出来,谢谢。 –

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

你能向OP解释你的代码吗? –