Example #1
0
 private bool RecursiveCalculateSolution(LevelSimulationSnapshot state)
 {
     _numCalculationSteps++;
     _oldStates[state.Hash] = state;
     foreach (var move in state.GetMoves().ToArray().Shuffled())
     {
         var newState = state.MakeMove(move);
         if (_oldStates.ContainsKey(newState.Hash))
         {
             continue;                                        // Already examined this tree
         }
         if (newState.IsGameOver())
         {
             if (newState.HasWon())
             {
                 _movesToWin.Add(move);
                 return(true);
             }
         }
         else if (RecursiveCalculateSolution(newState))
         {
             _movesToWin.Add(move);
             return(true);
         }
     }
     return(false);
 }
Example #2
0
 private PossibleOutcomes RecursiveCalculateMoveTree(LevelSimulationSnapshot state, string stateHash)
 {
     _oldStates[stateHash]        = state;
     _possibleOutcomes[stateHash] = PossibleOutcomes.Uncalculated;
     foreach (var move in state.GetMoves().ToArray().Shuffled())
     {
         var newState     = state.MakeMove(move);
         var newStateHash = newState.Hash;
         if (_oldStates.ContainsKey(newStateHash))
         {
             _possibleOutcomes[stateHash] = _possibleOutcomes[stateHash] | _possibleOutcomes[newStateHash];
         }
         else if (newState.IsGameOver())
         {
             _numberOfWinningBranches++;
             var stars = newState.GetStars();
             if (stars == 3)
             {
                 _possibleOutcomes[stateHash] = _possibleOutcomes[stateHash] | PossibleOutcomes.ThreeStar;
             }
             else if (stars == 2)
             {
                 _possibleOutcomes[stateHash] = _possibleOutcomes[stateHash] | PossibleOutcomes.TwoStar;
             }
             else
             {
                 _possibleOutcomes[stateHash] = _possibleOutcomes[stateHash] | PossibleOutcomes.OneStar;
             }
         }
         else
         {
             _possibleOutcomes[stateHash] = _possibleOutcomes[stateHash] | RecursiveCalculateMoveTree(newState, newStateHash);
         }
     }
     if ((int)_possibleOutcomes[stateHash] == 0)
     {
         _numberOfDeadBranches++;
     }
     if ((int)_possibleOutcomes[stateHash] <= 1)
     {
         _possibleOutcomes[stateHash] = _possibleOutcomes[stateHash] | PossibleOutcomes.DeadEnd;
     }
     return(_possibleOutcomes[stateHash]);
 }