Exemplo n.º 1
0
 private bool AddToHashSet(HashSet <int> hashSet, int x, int y, ICell centerCell, out ICell cell)
 {
     cell = GetCell(x, y);
     if (cell.Equals(centerCell))
     {
         return(false);
     }
     return(hashSet.Add(IndexFor(cell)));
 }
Exemplo n.º 2
0
        public Tuple <int, int> GetClueTo(int i, int j)
        {
            if (!this.IsSolved())
            {
                this.SolveMaze(defaultSolve);
            }
            if (i % 2 != 0 || j % 2 != 0)
            {
                ICell first, sec;
                if (j % 2 != 0)
                {
                    first = this.theGraph.GetTheGraph()[i / 2, (j + 1) / 2];
                    sec   = this.theGraph.GetTheGraph()[i / 2, (j - 1) / 2];
                }
                else
                {
                    first = this.theGraph.GetTheGraph()[(i + 1) / 2, j / 2];
                    sec   = this.theGraph.GetTheGraph()[(i - 1) / 2, j / 2];
                }
                Graphs tempGrap = new Graphs(this.theGraph);
                tempGrap.ChangeToBeginSituation(first);
                List <ICell> firstSol = new FactorySolvable().SolveTheMaze(tempGrap, defaultSolve);
                tempGrap.ChangeToBeginSituation(sec);
                List <ICell> secSol = new FactorySolvable().SolveTheMaze(tempGrap, defaultSolve);
                if (firstSol.Count < secSol.Count)
                {
                    return(this.GetPlaceCellInMatrix(first));
                }
                else
                {
                    return(this.GetPlaceCellInMatrix(sec));
                }
            }
            List <ICell> miniSol     = null;
            ICell        next        = null;
            ICell        currentCell = this.theGraph.GetTheGraph()[i / 2, j / 2];

            if (currentCell.Equals(this.theGraph.End))
            {
                return(null);
            }
            List <ICell> ne = this.theGraph.GetNeighbors(currentCell);

            foreach (ICell item in ne)
            {
                Graphs tempGrap = new Graphs(this.theGraph);
                tempGrap.ChangeToBeginSituation(item);
                List <ICell> sol = new FactorySolvable().SolveTheMaze(tempGrap, defaultSolve);
                if (miniSol == null || sol.Count < miniSol.Count)
                {
                    miniSol = sol;
                    next    = item;
                }
            }
            return(this.GetPlaceCellInMatrix(next));
        }
Exemplo n.º 3
0
        private void SetNextArea(ICell cell)
        {
            var smallArea = BigArea.Cells.Single(x => cell.Equals(x)) as SmallArea;

            BigArea.CurrentCell = smallArea;
            if (!smallArea.Cells.Any(x => x.State == States.Empty))
            {
                BigArea.CurrentCell = null;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// The function handle if in the creation we reached to the given cell.
        /// In the function we are making this cell as unreachable.
        /// </summary>
        /// <param name="src"></param> the givem cell.
        public void ChangeCellState(ICell src)
        {
            if (!myNodes.Contains(src))
            {
                return;
            }
            src.SetReached();
            List <ICell> canReach = null;

            foreach (ICell item in reachable.Keys)
            {
                if (src.Equals(item))
                {
                    canReach = reachable[item]; break;
                }
            }

            foreach (List <ICell> item in reachable.Values)
            {
                item.Remove(src);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Returns an List of Cells representing a shortest path from the specified source to the specified destination
        /// </summary>
        /// <param name="source">The source Cell to find a shortest path from</param>
        /// <param name="destination">The destination Cell to find a shortest path to</param>
        /// <param name="map">The Map on which to find the shortest path between Cells</param>
        /// <returns>List of Cells representing a shortest path from the specified source to the specified destination</returns>
        public List <TCell> FindPath(TCell source, TCell destination, IMap <TCell> map)
        {
            // OPEN = the set of nodes to be evaluated
            IndexMinPriorityQueue <PathNode> openNodes = new IndexMinPriorityQueue <PathNode>(map.Height * map.Width);

            // CLOSED = the set of nodes already evaluated
            bool[] isNodeClosed = new bool[map.Height * map.Width];

            // add the start node to OPEN
            openNodes.Insert(map.IndexFor(source), new PathNode
            {
                DistanceFromStart        = 0,
                HeuristicDistanceFromEnd = CalculateDistance(source, destination, _diagonalCost),
                X      = source.X,
                Y      = source.Y,
                Parent = null
            });

            PathNode currentNode;

            // loop
            while (true)
            {
                // current = node in OPEN with the lowest f_cost
                if (openNodes.Size < 1)
                {
                    return(null);
                }
                currentNode = openNodes.MinKey();
                // remove current from OPEN
                int currentIndex = openNodes.DeleteMin();
                // add current to CLOSED
                isNodeClosed[currentIndex] = true;

                ICell currentCell = map.CellFor(currentIndex);
                // if current is the target node the path has been found
                if (currentCell.Equals(destination))
                {
                    break;
                }

                // foreach neighbor of the current node
                bool includeDiagonals = _diagonalCost.HasValue;
                foreach (TCell neighbor in map.GetAdjacentCells(currentCell.X, currentCell.Y, includeDiagonals))
                {
                    int neighborIndex = map.IndexFor(neighbor);
                    // if neighbor is not walkable or neighbor is in CLOSED
                    if (neighbor.IsWalkable == false || isNodeClosed[neighborIndex])
                    {
                        // skip to the next neighbor
                        continue;
                    }

                    bool isNeighborInOpen = openNodes.Contains(neighborIndex);

                    // if neighbor is in OPEN
                    if (isNeighborInOpen)
                    {
                        // if new path to neighbor is shorter
                        PathNode neighborNode = openNodes.KeyAt(neighborIndex);
                        double   newDistance  = currentNode.DistanceFromStart + 1;
                        if (newDistance < neighborNode.DistanceFromStart)
                        {
                            // update neighbor distance
                            neighborNode.DistanceFromStart = newDistance;
                            // set parent of neighbor to current
                            neighborNode.Parent = currentNode;
                        }
                    }
                    else // if neighbor is not in OPEN
                    {
                        // set f_cost of neighbor
                        // set parent of neighbor to current
                        PathNode neighborNode = new PathNode
                        {
                            DistanceFromStart        = currentNode.DistanceFromStart + 1,
                            HeuristicDistanceFromEnd = CalculateDistance(source, destination, _diagonalCost),
                            X      = neighbor.X,
                            Y      = neighbor.Y,
                            Parent = currentNode
                        };
                        // add neighbor to OPEN
                        openNodes.Insert(neighborIndex, neighborNode);
                    }
                }
            }

            List <TCell> path = new List <TCell>();

            path.Add(map.GetCell(currentNode.X, currentNode.Y));
            while (currentNode.Parent != null)
            {
                currentNode = currentNode.Parent;
                path.Add(map.GetCell(currentNode.X, currentNode.Y));
            }

            path.Reverse();
            return(path);
        }