check draw

This commit is contained in:
2025-04-13 12:57:13 +02:00
parent 224a09c711
commit b336784a5d
15 changed files with 108 additions and 26 deletions

View File

@@ -1,5 +1,7 @@
package chess.model;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import chess.controller.PlayerCommand;
@@ -9,6 +11,7 @@ public class Game {
private final ChessBoard board;
private Color playerTurn;
private final Stack<PlayerCommand> movesHistory;
private final Map<Color, Map<Integer, Integer>> traitsPos;
public enum GameStatus {
Check, CheckMate, OnGoing, Pat;
@@ -17,6 +20,9 @@ public class Game {
public Game(ChessBoard board) {
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() {
@@ -31,8 +37,26 @@ public class Game {
this.playerTurn = Color.White;
}
public void switchPlayerTurn() {
/**
*
* @param color the current player turn
* @return true if a draw should be declared
*/
private boolean saveTraitPiecesPos(Color color) {
int piecesHash = this.board.hashPlayerPieces(color);
Integer count = this.traitsPos.get(color).get(piecesHash);
this.traitsPos.get(color).put(piecesHash, count == null ? 1 : count + 1);
return count == null ? false : count == (3 - 1);
}
/**
*
* @return true if a draw should occur
*/
public boolean switchPlayerTurn() {
boolean draw = saveTraitPiecesPos(this.playerTurn);
playerTurn = Color.getEnemy(playerTurn);
return draw;
}
public GameStatus checkGameStatus() {