コード例 #1
0
        internal bool CheckKingsSafety(Piece pieceMoving, Position newPos, Chessboard board)
        {
            bool       mat       = false;
            Side       defSide   = pieceMoving.Side;
            Chessboard tempBoard = Chessboard.DeepClone(board);
            //Piece pieceMirrored = DeepClone(pieceMoving);
            King defKing = tempBoard.FindKing(defSide);

            int Dx = pieceMoving.CurrentPosition.X - defKing.CurrentPosition.X;
            int Dy = pieceMoving.CurrentPosition.Y - defKing.CurrentPosition.Y;

            // Perform mirror operations when needed
            if (Dx < 0)
            {
                tempBoard.State = HorizontalMirror(tempBoard.State);
            }
            if (Dy < 0)
            {
                tempBoard.State = VerticalMirror(tempBoard.State);
            }

            // Find king again if board was mirrored
            defKing = tempBoard.FindKing(defSide);

            // Store possible enemy piece that might has mat.
            Piece possibleMatPiece = null;

            if (Dx == 0)
            {
                for (int iy = defKing.CurrentPosition.Y + 1; iy < 8; iy++)
                {
                    if (tempBoard.State[defKing.CurrentPosition.X, iy]?.Side != defSide)
                    {
                        possibleMatPiece = tempBoard.State[defKing.CurrentPosition.X, iy];
                    }
                }
            }
            else if (Dy == 0)
            {
                for (int ix = defKing.CurrentPosition.X + 1; ix < 8; ix++)
                {
                    if (tempBoard.State[ix, defKing.CurrentPosition.Y]?.Side != defSide)
                    {
                        possibleMatPiece = tempBoard.State[ix, defKing.CurrentPosition.Y];
                    }
                }
            }
            else
            {
                int ix = defKing.CurrentPosition.X + 1;
                int iy = defKing.CurrentPosition.Y + 1;

                for (int i = defKing.CurrentPosition.X; i < 8; i++)
                {
                    if (tempBoard.State[ix, iy] == null)
                    {
                        ix++;
                        iy++;
                        continue;
                    }

                    if (tempBoard.State[ix, iy].Side != defKing.Side)
                    {
                        possibleMatPiece = tempBoard.State[ix, iy];
                    }

                    ix++;
                    iy++;
                }
            }

            // Try to kill defendant King.


            return(false);
        }