Esempio n. 1
0
File: Game1.cs Progetto: tgy/CSharp
 public Game1()
 {
     graphics = new GraphicsDeviceManager(this);
     ServiceHelper.Game = this;
     Components.Add(new MouseService(this));
     Content.RootDirectory = "Content";
     map = new Map(new byte[,] {
         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 1, 0},
         {0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0},
         {0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0},
         {0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 2, 1, 1, 1, 1, 0, 0, 0},
         {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 2, 0, 0, 1, 0, 1, 0},
         {1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 2, 0, 0, 1, 0, 1, 0},
         {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 0, 1, 1},
         {0, 0, 0, 1, 0, 1, 0, 0, 3, 3, 0, 1, 2, 0, 0, 0, 0, 1},
         {0, 0, 0, 1, 0, 1, 1, 3, 3, 1, 1, 1, 2, 0, 1, 0, 0, 1},
         {0, 0, 0, 1, 0, 0, 1, 3, 3, 3, 3, 2, 2, 0, 1, 1, 0, 1},
         {0, 0, 0, 1, 0, 0, 1, 3, 3, 3, 3, 2, 1, 1, 1, 0, 0, 0},
         {0, 0, 0, 1, 0, 0, 0, 3, 1, 1, 3, 2, 0, 0, 1, 0, 0, 0},
         {0, 0, 0, 1, 0, 0, 3, 3, 1, 1, 3, 2, 0, 0, 0, 0, 0, 0}
     });
     graphics.PreferredBackBufferWidth = map.TileList.GetLength(1) * 50;
     graphics.PreferredBackBufferHeight = map.TileList.GetLength(0) * 50;
     IsMouseVisible = true;
 }
Esempio n. 2
0
        public static List<Node> PossibleNode = new List<Node>(); // Les noeuds possibles (cases adjacentes de tout le chemin)

        #endregion Fields

        #region Methods

        public static MyLinkedList<Tile> CalculatePathWithAStar(Map map, Tile startTile, Tile endTile)
        {
            PossibleNode.Clear();
            NodeList<Node> openList = new NodeList<Node>(); // Contiens tout les noeuds candidat (qui vont être examinés)
            NodeList<Node> closedList = new NodeList<Node>(); // Contiens la liste des meilleurs noeuds (le resultat du plus cours chemin)
            List<Node> possibleNodes; // cases adjacentes du noeud courant

            // Le noeud de départ
            Node startNode = new Node(startTile, null, endTile); // FIXME : on recupère le noeud de départ

            /**********************************/
            /* Traitement des noeuds candidat */
            /**********************************/

            openList.Add(startNode);

            while (openList.Count > 0) // Tant que la liste ouverte contient des éléments
            {
                Node current = openList[0];
                openList.RemoveAt(0);
                closedList.Add(current);

                if (current.Tile == endTile) // si l'élément courant est la case destination
                {
                    MyLinkedList<Tile> solution = new MyLinkedList<Tile>();
                    // on reverse la liste fermée et on la retourne pour l'avoir dans le bonne ordre
                    while (current.Parent != null)
                    {
                        solution.AddFirst(current.Tile);
                        current = current.Parent;
                    }
                    return solution;
                }
                possibleNodes = current.GetPossibleNode(map, endTile); // FIXME : recupère la listes des cases adjacentes

                // on ajoute cette liste a notre variable static qui contient l'ensemble des listes adjacentes (gestion de l'affichage)
                PossibleNode.AddRange(possibleNodes) ;

                /***************************************/
                /* Ajout des noeuds adjacents candidat */
                /***************************************/
                for (int i = 0; i < possibleNodes.Count; i++) // on vérifie que chaque noeuds adjacent (possibleNodes)
                {
                    if (!closedList.Contains(possibleNodes[i])) // n'existe pas dans la liste fermée (eviter la redondance)
                    {
                        if (openList.Contains(possibleNodes[i])) // FIXME : Si il existe dans la liste ouverte on vérifie
                        {
                            if (possibleNodes[i].EstimatedMovement < openList[possibleNodes[i]].EstimatedMovement) // si le cout de deplacement du
                                // noeud est inferieur a un coût calculer précedement, dance cas la on remonte le chemin dans la liste ouverte
                                openList[possibleNodes[i]].Parent = current;
                        }
                        else
                            openList.DichotomicInsertion(possibleNodes[i]);
                    }
                }
            }
            return null;
        }
Esempio n. 3
0
File: Node.cs Progetto: tgy/CSharp
 // recupère les 8 cases adjacentes
 public List<Node> GetPossibleNode(Map map, Tile destination)
 {
     List<Node> result = new List<Node>();
     // Bottom
     if (map.ValidCoordinates(tile.X, tile.Y + 1) && map.TileList[tile.Y + 1,
     tile.X].Type != TileType.Wall)
         result.Add(new Node(map.TileList[tile.Y + 1, tile.X], this,
         destination));
     // Right
     if (map.ValidCoordinates(tile.X + 1, tile.Y) && map.TileList[tile.Y,
     tile.X + 1].Type != TileType.Wall)
         result.Add(new Node(map.TileList[tile.Y, tile.X + 1], this,
         destination));
     // Top
     if (map.ValidCoordinates(tile.X, tile.Y - 1) && map.TileList[tile.Y - 1,
     tile.X].Type != TileType.Wall)
         result.Add(new Node(map.TileList[tile.Y - 1, tile.X], this,
         destination));
     // Left
     if (map.ValidCoordinates(tile.X - 1, tile.Y) && map.TileList[tile.Y,
     tile.X - 1].Type != TileType.Wall)
         result.Add(new Node(map.TileList[tile.Y, tile.X - 1], this,
         destination));
     // Bottom Left
     if (map.ValidCoordinates(tile.X - 1, tile.Y + 1) && map.TileList[tile.Y + 1,
     tile.X - 1].Type != TileType.Wall)
         result.Add(new Node(map.TileList[tile.Y + 1, tile.X - 1], this,
         destination));
     // Bottom Right
     if (map.ValidCoordinates(tile.X + 1, tile.Y + 1) && map.TileList[tile.Y + 1,
     tile.X + 1].Type != TileType.Wall)
         result.Add(new Node(map.TileList[tile.Y + 1, tile.X + 1], this,
         destination));
     // Top Left
     if (map.ValidCoordinates(tile.X - 1, tile.Y - 1) && map.TileList[tile.Y - 1,
     tile.X - 1].Type != TileType.Wall)
         result.Add(new Node(map.TileList[tile.Y - 1, tile.X - 1], this,
         destination));
     // Top Right
     if (map.ValidCoordinates(tile.X + 1, tile.Y - 1) && map.TileList[tile.Y - 1,
     tile.X + 1].Type != TileType.Wall)
         result.Add(new Node(map.TileList[tile.Y - 1, tile.X + 1], this,
         destination));
     return result;
 }