private SquareCollection FindNeighBourSquares(int row, int column) { SquareCollection neighBourSquares = new SquareCollection(); // Check neighbourhood try { if (IsSquareAvailable(this.innerBoard_[row, column - 1])) // left { neighBourSquares.Add(this.innerBoard_[row, column - 1]); } if (IsSquareAvailable(this.innerBoard_[row, column + 1])) // right { neighBourSquares.Add(this.innerBoard_[row, column + 1]); } if (IsSquareAvailable(this.innerBoard_[row - 1, column])) // up { neighBourSquares.Add(this.innerBoard_[row - 1, column]); } if (IsSquareAvailable(this.innerBoard_[row + 1, column])) // down { neighBourSquares.Add(this.innerBoard_[row + 1, column]); } } catch (Exception ex) { MessageBox.Show(ex.Message + row.ToString() + column.ToString()); } return(neighBourSquares); }
private void FindSolution(int row, int column) { SquareCollection neighBours = new SquareCollection(); SelectSquare(new Square(row, column)); // increment the cost for this path manager this.totalCost_++; // look for the neighbour squares neighBours = FindNeighBourSquares(row, column); // determine what to do about the neighbours if (!endManagePath_) { if (row == this.TargetSquare.X && column == this.TargetSquare.Y) { // first check if we have found the solution EndManagePath(new Square(row, column), true); } else if (neighBours.Count == 0) { // no more solution EndManagePath(new Square(row, column), true); } else if (neighBours.Count == 1) { // continue managing the path FindSolution(neighBours[0].X, neighBours[0].Y); } else if (neighBours.Count > 1) { // more than one neighbours found, so end managing process with the current square // then assing new pathmanagers for the rest of the neighbours EndManagePath(new Square(row, column), false); // assign new path managers for (int i = 0; i < neighBours.Count; i++) { // assign this object as parent path for the sub path manager and indicate that sub manager is not root PathManager subPathManager = new PathManager(this, i, new Square(neighBours[i].X, neighBours[i].Y), this.SourceSquare, this.TargetSquare, this.innerBoard_, false); PathManagerFetcher nFetcher = new PathManagerFetcher(this, subPathManager); Thread workerThread = new Thread(new ThreadStart(nFetcher.Fetch)); workerThread.Name = "PathFinder" + this.Name; workerThread.Start(); } } } else { // this situation is not normal, not expected to happen; even if it happens assume no more solution EndManagePath(new Square(row, column), true); } }
public PathManager(PathManager parentPathManager, int subIndex, Square startPoint, Square sourceSquare, Square targetSquare, SquareBoard board, bool isRoot) { // assign square board this.innerBoard_ = board; // which square this manager starts? this.startPointSquare_ = startPoint; // is this a root manager? this.isRoot_ = isRoot; // parent manager of this manager this.parentPathManager_ = parentPathManager; // total cost of this manager if we decide to take it this.totalCost_ = 0; this.innerThreadCount_ = 0; this.totalSolutions_ = 0; this.innerPathSquares_ = new SquareCollection(); // sub mangers collections this.subPathManagers_ = new PathManagerCollection(); this.innerSolutions_ = new PathManagerCollection(); // which path are we looking for a solution? this.sourceSquare_ = sourceSquare; this.targetSquare_ = targetSquare; this.endPointSquare_ = null; this.pathStatus_ = SquarePathStatus.PATH; // assign a unique name to the manager if (isRoot) { this.managerName_ = "0"; } else { this.managerName_ = parentPathManager.Name + subIndex.ToString(); } }