Hi guys so im basically in the following problem:
I have a minmax alghoritm for my chess game (functionality calculation of AI movement).
Now i need a way to send the current state of the game WITHOUT that the state is getting modified when calculating the moves by the AI. (Somehow the references are still in the copied object from the original one.)
Basically the board property has two lists inside them called, the properties inside the arrayList will somehow when changed from the copied object, it will also change in the original object:
public class Board {
private ArrayList<Piece> whitePieces;
private ArrayList<Piece> blackPieces;
}
So what i've tried so far:
Create a copy of the game object using Clonable interface in JAVA. then create a second variable and store the current game class object into that one and send it to the AI.
Create a new Class(GameState) with the necessary properties that are needed for the AI calculation, store the current game properties in that one and send it to the aI.
The problem is: Whenever i create a second object, the references of variables inside Board board variable (List of black and white pieces) are being references the the original object.
This basically means, that whenever i send the gamestate to the minimax alghoritm. the minimax is trying/setting moves but when doing these moves, it will affect the original object aswell as the created "Copy" of the object
Attempt 1:
public class GameState
{
private Player whitePlayer;
private Player currentTurn;
private Player blackPlayer;
private Board board;
private GameStatus gameStatus;
}
public void computerMove() {
var tempGameState = new GameState(whitePlayer, currentTurn, blackPlayer, board, gameStatus);
var bestMove = moveStrategy.execute(tempGameState, 2);
var piece = bestMove.getSelectedPiece()
piece.setPosition(bestMove.getDesPos());
}
attempt 2:
public void computerMove() throws CloneNotSupportedException {
var cloneGame = clone();
var bestMove = moveStrategy.execute(cloneGame, 3);
var piece = bestMove.getSelectedPiece();
piece.setPosition(bestMove.getDesPos());
}
@Override
public ChessGame clone() throws CloneNotSupportedException {
return (ChessGame)super.clone();
}