Exemplo n.º 1
0
Arquivo: Game.cs Projeto: vgichar/shah
        /// <summary>
        /// Verifies the current board for draw by insufficient material.
        /// </summary>
        /// <returns></returns>
        private bool IsDrawInsufficientMaterial()
        {
            // "N" is true if there is one White Knight
            // "B" is true if there is one White Bishop
            // "BW" is true if the White Bishop is on a white square
            // "n" is true if there is one Black Knight
            // "b" is true if there is one Black Bishop
            // "bw" is true if the Black Bishop is on a white square
            bool N, B, BW, n, b, bw;

            N = B = BW = n = b = bw = false;

            // loop through the squares
            for (int sqIndex = 0; sqIndex < Board.SquareNo; sqIndex++)
            {
                if (currentBoard[sqIndex] != null)
                {
                    if (currentBoard[sqIndex] is WhiteKnight)
                    {
                        // if there is more than one White Knight there is no draw by insufficient material
                        if (N)
                        {
                            return(false);
                        }

                        N = true;

                        continue;
                    }

                    if (currentBoard[sqIndex] is WhiteBishop)
                    {
                        // if there is more than one White Bishop there is no draw by insufficient material
                        if (B)
                        {
                            return(false);
                        }

                        B = true;

                        // remember the Bishop square colour
                        BW = Board.IsWhiteSquare(sqIndex);

                        continue;
                    }

                    if (currentBoard[sqIndex] is BlackKnight)
                    {
                        // if there is more than one Black Knight there is no draw by insufficient material
                        if (n)
                        {
                            return(false);
                        }

                        n = true;

                        continue;
                    }

                    if (currentBoard[sqIndex] is BlackBishop)
                    {
                        // if there are more than one Black Bishop there is no draw by insufficient material
                        if (b)
                        {
                            return(false);
                        }

                        b = true;

                        // remember the Bishop square colour
                        bw = Board.IsWhiteSquare(sqIndex);

                        continue;
                    }

                    // there will be always the King
                    if (currentBoard[sqIndex] is IKing)
                    {
                        continue;
                    }

                    // if there is a piece which is not Bishop, Knight or King there is no draw by insufficient material
                    return(false);
                }
            }

            // if there are only the kings
            if (!B && !b && !N && !n)
            {
                return(true);
            }

            // if there are only the kings and one bishop on one side
            if ((b && !B && !N && !n) || (B && !b && !n && !N))
            {
                return(true);
            }

            // if there are only the kings and one knight on one side
            if ((n && !N && !B && !b) || (N && !n && !b && !B))
            {
                return(true);
            }

            // if there are only the kings and one bishop on each side, both of them on squares with the same colour
            if (B && b && !N && !n && (bw == BW))
            {
                return(true);
            }

            return(false);
        }