public async Task <IList <IPuzzle> > Build(PuzzleEvents events) { tree = new PuzzleTreeWithInfo(); buildTask = new Task <IList <IPuzzle> >(() => StartToBuildPuzzleTree(events)); buildTask.Start(); buildTask.Wait(); return(buildTask.Result); }
public async Task <IList <IPuzzle> > Build(PuzzleEvents events) { tree = new PuzzleTreeWithoutInfo <IPuzzle>(); puzzleRepeatControl = new Dictionary <string, IPuzzle>(); buildTask = new Task <IList <IPuzzle> >(() => StartToBuildPuzzleTree(events)); buildTask.Start(); buildTask.Wait(); return(buildTask.Result); }
private IList <IPuzzle> StartToBuildPuzzleTree(PuzzleEvents events) { var parent = tree.Insert(Puzzle); var puzzleNode = StartToBuildPuzzleTree(events, parent); if (puzzleNode == null) { throw new Exception("There isn't a solution for this puzzle"); } return(tree.GetNodePathToRoot(puzzleNode)); }
private PuzzleTreeNode <IPuzzle> StartToBuildPuzzleTree(PuzzleEvents events, PuzzleTreeNode <IPuzzle> parent) { var parentPuzzle = parent.Data; if (IsARepeatedPuzzle(parentPuzzle)) { return(null); } AddToPuzzleRepeatedListIfNeed(parentPuzzle); foreach (var allowedMovement in parentPuzzle.AllowedMovements()) { var puzzleChild = (Puzzle)parentPuzzle.Clone(); Console.WriteLine($"allowedMovement: {allowedMovement}"); puzzleChild.Move(allowedMovement); if (!IsARepeatedPuzzle(puzzleChild)) { var puzzleChildNode = tree.Insert(puzzleChild, parent); if (puzzleChild.IsDone()) { return(puzzleChildNode); } events.onStateChange.Invoke(puzzleChild); var childrenResolution = StartToBuildPuzzleTree(events, puzzleChildNode); if (childrenResolution != null) { return(childrenResolution); } } } return(null); }
private PuzzleTreeNode <IPuzzle> StartToBuildPuzzleTree(PuzzleEvents events, PuzzleTreeNode <IPuzzle> parent) { PuzzleTreeNode <IPuzzle> solution = null; var hasMoreItems = true; var foundSolution = false; var openedParents = new Dictionary <string, PuzzleTreeNode <IPuzzle> >(); var closedParents = new Dictionary <string, PuzzleTreeNode <IPuzzle> >(); var childRepeatControl = new Dictionary <string, PuzzleTreeNode <IPuzzle> >(); var parentPuzzle = parent.Data; var parentPuzzleString = parentPuzzle.ToString(); if (parentPuzzle.IsDone()) { solution = parent; return(solution); } openedParents[parentPuzzleString] = parent; while (hasMoreItems && !foundSolution) { foreach (var allowedMovement in parentPuzzle.AllowedMovements()) { var puzzleChild = (Puzzle)parentPuzzle.Clone(); puzzleChild.Move(allowedMovement); var puzzleChildString = puzzleChild.ToString(); var isARepeatedPuzzle = childRepeatControl.ContainsKey(puzzleChildString); var childWasAParent = closedParents.ContainsKey(puzzleChildString); var childWillBeAParent = openedParents.ContainsKey(puzzleChildString); if (!childWasAParent || !childWillBeAParent || !isARepeatedPuzzle) { var puzzleChildNode = tree.Insert(puzzleChild, parent); if (!childWasAParent) { openedParents[puzzleChildString] = puzzleChildNode; } if (!isARepeatedPuzzle) { if (puzzleChild.IsDone()) { solution = puzzleChildNode; foundSolution = true; return(puzzleChildNode); } events.onStateChange.Invoke(puzzleChild); childRepeatControl[puzzleChildString] = puzzleChildNode; } } } openedParents.Remove(parentPuzzleString); closedParents[parentPuzzleString] = parent; if (!openedParents.Any()) { hasMoreItems = false; break; } childRepeatControl[parentPuzzleString] = parent; parent = GetBestNodeByHeuristic(openedParents); parentPuzzle = parent.Data; parentPuzzleString = parentPuzzle.ToString(); } return(solution); }
private PuzzleTreeNode <IPuzzle> StartToBuildPuzzleTree(PuzzleEvents events, PuzzleTreeNode <IPuzzle> parent) { PuzzleTreeNode <IPuzzle> nodeSolution = null; var hasMoreItems = true; var foundSolution = false; var openedParents = new Dictionary <string, PuzzleTreeNode <IPuzzle> >(); var closedParents = new Dictionary <string, PuzzleTreeNode <IPuzzle> >(); var parentPuzzle = parent.Data; if (parentPuzzle.IsDone()) { foundSolution = true; return(parent); } openedParents.Add(parentPuzzle.ToString(), parent); while (hasMoreItems && !foundSolution) { foreach (var allowedMovement in parentPuzzle.AllowedMovements()) { var puzzleChild = (Puzzle)parentPuzzle.Clone(); puzzleChild.Move(allowedMovement); if (!IsARepeatedPuzzle(puzzleChild)) { var puzzleChildNode = tree.Insert(puzzleChild, parent); if (puzzleChild.IsDone()) { foundSolution = true; return(puzzleChildNode); } events.onStateChange.Invoke(puzzleChild); openedParents.Add(puzzleChild.ToString(), puzzleChildNode); AddToPuzzleRepeatedListIfNeed(puzzleChild); } } var parentPuzzleString = parentPuzzle.ToString(); openedParents.Remove(parentPuzzleString); if (!closedParents.ContainsKey(parentPuzzleString)) { closedParents.Add(parentPuzzleString, parent); } if (openedParents.Count == 0) { hasMoreItems = false; break; } AddToPuzzleRepeatedListIfNeed(parentPuzzle); parent = openedParents.First().Value; parentPuzzle = parent.Data; } return(nodeSolution); }