이유를 정확히 이해하지 못하는 오류가 나타납니다. 는 (N × N)의 여기 테이블 클래스를이다하여 ticTacToe 게임 ArrayIndexOutOfBounds TicTacToe 게임에서 예외가 발생했습니다.
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];
}
그 제가 game.move (e.x, e.y)에 함수를 호출 한 후에 에러를주는 getElement() 메소드; 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());
어떤 아이디어를 얻을 곳
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();
}
이 무엇입니까? 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)
여기하여 마우스 클릭 좌표하는 SelectionEvent
e
의
if(table.getElement(x, y) != PieceType.None) return MoveResult.InvalidMove;
/당신은 어떻게'getElement' 방법을 요구하고있다? – npinti
스택 추적을 게시하십시오. 문제가 발생한 줄을 정확히 보여줍니다. 또한 기본 생성자를 사용하면 필드가 초기화되지 않으므로 문제가 발생할 수 있습니다. 기본값을 제공하거나 기본 생성자를 제공하지 않는 것이 좋습니다. – toniedzwiedz