A* Algorithm 진행중

Algorithm/풀고있는 문제 2019. 5. 17. 20:23

A* Algorithm

하나의 노드로부터 타겟 노드까지의 최단 경로를 찾는 알고리즘

f(n) = g(n) + h(n) 을 통해 최단 경로를 찾는다.

풀이는 집가서 써야지




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test0517 : MonoBehaviour
{
    public int row, col;
    public int startX, startY;
    public int targetX, targetY;
 
    private GameObject[] tilePrefabs;
    private GameObject[,] tiles;
    private GameObject motherNode;
    private GameObject startNode;
    private GameObject targetNode;
    private List<GameObject> listTile;
    private List<GameObject> openList;
    private List<GameObject> closeList;
    private List<GameObject> motherNodes;
 
    private Vector2[,] coordinate;
    
    void Start()
    {
        this.tilePrefabs = Resources.LoadAll<GameObject>("Tiles");
        this.coordinate = new Vector2[this.row, this.col];
        this.tiles = new GameObject[this.row, this.col];
        this.listTile = new List<GameObject>();
 
        this.SetCorrdinate();
        this.CreateTiles();
        
 
        this.SetMotherNode(this.tiles[startX, startY]);
        this.FindPath(this.tiles[25]);
 
    }
 
    private void FindPath(GameObject target)
    {
        this.FindNodes();
    }
 
    private void FindNodes()
    {
        var mother = this.motherNode.GetComponent<TestTile>();
        var motherX = this.motherNode.GetComponent<TestTile>().x - 1;
        var motherY = this.motherNode.GetComponent<TestTile>().y - 1;
        var maxX = this.motherNode.GetComponent<TestTile>().x + 1;
        var maxY = this.motherNode.GetComponent<TestTile>().y + 1;
        if (motherX < 0)
        {
            motherX = 0;
        }
        if(motherY < 0)
        {
            motherY = 0;
        }
 
        for (int i = motherX; i <= maxX; i++)
        {
            for (int j = motherY; j <= maxY; j++)
            {
                if (this.tiles[i, j].GetComponent<TestTile>().tileType == 0)
                {
                    this.listTile.Add(this.tiles[i, j]);
                    this.SetG(this.tiles[i, j]);
                    this.SetH(this.tiles[i, j]);
                    this.SetF(this.tiles[i, j]);
                    this.TurnArrow(this.tiles[i,j]);
                    var sprite = this.tiles[i, j].GetComponentInChildren<SpriteRenderer>();
                    sprite.color = Color.cyan;
                }
            }
        }
    }
 
    private void TurnArrow(GameObject tile)
    {
        var script = tile.GetComponent<TestTile>();
        var xPos = this.startNode.transform.position.x - script.Arrow.transform.position.x;
        var yPos = this.startNode.transform.position.y - script.Arrow.transform.position.y;
        var rad = Mathf.Atan2(yPos, xPos) * Mathf.Rad2Deg;
        script.Arrow.transform.rotation = Quaternion.Euler(new Vector3(00, rad+270));
 
        script.Arrow.SetActive(true);
    }
 
    private void SetMotherNode(GameObject mother)
    {
        this.motherNode = mother;
        this.motherNode.GetComponent<TestTile>().tileType = 2;
    }
 
    private void SetCorrdinate()
    {
        for (int i = 0; i < this.row; i++)
        {
            for (int j = 0; j < this.col; j++)
            {
                this.coordinate[i, j] = new Vector2(i, j);
            }
        }
    }
 
    private void SetF(GameObject tile)
    {
        var tileScript = tile.GetComponent<TestTile>();
        tileScript.f = tileScript.g + tileScript.h;
        tileScript.textF.text = tileScript.f.ToString();
    }
    private void SetG(GameObject tile)
    {
        var tileScript = tile.GetComponent<TestTile>();
        var targetScript = this.targetNode.GetComponent<TestTile>();
        var dis = (int)(Vector2.Distance(tile.transform.position, this.targetNode.transform.position)) * 10;
        Debug.Log(dis);
        tileScript.textH.text = dis.ToString();
        tileScript.h = dis;
    }
    private void SetH(GameObject tile)
    {
        var tileScript = tile.GetComponent<TestTile>();
        var targetScript = this.targetNode.GetComponent<TestTile>();
        var dis = (int)(Vector2.Distance(tile.transform.position, this.startNode.transform.position)) * 10;
        Debug.Log(dis);
        tileScript.textG.text = dis.ToString();
        tileScript.g = dis;
    }
 
 
    private void CreateTiles()
    {
 
        for (int i = 0; i < this.row; i++)
        {
            for (int j = 0; j < this.col; j++)
            {
                var screenPos = Camera.main.ScreenToWorldPoint(new Vector2((j * 100+ 400, (i * -100+ 550));
                screenPos.z = 0;
                if (i == this.startX && j == this.startY)
                {
                    var tile = GameObject.Instantiate(this.tilePrefabs[1]);
                    this.tiles[i, j] = tile;
                    this.startNode = tile;
                    var script = tile.GetComponent<TestTile>();
                    script.x = i;
                    script.y = j;
                    script.textCoord.text = script.x.ToString() + ", " + script.y.ToString();
                    tile.transform.position = screenPos;
                }
                else if (i == this.targetX && j == this.targetY)
                {
                    var tile = GameObject.Instantiate(this.tilePrefabs[2]);
                    this.tiles[i, j] = tile;
                    this.targetNode = tile;
                    var script = tile.GetComponent<TestTile>();
                    script.x = i;
                    script.y = j;
                    script.textCoord.text = script.x.ToString() + ", " + script.y.ToString();
                    tile.transform.position = screenPos;
                }
                else if ((i >= 1 && i < 4&& j == 3)
                {
                    var tile = GameObject.Instantiate(this.tilePrefabs[0]);
                    this.tiles[i, j] = tile;
                    var script = tile.GetComponent<TestTile>();
                    script.tileType = 1;
                    script.x = i;
                    script.y = j;
                    script.textCoord.text = script.x.ToString() + ", " + script.y.ToString();
                    tile.transform.position = screenPos;
                }
                else
                {
                    var tile = GameObject.Instantiate(this.tilePrefabs[3]);
                    this.tiles[i, j] = tile;
                    var script = tile.GetComponent<TestTile>();
                    script.x = i;
                    script.y = j;
                    script.textCoord.text = script.x.ToString() + ", " + script.y.ToString();
                    tile.transform.position = screenPos;
                }
            }
        }
    }
}
 
cs


'Algorithm > 풀고있는 문제' 카테고리의 다른 글

A* Algorithm 좋은 영상  (0) 2019.05.23
A* Algorithm Simulation  (0) 2019.05.22
A* Algorithm 구현  (0) 2019.05.21
A* Algorithm 4방향/8방향 탐색  (0) 2019.05.20
: