diff --git a/ChessForms/BoardDisplay.cs b/ChessForms/BoardDisplay.cs index 6e6a95e..8f6964b 100644 --- a/ChessForms/BoardDisplay.cs +++ b/ChessForms/BoardDisplay.cs @@ -356,6 +356,11 @@ void DrawDangerzone() continue; } + if (item.Value.Count == 0) + { + continue; + } + ColorSquare(item.Key, DangersquareColor); }*/ @@ -566,7 +571,7 @@ void CellClicked(object sender, EventArgs e) case MouseButtons.None: break; case MouseButtons.Right: // Mark square. - // do not change color if square is already colored. + // do not change color if square is already colored. if ((recentMoveFrom?.File == cellX && recentMoveFrom?.Rank == cellY) || (recentMoveTo?.File == cellX && recentMoveTo?.Rank == cellY)) { diff --git a/ChessGame/Chessboard.cs b/ChessGame/Chessboard.cs index 9236cdc..4f6bc44 100644 --- a/ChessGame/Chessboard.cs +++ b/ChessGame/Chessboard.cs @@ -50,8 +50,12 @@ public Chessboard(Chessboard board) MovedPieces = new HashSet(board.MovedPieces); Moves = new Stack(board.Moves); - // not needed before executing move - Dangerzone = new Dictionary>(); + + Dangerzone = new Dictionary>(); + foreach (var item in board.Dangerzone) + { + Dangerzone[item.Key] = new HashSet(item.Value); + } } /// @@ -71,8 +75,11 @@ public Chessboard(Chessboard board, Move move) MovedPieces = new HashSet(board.MovedPieces); Moves = new Stack(board.Moves.Reverse()); - // is refreshed in simulatemove - Dangerzone = new Dictionary>(); + Dangerzone = new Dictionary>(); + foreach (var item in board.Dangerzone) + { + Dangerzone[item.Key] = new HashSet(item.Value); + } SimulateMove(move); } @@ -92,7 +99,7 @@ public Chessboard(byte width, byte height, Gamemode gamemode) CurrentState = GameState.NotStarted; Pieces = new Dictionary(); - Dangerzone = new Dictionary>(); + Dangerzone = new Dictionary>(); Moves = new Stack(); MovedPieces = new HashSet(); } @@ -105,7 +112,7 @@ public Chessboard(byte width, byte height, Gamemode gamemode) /// /// Gets a dictionary that describes intersection squares. An intersection square is a square which one or more pieces threaten at once. /// - public Dictionary> Dangerzone { get; } + public Dictionary> Dangerzone { get; } /// /// Gets previous moves, that resolve to this position. @@ -361,7 +368,7 @@ public bool IsKingInCheck(TeamColor color) } // Get list of pieces aiming on the square the king sits on. - if (Dangerzone.TryGetValue(position, out List pieces)) + if (Dangerzone.TryGetValue(position, out HashSet pieces)) { // Returns true if any of the pieces are of opposite teamColor. return pieces.Any(p => p.Color != color); @@ -528,7 +535,7 @@ public Move GetMoveByNotation(string notation, TeamColor teamColor, MoveNotation /// public int IsDangerSquare(Coordinate position, TeamColor teamColor) { - if (Dangerzone.TryGetValue(position, out List pieces)) + if (Dangerzone.TryGetValue(position, out HashSet pieces)) { return pieces is null ? 0 : pieces.Count(p => p.Color == teamColor); } @@ -543,7 +550,7 @@ public int IsDangerSquare(Coordinate position, TeamColor teamColor) /// public int GetDangerSquareSum(Coordinate position) { - if (!Dangerzone.TryGetValue(position, out List pieces)) + if (!Dangerzone.TryGetValue(position, out HashSet pieces)) { // No pieces can capture this tile. return 0; @@ -697,7 +704,7 @@ void UpdateDangerzones(Piece piece) // Make new list of pieces aiming on this square if there isn't one already. if (!Dangerzone.ContainsKey(destination)) { - Dangerzone[destination] = new List(); + Dangerzone[destination] = new HashSet(); } // Add this move to dangerzone. diff --git a/ChessGame/Piece.cs b/ChessGame/Piece.cs index bead1bc..17c831f 100644 --- a/ChessGame/Piece.cs +++ b/ChessGame/Piece.cs @@ -70,6 +70,7 @@ public IEnumerable GetMoves(Chessboard board, bool guardedSquaresOnly = fa public override string ToString() => Notation.ToString(); + /* public override bool Equals(object obj) { return obj is Piece piece && @@ -85,5 +86,5 @@ public override int GetHashCode() hashCode = hashCode * -1521134295 + Color.GetHashCode(); hashCode = hashCode * -1521134295 + MaterialValue.GetHashCode(); return hashCode; - } + }*/ }