示例#1
0
        public PositionTree(Position position, CellState whoMoves, Dictionary <int, PositionTree> children)
        {
            Position = position;
            WhoMoves = whoMoves;
            Children = children;

            if (Children.Any())
            {
                if (whoMoves == CellState.Cross)
                {
                    Score = Children.Values.Max(c => c.Score);
                }
                else
                {
                    Score = Children.Values.Min(c => c.Score);
                }

                Depth = Children.Values.Max(c => c.Depth) + 1;
                if (whoMoves == CellState.Circle)
                {
                    // this position resulted from the cross's move;
                    // recommended move for circles is the one with the minimum score
                    RecommendedMove = Children.Aggregate((curMin, x) => IsLess(x.Value, curMin.Value) ? x : curMin).Key;
                }
            }
            else
            {
                Score = PositionScore.Of(position);
                Depth = 1;
            }
        }
示例#2
0
        private PositionTree BuildTreeNoCache(Position position, CellState whoMoves)
        {
            if (!position.HasEmptyCells() || PositionScore.Of(position) != 0)
            {
                // final position, no children possible
                return(new PositionTree(position, whoMoves, new Dictionary <int, PositionTree>()));
            }

            var otherSide = whoMoves == CellState.Cross ? CellState.Circle : CellState.Cross;
            var children  = new Dictionary <int, PositionTree>();

            for (int cell = 0; cell < Position.TotalCells; ++cell)
            {
                if (position[cell] != CellState.Empty)
                {
                    continue;
                }
                children[cell] = BuildTree(position.Move(cell, whoMoves), otherSide); // recursive call
            }

            return(new PositionTree(position, whoMoves, children));
        }