So my task is a machine learning tic tac toe game. I am tackling it by having the machine calculate every possible board state and then finding the expected value each move yields.The way I do so is with a class, Board, that contains an array showing the current board, and another array with all possible subsequent "Boards". The problems is when making the new, subsequent Board object, the current board in the parent object is changed when it should remain constant.
The constructor for Board is as follows
public Board(int[][] board, int[] newSpot, int turn) {
this.current = board; //current is the current board state
if (newSpot != null) {
this.current[newSpot[0]][newSpot[1]] = turn*-1; //adds the latest move
}
this.newSpot = newSpot;
this.TURN = turn;
WON = getWinner();
if (WON != 0) {
VALUE = WON;
}
else {
options = getOptions();
}
}
I have traced the problem to the getOptions()
method, which is as follows
private ArrayList<Board> getOptions() {
ArrayList<Board> workingOptions = new ArrayList<Board>();
int workingValue = 0;
for (int r=0; r<this.current.length; r++) {
for (int c=0; c<this.current[r].length; c++) {
if (this.current[r][c] == 0) {
Board workingBoard = new Board(Arrays.copyOf(this.current, this.current.length), new int[] {r, c}, TURN*-1);
workingValue += workingBoard.VALUE;
workingOptions.add(workingBoard);
}
}
}
VALUE = workingValue / openSpaces;
return workingOptions;
}
Thank you for the help