| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace PathfindingPCL
- {
- public class Map : Graph
- {
- Tile[,] tiles;
- int nbRows;
- int nbCols;
- Tile beginNode;
- Tile exitNode;
- List<Node> nodesList = null;
- List<Arc> arcsList = null;
- public Map(String _map, int _beginRow, int _beginColumn, int _exitRow, int _exitColumn)
- {
- // Création du tableau Tiles
- String[] mapRows = _map.Split(new char[] { '\n' });
- nbRows = mapRows.Length;
- nbCols = mapRows[0].Length;
- tiles = new Tile[nbRows,nbCols];
- // Remplissage
- for(int row = 0; row < nbRows; row++)
- {
- for (int col = 0; col < nbCols; col++)
- {
- tiles[row, col] = new Tile(TileTypeConverter.TypeFromChar(mapRows[row][col]), row, col);
- }
- }
- // Entrée et sortie
- beginNode = tiles[_beginRow, _beginColumn];
- beginNode.DistanceFromBegin = beginNode.Cost();
- exitNode = tiles[_exitRow, _exitColumn];
- // Liste des noeuds et des arcs
- NodesList();
- ArcsList();
- }
- public Node BeginningNode()
- {
- return beginNode;
- }
- public Node ExitNode()
- {
- return exitNode;
- }
- public List<Node> NodesList()
- {
- if (nodesList == null)
- {
- nodesList = new List<Node>();
- foreach (Node node in tiles)
- {
- nodesList.Add(node);
- }
- }
- return nodesList;
- }
- public List<Node> NodesList(Node _currentNode)
- {
- List<Node> nodesList = new List<Node>();
- int currentRow = ((Tile)_currentNode).Row;
- int currentCol = ((Tile)_currentNode).Col;
- // Renvoyer les voisins s'ils existent et sont accessibles
- if (currentRow - 1 >= 0 && tiles[currentRow - 1, currentCol].IsValidPath())
- {
- nodesList.Add(tiles[currentRow - 1, currentCol]);
- }
- if (currentCol - 1 >= 0 && tiles[currentRow, currentCol - 1].IsValidPath())
- {
- nodesList.Add(tiles[currentRow, currentCol - 1]);
- }
- if (currentRow + 1 < nbRows && tiles[currentRow + 1, currentCol].IsValidPath())
- {
- nodesList.Add(tiles[currentRow + 1, currentCol]);
- }
- if (currentCol + 1 < nbCols && tiles[currentRow, currentCol + 1].IsValidPath())
- {
- nodesList.Add(tiles[currentRow, currentCol + 1]);
- }
- return nodesList;
- }
- public int NodesCount()
- {
- return nbRows * nbCols;
- }
- public List<Arc> ArcsList()
- {
- if (arcsList == null)
- {
- arcsList = new List<Arc>();
- for (int row = 0; row < nbRows; row++)
- {
- for (int col = 0; col < nbCols; col++)
- {
- if (tiles[row, col].IsValidPath())
- {
- // Haut
- if (row - 1 >= 0 && tiles[row - 1, col].IsValidPath())
- {
- arcsList.Add(new Arc(tiles[row, col], tiles[row - 1, col], tiles[row - 1, col].Cost()));
- }
- // Gauche
- if (col - 1 >= 0 && tiles[row, col - 1].IsValidPath())
- {
- arcsList.Add(new Arc(tiles[row, col], tiles[row, col - 1], tiles[row, col - 1].Cost()));
- }
- // Bas
- if (row + 1 < nbRows && tiles[row + 1, col].IsValidPath())
- {
- arcsList.Add(new Arc(tiles[row, col], tiles[row + 1, col], tiles[row + 1, col].Cost()));
- }
- // Droite
- if (col + 1 < nbCols && tiles[row, col + 1].IsValidPath())
- {
- arcsList.Add(new Arc(tiles[row, col], tiles[row, col + 1], tiles[row, col + 1].Cost()));
- }
- }
- }
- }
- }
- return arcsList;
- }
- public List<Arc> ArcsList(Node _currentNode)
- {
- List<Arc> list = new List<Arc>();
- int currentRow = ((Tile)_currentNode).Row;
- int currentCol = ((Tile)_currentNode).Col;
- // Renvoyer les voisins
- if (currentRow - 1 >= 0 && tiles[currentRow - 1, currentCol].IsValidPath())
- {
- list.Add(new Arc(_currentNode, tiles[currentRow - 1, currentCol], tiles[currentRow - 1, currentCol].Cost()));
- }
- if (currentCol - 1 >= 0 && tiles[currentRow, currentCol - 1].IsValidPath())
- {
- list.Add(new Arc(_currentNode, tiles[currentRow, currentCol - 1], tiles[currentRow, currentCol - 1].Cost()));
- }
- if (currentRow + 1 < nbRows && tiles[currentRow + 1, currentCol].IsValidPath())
- {
- list.Add(new Arc(_currentNode, tiles[currentRow + 1, currentCol], tiles[currentRow + 1, currentCol].Cost()));
- }
- if (currentCol + 1 < nbCols && tiles[currentRow, currentCol + 1].IsValidPath())
- {
- list.Add(new Arc(_currentNode, tiles[currentRow, currentCol + 1], tiles[currentRow, currentCol + 1].Cost()));
- }
- return list;
- }
- public double CostBetweenNodes(Node _fromNode, Node _toNode)
- {
- return ((Tile)_toNode).Cost();
- }
- public String ReconstructPath()
- {
- String resPath = "";
- Tile currentNode = exitNode;
- Tile prevNode = (Tile) exitNode.Precursor;
- while (prevNode != null)
- {
- resPath = "-" + currentNode.ToString() + resPath;
- currentNode = prevNode;
- prevNode = (Tile) prevNode.Precursor;
- }
- resPath = currentNode.ToString() + resPath;
- return resPath;
- }
- public void ComputeEstimatedDistanceToExit()
- {
- foreach (Tile tile in tiles)
- {
- tile.EstimatedDistance = Math.Abs(exitNode.Row - tile.Row) + Math.Abs(exitNode.Col - tile.Col);
- }
- }
- public void Clear()
- {
- nodesList = null;
- arcsList = null;
- for (int row = 0; row < nbRows; row++)
- {
- for (int col = 0; col < nbCols; col++)
- {
- tiles[row, col].DistanceFromBegin = double.PositiveInfinity;
- tiles[row, col].Precursor = null;
- }
- }
- beginNode.DistanceFromBegin = beginNode.Cost();
- }
- }
- }
|