Beispiel #1
0
 private void ResolveWithMemoization(PegNode pegNode)
 {
     foreach (var memoization in pegNode.GetMemoization().ToList())
     {
         _probabilityByColumn[memoization.Column] += UpdateMemoizedProbability(pegNode.Peg, memoization);
     }
     _pegNodeStack.Clear();
 }
Beispiel #2
0
            public PegNode GetByPeg(Peg peg)
            {
                if (!_pegNodesCreated.ContainsKey(peg))
                {
                    _pegNodesCreated[peg] = new PegNode(peg);
                }

                return(_pegNodesCreated[peg]);
            }
Beispiel #3
0
        private void SetupMemoization(PegNode peg)
        {
            var memoizationProbability = _pegNodeStack.PopPeg().Probability;

            while (_pegNodeStack.Count > 0)
            {
                var previousNode = _pegNodeStack.Pop();
                if (previousNode.Peg.Row == _pegBoard.Rows)
                {
                    continue;
                }

                var previousProbability = memoizationProbability * previousNode.Peg.Probability;
                memoizationProbability = previousProbability;
                previousNode.SetMemoization(peg.Peg.Column, previousProbability);
            }
        }
Beispiel #4
0
        private void TraversePegTree(PegNode pegNode, double cumulativeProbability)
        {
            _pegNodeStack.Push(pegNode);
            if (pegNode.HasMemoization())
            {
                ResolveWithMemoization(pegNode);
                return;
            }

            var pegNeighbours = _pegBoard.GetPegUpNeighboursFrom(pegNode.Peg);

            if (!pegNeighbours.Any() || pegNode.Peg.IsInitialPeg)
            {
                _probabilityByColumn[pegNode.Peg.Column] += cumulativeProbability;
                SetupMemoization(pegNode);
                return;
            }

            foreach (var neighbour in pegNeighbours)
            {
                var linkedNode = pegNode.AddLink(_pegNodesFactory.GetByPeg(neighbour));
                TraversePegTree(linkedNode, cumulativeProbability * linkedNode.Peg.Probability);
            }
        }
Beispiel #5
0
 public PegNode AddLink(PegNode peg)
 {
     _linkedPegs.Add(peg);
     return(peg);
 }