Esempio n. 1
0
        public Cell AStarMove2Goal(Cell goal)
        {
            List <Point> pathGoal = null;
            List <Point> pathTail = null;

            pathGoal = GetPath2Goal(goal);
            if (pathGoal.Count > 0)
            {
                Serpent vsp = new Serpent(this);
                vsp.VirtualMoveByPath(pathGoal);
                Cell vtail = vsp.GetTail();
                pathTail = vsp.GetPath2Goal(vtail);
                if (pathTail.Count == 0)
                {
                    return(AStarMove2Tail(goal));
                }
                else
                {
                    return(Point2MapCell(pathGoal[0]));
                }
            }
            else
            {
                return(AStarMove2Tail(goal));
            }
        }
Esempio n. 2
0
        public Cell AStarMove2Tail(Cell goal)
        {
            Cell head = GetHead();

            // Get the list of movable cells
            List <Cell> nextCells = GetMovableCells(head.Row, head.Col);

            if (nextCells.Count == 0)
            {
                return(null);
            }

            List <Cell> tails = new List <Cell>();

            // If moved cell can find path to tail, add to the tail list
            foreach (Cell cell in nextCells)
            {
                Serpent vsp = new Serpent(this);
                vsp.VirtualMoveCell(cell);
                Cell tail = vsp.GetTail();
                if (vsp.GetPath2Goal(tail).Count > 0)
                {
                    tails.Add(cell);
                }
            }

            // If not cell can find tail then check the nextcells
            if (tails.Count == 0)
            {
                tails.AddRange(nextCells);
            }

            // Get farthest cell to goal
            int  dis0    = GetCellsDistance(tails[0], goal);
            Cell cellGot = tails[0];

            for (int i = 1; i < tails.Count; i++)
            {
                int disloop = GetCellsDistance(tails[i], goal);
                if (disloop > dis0)
                {
                    dis0    = disloop;
                    cellGot = tails[i];
                }
            }

            return(cellGot);
        }