private void CalcFiguresMovesMaps()
        {
            for (var i = 0; i < BitBoard.FieldSize; i++)
            {
                for (var j = 0; j < BitBoard.FieldSize; j++)
                {
                    var figure = ChessBoard[i, j];
                    if (figure.Equals(Figure.None))
                    {
                        continue;
                    }
                    if (figure.color == Color.White)
                    {
                        BitBoard moves = WMovesGen.Get(figure.piece, new Cell(i, j));
                        WCellToMovesMap.Add(new Cell(i, j), moves);
                    }
                    else
                    {
                        BitBoard moves = BMovesGen.Get(figure.piece, new Cell(i, j));
                        BCellToMovesMap.Add(new Cell(i, j), moves);
                    }
                }
            }

            foreach (var movesMap in WCellToMovesMap)
            {
                WMovesSum.Or(movesMap.Value);
            }

            foreach (var movesMap in BCellToMovesMap)
            {
                BMovesSum.Or(movesMap.Value);
            }
        }
        private BitBoard GetQueenMoves(int m, int n)
        {
            var moves = new BitBoard();

            moves.Or(GetBishopMoves(m, n));
            moves.Or(GetRookMoves(m, n));
            return(moves);
        }
        public SimpleAnalyser()
        {
            ChessBoard      = new Figure[BitBoard.FieldSize, BitBoard.FieldSize];
            BCellToMovesMap = new Dictionary <Cell, BitBoard>();
            WCellToMovesMap = new Dictionary <Cell, BitBoard>();
            BMovesSum       = new BitBoard();
            WMovesSum       = new BitBoard();
            BPositions      = new BitBoard();
            WPositions      = new BitBoard();

            BMovesGen = new MovesGenerator(ref BPositions, ref WPositions);
            WMovesGen = new MovesGenerator(ref WPositions, ref BPositions);
        }
        // Проверка возможности контратаки или защиты
        private bool FindNextMove()
        {
            //	Ищем пересечения каждой фигуры с общим полем BMovesSum + BPositions
            //	Пробуем переместить фигуру поочередно в каждую точку пересечения
            //	Вызываем функцию анализа для временного поля

            //	Исключение в логике, добавляем ходы отступления короля как есть
            var posibleCellToMovesMap = new Dictionary <Cell, BitBoard> {
                { KingPosition, WCellToMovesMap[KingPosition] }
            };

            WCellToMovesMap.Remove(KingPosition);

            var BPosAndMoves = new BitBoard();

            BPosAndMoves.Or(BPositions);
            BPosAndMoves.Or(BMovesSum);

            foreach (var cellMovesMap in WCellToMovesMap)
            {
                var moves = new BitBoard();
                moves.Or(BPosAndMoves);
                moves.And(cellMovesMap.Value);
                posibleCellToMovesMap.Add(cellMovesMap.Key, moves);
            }

            foreach (var cellMovesMap in posibleCellToMovesMap)
            {
                for (var i = 0; i < BitBoard.FieldSize; i++)
                {
                    for (var j = 0; j < BitBoard.FieldSize; j++)
                    {
                        if (cellMovesMap.Value.Get(i, j) == false)
                        {
                            continue;
                        }

                        var nextMove = new SimpleAnalyser(ChessBoard);
                        nextMove.MoveFigure(cellMovesMap.Key, new Cell(i, j));
                        nextMove.SimpleAnalyse();
                        if (!nextMove.isKingAttacked)
                        {
                            return(true);                                              // Этим ходом король защищен
                        }
                    }
                }
            }

            return(false);            // Никакой защиты не найдено
        }
        private BitBoard GetKingMoves(int m, int n)
        {
            var moves = new BitBoard();

            var list = new List <int> {
                1, 0, -1
            };
            var combinations = from i in list from j in list
                               where !(i == 0 && j == 0)
                               select new { i, j };

            foreach (var comb in combinations)
            {
                MoveInto(m + comb.i, n + comb.j, moves);
            }

            return(moves);
        }
        private BitBoard GetRookMoves(int m, int n)
        {
            var moves = new BitBoard();

            for (var i = m + 1; MoveInto(i, n, moves); i++)
            {
            }                                                  // east
            for (var i = m - 1; MoveInto(i, n, moves); i--)
            {
            }                                                  // west
            for (var i = n + 1; MoveInto(m, i, moves); i++)
            {
            }                                                  // south
            for (var i = n - 1; MoveInto(m, i, moves); i--)
            {
            }                                                  // north
            return(moves);
        }
        private BitBoard GetBishopMoves(int m, int n)
        {
            var moves = new BitBoard();

            for (int i = m + 1, j = n + 1; MoveInto(i, j, moves); i++, j++)
            {
            }                                                                  // south-east
            for (int i = m + 1, j = n - 1; MoveInto(i, j, moves); i++, j--)
            {
            }                                                                  // north-east
            for (int i = m - 1, j = n + 1; MoveInto(i, j, moves); i--, j++)
            {
            }                                                                  // south-west
            for (int i = m - 1, j = n - 1; MoveInto(i, j, moves); i--, j--)
            {
            }                                                                  // north-west
            return(moves);
        }
        private BitBoard GetKnightMoves(int m, int n)
        {
            var moves = new BitBoard();

            var list = new List <int> {
                1, -1, 2, -2
            };
            var combinations = from i in list from j in list
                               where Math.Abs(i) != Math.Abs(j)
                               select new { i, j };

            foreach (var comb in combinations)
            {
                MoveInto(m + comb.i, n + comb.j, moves);
            }

            return(moves);
        }
 private static char CellInfo(Cell x, ref BitBoard positions, ref BitBoard moves)
 {
     if (positions.Get(x.M, x.N) && moves.Get(x.M, x.N))
     {
         return('?');
     }
     else if (positions.Get(x.M, x.N))
     {
         return('O');
     }
     else if (moves.Get(x.M, x.N))
     {
         return('x');
     }
     else
     {
         return('.');
     }
 }
        private bool MoveInto(int m, int n, BitBoard output)
        // false - дальнейшее движение фигуры в данном направлении невозможно
        {
            if (BitMatrix.CheckBoundaries(m, n) == false)
            {
                return(false);
            }

            switch (ProbeCell(m, n))
            {
            case Obstacle.Friend:
                return(false);

            case Obstacle.Enemy:
                output.Set(m, n);
                return(false);

            case Obstacle.None:
                output.Set(m, n);
                return(true);

            default: throw new Exception("Unknown Obstacle entery");
            }
        }
 public MovesGenerator(ref BitBoard rFriends, ref BitBoard rEnemies)
 {
     friends = rFriends;
     enemies = rEnemies;
 }