Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved dangerzone calculation #9

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
7 changes: 6 additions & 1 deletion ChessForms/BoardDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ void DrawDangerzone()
continue;
}

if (item.Value.Count == 0)
{
continue;
}

ColorSquare(item.Key, DangersquareColor);
}*/

Expand Down Expand Up @@ -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))
{
Expand Down
27 changes: 17 additions & 10 deletions ChessGame/Chessboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ public Chessboard(Chessboard board)
MovedPieces = new HashSet<Piece>(board.MovedPieces);
Moves = new Stack<Move>(board.Moves);

// not needed before executing move
Dangerzone = new Dictionary<Coordinate, List<Piece>>();

Dangerzone = new Dictionary<Coordinate, HashSet<Piece>>();
foreach (var item in board.Dangerzone)
{
Dangerzone[item.Key] = new HashSet<Piece>(item.Value);
}
}

/// <summary>
Expand All @@ -71,8 +75,11 @@ public Chessboard(Chessboard board, Move move)
MovedPieces = new HashSet<Piece>(board.MovedPieces);
Moves = new Stack<Move>(board.Moves.Reverse());

// is refreshed in simulatemove
Dangerzone = new Dictionary<Coordinate, List<Piece>>();
Dangerzone = new Dictionary<Coordinate, HashSet<Piece>>();
foreach (var item in board.Dangerzone)
{
Dangerzone[item.Key] = new HashSet<Piece>(item.Value);
}

SimulateMove(move);
}
Expand All @@ -92,7 +99,7 @@ public Chessboard(byte width, byte height, Gamemode gamemode)
CurrentState = GameState.NotStarted;

Pieces = new Dictionary<Coordinate, Piece>();
Dangerzone = new Dictionary<Coordinate, List<Piece>>();
Dangerzone = new Dictionary<Coordinate, HashSet<Piece>>();
Moves = new Stack<Move>();
MovedPieces = new HashSet<Piece>();
}
Expand All @@ -105,7 +112,7 @@ public Chessboard(byte width, byte height, Gamemode gamemode)
/// <summary>
/// Gets a dictionary that describes intersection squares. An intersection square is a square which one or more pieces threaten at once.
/// </summary>
public Dictionary<Coordinate, List<Piece>> Dangerzone { get; }
public Dictionary<Coordinate, HashSet<Piece>> Dangerzone { get; }

/// <summary>
/// Gets previous moves, that resolve to this position.
Expand Down Expand Up @@ -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<Piece> pieces))
if (Dangerzone.TryGetValue(position, out HashSet<Piece> pieces))
{
// Returns true if any of the pieces are of opposite teamColor.
return pieces.Any(p => p.Color != color);
Expand Down Expand Up @@ -528,7 +535,7 @@ public Move GetMoveByNotation(string notation, TeamColor teamColor, MoveNotation
/// <returns></returns>
public int IsDangerSquare(Coordinate position, TeamColor teamColor)
{
if (Dangerzone.TryGetValue(position, out List<Piece> pieces))
if (Dangerzone.TryGetValue(position, out HashSet<Piece> pieces))
{
return pieces is null ? 0 : pieces.Count(p => p.Color == teamColor);
}
Expand All @@ -543,7 +550,7 @@ public int IsDangerSquare(Coordinate position, TeamColor teamColor)
/// <returns></returns>
public int GetDangerSquareSum(Coordinate position)
{
if (!Dangerzone.TryGetValue(position, out List<Piece> pieces))
if (!Dangerzone.TryGetValue(position, out HashSet<Piece> pieces))
{
// No pieces can capture this tile.
return 0;
Expand Down Expand Up @@ -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<Piece>();
Dangerzone[destination] = new HashSet<Piece>();
}

// Add this move to dangerzone.
Expand Down
3 changes: 2 additions & 1 deletion ChessGame/Piece.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public IEnumerable<Move> GetMoves(Chessboard board, bool guardedSquaresOnly = fa

public override string ToString() => Notation.ToString();

/*
public override bool Equals(object obj)
{
return obj is Piece piece &&
Expand All @@ -85,5 +86,5 @@ public override int GetHashCode()
hashCode = hashCode * -1521134295 + Color.GetHashCode();
hashCode = hashCode * -1521134295 + MaterialValue.GetHashCode();
return hashCode;
}
}*/
}