コード例 #1
0
ファイル: ChessBoard.cs プロジェクト: JacekCzupyt/ChessGame
        public bool TileIsThreatened(ChessTile Position, ChessColor Color)
        {
            //Warning!
            //This function is meant to be used for identifying checks, and does not detect "en passant"

            List <(int, int)> RookDir = new List <(int, int)>()
            {
                (1, 0), (0, 1), (-1, 0), (0, -1)
            };
            List <(int, int)> BishopDir = new List <(int, int)> {
                (1, 1), (-1, 1), (-1, -1), (1, -1)
            };
            List <(int, int)> KnightDir = new List <(int, int)> {
                (2, 1), (2, -1), (-2, 1), (-2, -1), (1, 2), (1, -2), (-1, 2), (-1, -2)
            };

            //check rook
            foreach ((int, int)dir in RookDir)
            {
                ChessTile pos = Position + dir;
                while (pos.InBound())
                {
                    if (this[pos] != null)
                    {
                        if (this[pos].Color != Color && (this[pos] is Rook || this[pos] is Queen))
                        {
                            return(true);
                        }
                        break;
                    }
                    pos += dir;
                }
            }

            //check bishop
            foreach ((int, int)dir in BishopDir)
            {
                ChessTile pos = Position + dir;
                while (pos.InBound())
                {
                    if (this[pos] != null)
                    {
                        if (this[pos].Color != Color && (this[pos] is Bishop || this[pos] is Queen))
                        {
                            return(true);
                        }
                        break;
                    }
                    pos += dir;
                }
            }

            //check knight
            foreach ((int, int)dir in KnightDir)
            {
                ChessTile pos = Position + dir;
                if (pos.InBound())
                {
                    if (this[pos] != null)
                    {
                        if (this[pos].Color != Color && this[pos] is Knight)
                        {
                            return(true);
                        }
                    }
                }
            }

            //check king
            foreach ((int, int)dir in RookDir.Concat(BishopDir))
            {
                ChessTile pos = Position + dir;
                if (pos.InBound())
                {
                    if (this[pos] != null)
                    {
                        if (this[pos].Color != Color && this[pos] is King)
                        {
                            return(true);
                        }
                    }
                }
            }

            //check pawn
            int        PawnDir  = Color == ChessColor.White ? 1 : -1;
            List <int> PawnDiag = new List <int>()
            {
                1, -1
            };

            foreach (var diag in PawnDiag)
            {
                ChessTile pos = Position + (diag, PawnDir);
                if (pos.InBound() && this[pos] != null && this[pos].Color != Color && this[pos] is Pawn)
                {
                    return(true);
                }
            }

            return(false);
        }