Exemplo n.º 1
0
        public void MoveChess()
        {
            Position p = new Position();

            p.IsWhiteMove = true;
            p.Board[0, 4] = new Square(new King(ColorFigur.white));
            p.Board[0, 7] = new Square(new Rock(ColorFigur.white));
            MoveCoord mc = new MoveCoord();

            mc.StartFigure = new King(ColorFigur.white);
            mc.xStart      = 0;
            mc.yStart      = 4;
            mc.EndFigure   = new NotFigur();
            mc.xEnd        = 0;
            mc.yEnd        = 6;
            mc.IsCastling  = true;
            p.MoveChess(mc);

            Position p1 = new Position();

            p1.Board[4, 4] = new Square(new Peen(ColorFigur.white));
            p1.Board[4, 5] = new Square(new Peen(ColorFigur.black));
            ((Peen)p1.Board[4, 5].Figure).IsEnPassant = true;
            MoveCoord mc1 = new MoveCoord();

            mc1.StartFigure = new Peen(ColorFigur.white);
            mc1.xStart      = 4;
            mc1.yStart      = 4;
            mc1.xEnd        = 5;
            mc1.yEnd        = 5;
            mc1.IsEnPassant = true;
            p1.MoveChess(mc1);

            Position p2 = new Position();

            p2.IsWhiteMove = true;
            p2.Board[1, 1] = new Square(new Peen(ColorFigur.white));
            MoveCoord mc2 = new MoveCoord();

            mc2.StartFigure = new Peen(ColorFigur.white);
            mc2.xStart      = 1;
            mc2.yStart      = 1;
            mc2.xEnd        = 3;
            mc2.yEnd        = 1;
            p2.MoveChess(mc2);

            Assert.AreEqual(((Peen)p2.Board[3, 1].Figure).IsEnPassant, true);
            Assert.AreEqual(p.IsWhiteMove, false);
            Assert.AreEqual(p.IsKingMovedWhite, true);
            Assert.AreEqual(p.Board[0, 7], new Square(new NotFigur()));
            Assert.AreEqual(p.Board[0, 6], new Square(new King(ColorFigur.white)));
            Assert.AreEqual(p.Board[0, 5], new Square(new Rock(ColorFigur.white)));
            Assert.AreEqual(p.Board[0, 4], new Square(new NotFigur()));

            Assert.AreEqual(p1.Board[5, 5], new Square(new Peen(ColorFigur.white)));
            Assert.AreEqual(p1.Board[4, 5], new Square(new NotFigur()));
            Assert.AreEqual(p1.Board[4, 4], new Square(new NotFigur()));
        }
Exemplo n.º 2
0
        public void CheckBishop()
        {
            Position p = new Position();

            p.IsWhiteMove = true;
            p.Board[4, 4] = new Square(new Bishop(ColorFigur.white));
            MoveCoord mc = new MoveCoord();

            mc.xStart      = 4;
            mc.yStart      = 4;
            mc.StartFigure = new Bishop(ColorFigur.white);
            //возможные ходы
            for (int i = 1; ((mc.xStart + i <= 7) && (mc.yStart + i <= 7)) || ((mc.xStart - i >= 0) && (mc.yStart + i <= 7)) ||
                 ((mc.xStart + i <= 7) && (mc.yStart - i >= 0)) || ((mc.xStart - i >= 0) && (mc.yStart - i >= 0)); i++)
            {
                if ((mc.xStart + i <= 7) && (mc.yStart + i <= 7))
                {
                    mc.xEnd = mc.xStart + i;
                    mc.yEnd = mc.yStart + i;
                    Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
                }
                if ((mc.xStart - i >= 0) && (mc.yStart + i <= 7))
                {
                    mc.xEnd = mc.xStart - i;
                    mc.yEnd = mc.yStart + i;
                    Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
                }
                if ((mc.xStart + i <= 7) && (mc.yStart - i >= 0))
                {
                    mc.xEnd = mc.xStart + i;
                    mc.yEnd = mc.yStart - i;
                    Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
                }
                if ((mc.xStart - i >= 0) && (mc.yStart - i >= 0))
                {
                    mc.xEnd = mc.xStart - i;
                    mc.yEnd = mc.yStart - i;
                    Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
                }
            }
            //король под шахом
            p.Board[1, 0] = new Square(new King(ColorFigur.white));
            p.Board[2, 1] = new Square(new Queen(ColorFigur.black));
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            p.Board[2, 1] = new Square(new NotFigur());
            //препятствие
            p.Board[1, 1] = new Square(new Peen(ColorFigur.white));
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            //вне диапозона
            mc.xEnd = 7;
            mc.yEnd = 6;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
        }
Exemplo n.º 3
0
 public ChessBoard()
 {
     IsWithPC   = false;
     engine     = new Engine.Engine();
     move       = new MoveCoord();
     IsPCMove   = false;
     isSelected = false;
     newFigure  = new Square(new NotFigur());
     InitializeComponent();
     NewChessBoard(true);
     IsInsert    = false;
     IsStartGame = false;
 }
Exemplo n.º 4
0
        public void CheckPeen()
        {
            Position p = new Position();

            p.IsWhiteMove = true;
            p.Board[1, 4] = new Square(new Peen(ColorFigur.white));
            MoveCoord mc = new MoveCoord();

            mc.xStart      = 1;
            mc.yStart      = 4;
            mc.yEnd        = 4;
            mc.StartFigure = new Peen(ColorFigur.white);

            //возможные ходы
            mc.xEnd = 2;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 3;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            //возможно взятие на проходе
            p.Board[4, 4] = new Square(new Peen(ColorFigur.white));
            p.Board[4, 5] = new Square(new Peen(ColorFigur.black));
            ((Peen)p.Board[4, 5].Figure).IsEnPassant = true;
            p.Board[5, 3] = new Square(new Peen(ColorFigur.black));
            mc.xStart     = 4;
            mc.yStart     = 4;
            mc.xEnd       = 5;
            mc.yEnd       = 5;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            //обычное взятие
            mc.yEnd = 3;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            //ход вперед, когда там фигура
            p.Board[5, 4] = new Square(new Peen(ColorFigur.black));
            mc.yEnd       = 4;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            //нельзя взять на проходе
            ((Peen)p.Board[4, 5].Figure).IsEnPassant = false;
            mc.yEnd = 5;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            //ход, когда король под шахом
            p.Board[0, 0] = new Square(new King(ColorFigur.white));
            p.Board[0, 1] = new Square(new Queen(ColorFigur.black));
            mc.yEnd       = 3;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            p.Board[0, 1] = new Square(new NotFigur());
            //вне диапозона
            mc.yEnd = 4;
            mc.xEnd = 6;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
        }
Exemplo n.º 5
0
        public void CheckRock()
        {
            Position p = new Position();

            p.IsWhiteMove = true;
            p.Board[4, 4] = new Square(new Rock(ColorFigur.white));
            MoveCoord mc = new MoveCoord();

            mc.xStart      = 4;
            mc.yStart      = 4;
            mc.StartFigure = new Rock(ColorFigur.white);

            //Возможные ходы
            mc.xEnd = mc.xStart;
            for (int i = 1; (i + mc.yStart) <= 7; i++)
            {
                mc.yEnd = mc.yStart + i;
                Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            }
            for (int i = 1; (mc.yStart - i) >= 0; i++)
            {
                mc.yEnd = mc.yStart - i;
                Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            }
            mc.yEnd = mc.yStart;
            for (int i = 1; (i + mc.xStart) <= 7; i++)
            {
                mc.xEnd = mc.xStart + i;
                Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            }
            for (int i = 1; (mc.xStart - i) >= 0; i++)
            {
                mc.xEnd = mc.xStart - i;
                Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            }
            //король под шахом
            p.Board[0, 0] = new Square(new King(ColorFigur.white));
            p.Board[0, 1] = new Square(new Queen(ColorFigur.black));
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            p.Board[0, 1] = new Square(new NotFigur());
            //путь перегорожден
            p.Board[1, mc.yStart] = new Square(new Peen(ColorFigur.white));
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            //вне диапозона
            mc.yEnd = 7;
            mc.xEnd = 7;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
        }
Exemplo n.º 6
0
        public void CheckKnight()
        {
            Position p = new Position();

            p.IsWhiteMove = true;
            p.Board[4, 4] = new Square(new Knight(ColorFigur.white));
            p.Board[5, 2] = new Square(new Peen(ColorFigur.white));
            MoveCoord mc = new MoveCoord();

            mc.xStart      = 4;
            mc.yStart      = 4;
            mc.StartFigure = new Knight(ColorFigur.white);
            //ход не будет возможен т.к. на клетке пешка;
            mc.xEnd = 5;
            mc.yEnd = 2;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            //Остальные ходы возможны
            mc.xEnd = 5;
            mc.yEnd = 6;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 6;
            mc.yEnd = 3;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 6;
            mc.yEnd = 5;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 3;
            mc.yEnd = 6;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 3;
            mc.yEnd = 2;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 2;
            mc.yEnd = 3;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 2;
            mc.yEnd = 3;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            //Король атакован после хода
            p.Board[0, 0] = new Square(new King(ColorFigur.white));
            p.Board[0, 1] = new Square(new Queen(ColorFigur.black));
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            p.Board[0, 1] = new Square(new NotFigur());
            //Вне диапозона
            mc.xEnd = 7;
            mc.yEnd = 7;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
        }
Exemplo n.º 7
0
        public void MovePosition()
        {
            Position p = new Position();

            p.Board[2, 2] = new Square(new Bishop(ColorFigur.white));
            MoveCoord mc = new MoveCoord();

            mc.xStart      = 2;
            mc.yStart      = 2;
            mc.yEnd        = 6;
            mc.xEnd        = 3;
            mc.IsCastling  = false;
            mc.StartFigure = p.Board[2, 2].Figure;
            mc.EndFigure   = new NotFigur();
            p.Move(mc);
            Assert.AreEqual(p.Board[2, 2], new Square(new NotFigur()));
            Assert.AreEqual(p.Board[3, 6], new Square(new Bishop(ColorFigur.white)));
        }
Exemplo n.º 8
0
        public void BackMove()
        {
            Position p = new Position();

            p.Board[0, 4] = new Square(new King(ColorFigur.white));
            p.Board[0, 7] = new Square(new Rock(ColorFigur.white));
            p.IsWhiteMove = true;
            Position  p1 = (Position)p.DeepCopy();
            MoveCoord mc = new MoveCoord();

            mc.xStart     = mc.xEnd = 0;
            mc.yStart     = 4;
            mc.yEnd       = 6;
            mc.IsCastling = true;
            p1.MoveChess(mc);
            Assert.AreEqual(p1.Board[0, 5] == new Square(new Rock(ColorFigur.white)), true);
            p1.MoveBack(mc);
            Assert.AreEqual(p.Equals(p1), true);

            p             = new Position();
            p.Board[4, 4] = new Square(new Peen(ColorFigur.white));
            p.Board[4, 5] = new Square(new Peen(ColorFigur.black));
            ((Peen)p.Board[4, 5].Figure).IsEnPassant = true;
            mc             = new MoveCoord();
            mc.IsEnPassant = true;
            mc.xStart      = 4;
            mc.xEnd        = 5;
            mc.yEnd        = 5;
            mc.yStart      = 4;
            p.IsWhiteMove  = true;
            p1             = (Position)p.DeepCopy();
            p1.MoveChess(mc);
            Assert.AreEqual(p1.Board[5, 5] == new Square(new Peen(ColorFigur.white)), true);
            Assert.AreEqual(p1.Board[4, 5] == new Square(new NotFigur()), true);
            p1.MoveBack(mc);
            Assert.AreEqual(p.Equals(p1), true);
        }
Exemplo n.º 9
0
        public void CheckKing()
        {
            Position  p  = new Position("start");//король не имеет ходов
            MoveCoord mc = new MoveCoord();

            mc.xStart      = 0;
            mc.yStart      = 4;
            mc.StartFigure = new King(ColorFigur.white);
            //ни один ход не будет возможен;
            mc.xEnd = 1;
            mc.yEnd = 3;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            mc.xEnd = 1;
            mc.yEnd = 4;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            mc.xEnd = 1;
            mc.yEnd = 5;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            mc.xEnd = 0;
            mc.yEnd = 5;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            mc.xEnd = 0;
            mc.yEnd = 3;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);

            p             = new Position();
            p.Board[4, 4] = new Square(new King(ColorFigur.white));
            p.IsWhiteMove = true;
            //все ходы возможны
            mc.xStart      = 4;
            mc.yStart      = 4;
            mc.StartFigure = new King(ColorFigur.white);
            mc.xEnd        = 4;
            mc.yEnd        = 3;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 4;
            mc.yEnd = 5;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 3;
            mc.yEnd = 5;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 3;
            mc.yEnd = 4;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 3;
            mc.yEnd = 3;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 5;
            mc.yEnd = 5;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            mc.xEnd = 5;
            mc.yEnd = 4;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), true);
            //атакованная клетка
            mc.xEnd       = 5;
            mc.yEnd       = 3;
            p.Board[5, 6] = new Square(new Queen(ColorFigur.black));
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
            p.Board[5, 6] = new Square(new NotFigur());
            //вне диапазона ходов
            mc.xEnd = 5;
            mc.yEnd = 0;
            Assert.AreEqual(mc.StartFigure.CheckMove(p, mc), false);
        }
Exemplo n.º 10
0
        public void CheckKingRook()
        {
            //Создаем позицию с возможными рокировками в обе стороны
            Position p = new Position();

            p.IsWhiteMove           = true;
            p.IsKingMovedWhite      = false;
            p.IsLeftRockMovedWhite  = false;
            p.IsRightRockMovedWhite = false;
            p.Board[0, 4]           = new Square(new King(ColorFigur.white));
            p.Board[0, 0]           = new Square(new Rock(ColorFigur.white));
            p.Board[0, 7]           = new Square(new Rock(ColorFigur.white));
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j <= 7; j++)
                {
                    p.Board[i, j].IsAttackBlack = false;//ни одна из клеток не атакована противником
                }
            }
            MoveCoord mcL = new MoveCoord(); //Длинная рокировка
            MoveCoord mcR = new MoveCoord(); //Короткая рокировка

            mcL.xStart      = mcR.xStart = 0;
            mcL.yStart      = mcR.yStart = 4;
            mcL.xEnd        = mcR.xEnd = 0;
            mcL.yEnd        = 2;
            mcR.yEnd        = 6;
            mcL.StartFigure = new King(ColorFigur.white);
            mcR.StartFigure = new King(ColorFigur.white);
            mcL.NewFigure   = new King(ColorFigur.white);
            mcR.NewFigure   = new King(ColorFigur.white);

            Assert.AreEqual(mcL.StartFigure.CheckMove(p, mcL), true);
            Assert.AreEqual(mcR.StartFigure.CheckMove(p, mcR), true);
            //Варианты при которых рокировка невозможна
            //атакованы промежуточные клетки
            p.Board[4, 3] = new Square(new Queen(ColorFigur.black));
            Assert.AreEqual(mcL.StartFigure.CheckMove(p, mcL), false);
            p.Board[4, 3] = new Square(new NotFigur());
            p.Board[4, 5] = new Square(new Queen(ColorFigur.black));
            Assert.AreEqual(mcR.StartFigure.CheckMove(p, mcR), false);
            p.Board[4, 4] = new Square(new NotFigur());
            //король под шахом
            p.Board[4, 4] = new Square(new Queen(ColorFigur.black));
            Assert.AreEqual(mcL.StartFigure.CheckMove(p, mcL), false);
            Assert.AreEqual(mcR.StartFigure.CheckMove(p, mcR), false);
            p.Board[4, 4] = new Square(new NotFigur());
            //атакованы конечные клетки
            p.Board[4, 6] = new Square(new Queen(ColorFigur.black));
            p.Board[4, 2] = new Square(new Queen(ColorFigur.black));
            Assert.AreEqual(mcL.StartFigure.CheckMove(p, mcL), false);
            Assert.AreEqual(mcR.StartFigure.CheckMove(p, mcR), false);
            p.Board[4, 6] = new Square(new NotFigur());
            p.Board[4, 2] = new Square(new NotFigur());
            //король уже ходил
            p.IsKingMovedWhite = true;
            Assert.AreEqual(mcL.StartFigure.CheckMove(p, mcL), false);
            Assert.AreEqual(mcR.StartFigure.CheckMove(p, mcR), false);
            p.IsKingMovedWhite = false;
            //ладья уже ходила
            p.IsLeftRockMovedWhite  = true;
            p.IsRightRockMovedWhite = true;
            Assert.AreEqual(mcL.StartFigure.CheckMove(p, mcL), false);
            Assert.AreEqual(mcR.StartFigure.CheckMove(p, mcR), false);
            p.IsLeftRockMovedWhite  = false;
            p.IsRightRockMovedWhite = false;
            //отсутствуют ладьи
            p.Board[0, 0] = new Square(new NotFigur());
            p.Board[0, 7] = new Square(new NotFigur());
            Assert.AreEqual(mcL.StartFigure.CheckMove(p, mcL), false);
            Assert.AreEqual(mcR.StartFigure.CheckMove(p, mcR), false);
        }
Exemplo n.º 11
0
        private void PCMove()
        {
            // Проверка, что игра не окончена и она идет с ПК
            if (IsPCMove && IsStartGame && IsWithPC)
            {
                if (GameLogic.GameLogic.BlackWin || GameLogic.GameLogic.WhiteWin || GameLogic.GameLogic.GamePosition.IsMat ||
                    GameLogic.GameLogic.GamePosition.IsPat)
                {
                    return;
                }

                //Запуск поиска хода за ПК
                Task.Run(() =>
                {
                    if (GameLogic.GameLogic.Moves.Count == 0)
                    {
                        engine = new Engine.Engine();
                    }
                    int?myK      = engine.SearchInTime(GameLogic.GameLogic.GamePosition);
                    MoveCoord mc = new MoveCoord();
                    mc.xStart    = -1;
                    if (engine.MovesWithScore.Count > 0)
                    {
                        mc = engine.MovesWithScore.Where(vpm => vpm != null).Where(vpm => vpm.Score == myK).FirstOrDefault().MC;
                    }
                    if (mc.xStart != -1)
                    {
                        bool b = GameLogic.GameLogic.TryMoved(mc);
                        if (!b)
                        {
                            mc = engine.SearchAllLegalMoves(GameLogic.GameLogic.GamePosition).First();     // Если движок не присал ходов, сделать первый возможный
                        }
                        if (mc != null)
                        {
                            GameLogic.GameLogic.TryMoved(mc);
                        }
                        if (GameLogic.GameLogic.GamePosition.Board[mc.xEnd, mc.yEnd].Figure.Type == TypeFigur.peen && (mc.xEnd == 0 || mc.xEnd == 7))
                        {
                            GameLogic.GameLogic.GamePosition.Board[mc.xEnd, mc.yEnd] = new Square(new Queen(GameLogic.GameLogic.GamePosition.Board[mc.xEnd, mc.yEnd].Figure.Color));
                        }
                        IsPCMove = !IsPCMove;
                    }

                    //Проверка на наличие мата или пата
                    Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                           (ThreadStart) delegate()
                    {
                        UpdatePosition();
                        int?isWin = GameLogic.GameLogic.CheckPosition(GameLogic.GameLogic.GamePosition);

                        if (GameLogic.GameLogic.BlackWin || isWin < 0 && isWin != null)
                        {
                            MessageBox.Show("Чёрные победили!");
                            Engine.Engine.IsStopSearch = true;
                        }
                        else if (GameLogic.GameLogic.WhiteWin || isWin > 0)
                        {
                            MessageBox.Show("Белые победили!");
                            Engine.Engine.IsStopSearch = true;
                        }
                        else if (isWin == 0 || GameLogic.GameLogic.MovesWithoutEating >= 50)
                        {
                            if (!(GameLogic.GameLogic.MovesWithoutEating > 49))
                            {
                                MessageBox.Show("Ничья");
                            }
                            Engine.Engine.IsStopSearch = true;
                        }
                        // Звук о совершенном ходе
                        Console.Beep(240, 300);

                        //Запуск поиска ходов в режиме хода игрока
                        if (!Engine.Engine.IsStopSearch)
                        {
                            Task.Run(() =>
                            {
                                engine.SearchInTime(GameLogic.GameLogic.GamePosition, 1, true);
                            });
                        }
                        else
                        {
                            Engine.Engine.IsStopSearch = false;
                        }
                    });
                });
                return;
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Событие нажатия на клетку
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClickChBut(object sender, RoutedEventArgs e)
        {
            if (IsPCMove)
            {
                return;           //Если ход компьютера - никакой реакции
            }
            //При редактировании фигур
            if (IsInsert)
            {
                if (newFigure.Figure.Type == TypeFigur.king) //Проверяем, что в случае установке короля на доску он будет один
                {
                    foreach (Square s in GameLogic.GameLogic.GamePosition.Board)
                    {
                        if (s.Figure.Type == TypeFigur.king && newFigure.Figure.Color == s.Figure.Color)
                        {
                            return;
                        }
                    }
                }
                Coord c = GetCoordFromButton((Button)e.OriginalSource);
                GameLogic.GameLogic.GamePosition.Board[c.x, c.y] = new Square(newFigure.Figure);
                UpdatePosition();
            }
            else
            {
                //Если фигура не выбрана
                if (!isSelected)
                {
                    foreach (Grid g in gridBoard.Children)
                    {
                        if (g.Name == ((Button)e.OriginalSource).Name)
                        {
                            #region//Если нажали на клетку с фигурой - выделение клетки и получение координат
                            if (g.Background is ImageBrush)
                            {
                                int x = Convert.ToInt32(g.Name[6].ToString());
                                int y = Convert.ToInt32(g.Name[7].ToString());

                                if (GameLogic.GameLogic.GamePosition.Board[x, y] != new Square(new NotFigur()))
                                {
                                    g.Background     = new SolidColorBrush(Colors.Aqua);
                                    isSelected       = true;
                                    move.StartFigure = GameLogic.GameLogic.GamePosition.Board[x, y].Figure;
                                    move.xStart      = x;
                                    move.yStart      = y;
                                    break;
                                }
                            }
                            #endregion

                            #region//Если нажали на клетку с уже выделенной фигурой - отменить выделение
                            else
                            {
                                isSelected = false;
                                move       = new MoveCoord();
                                int x = Convert.ToInt32(g.Name[6].ToString());
                                int y = Convert.ToInt32(g.Name[7].ToString());
                                if (x % 2 == 1)
                                {
                                    if (y % 2 == 0)
                                    {
                                        var ib = new ImageBrush();
                                        ib.ImageSource = new BitmapImage(new Uri(@"Resourses\B.png", UriKind.Relative));
                                        g.Background   = ib;
                                    }
                                    else
                                    {
                                        var ib = new ImageBrush();
                                        ib.ImageSource = new BitmapImage(new Uri(@"Resourses\W.png", UriKind.Relative));
                                        g.Background   = ib;
                                    }
                                }
                                else
                                {
                                    if (y % 2 == 0)
                                    {
                                        var ib = new ImageBrush();
                                        ib.ImageSource = new BitmapImage(new Uri(@"Resourses\W.png", UriKind.Relative));
                                        g.Background   = ib;
                                    }
                                    else
                                    {
                                        var ib = new ImageBrush();
                                        ib.ImageSource = new BitmapImage(new Uri(@"Resourses\B.png", UriKind.Relative));
                                        g.Background   = ib;
                                    }
                                }
                                break;
                            }
                            #endregion
                        }
                    }
                }
                else
                {
                    //Gлучаем конечные координаты и делаем ход
                    isSelected      = false;
                    move.IsCastling = false;
                    foreach (Grid g in gridBoard.Children)
                    {
                        if (!(g.Background is ImageBrush))
                        {
                            int x = Convert.ToInt32(g.Name[6].ToString());
                            int y = Convert.ToInt32(g.Name[7].ToString());
                            if (x % 2 == 0)
                            {
                                if (y % 2 == 0)
                                {
                                    var ib = new ImageBrush();
                                    ib.ImageSource = new BitmapImage(new Uri(@"Resourses\B.png", UriKind.Relative));
                                    g.Background   = ib;
                                }
                                else
                                {
                                    var ib = new ImageBrush();
                                    ib.ImageSource = new BitmapImage(new Uri(@"Resourses\W.png", UriKind.Relative));
                                    g.Background   = ib;
                                }
                            }
                            else
                            {
                                if (y % 2 == 0)
                                {
                                    var ib = new ImageBrush();
                                    ib.ImageSource = new BitmapImage(new Uri(@"Resourses\W.png", UriKind.Relative));
                                    g.Background   = ib;
                                }
                                else
                                {
                                    var ib = new ImageBrush();
                                    ib.ImageSource = new BitmapImage(new Uri(@"Resourses\B.png", UriKind.Relative));
                                    g.Background   = ib;
                                }
                            }
                        }
                        if (g.Name == ((Button)e.OriginalSource).Name)
                        {
                            int x = Convert.ToInt32(g.Name[6].ToString());
                            int y = Convert.ToInt32(g.Name[7].ToString());
                            if (x == move.xStart && y == move.yStart)
                            {
                                move = new MoveCoord();
                                return;
                            }
                            //Получаем конечные координаты
                            move.xEnd      = x;
                            move.yEnd      = y;
                            move.NewFigure = move.StartFigure;

                            //Если игра началась, то ходим по правилам
                            if (IsStartGame)
                            {
                                //Делаем ход
                                if (GameLogic.GameLogic.TryMoved(move))
                                {
                                    //Переключаем очередь хода для ИИ, если играем с ПК
                                    if (IsWithPC)
                                    {
                                        IsPCMove = !IsPCMove;
                                    }

                                    // Если был ход пешкой на последнюю горизонталь - открыть окно выбора фигуры
                                    #region
                                    if (GameLogic.GameLogic.GamePosition.Board[x, y].Figure.Type == TypeFigur.peen)
                                    {
                                        if (GameLogic.GameLogic.GamePosition.Board[x, y].Figure.Color == ColorFigur.white && x == 7)
                                        {
                                            GameLogic.GameLogic.TimeBlack.Stop();
                                            GameLogic.GameLogic.TimeWhite.Start();
                                            WindowConvertPeen wp = new WindowConvertPeen();
                                            wp.ShowDialog();

                                            if ((bool)wp.DialogResult)
                                            {
                                                GameLogic.GameLogic.GamePosition.Board[x, y] = new Square(wp.Figure.Figure);
                                                GameLogic.GameLogic.GamePosition.Board[x, y].Figure.Color = ColorFigur.white;
                                            }
                                            else
                                            {
                                                GameLogic.GameLogic.GamePosition.Board[x, y] = new Square(new Queen(ColorFigur.white));
                                            }
                                            GameLogic.GameLogic.TimeBlack.Start();
                                            GameLogic.GameLogic.TimeWhite.Stop();
                                        }
                                        else if (GameLogic.GameLogic.GamePosition.Board[x, y].Figure.Color == ColorFigur.black && x == 0)
                                        {
                                            GameLogic.GameLogic.TimeBlack.Start();
                                            GameLogic.GameLogic.TimeWhite.Stop();
                                            WindowConvertPeen wp = new WindowConvertPeen();
                                            wp.ShowDialog();
                                            if ((bool)wp.DialogResult)
                                            {
                                                GameLogic.GameLogic.GamePosition.Board[x, y] = new Square(wp.Figure.Figure);
                                                GameLogic.GameLogic.GamePosition.Board[x, y].Figure.Color = ColorFigur.black;
                                            }
                                            else
                                            {
                                                GameLogic.GameLogic.GamePosition.Board[x, y] = new Square(new Queen(ColorFigur.black));
                                            }
                                            GameLogic.GameLogic.TimeBlack.Stop();
                                            GameLogic.GameLogic.TimeWhite.Start();
                                        }
                                    }
                                    #endregion
                                    // Проверить позицию на наличие пата или мата
                                    int?isWin = GameLogic.GameLogic.CheckPosition(GameLogic.GameLogic.GamePosition);
                                    if (GameLogic.GameLogic.BlackWin || isWin < 0 && isWin != null)
                                    {
                                        MessageBox.Show("BlackWin");
                                    }
                                    else if (GameLogic.GameLogic.WhiteWin || isWin > 0)
                                    {
                                        MessageBox.Show("WhiteWin");
                                    }
                                    else if (isWin == 0)
                                    {
                                        MessageBox.Show("Ничья");
                                    }
                                    // Звук, уведомляющий о перемещении фигуры
                                    Console.Beep(240, 300);
                                }
                            }
                            //Если мы вводим позицию (не играем), то просто переместить фигуру
                            else
                            {
                                #region
                                GameLogic.GameLogic.GamePosition.Move(move);
                                if (GameLogic.GameLogic.GamePosition.Board[x, y].Figure.Type == TypeFigur.peen)
                                {
                                    if (GameLogic.GameLogic.GamePosition.Board[x, y].Figure.Color == ColorFigur.white && x == 7)
                                    {
                                        WindowConvertPeen wp = new WindowConvertPeen();
                                        wp.ShowDialog();
                                        if ((bool)wp.DialogResult)
                                        {
                                            GameLogic.GameLogic.GamePosition.Board[x, y] = new Square(wp.Figure.Figure);
                                        }
                                        else
                                        {
                                            GameLogic.GameLogic.GamePosition.Board[x, y] = new Square(new Queen(ColorFigur.white));
                                        }
                                    }
                                    else if (GameLogic.GameLogic.GamePosition.Board[x, y].Figure.Color == ColorFigur.black && x == 0)
                                    {
                                        WindowConvertPeen wp = new WindowConvertPeen();
                                        wp.ShowDialog();
                                        if ((bool)wp.DialogResult)
                                        {
                                            GameLogic.GameLogic.GamePosition.Board[x, y] = new Square(wp.Figure.Figure);
                                        }
                                        else
                                        {
                                            GameLogic.GameLogic.GamePosition.Board[x, y] = new Square(new Queen(ColorFigur.black));
                                        }
                                    }
                                }
                                #endregion
                            }
                        }
                    }
                }
                UpdatePosition();

                PCMove(); // Передать ход ПК
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// Класс используемый для хранения ходов и их весов при расчетах
 /// </summary>
 /// <param name="s">Вес хода</param>
 /// <param name="mc">Ход</param>
 public Move(int s, MoveCoord mc)
 {
     MC    = mc;
     Score = s;
 }
Exemplo n.º 14
0
        public WindowBoard(Position pos, TimeSpan tW, TimeSpan tB, bool IsWhite, bool IsPC, bool fliped)
        {
            //Установка параметров игры
            isFliped = fliped;
            timer    = new System.Timers.Timer(300);
            InitializeComponent();
            chessBoard.IsStartGame = true;
            TecSizeX = Height;
            TecSizeY = Width;
            tw       = tW;
            tb       = tB;
            isWhite  = IsWhite;
            isPC     = IsPC;
            timeWhite.UpdateTime(tW);
            timeBlack.Color = ColorFigur.black;
            timeWhite.Color = ColorFigur.white;
            timeBlack.UpdateTime(tB);
            GameLogic.GameLogic.GamePosition             = (Position)pos.DeepCopy();
            GameLogic.GameLogic.GamePosition.IsWhiteMove = IsWhite;
            if (!IsWhite)
            {
                chessBoard.Flip();
            }
            GameLogic.GameLogic.NewGame(tw, tb, null, null, (Position)pos.DeepCopy());
            timer.Enabled       = true;
            timer.Elapsed      += UpdateTime;
            timer.Elapsed      += changeListMoves;
            chessBoard.IsWithPC = IsPC;
            Moves.Text          = "";
            if (isFliped)
            {
                Grid.SetRow(timeBlack, 2);
                Grid.SetRow(timeWhite, 0);
            }

            //Проверяем в каком режиме включать движок
            if (IsWhite)
            {
                if (GameLogic.GameLogic.GamePosition.IsWhiteMove && chessBoard.IsWithPC)
                {
                    chessBoard.IsPCMove = false;
                    Task.Run(() =>
                    {
                        chessBoard.engine.SearchInTime(GameLogic.GameLogic.GamePosition, 1, true);
                    });
                }
                else if (chessBoard.IsWithPC)
                {
                    chessBoard.IsPCMove = true;
                    Task.Run(() =>
                    {
                        int?myK      = chessBoard.engine.SearchInTime(GameLogic.GameLogic.GamePosition);
                        MoveCoord mc = chessBoard.engine.MovesWithScore.Where(vpm => vpm.Score == myK).FirstOrDefault().MC;
                        if (mc != null)
                        {
                            GameLogic.GameLogic.TryMoved(mc);
                        }
                        chessBoard.IsPCMove = !chessBoard.IsPCMove;
                        Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                               (ThreadStart) delegate()
                        {
                            chessBoard.UpdatePosition();
                        });
                        Task.Run(() =>
                        {
                            chessBoard.engine.SearchInTime(GameLogic.GameLogic.GamePosition, 1, true);
                        });
                    });
                }
            }
            else if (chessBoard.IsWithPC)
            {
                if (GameLogic.GameLogic.GamePosition.IsWhiteMove)
                {
                    chessBoard.IsPCMove = true;
                    Task.Run(() =>
                    {
                        double?myK   = chessBoard.engine.SearchInTime(GameLogic.GameLogic.GamePosition);
                        MoveCoord mc = chessBoard.engine.MovesWithScore.Where(vpm => vpm.Score == myK).FirstOrDefault().MC;
                        if (mc != null)
                        {
                            GameLogic.GameLogic.TryMoved(mc);
                        }
                        chessBoard.IsPCMove = !chessBoard.IsPCMove;
                        Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                                               (ThreadStart) delegate()
                        {
                            chessBoard.UpdatePosition();
                        });
                        Task.Run(() =>
                        {
                            chessBoard.engine.SearchInTime(GameLogic.GameLogic.GamePosition, 1, true);
                        });
                    });
                }
                else
                {
                    chessBoard.IsPCMove = false;
                    Task.Run(() =>
                    {
                        chessBoard.engine.SearchInTime(GameLogic.GameLogic.GamePosition, 1, true);
                    });
                }
            }
            chessBoard.UpdatePosition();
        }