Map.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. namespace PathfindingPCL
  5. {
  6. public class Map : Graph
  7. {
  8. Tile[,] tiles;
  9. int nbRows;
  10. int nbCols;
  11. Tile beginNode;
  12. Tile exitNode;
  13. List<Node> nodesList = null;
  14. List<Arc> arcsList = null;
  15. public Map(String _map, int _beginRow, int _beginColumn, int _exitRow, int _exitColumn)
  16. {
  17. // Création du tableau Tiles
  18. String[] mapRows = _map.Split(new char[] { '\n' });
  19. nbRows = mapRows.Length;
  20. nbCols = mapRows[0].Length;
  21. tiles = new Tile[nbRows,nbCols];
  22. // Remplissage
  23. for(int row = 0; row < nbRows; row++)
  24. {
  25. for (int col = 0; col < nbCols; col++)
  26. {
  27. tiles[row, col] = new Tile(TileTypeConverter.TypeFromChar(mapRows[row][col]), row, col);
  28. }
  29. }
  30. // Entrée et sortie
  31. beginNode = tiles[_beginRow, _beginColumn];
  32. beginNode.DistanceFromBegin = beginNode.Cost();
  33. exitNode = tiles[_exitRow, _exitColumn];
  34. // Liste des noeuds et des arcs
  35. NodesList();
  36. ArcsList();
  37. }
  38. public Node BeginningNode()
  39. {
  40. return beginNode;
  41. }
  42. public Node ExitNode()
  43. {
  44. return exitNode;
  45. }
  46. public List<Node> NodesList()
  47. {
  48. if (nodesList == null)
  49. {
  50. nodesList = new List<Node>();
  51. foreach (Node node in tiles)
  52. {
  53. nodesList.Add(node);
  54. }
  55. }
  56. return nodesList;
  57. }
  58. public List<Node> NodesList(Node _currentNode)
  59. {
  60. List<Node> nodesList = new List<Node>();
  61. int currentRow = ((Tile)_currentNode).Row;
  62. int currentCol = ((Tile)_currentNode).Col;
  63. // Renvoyer les voisins s'ils existent et sont accessibles
  64. if (currentRow - 1 >= 0 && tiles[currentRow - 1, currentCol].IsValidPath())
  65. {
  66. nodesList.Add(tiles[currentRow - 1, currentCol]);
  67. }
  68. if (currentCol - 1 >= 0 && tiles[currentRow, currentCol - 1].IsValidPath())
  69. {
  70. nodesList.Add(tiles[currentRow, currentCol - 1]);
  71. }
  72. if (currentRow + 1 < nbRows && tiles[currentRow + 1, currentCol].IsValidPath())
  73. {
  74. nodesList.Add(tiles[currentRow + 1, currentCol]);
  75. }
  76. if (currentCol + 1 < nbCols && tiles[currentRow, currentCol + 1].IsValidPath())
  77. {
  78. nodesList.Add(tiles[currentRow, currentCol + 1]);
  79. }
  80. return nodesList;
  81. }
  82. public int NodesCount()
  83. {
  84. return nbRows * nbCols;
  85. }
  86. public List<Arc> ArcsList()
  87. {
  88. if (arcsList == null)
  89. {
  90. arcsList = new List<Arc>();
  91. for (int row = 0; row < nbRows; row++)
  92. {
  93. for (int col = 0; col < nbCols; col++)
  94. {
  95. if (tiles[row, col].IsValidPath())
  96. {
  97. // Haut
  98. if (row - 1 >= 0 && tiles[row - 1, col].IsValidPath())
  99. {
  100. arcsList.Add(new Arc(tiles[row, col], tiles[row - 1, col], tiles[row - 1, col].Cost()));
  101. }
  102. // Gauche
  103. if (col - 1 >= 0 && tiles[row, col - 1].IsValidPath())
  104. {
  105. arcsList.Add(new Arc(tiles[row, col], tiles[row, col - 1], tiles[row, col - 1].Cost()));
  106. }
  107. // Bas
  108. if (row + 1 < nbRows && tiles[row + 1, col].IsValidPath())
  109. {
  110. arcsList.Add(new Arc(tiles[row, col], tiles[row + 1, col], tiles[row + 1, col].Cost()));
  111. }
  112. // Droite
  113. if (col + 1 < nbCols && tiles[row, col + 1].IsValidPath())
  114. {
  115. arcsList.Add(new Arc(tiles[row, col], tiles[row, col + 1], tiles[row, col + 1].Cost()));
  116. }
  117. }
  118. }
  119. }
  120. }
  121. return arcsList;
  122. }
  123. public List<Arc> ArcsList(Node _currentNode)
  124. {
  125. List<Arc> list = new List<Arc>();
  126. int currentRow = ((Tile)_currentNode).Row;
  127. int currentCol = ((Tile)_currentNode).Col;
  128. // Renvoyer les voisins
  129. if (currentRow - 1 >= 0 && tiles[currentRow - 1, currentCol].IsValidPath())
  130. {
  131. list.Add(new Arc(_currentNode, tiles[currentRow - 1, currentCol], tiles[currentRow - 1, currentCol].Cost()));
  132. }
  133. if (currentCol - 1 >= 0 && tiles[currentRow, currentCol - 1].IsValidPath())
  134. {
  135. list.Add(new Arc(_currentNode, tiles[currentRow, currentCol - 1], tiles[currentRow, currentCol - 1].Cost()));
  136. }
  137. if (currentRow + 1 < nbRows && tiles[currentRow + 1, currentCol].IsValidPath())
  138. {
  139. list.Add(new Arc(_currentNode, tiles[currentRow + 1, currentCol], tiles[currentRow + 1, currentCol].Cost()));
  140. }
  141. if (currentCol + 1 < nbCols && tiles[currentRow, currentCol + 1].IsValidPath())
  142. {
  143. list.Add(new Arc(_currentNode, tiles[currentRow, currentCol + 1], tiles[currentRow, currentCol + 1].Cost()));
  144. }
  145. return list;
  146. }
  147. public double CostBetweenNodes(Node _fromNode, Node _toNode)
  148. {
  149. return ((Tile)_toNode).Cost();
  150. }
  151. public String ReconstructPath()
  152. {
  153. String resPath = "";
  154. Tile currentNode = exitNode;
  155. Tile prevNode = (Tile) exitNode.Precursor;
  156. while (prevNode != null)
  157. {
  158. resPath = "-" + currentNode.ToString() + resPath;
  159. currentNode = prevNode;
  160. prevNode = (Tile) prevNode.Precursor;
  161. }
  162. resPath = currentNode.ToString() + resPath;
  163. return resPath;
  164. }
  165. public void ComputeEstimatedDistanceToExit()
  166. {
  167. foreach (Tile tile in tiles)
  168. {
  169. tile.EstimatedDistance = Math.Abs(exitNode.Row - tile.Row) + Math.Abs(exitNode.Col - tile.Col);
  170. }
  171. }
  172. public void Clear()
  173. {
  174. nodesList = null;
  175. arcsList = null;
  176. for (int row = 0; row < nbRows; row++)
  177. {
  178. for (int col = 0; col < nbCols; col++)
  179. {
  180. tiles[row, col].DistanceFromBegin = double.PositiveInfinity;
  181. tiles[row, col].Precursor = null;
  182. }
  183. }
  184. beginNode.DistanceFromBegin = beginNode.Cost();
  185. }
  186. }
  187. }