コード例 #1
0
    void FindPathRecursive(Cell cell)
    {
        //인접 셀 중 지나갈 수 있는 셀을 open list에 추가
        foreach (var around in cell.AroundDic)
        {
            Cell aroundCell = CellCreator.GetCell(around.Key);
            if (!aroundCell.IsObstacle && !closeList.Contains(aroundCell))
            {
                if (openList.Contains(aroundCell))
                {
                    if (aroundCell.G > cell.G + cell[around.Key])
                    {
                        aroundCell.Parent = cell;
                    }
                }
                else
                {
                    aroundCell.Parent = cell;
                    openList.Add(aroundCell);

                    //만약 목적지가 열린목록에 추가된다면, 길 찾기를 완료한것으로 판단
                    if (aroundCell == destinationCell)
                    {
                        SuccessFindPath();
                        return;
                    }
                }
            }
        }

        openList.Remove(cell);
        closeList.Add(cell);

        Tuple <int, Cell> minF = new Tuple <int, Cell>(int.MaxValue, null);

        foreach (var checkCell in openList)
        {
            Cell parent = checkCell.Parent;

            //나의 G 비용은 부모의 G + 부모로부터 나한테 오는데 드는 비용 (대각선이면 14, 직각이면 10)
            int G = parent.G + parent[checkCell.Idx];

            checkCell.G = G;

            //H 비용
            int H = GetH(destinationCell, checkCell);

            int F = G + H;

            if (minF.Item1 > F)
            {
                minF = new Tuple <int, Cell>(F, checkCell);
            }
        }

        FindPathRecursive(minF.Item2);
    }