From ae55e0d94c6d234d4f6cab9517dd1eb1fd02d70e Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 16 Apr 2025 11:25:31 +0200 Subject: [PATCH] change draw --- app/src/main/java/chess/model/ChessBoard.java | 15 ++++--- app/src/main/java/chess/model/Game.java | 43 ++++--------------- 2 files changed, 17 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/chess/model/ChessBoard.java b/app/src/main/java/chess/model/ChessBoard.java index 7630c73..ebc15e5 100644 --- a/app/src/main/java/chess/model/ChessBoard.java +++ b/app/src/main/java/chess/model/ChessBoard.java @@ -280,14 +280,15 @@ public class ChessBoard { return null; } - public int hashPlayerPieces(Color color) { + @Override + public int hashCode() { int result = 0; for (int i = 0; i < Coordinate.VALUE_MAX; i++) { for (int j = 0; j < Coordinate.VALUE_MAX; j++) { Piece piece = pieceAt(new Coordinate(i, j)); - if (piece == null || piece.getColor() != color) + if (piece == null) continue; - result = Objects.hash(result, new Coordinate(i, j), piece); + result = Objects.hash(result, piece.getColor(), new Coordinate(i, j), piece); } } return result; @@ -302,10 +303,10 @@ public class ChessBoard { } @Override - public boolean equals(Object otherBoard){ - if (!(otherBoard instanceof ChessBoard)) return false; - return ((hashPlayerPieces(Color.White) == ((ChessBoard)otherBoard).hashPlayerPieces(Color.White)) - && (hashPlayerPieces(Color.Black) == ((ChessBoard)otherBoard).hashPlayerPieces(Color.Black))); + public boolean equals(Object obj){ + if (obj instanceof ChessBoard board) + return board.hashCode() == this.hashCode(); + return false; } } diff --git a/app/src/main/java/chess/model/Game.java b/app/src/main/java/chess/model/Game.java index d8c0bf4..2ad9a6e 100644 --- a/app/src/main/java/chess/model/Game.java +++ b/app/src/main/java/chess/model/Game.java @@ -12,7 +12,7 @@ public class Game { private final ChessBoard board; private Color playerTurn; private final Stack movesHistory; - private final Map> traitsPos; + private final Map traitsPos; private static final int DRAW_REPETITONS = 3; @@ -37,13 +37,6 @@ public class Game { 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() { @@ -55,32 +48,18 @@ public class Game { * @param color the current player turn * @return true if a draw should be declared */ - private void 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); - } - public void saveTraitPiecesPos() { - saveTraitPiecesPos(playerTurn); + int piecesHash = this.board.hashCode(); + Integer count = this.traitsPos.get(piecesHash); + this.traitsPos.put(piecesHash, count == null ? 1 : count + 1); } - /** - * - * @param player the trait to check - * @return true if a position is repeated DRAW_REPETITONS times - */ - private boolean checkDraw(Color player) { - return this.traitsPos.get(player).containsValue(DRAW_REPETITONS); - } /** * @return whether the game is in a draw situation */ private boolean checkDraw() { - if (checkDraw(Color.White)) - return true; - return checkDraw(Color.Black); + return this.traitsPos.containsValue(DRAW_REPETITONS); } /** @@ -129,15 +108,11 @@ 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)); + int piecesHash = this.board.hashCode(); + Integer count = this.traitsPos.get(piecesHash); + if (count != null) + this.traitsPos.put(piecesHash, count - 1); } public List getMoves() {