コード例 #1
0
ファイル: Knight.cs プロジェクト: yan1304/YanChess
        /// <summary>
        /// проверка возможности хода для фигуры
        /// </summary>
        public override bool CheckMove(Position position, MoveCoord mc)
        {
            bool isLegal = false;

            if (position.Board[mc.xEnd, mc.yEnd].Figure.Color == position.Board[mc.xStart, mc.yStart].Figure.Color)
            {
                return(false);
            }
            if (Math.Abs(mc.xEnd - mc.xStart) == 2)
            {
                if (Math.Abs(mc.yEnd - mc.yStart) == 1)
                {
                    isLegal = true;
                }
                else
                {
                    return(false);
                }
            }
            else if (Math.Abs(mc.xEnd - mc.xStart) == 1)
            {
                if (Math.Abs(mc.yEnd - mc.yStart) == 2)
                {
                    isLegal = true;
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
            if (position.IsWhiteMove)
            {
                if (position.Board[mc.xStart, mc.yStart].Figure.Color == ColorFigur.black)
                {
                    return(false);
                }
            }
            else
            {
                if (position.Board[mc.xStart, mc.yStart].Figure.Color == ColorFigur.white)
                {
                    return(false);
                }
            }

            if (isLegal)
            {
                //проверка на отсутствие шаха королю после хода
                position.MoveChess(mc);
                //черным
                isLegal = IsHaventCheck(position);
            }

            position.MoveBack(mc);
            return(isLegal);
        }
コード例 #2
0
ファイル: GameLogic.cs プロジェクト: yan1304/YanChess
        private static bool CheckBishop(Position position, int i, int j)
        {
            MoveCoord mc = new MoveCoord();

            mc.xStart      = i;
            mc.yStart      = j;
            mc.StartFigure = position.Board[i, j].Figure;
            bool isLegal = false;

            for (int x = 0; x < 8; x++)
            {
                if (mc.xStart + x < 8)
                {
                    if (mc.yStart + x < 8)
                    {
                        mc.xEnd = mc.xStart + x;
                        mc.yEnd = mc.yStart + x;
                        if (mc.StartFigure.CheckMove(position, mc))
                        {
                            isLegal = true;
                            break;
                        }
                    }
                    if (mc.yStart - x >= 0)
                    {
                        mc.xEnd = mc.xStart + x;
                        mc.yEnd = mc.yStart - x;
                        if (mc.StartFigure.CheckMove(position, mc))
                        {
                            isLegal = true;
                            break;
                        }
                    }
                }
                if (mc.xStart - x >= 0)
                {
                    if (mc.yStart + x < 8)
                    {
                        mc.xEnd = mc.xStart - x;
                        mc.yEnd = mc.yStart + x;
                        if (mc.StartFigure.CheckMove(position, mc))
                        {
                            isLegal = true;
                            break;
                        }
                    }
                    if (mc.yStart - x >= 0)
                    {
                        mc.xEnd = mc.xStart - x;
                        mc.yEnd = mc.yStart - x;
                        if (mc.StartFigure.CheckMove(position, mc))
                        {
                            isLegal = true;
                            break;
                        }
                    }
                }
            }
            return(isLegal);
        }
コード例 #3
0
 /// <summary>
 /// Сделать ход (независимо от правил)
 /// </summary>
 public void Move(MoveCoord move)
 {
     move.StartFigure = Board[move.xStart, move.yStart].Figure;
     move.EndFigure   = Board[move.xEnd, move.yEnd].Figure;
     if (move.NewFigure.Type == TypeFigur.none)
     {
         move.NewFigure = move.StartFigure;
     }
     Board[move.xStart, move.yStart] = new Square(new NotFigur());
     Board[move.xEnd, move.yEnd]     = new Square(move.NewFigure);
 }
コード例 #4
0
ファイル: MoveCoord.cs プロジェクト: yan1304/YanChess
        private int ScoreInFigur(MoveCoord mc)
        {
            int d = 0;

            switch (mc.EndFigure.Type)
            {
            case TypeFigur.king: d += 60; break;

            case TypeFigur.bishop: d += 30; break;

            case TypeFigur.knight: d += 31; break;

            case TypeFigur.peen: d += 10; break;

            case TypeFigur.queen: d += 90; break;

            case TypeFigur.rock: d += 50; break;
            }
            switch (mc.StartFigure.Type)
            {
            case TypeFigur.king: d += 6; break;

            case TypeFigur.bishop: d += 3; break;

            case TypeFigur.knight: d += 3; break;

            case TypeFigur.peen: d += 1; break;

            case TypeFigur.queen: d += 9; break;

            case TypeFigur.rock: d += 5; break;
            }
            if (mc.StartFigure.Type == TypeFigur.peen && mc.NewFigure.Type == TypeFigur.queen)
            {
                d += 100;
            }
            d += Math.Min(7 - mc.xEnd, xEnd); //первыми лучше проверять фигуры в центре
            d += Math.Min(7 - mc.yEnd, yEnd);
            return(d);
        }
コード例 #5
0
 /// <summary>
 /// проверка возможности хода для фигуры
 /// </summary>
 abstract public bool CheckMove(Position position, MoveCoord mc);
コード例 #6
0
        /// <summary>
        /// Ход с правилами т.е. с переключением хода, рокировкой и т.п.
        /// </summary>
        /// <param name="move"></param>
        public void MoveChess(MoveCoord move)
        {
            move.EndFigure = Board[move.xEnd, move.yEnd].Figure;
            if (Board[move.xStart, move.yStart].Figure.Type == TypeFigur.peen)
            {
                if (Board[move.xStart, move.yStart].Figure.Color == ColorFigur.white)
                {
                    if (Math.Abs(move.yStart - move.yEnd) == 1 && Math.Abs(move.xStart - move.yEnd) == 1)
                    {
                        if (Board[move.xStart, move.yEnd].Figure.Color == ColorFigur.black && Board[move.xStart, move.yEnd].Figure.Type == TypeFigur.peen)
                        {
                            if (((Peen)Board[move.xStart, move.yEnd].Figure).IsEnPassant)
                            {
                                move.IsEnPassant = true;
                            }
                        }
                    }
                }
                else
                {
                    if (Math.Abs(move.yStart - move.yEnd) == 1 && Math.Abs(move.xStart - move.yEnd) == 1)
                    {
                        if (Board[move.xStart, move.yEnd].Figure.Color == ColorFigur.white && Board[move.xStart, move.yEnd].Figure.Type == TypeFigur.peen)
                        {
                            if (((Peen)Board[move.xStart, move.yEnd].Figure).IsEnPassant)
                            {
                                move.IsEnPassant = true;
                            }
                        }
                    }
                }
            }
            //перемещаем фигуру
            Move(move);

            //убираем возможность взятий на проходе если такие были
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (Board[i, j].Figure.Type == TypeFigur.peen && Board[i, j].Figure.Color != move.StartFigure.Color)
                    {
                        if (((Peen)Board[i, j].Figure).IsEnPassant)
                        {
                            ((Peen)Board[i, j].Figure).IsEnPassant = false;
                            move.HavePassant = true;
                            move.xPassant    = i;
                            move.yPassant    = j;
                            break;
                        }
                    }
                }
            }
            //для короля
            if (move.StartFigure.Type == TypeFigur.king)
            {
                move.IsKingParamChange = true;
                if (move.StartFigure.Color == ColorFigur.white)
                {
                    IsKingMovedWhite = true;//король ходил
                    //при рокировке перемещаем ладью
                    if (move.IsCastling)
                    {
                        if (move.yEnd == 6)
                        {
                            Board[0, 7]            = new Square(new NotFigur());
                            Board[0, 5]            = new Square(new Rock(ColorFigur.white));
                            move.IsRockParamChange = true;
                        }
                        else
                        {
                            Board[0, 0]            = new Square(new NotFigur());
                            Board[0, 3]            = new Square(new Rock(ColorFigur.white));
                            move.IsRockParamChange = true;
                        }
                    }
                }
                else
                {
                    //Эквивалентные действия для черного короля
                    IsKingMovedBlack = true;
                    if (move.IsCastling)
                    {
                        if (move.yEnd == 6)
                        {
                            Board[7, 7]            = new Square(new NotFigur());
                            Board[7, 5]            = new Square(new Rock(ColorFigur.black));
                            move.IsRockParamChange = true;
                        }
                        else
                        {
                            Board[7, 0]            = new Square(new NotFigur());
                            Board[7, 3]            = new Square(new Rock(ColorFigur.black));
                            move.IsRockParamChange = true;
                        }
                    }
                }
            }
            else if (move.StartFigure.Type == TypeFigur.rock)
            {
                move.IsRockParamChange = true;
                //Для ладьи указать, что она двигалась (при попытке рокировки это будет учтено)
                if (move.xStart == 0 && move.StartFigure.Color == ColorFigur.white)
                {
                    if (move.yStart == 0)
                    {
                        IsLeftRockMovedWhite = true;
                    }
                    else if (move.yStart == 7)
                    {
                        IsRightRockMovedWhite = true;
                    }
                }
                else if (move.xStart == 7 && move.StartFigure.Color == ColorFigur.black)
                {
                    if (move.yStart == 0)
                    {
                        IsLeftRockMovedBlack = true;
                    }
                    else if (move.yStart == 7)
                    {
                        IsRightRockMovedBlack = true;
                    }
                }
            }
            else if (move.StartFigure.Type == TypeFigur.peen)
            {
                //для пешки нужно реализовать взятие на проходе и возможность взятия данной пешки на проходе
                if (move.IsEnPassant)
                {
                    //взятие на проходе
                    if (Board[move.xStart, move.yEnd].Figure.Type == TypeFigur.peen &&
                        Board[move.xStart, move.yEnd].Figure.Color != move.StartFigure.Color)
                    {
                        Board[move.xStart, move.yEnd] = new Square(new NotFigur());
                    }
                    else
                    {
                        move.IsEnPassant = false;
                    }
                }
                else if (Math.Abs(move.xStart - move.xEnd) == 2)
                {
                    if (move.yStart == move.yEnd && ((move.StartFigure.Color == ColorFigur.white && move.xStart == 1) || (move.StartFigure.Color == ColorFigur.black && move.xStart == 6)))
                    {
                        ((Peen)Board[move.xEnd, move.yEnd].Figure).IsEnPassant = true;//пешка сделала ход через две клетки, т.е. ее можно взять на проходе (если есть чем)
                    }
                }
            }
            IsWhiteMove = !IsWhiteMove;//смена очереди хода
        }
コード例 #7
0
 /// <summary>
 /// Откатить ход
 /// </summary>
 public void MoveBack(MoveCoord move)
 {
     Board[move.xEnd, move.yEnd]     = new Square(move.EndFigure);
     Board[move.xStart, move.yStart] = new Square(move.StartFigure);
     if (move.StartFigure.Color == ColorFigur.white)
     {
         IsWhiteMove = true;
     }
     else
     {
         IsWhiteMove = false;
     }
     if (move.IsEnPassant)
     {
         if (move.NewFigure.Color == ColorFigur.white)
         {
             Board[move.xStart, move.yEnd] = new Square(new Peen(ColorFigur.black));
         }
         else
         {
             Board[move.xStart, move.yEnd] = new Square(new Peen(ColorFigur.white));
         }
         ((Peen)Board[move.xStart, move.yEnd].Figure).IsEnPassant = true;
     }
     if (move.HavePassant && Board[move.xPassant, move.yPassant].Figure.Type == TypeFigur.peen)
     {
         ((Peen)Board[move.xPassant, move.yPassant].Figure).IsEnPassant = true;
     }
     if (move.IsCastling)
     {
         if (move.NewFigure.Color == ColorFigur.white)
         {
             if (move.yEnd == 6 && move.xEnd == 0)
             {
                 Board[move.xEnd, 7]   = new Square(new Rock(ColorFigur.white));
                 Board[move.xEnd, 5]   = new Square(new NotFigur());
                 IsRightRockMovedWhite = false;
             }
             else if (move.yEnd == 2 && move.xEnd == 0)
             {
                 Board[move.xEnd, 0]  = new Square(new Rock(ColorFigur.white));
                 Board[move.xEnd, 3]  = new Square(new NotFigur());
                 IsLeftRockMovedWhite = false;
             }
             IsKingMovedWhite = false;
         }
         else
         {
             if (move.yEnd == 6 && move.xEnd == 7)
             {
                 Board[move.xEnd, 7]   = new Square(new Rock(ColorFigur.black));
                 Board[move.xEnd, 5]   = new Square(new NotFigur());
                 IsRightRockMovedBlack = false;
             }
             else if (move.yEnd == 2 && move.xEnd == 7)
             {
                 Board[move.xEnd, 0]  = new Square(new Rock(ColorFigur.black));
                 Board[move.xEnd, 3]  = new Square(new NotFigur());
                 IsLeftRockMovedBlack = false;
             }
             IsKingMovedBlack = false;
         }
     }
     if (move.IsKingParamChange)
     {
         if (move.StartFigure.Color == ColorFigur.white)
         {
             IsKingMovedWhite = false;
         }
         else
         {
             IsKingMovedBlack = false;
         }
     }
     if (move.IsRockParamChange)
     {
         if (move.StartFigure.Color == ColorFigur.white)
         {
             if (move.yStart == 7)
             {
                 IsRightRockMovedWhite = false;
             }
             else
             {
                 IsLeftRockMovedWhite = false;
             }
         }
         else
         {
             if (move.yStart == 7)
             {
                 IsRightRockMovedBlack = false;
             }
             else
             {
                 IsLeftRockMovedBlack = false;
             }
         }
     }
 }
コード例 #8
0
        /// <summary>
        /// проверка возможности хода для фигуры
        /// </summary>
        public override bool CheckMove(Position position, MoveCoord mc)
        {
            bool isLegal = true;

            if (position.Board[mc.xEnd, mc.yEnd].Figure.Color == position.Board[mc.xStart, mc.yStart].Figure.Color)
            {
                return(false);
            }
            if ((position.Board[mc.xStart, mc.yStart].Figure.Color == ColorFigur.white && position.IsWhiteMove) ||
                (position.Board[mc.xStart, mc.yStart].Figure.Color == ColorFigur.black && !(position.IsWhiteMove)))
            {
                if (Math.Abs(mc.xStart - mc.xEnd) == Math.Abs(mc.yEnd - mc.yStart))
                {
                    for (int x = 1; x < 8; x++)
                    {
                        if (mc.xEnd > mc.xStart)
                        {
                            if ((mc.xStart + x <= 7) && (mc.xStart + x < mc.xEnd))
                            {
                                if (mc.yEnd > mc.yStart)
                                {
                                    if ((mc.yStart + x <= 7) && (mc.yStart + x < mc.yEnd))
                                    {
                                        if (position.Board[mc.xStart + x, mc.yStart + x].Figure.Type != TypeFigur.none)
                                        {
                                            isLegal = false;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    if ((mc.yStart - x >= 0) && (mc.yStart - x > mc.yEnd))
                                    {
                                        if (position.Board[mc.xStart + x, mc.yStart - x].Figure.Type != TypeFigur.none)
                                        {
                                            isLegal = false;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            if ((mc.xStart - x >= 0) && (mc.xStart - x > mc.xEnd))
                            {
                                if (mc.yEnd > mc.yStart)
                                {
                                    if ((mc.yStart + x <= 7) && (mc.yStart + x < mc.yEnd))
                                    {
                                        if (position.Board[mc.xStart - x, mc.yStart + x].Figure.Type != TypeFigur.none)
                                        {
                                            isLegal = false;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    if ((mc.yStart - x >= 0) && (mc.yStart - x > mc.yEnd))
                                    {
                                        if (position.Board[mc.xStart - x, mc.yStart - x].Figure.Type != TypeFigur.none)
                                        {
                                            isLegal = false;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (mc.yEnd == mc.yStart)
                    {
                        for (int x = 1; x < 8; x++)
                        {
                            if (((mc.xStart + x) < mc.xEnd) && ((mc.xStart + x) < 8))
                            {
                                if (position.Board[mc.xStart + x, mc.yEnd].Figure.Type != TypeFigur.none)
                                {
                                    isLegal = false;
                                    break;
                                }
                            }
                            else if (((mc.xStart - x) > mc.xEnd) && ((mc.xStart - x) >= 0))
                            {
                                if (position.Board[mc.xStart - x, mc.yEnd].Figure.Type != TypeFigur.none)
                                {
                                    isLegal = false;
                                    break;
                                }
                            }
                        }
                    }
                    else if (mc.xEnd == mc.xStart)
                    {
                        for (int x = 1; x < 8; x++)
                        {
                            if (((mc.yStart + x) < mc.yEnd) && ((mc.yStart + x) < 8))
                            {
                                if (position.Board[mc.xStart, mc.yStart + x].Figure.Type != TypeFigur.none)
                                {
                                    isLegal = false;
                                    break;
                                }
                            }
                            else if (((mc.yStart - x) > mc.yEnd) && ((mc.yStart - x) >= 0))
                            {
                                if (position.Board[mc.xStart, mc.yStart - x].Figure.Type != TypeFigur.none)
                                {
                                    isLegal = false;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else
            {
                return(false);
            }

            if (isLegal)
            {
                //проверка на отсутствие шаха королю после хода
                position.MoveChess(mc);
                //черным
                isLegal = IsHaventCheck(position);
            }

            position.MoveBack(mc);
            return(isLegal);
        }
コード例 #9
0
 /// <summary>
 /// проверка возможности хода для фигуры
 /// </summary>
 public override bool CheckMove(Position position, MoveCoord mc)
 {
     return(false);
 }
コード例 #10
0
ファイル: GameLogic.cs プロジェクト: yan1304/YanChess
        private static bool CheckKing(Position position, int i, int j)
        {
            MoveCoord mc = new MoveCoord();

            mc.xStart      = i;
            mc.yStart      = j;
            mc.StartFigure = position.Board[i, j].Figure;
            int x = 1;

            if (position.IsWhiteMove)
            {
                if (i == 0 && j == 4 && !position.IsKingMovedWhite)
                {
                    if (!position.IsLeftRockMovedWhite)
                    {
                        mc.xEnd = i;
                        mc.yEnd = 2;
                        if (mc.StartFigure.CheckMove(position, mc))
                        {
                            return(true);
                        }
                    }
                    if (!position.IsRightRockMovedWhite)
                    {
                        mc.xEnd = i;
                        mc.yEnd = 6;
                        if (mc.StartFigure.CheckMove(position, mc))
                        {
                            return(true);
                        }
                    }
                }
            }
            else
            {
                if (i == 7 && j == 4 && !position.IsKingMovedBlack)
                {
                    if (!position.IsLeftRockMovedBlack)
                    {
                        mc.xEnd = i;
                        mc.yEnd = 2;
                        if (mc.StartFigure.CheckMove(position, mc))
                        {
                            return(true);
                        }
                    }
                    if (!position.IsRightRockMovedBlack)
                    {
                        mc.xEnd = i;
                        mc.yEnd = 6;
                        if (mc.StartFigure.CheckMove(position, mc))
                        {
                            return(true);
                        }
                    }
                }
            }
            if (mc.xStart + x < 8)
            {
                mc.xEnd = mc.xStart + x;
                mc.yEnd = mc.yStart;
                if (mc.StartFigure.CheckMove(position, mc))
                {
                    return(true);
                }
            }
            if (mc.xStart - x >= 0)
            {
                mc.xEnd = mc.xStart - x;
                mc.yEnd = mc.yStart;
                if (mc.StartFigure.CheckMove(position, mc))
                {
                    return(true);
                }
            }
            if (mc.yStart + x < 8)
            {
                mc.xEnd = mc.xStart;
                mc.yEnd = mc.yStart + x;
                if (mc.StartFigure.CheckMove(position, mc))
                {
                    return(true);
                }
            }
            if (mc.yStart - x >= 0)
            {
                mc.xEnd = mc.xStart;
                mc.yEnd = mc.yStart - x;
                if (mc.StartFigure.CheckMove(position, mc))
                {
                    return(true);
                }
            }
            if (mc.xStart + x < 8)
            {
                if (mc.yStart + x < 8)
                {
                    mc.xEnd = mc.xStart + x;
                    mc.yEnd = mc.yStart + x;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
                if (mc.yStart - x >= 0)
                {
                    mc.xEnd = mc.xStart + x;
                    mc.yEnd = mc.yStart - x;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
            }
            if (mc.xStart - x >= 0)
            {
                if (mc.yStart + x < 8)
                {
                    mc.xEnd = mc.xStart - x;
                    mc.yEnd = mc.yStart + x;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
                if (mc.yStart - x >= 0)
                {
                    mc.xEnd = mc.xStart - x;
                    mc.yEnd = mc.yStart - x;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
コード例 #11
0
ファイル: GameLogic.cs プロジェクト: yan1304/YanChess
        private static bool CheckKnight(Position position, int i, int j)
        {
            MoveCoord mc = new MoveCoord();

            mc.xStart      = i;
            mc.yStart      = j;
            mc.StartFigure = position.Board[i, j].Figure;
            if (i + 1 < 8)
            {
                if (i + 2 < 8)
                {
                    if (j + 1 < 8)
                    {
                        mc.xEnd = mc.xStart + 2;
                        mc.yEnd = mc.yStart + 1;
                        if (mc.StartFigure.CheckMove(position, mc))
                        {
                            return(true);
                        }
                    }
                    if (j - 1 >= 0)
                    {
                        mc.xEnd = mc.xStart + 2;
                        mc.yEnd = mc.yStart - 1;
                        if (mc.StartFigure.CheckMove(position, mc))
                        {
                            return(true);
                        }
                    }
                }
                if (j + 2 < 8)
                {
                    mc.xEnd = mc.xStart + 1;
                    mc.yEnd = mc.yStart + 2;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
                if (j - 2 >= 0)
                {
                    mc.xEnd = mc.xStart + 1;
                    mc.yEnd = mc.yStart - 2;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
            }
            if (i - 1 >= 0)
            {
                if (i - 2 >= 0)
                {
                    if (j + 1 < 8)
                    {
                        mc.xEnd = mc.xStart - 2;
                        mc.yEnd = mc.yStart + 1;
                        if (mc.StartFigure.CheckMove(position, mc))
                        {
                            return(true);
                        }
                    }
                    if (j - 1 >= 0)
                    {
                        mc.xEnd = mc.xStart - 2;
                        mc.yEnd = mc.yStart - 1;
                        if (mc.StartFigure.CheckMove(position, mc))
                        {
                            return(true);
                        }
                    }
                }
                if (j + 2 < 8)
                {
                    mc.xEnd = mc.xStart - 1;
                    mc.yEnd = mc.yStart + 2;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
                if (j - 2 >= 0)
                {
                    mc.xEnd = mc.xStart - 1;
                    mc.yEnd = mc.yStart - 2;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
コード例 #12
0
ファイル: GameLogic.cs プロジェクト: yan1304/YanChess
        private static bool CheckPeen(Position position, int i, int j)
        {
            MoveCoord mc = new MoveCoord();

            mc.xStart      = i;
            mc.yStart      = j;
            mc.StartFigure = position.Board[i, j].Figure;
            if (position.IsWhiteMove)
            {
                mc.yEnd = mc.yStart;
                if (i == 1)
                {
                    mc.xEnd = 3;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
                mc.xEnd = mc.xStart + 1;
                if (mc.StartFigure.CheckMove(position, mc))
                {
                    return(true);
                }
                if (j > 0)
                {
                    mc.yEnd = mc.yStart - 1;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
                if (j < 7)
                {
                    mc.yEnd = mc.yStart + 1;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
            }
            else
            {
                mc.yEnd = mc.yStart;
                if (i == 6)
                {
                    mc.xEnd = 4;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
                mc.xEnd = mc.xStart - 1;
                if (mc.StartFigure.CheckMove(position, mc))
                {
                    return(true);
                }
                if (j > 0)
                {
                    mc.yEnd = mc.yStart - 1;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
                if (j < 7)
                {
                    mc.yEnd = mc.yStart + 1;
                    if (mc.StartFigure.CheckMove(position, mc))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
コード例 #13
0
ファイル: GameLogic.cs プロジェクト: yan1304/YanChess
 /// <summary>
 /// Сделать ход в позиции (возвращает - возможен ли он)
 /// </summary>
 public static bool TryMoved(MoveCoord move)
 {
     if (GamePosition.Board[move.xStart, move.yStart].Figure.Type == TypeFigur.king)
     {
         if (move.yStart == 4)
         {
             if ((move.xStart == 0 && GamePosition.IsWhiteMove) || (move.xStart == 7 && GamePosition.IsWhiteMove == false))
             {
                 if (move.yEnd == 2 || move.yEnd == 6)
                 {
                     move.IsCastling = true;
                 }
             }
         }
     }
     if (GamePosition.Board[move.xStart, move.yStart].Figure.CheckMove(GamePosition, move))
     {
         Positions.Add((Position)GamePosition.DeepCopy());
         MoveCoord mc = new MoveCoord();
         mc.xStart      = move.xStart;
         mc.yStart      = move.yStart;
         mc.yEnd        = move.yEnd;
         mc.xEnd        = move.xEnd;
         mc.StartFigure = move.StartFigure;
         Moves.Add(mc);
         int coll = 0;
         GamePosition.MoveChess(move);
         if (move.EndFigure == new NotFigur() && !move.IsEnPassant)
         {
             MovesWithoutEating++;
         }
         else
         {
             MovesWithoutEating = 0;
         }
         foreach (Position p in Positions)
         {
             if (p.Equals(GamePosition))
             {
                 coll++;
             }
         }
         if (GamePosition.IsWhiteMove)
         {
             TimeBlack.Stop();
             TimeWhite.Start();
             int?i = CheckPosition(GamePosition);
             if (i < 0)
             {
                 BlackWin = true;
                 StopGame();
             }
             else if (i == 0)
             {
                 StopGame();
             }
             else if (coll > 2)
             {
                 MessageBox.Show("Троекратное повторение ходов. Ничья!");
                 MovesWithoutEating = 50;
                 StopGame();
             }
             else if (MovesWithoutEating > 49)
             {
                 MessageBox.Show("50 ходов без взятий. Ничья!");
                 StopGame();
             }
             return(true);
         }
         else
         {
             TimeWhite.Stop();
             TimeBlack.Start();
             int?i = CheckPosition(GamePosition);
             if (i > 0)
             {
                 WhiteWin = true;
                 StopGame();
             }
             if (i == 0)
             {
                 StopGame();
             }
             if (coll > 2 || MovesWithoutEating > 49)
             {
                 StopGame();
             }
             return(true);
         }
     }
     move = new MoveCoord();
     return(false);
 }
コード例 #14
0
        /// <summary>
        /// проверка возможности хода для фигуры
        /// </summary>
        public override bool CheckMove(Position position, MoveCoord mc)
        {
            bool isLegal = false;

            if (position.Board[mc.xEnd, mc.yEnd].Figure.Color == position.Board[mc.xStart, mc.yStart].Figure.Color)
            {
                return(false);
            }
            //проверка рокировки
            if (position.Board[mc.xStart, mc.yStart].Figure.Color == ColorFigur.white && position.IsWhiteMove)
            {
                if (mc.xStart == 0 && mc.xEnd == 0 && mc.yStart == 4 && (mc.yEnd == 6 || mc.yEnd == 2))
                {
                    if (position.IsKingMovedWhite)
                    {
                        return(false);
                    }
                    if (mc.yEnd == 6)
                    {
                        if (position.Board[mc.xEnd, 7].Figure.Type != TypeFigur.rock && position.Board[mc.xEnd, 7].Figure.Color != ColorFigur.white)
                        {
                            return(false);
                        }
                        if (position.IsRightRockMovedWhite)
                        {
                            return(false);
                        }
                        AttackChecker.CheckAttack(ref position, 0, 4, ColorFigur.black);
                        AttackChecker.CheckAttack(ref position, 0, 5, ColorFigur.black);
                        AttackChecker.CheckAttack(ref position, 0, 6, ColorFigur.black);
                        if (position.Board[0, 4].IsAttackBlack || position.Board[0, 5].IsAttackBlack || position.Board[0, 6].IsAttackBlack ||
                            position.Board[0, 5].Figure.Type != TypeFigur.none || position.Board[0, 6].Figure.Type != TypeFigur.none)
                        {
                            position.Board[0, 4].IsAttackBlack = false;
                            position.Board[0, 5].IsAttackBlack = false;
                            position.Board[0, 6].IsAttackBlack = false;
                            return(false);
                        }
                        else
                        {
                            position.Board[0, 4].IsAttackBlack = false;
                            position.Board[0, 5].IsAttackBlack = false;
                            position.Board[0, 6].IsAttackBlack = false;
                            mc.IsCastling = true;
                            isLegal       = true;
                        }
                    }
                    else
                    {
                        if (position.Board[mc.xEnd, 0].Figure.Type != TypeFigur.rock && position.Board[mc.xEnd, 0].Figure.Color != ColorFigur.white)
                        {
                            return(false);
                        }
                        if (position.IsLeftRockMovedWhite)
                        {
                            return(false);
                        }
                        AttackChecker.CheckAttack(ref position, 0, 4, ColorFigur.black);
                        AttackChecker.CheckAttack(ref position, 0, 3, ColorFigur.black);
                        AttackChecker.CheckAttack(ref position, 0, 2, ColorFigur.black);
                        if (position.Board[0, 4].IsAttackBlack || position.Board[0, 3].IsAttackBlack || position.Board[0, 2].IsAttackBlack ||
                            position.Board[0, 2].Figure.Type != TypeFigur.none || position.Board[0, 3].Figure.Type != TypeFigur.none)
                        {
                            position.Board[0, 4].IsAttackBlack = false;
                            position.Board[0, 3].IsAttackBlack = false;
                            position.Board[0, 2].IsAttackBlack = false;
                            return(false);
                        }
                        else
                        {
                            position.Board[0, 4].IsAttackBlack = false;
                            position.Board[0, 3].IsAttackBlack = false;
                            position.Board[0, 2].IsAttackBlack = false;
                            mc.IsCastling = true;
                            isLegal       = true;
                        }
                    }
                }
                else
                {
                    if (Math.Abs(mc.xEnd - mc.xStart) <= 1)
                    {
                        if (Math.Abs(mc.yEnd - mc.yStart) <= 1)
                        {
                            AttackChecker.CheckAttack(ref position, mc.xEnd, mc.yEnd, ColorFigur.black);
                            if (position.Board[mc.xEnd, mc.yEnd].IsAttackBlack)
                            {
                                position.Board[mc.xEnd, mc.yEnd].IsAttackBlack = false;
                                return(false);
                            }
                            else
                            {
                                isLegal = true;
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else if ((!position.IsWhiteMove) && position.Board[mc.xStart, mc.yStart].Figure.Color == ColorFigur.black)
            {
                if (mc.xStart == 7 && mc.xEnd == 7 && mc.yStart == 4 && (mc.yEnd == 6 || mc.yEnd == 2))
                {
                    if (position.IsKingMovedBlack)
                    {
                        return(false);
                    }
                    if (mc.yEnd == 6)
                    {
                        if (position.Board[mc.xEnd, 7].Figure.Type != TypeFigur.rock && position.Board[mc.xEnd, 7].Figure.Color != ColorFigur.black)
                        {
                            return(false);
                        }
                        if (position.IsRightRockMovedBlack)
                        {
                            return(false);
                        }
                        AttackChecker.CheckAttack(ref position, 7, 4, ColorFigur.white);
                        AttackChecker.CheckAttack(ref position, 7, 5, ColorFigur.white);
                        AttackChecker.CheckAttack(ref position, 7, 6, ColorFigur.white);
                        if (position.Board[7, 4].IsAttackWhite || position.Board[7, 5].IsAttackWhite || position.Board[7, 6].IsAttackWhite ||
                            position.Board[7, 5].Figure.Type != TypeFigur.none || position.Board[7, 6].Figure.Type != TypeFigur.none)
                        {
                            position.Board[7, 4].IsAttackWhite = false;
                            position.Board[7, 5].IsAttackWhite = false;
                            position.Board[7, 6].IsAttackWhite = false;
                            return(false);
                        }
                        else
                        {
                            position.Board[7, 4].IsAttackWhite = false;
                            position.Board[7, 5].IsAttackWhite = false;
                            position.Board[7, 6].IsAttackWhite = false;
                            mc.IsCastling = true;
                            isLegal       = true;
                        }
                    }
                    else
                    {
                        if (position.Board[mc.xEnd, 0].Figure.Type != TypeFigur.rock && position.Board[mc.xEnd, 0].Figure.Color != ColorFigur.black)
                        {
                            return(false);
                        }
                        if (position.IsLeftRockMovedBlack)
                        {
                            return(false);
                        }
                        AttackChecker.CheckAttack(ref position, 7, 4, ColorFigur.white);
                        AttackChecker.CheckAttack(ref position, 7, 3, ColorFigur.white);
                        AttackChecker.CheckAttack(ref position, 7, 2, ColorFigur.white);
                        if (position.Board[7, 4].IsAttackWhite || position.Board[7, 3].IsAttackWhite || position.Board[7, 2].IsAttackWhite ||
                            position.Board[7, 2].Figure.Type != TypeFigur.none || position.Board[7, 3].Figure.Type != TypeFigur.none)
                        {
                            position.Board[7, 4].IsAttackWhite = false;
                            position.Board[7, 3].IsAttackWhite = false;
                            position.Board[7, 2].IsAttackWhite = false;
                            return(false);
                        }
                        else
                        {
                            position.Board[7, 4].IsAttackWhite = false;
                            position.Board[7, 3].IsAttackWhite = false;
                            position.Board[7, 2].IsAttackWhite = false;
                            mc.IsCastling = true;
                            isLegal       = true;
                        }
                    }
                }
                else
                {
                    if (Math.Abs(mc.xEnd - mc.xStart) <= 1)
                    {
                        if (Math.Abs(mc.yEnd - mc.yStart) <= 1)
                        {
                            AttackChecker.CheckAttack(ref position, mc.xEnd, mc.yEnd, ColorFigur.white);
                            if (position.Board[mc.xEnd, mc.yEnd].IsAttackWhite)
                            {
                                position.Board[mc.xEnd, mc.yEnd].IsAttackWhite = false;
                                return(false);
                            }
                            else
                            {
                                isLegal = true;
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else
            {
                return(false);
            }
            if (isLegal)
            {
                //проверка на отсутствие шаха королю после хода
                position.MoveChess(mc);
                //черным
                isLegal = IsHaventCheck(position);
            }

            position.MoveBack(mc);
            return(isLegal);
        }
コード例 #15
0
ファイル: Peen.cs プロジェクト: yan1304/YanChess
        /// <summary>
        /// проверка возможности хода для фигуры
        /// </summary>
        public override bool CheckMove(Position position, MoveCoord mc)
        {
            if (position.IsWhiteMove && mc.xEnd <= mc.xStart)
            {
                return(false);
            }
            if (!position.IsWhiteMove && mc.xEnd >= mc.xStart)
            {
                return(false);
            }
            bool isLegal = false;

            if (position.Board[mc.xEnd, mc.yEnd].Figure.Color == position.Board[mc.xStart, mc.yStart].Figure.Color)
            {
                return(false);
            }
            if (position.Board[mc.xStart, mc.yStart].Figure.Color == ColorFigur.white && position.IsWhiteMove)
            {
                //ход пешкой вперед
                if (mc.yEnd == mc.yStart)
                {
                    if (mc.xStart == 1)
                    {
                        if (mc.xEnd == 3)
                        {
                            if (position.Board[mc.xEnd, mc.yEnd].Figure.Type == TypeFigur.none && position.Board[mc.xEnd - 1, mc.yEnd].Figure.Type == TypeFigur.none)
                            {
                                isLegal = true;
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        else if (mc.xEnd == 2)
                        {
                            if (position.Board[mc.xEnd, mc.yEnd].Figure.Type == TypeFigur.none)
                            {
                                isLegal = true;
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (Math.Abs(mc.xEnd - mc.xStart) == 1)
                        {
                            if (position.Board[mc.xEnd, mc.yEnd].Figure.Type == TypeFigur.none)
                            {
                                isLegal = true;
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    //Взятия
                    if (Math.Abs(mc.xEnd - mc.xStart) == 1)
                    {
                        if (Math.Abs(mc.yEnd - mc.yStart) == 1)
                        {
                            if (position.Board[mc.xEnd, mc.yEnd].Figure.Color == ColorFigur.black)
                            {
                                isLegal = true;
                            }
                            else if (mc.xEnd - 1 >= 0)
                            {
                                if (position.Board[mc.xEnd - 1, mc.yEnd].Figure.Color == ColorFigur.black && position.Board[mc.xEnd - 1, mc.yEnd].Figure.Type == TypeFigur.peen)
                                {
                                    if (((Peen)position.Board[mc.xEnd - 1, mc.yEnd].Figure).IsEnPassant)
                                    {
                                        mc.IsEnPassant = true;
                                        isLegal        = true;
                                    }
                                    else
                                    {
                                        return(false);
                                    }
                                }
                                else
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else if (position.Board[mc.xStart, mc.yStart].Figure.Color == ColorFigur.black && !(position.IsWhiteMove))
            {
                //ход пешкой вперед
                if (mc.yEnd == mc.yStart)
                {
                    if (mc.xStart == 6)
                    {
                        if (mc.xEnd == 4)
                        {
                            if (position.Board[mc.xEnd, mc.yEnd].Figure.Type == TypeFigur.none && position.Board[mc.xEnd + 1, mc.yEnd].Figure.Type == TypeFigur.none)
                            {
                                isLegal = true;
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        else if (mc.xEnd == 5)
                        {
                            if (position.Board[mc.xEnd, mc.yEnd].Figure.Type == TypeFigur.none)
                            {
                                isLegal = true;
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (Math.Abs(mc.xEnd - mc.xStart) == 1)
                        {
                            if (position.Board[mc.xEnd, mc.yEnd].Figure.Type == TypeFigur.none)
                            {
                                isLegal = true;
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    //Взятия
                    if (Math.Abs(mc.xEnd - mc.xStart) == 1)
                    {
                        if (Math.Abs(mc.yEnd - mc.yStart) == 1)
                        {
                            if (position.Board[mc.xEnd, mc.yEnd].Figure.Color == ColorFigur.white)
                            {
                                isLegal = true;
                            }
                            else if (mc.xEnd + 1 < 8)
                            {
                                if (position.Board[mc.xEnd + 1, mc.yEnd].Figure.Color == ColorFigur.white && position.Board[mc.xEnd + 1, mc.yEnd].Figure.Type == TypeFigur.peen)
                                {
                                    if (((Peen)position.Board[mc.xEnd + 1, mc.yEnd].Figure).IsEnPassant)
                                    {
                                        mc.IsEnPassant = true;
                                        isLegal        = true;
                                    }
                                    else
                                    {
                                        return(false);
                                    }
                                }
                                else
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else
            {
                return(false);
            }
            if (isLegal)
            {
                //проверка на отсутствие шаха королю после хода
                position.MoveChess(mc);
                //черным
                isLegal = IsHaventCheck(position);
            }

            position.MoveBack(mc);
            return(isLegal);
        }