Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 KB | None | 0 0
  1. Labyrinthe Labyrinthe::generer(unsigned int largeur, unsigned int hauteur)
  2. {
  3.    Labyrinthe* lab = new Labyrinthe();
  4.    lab->m_graphe = Graphe();
  5.    lab->m_graphe.ajouterSommet(Coordonnee(1, 1));
  6.    lab->m_arrivee = Coordonnee(largeur - 2, hauteur - 2);
  7.    lab->m_depart = Coordonnee(1, 1);
  8.    lab->m_hauteur = hauteur;
  9.    lab->m_largeur = largeur;
  10.    Coordonnee courant = Coordonnee(1, 1);
  11.    int random;
  12.    bool courantChange;
  13.    srand(time(0));
  14.    while (courant != Coordonnee(largeur - 2, hauteur - 2))
  15.    {
  16.       random = rand() % 4;
  17.       courantChange = false;
  18.       switch (random)
  19.       {
  20.          case 0:
  21.          {
  22.             if (courant.y > 1)
  23.             {
  24.                courant = Coordonnee(courant.x, courant.y - 1);
  25.                courantChange = true;
  26.             }
  27.             break;
  28.          }
  29.          case 1:
  30.          {
  31.             if (courant.x < largeur - 2)
  32.             {
  33.                courant = Coordonnee(courant.x + 1, courant.y);
  34.                courantChange = true;
  35.             }
  36.             break;
  37.          }
  38.          case 2:
  39.          {
  40.             if (courant.y < hauteur - 2)
  41.             {
  42.                courant = Coordonnee(courant.x, courant.y + 1);
  43.                courantChange = true;
  44.             }
  45.             break;
  46.          }
  47.          case 3:
  48.          {
  49.             if (courant.x > 1)
  50.             {
  51.                courant = Coordonnee(courant.x - 1, courant.y);
  52.                courantChange = true;
  53.             }
  54.             break;
  55.          }
  56.       }
  57.       if (courantChange && !lab->m_graphe.sommetExiste(courant))
  58.       {
  59.          lab->m_graphe.ajouterSommet(courant);
  60.          Coordonnee enHaut = Coordonnee(courant.x, courant.y - 1);
  61.          Coordonnee enBas = Coordonnee(courant.x, courant.y + 1);
  62.          Coordonnee aDroite = Coordonnee(courant.x + 1, courant.y);
  63.          Coordonnee aGauche = Coordonnee(courant.x - 1, courant.y);
  64.  
  65.          if (lab->m_graphe.sommetExiste(enHaut) && !lab->m_graphe.areteExiste(enHaut, courant))
  66.          {
  67.             lab->m_graphe.ajouterArete(enHaut, courant, 1);
  68.          }
  69.          if (lab->m_graphe.sommetExiste(enBas) && !lab->m_graphe.areteExiste(enBas, courant))
  70.          {
  71.             lab->m_graphe.ajouterArete(enBas, courant, 1);
  72.          }
  73.          if (lab->m_graphe.sommetExiste(aDroite) && !lab->m_graphe.areteExiste(aDroite, courant))
  74.          {
  75.             lab->m_graphe.ajouterArete(aDroite, courant, 1);
  76.          }
  77.          if (lab->m_graphe.sommetExiste(aGauche) && !lab->m_graphe.areteExiste(aGauche, courant))
  78.          {
  79.             lab->m_graphe.ajouterArete(aGauche, courant, 1);
  80.          }
  81.       }
  82.  
  83.    }
  84.  
  85.    Chemin chPlusCourt =
  86.          rechercheCheminAlgorithmeAStar(lab->graphe(), lab->m_depart, lab->m_arrivee);
  87.    lab->m_graphe = Graphe();
  88.    courant = lab->arrivee();
  89.    lab->m_graphe.ajouterSommet(courant);
  90.    for (vector<Coordonnee>::iterator it = chPlusCourt.sequence.begin(); it
  91.          != chPlusCourt.sequence.end(); ++it)
  92.    {
  93.       if ((*it) != lab->arrivee())
  94.       {
  95.          lab->m_graphe.ajouterSommet((*it));
  96.          lab->m_graphe.ajouterArete((*it), courant, 1);
  97.          courant = *it;
  98.       }
  99.    }
  100.    int maxSommets = ((largeur - 2) * (hauteur - 2) - chPlusCourt.cout) / 2.5;
  101.    int cpt = 0;
  102.    int randomLargeur = 0;
  103.    int randomHauteur = 0;
  104.    while (cpt < maxSommets)
  105.    {
  106.       randomLargeur = rand() % (largeur - 2) + 1;
  107.       randomHauteur = rand() % (hauteur - 2) + 1;
  108.       courant = Coordonnee(randomLargeur, randomHauteur);
  109.       if (!lab->graphe().sommetExiste(courant))
  110.       {
  111.          cpt++;
  112.          lab->m_graphe.ajouterSommet(courant);
  113.          Coordonnee enHaut = Coordonnee(courant.x, courant.y - 1);
  114.          Coordonnee enBas = Coordonnee(courant.x, courant.y + 1);
  115.          Coordonnee aDroite = Coordonnee(courant.x + 1, courant.y);
  116.          Coordonnee aGauche = Coordonnee(courant.x - 1, courant.y);
  117.  
  118.          if (lab->m_graphe.sommetExiste(enHaut) && !lab->m_graphe.areteExiste(enHaut, courant))
  119.          {
  120.             lab->m_graphe.ajouterArete(enHaut, courant, 1);
  121.          }
  122.          if (lab->m_graphe.sommetExiste(enBas) && !lab->m_graphe.areteExiste(enBas, courant))
  123.          {
  124.             lab->m_graphe.ajouterArete(enBas, courant, 1);
  125.          }
  126.          if (lab->m_graphe.sommetExiste(aDroite) && !lab->m_graphe.areteExiste(aDroite, courant))
  127.          {
  128.             lab->m_graphe.ajouterArete(aDroite, courant, 1);
  129.          }
  130.          if (lab->m_graphe.sommetExiste(aGauche) && !lab->m_graphe.areteExiste(aGauche, courant))
  131.          {
  132.             lab->m_graphe.ajouterArete(aGauche, courant, 1);
  133.          }
  134.       }
  135.    }
  136.  
  137.    return *lab;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement