fix undo draw check

This commit is contained in:
2025-04-14 11:02:35 +02:00
parent 07016be5d8
commit 91678a42b2
4 changed files with 33 additions and 5 deletions

View File

@@ -23,8 +23,6 @@ public class Game {
this.board = board;
this.movesHistory = new Stack<>();
this.traitsPos = new HashMap<>();
this.traitsPos.put(Color.Black, new HashMap<>());
this.traitsPos.put(Color.White, new HashMap<>());
}
public ChessBoard getBoard() {
@@ -35,6 +33,18 @@ public class Game {
return playerTurn;
}
public void reset() {
resetPlayerTurn();
this.traitsPos.clear();
initPlayer(Color.Black);
initPlayer(Color.White);
}
private void initPlayer(Color color) {
this.traitsPos.put(color, new HashMap<>());
saveTraitPiecesPos(color);
}
public void resetPlayerTurn() {
this.playerTurn = Color.White;
}
@@ -50,6 +60,10 @@ public class Game {
this.traitsPos.get(color).put(piecesHash, count == null ? 1 : count + 1);
}
public void saveTraitPiecesPos() {
saveTraitPiecesPos(playerTurn);
}
/**
*
* @param player the trait to check
@@ -73,14 +87,13 @@ public class Game {
* @return true if a draw should occur
*/
public void switchPlayerTurn() {
saveTraitPiecesPos(this.playerTurn);
playerTurn = Color.getEnemy(playerTurn);
}
public GameStatus checkGameStatus() {
final Color enemy = Color.getEnemy(getPlayerTurn());
if(checkDraw())
if (checkDraw())
return GameStatus.Draw;
if (this.board.isKingInCheck(enemy))
@@ -115,4 +128,15 @@ public class Game {
}
}
private void undoTraitPiecesPos(Color color) {
int piecesHash = this.board.hashPlayerPieces(color);
Integer count = this.traitsPos.get(color).get(piecesHash);
if (count != null)
this.traitsPos.get(color).put(piecesHash, count - 1);
}
public void undoTraitPiecesPos() {
undoTraitPiecesPos(Color.getEnemy(playerTurn));
}
}