Esempio n. 1
0
        private bool IsIgnoreEnemyKing(ChessPiece targetPlace)
        {
            if (!IgnoreEnemyKingFlag)
            {
                return(false);
            }

            var targetColor = targetPlace.GetPlayerColor();

            if (targetColor == null)
            {
                return(false);
            }
            var targetColorValue = targetColor.Value;

            var isEnemyPiece = Piece.GetPlayerColor()?.Invert() == targetPlace.GetPlayerColor();
            var isKingPiece  = CheckMateLogic.MapKing(targetColorValue) == targetPlace;

            return(isEnemyPiece && isKingPiece);
        }
Esempio n. 2
0
        /// <summary>
        /// Get move status.
        /// </summary>
        /// <param name="newPosition">Check position.</param>
        /// <returns>Is not outside board.</returns>
        protected bool CanMove(ChessPoint newPosition)
        {
            int x = newPosition.X;
            int y = newPosition.Y;

            if (0 <= x && x < Field.Width && 0 <= y && y < Field.Height)
            {
                var targetPlace = Field[x, y];
                var sideName    = targetPlace.GetPlayerColor();

                var result = false;
                if (targetPlace == ChessPiece.Empty)
                {
                    result = true;
                }
                else
                {
                    result = SideName != sideName;
                }

                bool isYourKingIsUnderAttack = Master.CheckedPlayer.HasValue &&
                                               Master.CheckedPlayer == Piece.GetPlayerColor();

                if (result && isYourKingIsUnderAttack)
                {
                    // If king is under attack, you next step must prevent a check state
                    var fieldCopy = new VirtualField(Field.CloneMatrix());
                    fieldCopy[newPosition] = fieldCopy[Position];
                    fieldCopy[Position]    = ChessPiece.Empty;

                    var pieceColor = Piece.GetPlayerColor().Value.Invert();
                    return(!CheckMateLogic.IsCheck(pieceColor, fieldCopy, Master));
                }

                return(result);
            }

            return(false);
        }