public static bool AddIfCoordinateIsEmpty(this List <GameMove> list, Chessboard chessboard, GameMove move) { if (chessboard.GetChessPieceOrDefault(move.To).HasValue) { return(false); } list.Add(move); return(true); }
private bool IsValid(Chessboard chessboard, GameMove move, GameHistory gameHistory) { var chessPiece = chessboard.GetChessPieceOrDefault(move.From); if (!chessPiece.HasValue) { return(false); } var chessBoardCopy = (Chessboard)chessboard.Clone(); var gameHistoryCopy = (GameHistory)gameHistory.Clone(); gameHistoryCopy.Add(move, chessPiece.Value.Owner, chessBoardCopy); chessBoardCopy.Move(move); var kingCoordinate = chessBoardCopy.GetCoordinate(new ChessPiece { Owner = chessPiece.Value.Owner, Type = ChessPieceType.King }); var isKingInDanger = IsCoordinateInDanger(chessBoardCopy, chessPiece.Value.Owner, gameHistoryCopy, kingCoordinate); return(!isKingInDanger); }
private static bool IsPawnKilling(Chessboard chessboard, GameMove?previousMove, ChessColor chessPieceColor, int i, int j) { var chessPieceOnPotentialVictimPosition = chessboard.Board[i, j]; if (chessPieceOnPotentialVictimPosition.HasValue) { return(chessPieceOnPotentialVictimPosition.Value.Owner != chessPieceColor); } // "En Passant" checking if (!previousMove.HasValue || chessPieceColor == ChessColor.White && i != 5 || chessPieceColor == ChessColor.Black && i != 2) { return(false); } var previousMoveChessPiece = chessboard.GetChessPieceOrDefault(previousMove.Value.To); if (!previousMoveChessPiece.HasValue || previousMoveChessPiece.Value.Type != ChessPieceType.Pawn) { return(false); } previousMove.Value.From.ToArrayIndexes(out var prevMoveFromI, out var prevMoveFromJ); previousMove.Value.To.ToArrayIndexes(out var prevMoveToI, out _); var prevMoveOwner = previousMoveChessPiece.Value.Owner; if (prevMoveFromJ != j) { return(false); } return(prevMoveOwner == ChessColor.White && prevMoveFromI == 1 && prevMoveToI == 3 || prevMoveOwner == ChessColor.Black && prevMoveFromI == 6 && prevMoveToI == 4); }