Java Tictaoe游戏中的ArrayIndexOutOfBounds异常
我犯了一个错误,我真的不明白为什么。这是一个小游戏 这是课桌Java Tictaoe游戏中的ArrayIndexOutOfBounds异常,java,swt,indexoutofboundsexception,Java,Swt,Indexoutofboundsexception,我犯了一个错误,我真的不明白为什么。这是一个小游戏 这是课桌 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.r
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];
}
这是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());
SelectionEvent e的e.x和e.y是鼠标点击的坐标,而不是网格。您应该从按钮数据中获取单元格,然后将其用于游戏移动。
您可以这样修复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
您可能希望从按钮数据获取单元格,并使用i,j坐标调用getElement 注意:关于Table类的一些注释。如果您已经知道表的初始大小,那么默认构造函数就没有意义。如果是这样的话,将列、行和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];
}
您在哪里/如何调用getElement方法?请发布stacktrace。它准确地显示了问题发生在哪一行。此外,使用默认构造函数可能会导致问题,因为字段不会初始化。提供默认值或不提供默认构造函数都是一个好主意。我尝试了你的方法,可能有什么效果,因为现在我得到了相同的异常,但在相同的方法中得到了数字2。
if(table.getElement(x, y) != PieceType.None) return MoveResult.InvalidMove;
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
public PieceType getElement(int i, int j) {
if ((i < 0) || (i >= rows) ||
(j < 0) || (j >= columns)) {
return null;
}
return table[i][j];
}
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;
}
}
}