Пример #1
0
        public static Chessman GetKingAttacker(Color color, Point?exceptPoint, Chessman chessman, bool showMessage = true)
        {
            var king = _board.Values.First(_ => _.Type == FeagureType.King && _.Color == color);

            var      added           = false;
            Chessman removedChessman = null;

            //TODO
            if (chessman != null)
            {
                _board.Remove(chessman.CurrentPosition);
            }
            if (exceptPoint.HasValue && chessman != null)
            {
                if (_board.ContainsKey(exceptPoint.Value))
                {
                    removedChessman = _board[exceptPoint.Value];
                    _board.Remove(exceptPoint.Value);
                }

                _board.Add(exceptPoint.Value, chessman);
                added = true;
            }

            var enemies = _board.Values.Where(_ => _.Color != color && _.CurrentPosition != exceptPoint).Where(_ => _.CanHit(king.CurrentPosition)).ToList();

            foreach (var enemy in enemies)
            {
                Debug.WriteLine("{0} {1}", enemy.Color, enemy.Type);
            }

            if (added)
            {
                _board.Remove(exceptPoint.Value);
            }
            if (chessman != null)
            {
                _board.Add(chessman.CurrentPosition, chessman);
            }
            if (removedChessman != null)
            {
                _board.Add(exceptPoint.Value, removedChessman);
            }

            if (enemies.Any())
            {
                if (enemies.Count > 1)
                {
                    throw new FatalityException {
                              WinColor = ColorHelper.GetOpposite(color)
                    }
                }
                ;

                if (showMessage)
                {
                    MessageBox.Show("Your king need to run!");
                }
                return(enemies.First());
            }

            return(null);
        }
Пример #2
0
        public static bool CheckFatality(Color initiator)
        {
            var enemyKing = _board.Values.Single(_ => _.Color == ColorHelper.GetOpposite(initiator) && _.Type == FeagureType.King);

            var attacker = GetKingAttacker(enemyKing.Color, null, null, false);

            if (attacker != null)
            {
                //King cannot run
                if (!enemyKing.CanEscape())
                {
                    //Is somebody can kill attacker
                    var saviors =
                        _board.Values.Where(
                            _ =>
                            _.Color == enemyKing.Color && _.Type != FeagureType.King &&
                            _.CanHit(attacker.CurrentPosition)).ToList();

                    saviors = saviors.Where(_ => !IsKingUnderAttack(enemyKing.Color, attacker.CurrentPosition, _, false)).ToList();

                    Debug.WriteLine("*****BEGING Saviors ******");

                    foreach (var savior in saviors)
                    {
                        Debug.WriteLine("{0} {1}", savior.Type, savior.CurrentPosition);
                    }

                    Debug.WriteLine("*****END Saviors ******");

                    if (saviors.Any())
                    {
                        return(false);
                    }

                    IEnumerable <Point> safePoints = new List <Point>();
                    //Is somebody can saмe king
                    if (attacker.CurrentPosition.X == enemyKing.CurrentPosition.X)
                    {
                        int start = attacker.CurrentPosition.Y, end = enemyKing.CurrentPosition.Y;
                        if (start > end)
                        {
                            var temp = end;
                            end   = start;
                            start = temp;
                        }

                        for (var i = start + 1; i < end; i++)
                        {
                            if (SomebodyCanSave(enemyKing, new Point(attacker.CurrentPosition.X, i)))
                            {
                                return(false);
                            }
                        }
                    }
                    else if (attacker.CurrentPosition.Y == enemyKing.CurrentPosition.Y)
                    {
                        int start = attacker.CurrentPosition.X, end = enemyKing.CurrentPosition.X;
                        if (start > end)
                        {
                            var temp = end;
                            end   = start;
                            start = temp;
                        }

                        for (var i = start + 1; i < end; i++)
                        {
                            if (SomebodyCanSave(enemyKing, new Point(i, attacker.CurrentPosition.Y)))
                            {
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        int startX = attacker.CurrentPosition.X, endX = enemyKing.CurrentPosition.X;
                        if (startX > endX)
                        {
                            var temp = endX;
                            endX   = startX;
                            startX = temp;
                        }

                        int startY = attacker.CurrentPosition.Y, endY = enemyKing.CurrentPosition.Y;
                        if (startY > endY)
                        {
                            var temp = endY;
                            endY   = startY;
                            startY = temp;
                        }

                        for (int x = startX + 1, y = startY + 1; x < endX; x++, y++)
                        {
                            if (SomebodyCanSave(enemyKing, new Point(x, y)))
                            {
                                return(false);
                            }
                        }

                        return(true);
                    }
                    Trace.TraceWarning("not implemented");
                }
            }

            return(false);
        }