Пример #1
0
        private IEnumerable <string> GetPossibleMovesInner(Board board, int depth, int currentDepth, string currentString)
        {
            var moves = PossibleMovesService.GetAllPossibleMoves(board);

            foreach (var move in moves)
            {
                var moveString = currentString + (currentString.Length == 0 ? string.Empty : " ") + move.ToPositionString();
                if (currentDepth >= depth)
                {
                    yield return(moveString);
                }
                else
                {
                    var movedBoard = board.DoMove(move);
                    foreach (var otherBoards in GetPossibleMovesInner(movedBoard, depth, currentDepth + 1, moveString))
                    {
                        yield return(otherBoards);
                    }
                }
            }
        }
Пример #2
0
        public int GetPossibleMoveCountInner(Board board, int depth, int currentDepth)
        {
            var currentNum = 0;
            var moves      = PossibleMovesService.GetAllPossibleMoves(board);

            if (currentDepth >= depth)
            {
                currentNum = moves.Count;
            }
            else
            {
                if (currentDepth == 1)
                {
                    var sync = new object();
                    Parallel.ForEach(moves, m =>
                    {
                        var movedBoard             = board.DoMove(m);
                        var possibleMoveCountInner = GetPossibleMoveCountInner(movedBoard, depth, currentDepth + 1);
                        lock (sync)
                        {
                            currentNum += possibleMoveCountInner;
                        }
                    });
                }
                else
                {
                    foreach (var move in moves)
                    {
                        var movedBoard             = board.DoMove(move);
                        var possibleMoveCountInner = GetPossibleMoveCountInner(movedBoard, depth, currentDepth + 1);
                        currentNum += possibleMoveCountInner;
                    }
                }
            }
            return(currentNum);
        }
Пример #3
0
        public IList <MoveAndNodes> Divide(Board board, int depth)
        {
            var moves = PossibleMovesService.GetAllPossibleMoves(board);

            if (depth == 1)
            {
                return(moves.Select(x => new MoveAndNodes(x.ToPositionString(), 1)).OrderBy(x => x.Move).ToList());
            }
            var           results = new List <MoveAndNodes>();
            Action <Move> act     = m =>
            {
                var moved  = board.DoMove(m);
                var count  = GetPossibleMoveCountInner(moved, depth, 2);
                var posStr = m.ToPositionString();
                var man    = new MoveAndNodes(posStr, count, m);
                lock (results)
                {
                    results.Add(man);
                }
            };

            if (MultiThreaded)
            {
                Parallel.ForEach(moves, act);
            }
            else
            {
                foreach (var move in moves)
                {
                    act.Invoke(move);
                }
            }
            var ordered = results.OrderBy(x => x.Move).ToList();

            return(ordered);
        }