async private Task <List <StateView> > MakeResult(CancellationToken cancelToken) { return(await Task.Run(() => { List <StateView> resultCollection = null; //Do not use this approach in enterprise!. StateFactory.SetNumberOfEach(NumberOfEach); var p = new Problem(StateFactory.Make(NumberOfEach, NumberOfEach, BoatState.Left, 0), StateFactory.Make(0, 0, BoatState.Right, 0), BoatSize); var result = Algorithm.UniformCostSearch(p, cancelToken); if (result != null) { Stack <State> stack = new Stack <State>(); while (result != null) { stack.Push(result); result = result.PrevState; } cancelToken.ThrowIfCancellationRequested(); resultCollection = new List <StateView>(stack.Count); int n = 0; foreach (var state in stack) { resultCollection.Add(new StateView(state, n++)); } return resultCollection; } return new List <StateView>(); }, cancelToken)); }
public IEnumerable <State> GetSuccessors(State state) { var result = new List <State>(); //loop through all possible combinations for (int nMiss = 0; nMiss <= _boatSize; nMiss++) { for (int nCan = 0; nCan <= _boatSize; nCan++) { if (nMiss == 0 && nCan == 0) { continue; } if (nMiss + nCan > _boatSize) { break; } var BoatDirection = state.BoatOnTheSide.AsMultiplier(); var s = StateFactory.Make( state.Missionaries + nMiss * BoatDirection, state.Cannibals + nCan * BoatDirection, state.BoatOnTheSide.Inverse(), state, state.StateLevel + 1); if (s.IsValidState()) { result.Add(s); } } } return(result); }