Reputation: 11
I'm creating a game of tic tac toe and I'm using a 2D array to track what is in each square. The problem is the array is not taking the second a size for the column.
package TicTacToePrj;
import java.awt.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;
public class SuperTicTacToeGame {
private Cell[][] cBoard;
private GameStatus status;
private Cell turn;
private int connections;
int r = 3;
int c = 3;
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
private static final boolean AI = true;
private ArrayList<Point> backup = new ArrayList<Point>();
public SuperTicTacToeGame(int r, int c) {
status = GameStatus.IN_PROGRESS;
cBoard = new Cell[this.r][this.c];
reset();
}
}
The array is using these enumerator objects which is my only suspicion.
package TicTacToePrj;
public enum Cell {
X, O, EMPTY
}
Upvotes: 0
Views: 46
Reputation: 1672
Your constructor ignores the parameters (r
, c
) and uses the fields instead (this.r
, this.c
) to initialise the matrix.
If you are going to keep these field, to fix the code you need to assign parameters to fields first.
Also, your set
methods are likely to work improperly because you change the matrix size but not the matrix itself. You either should remove these methods or to do something to the matrix when a setter is called.
Upvotes: 3