Serge Hakobyan
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # PROJET Il nous manque pour l'instant: * ~~Modifier le type concret des termites pour ajouter les variables d’état du termite (sablier, brindille, tourneSurPlace). Implémenter chargerBrindille et dechargerBrindille. Puis l’algorithme principal `rassemblerBrindille`, en prenant progressivement en compte ces nouvelles variables d’état. ~~ * ~~Observer, ou tester que les termites peuvent s’enfermer tout seul dans un mur de brindilles, en posant une brindille sur la case par où ils sont arrivés. Modifier rassemblerBrindille pour éviter cela. (Cela prend trois lignes de programmes). ~~ * Regler l'affichage des directions * Graphique # Serge ## donnee.hpp ```cpp #ifndef donnee_hpp #define donnee_hpp using namespace std; //short nbTermites; // unsigned short t=0; static const short LON = 20, LAR = 30; static const short tempsVide = 6; const unsigned short probA_T = 1; const unsigned short proba_b = 10, proba_t = proba_b+probA_T; const short taille=LON*LAR; const unsigned short tailleTabIdT = taille*(probA_T+5)/100; struct Coord { int x; int y; }; struct Case{ bool brindille; short idT; }; struct Grille { Case tab[LON][LAR]; }; enum Direction { nordOuest=0, nord = 1, nordEst = 2, est = 3, sudEst = 4, sud = 5, sudOuest = 6, ouest = 7, NO = 0, N = 1, NE = 2, E = 3, SE = 4, S = 5, SO = 6, O = 7 }; struct Termite { short idT; Coord c; short sablier; bool brin; Direction d; bool tourneSurPlace; }; #endif ``` ## coord.cpp ```cpp #include <iostream> #include "grille.hpp" #define ASSERT(C) if ( !(C) ) { std::cerr << "Test failed: "#C << std::endl; } using namespace std; Coord creeCoord(int lig, int col){ Coord c; c.y = col; c.x = lig; return c; } void afficheCoord(Coord c){ cout<<"("<<c.x<<", "<<c.y<<")"<<endl; } int getX(Coord c){ return c.x; } int getY(Coord c){ return c.y; } bool egalCoord(Coord c1, Coord c2) { return (c1.x==c2.x and c1.y==c2.y); } void afficheDirection(Direction d){ if (d==0) cout << "nord-ouest" << endl; if (d==1) cout << "nord" << endl; if (d==2) cout << "nord-est" << endl; if (d==3) cout << "est" << endl; if (d==4) cout << "sud-est" << endl; if (d==5) cout << "sud" << endl; if (d==6) cout << "sud-ouest" << endl; if (d==7) cout << "ouest" << endl; } Direction changeDir(Direction a){ if(a == Direction(0)) return Direction(7); return Direction(0); } Direction aGauche(Direction dir){ if(/*dir==Direction(0) or*/ dir==Direction(7)) return changeDir(dir); return Direction(dir+1); } Direction aDroite(Direction dir){ if(/*dir==Direction(7) or*/ dir==Direction(0)) return changeDir(dir); return Direction(dir-1); } Coord devantCoord(Coord c, Direction d){ switch(d){ case nord: c.y--; break; case sud: c.y++; break; case est: c.x++; break; case 7: c.x--; break; case nordOuest: c.x--;c.y--; break; case nordEst: c.x++;c.y--; break; case sudEst: c.x++;c.y++; break; case sudOuest: c.x--;c.y++; break; } return c; } char afficheDirectionT(Direction d){ switch(d){ case nordOuest://NordOuest return '\\'; break; case nord://Nord return '^'; break; case nordEst://NordEst //case sudOuest: return '/'; break; case est://Est return '<'; break; case sudEst://SudEst return '\\'; break; case sud://Sud return 'v'; break; case sudOuest: return '/'; break; case ouest://Ouest return '>'; break; } } ``` ## coord.hpp ```cpp #include "donnee.hpp" #ifndef coord_hpp #define coord_hpp #define ASSERT(C) if ( !(C) ) { std::cerr << "Test failed: "#C << std::endl; } using namespace std; /** Cree un type abstrait Coord a partir des valeurs transmises * @param[in] int lig: ligne * @param[in] int col: colonne **/ Coord creeCoord(int lig, int col); /** Affiche les coordonnees que comporte le type abstrait Coord * @param[in] Coord c: coordonnees **/ void afficheCoord(Coord c); /** Renvoie l'abscisse * @param[in] Coord c: coordonnees * @return l'abscisse c.x **/ int getX(Coord c); /** Renvoie l'ordonnee * @param[in] Coord c: coordonnees * @return l'ordonne c.y **/ int getY(Coord c); /** Verifie si 2 coordonnees sont egaux * @param[in] : Coord c1: coordonnees * @param[in] : Coord c2: coordonnees * @return vrai si vrai, faux sinon **/ bool egalCoord(Coord c1, Coord c2); /** Affiche la direction * @param[in] Direction dir: direction **/ void afficheDirection(Direction dir); /** Renvoie la direction modifiee * @param[in] Direction d: direction a changer * @return direction changee **/ Direction aGauche(Direction dir); /** Renvoie la direction modifiee * @param[in] Direction d: direction a changer * @return direction changee **/ Direction aDroite(Direction dir); /** Renvoie les coordonnees d'en face, dans un type Coord * @param[in] Coord c: les coordonnees * @param[in] Direction d: la direction de l'objet * @return les coordonnees d'en face **/ Coord devantCoord(Coord c, Direction d); /** Renvoie des caracteres correspondant a la direction du termite * @param[in] Direction d: Direction du termite * @return un character, char **/ char afficheDirectionT(Direction); /** test de toutes les fonctions ci-dessus **/ void testEgalCoord(); #endif ``` ## grille.cpp ```cpp #include <iostream> #include "grille.hpp" using namespace std; void initialiseGrilleVide(Grille &g){ //resultat for(int i=0; i<LON; i++){ for(int j=0; j<LAR; j++){ g.tab[i][j].brindille = 0; g.tab[i][j].idT = -1; } } } void initialiseTabIdVide(Termite (&tab)[tailleTabIdT]){ for(short i=0; i<tailleTabIdT; i++) //initialise les id des termites a 0 tab[i].idT=-1; } bool dansGrille(Grille g, Coord c){ //donnees return(c.x < LON and c.y < LAR and c.y>=0 and c.x>=0); } bool estVide (Grille g, Coord c){ //donnees return (!g.tab[c.x][c.y].brindille and g.tab[c.x][c.y].idT == -1); } bool contientBrindille(Grille g, Coord c){ //donnees return (g.tab[c.x][c.y].brindille); } short int numeroTermite(Grille g , Coord c){ //donnees return g.tab[c.x][c.y].idT; } void poseBrindille(Grille &g, Coord c){ //DR, donee if(estVide(g,c)) g.tab[c.x][c.y].brindille=1; } void enleveBrindille(Grille &g, Coord c){ //DR, donee if(g.tab[c.x][c.y].brindille==1) g.tab[c.x][c.y].brindille=0; } void poseTermite(Grille &g, Coord c,short idT){ //DR, donee if(estVide(g,c)) g.tab[c.x][c.y].idT=idT; } void enleveTermite(Grille &g, Coord c){ //DR, donee g.tab[c.x][c.y].idT=-1; } ``` ## grille.hpp ```cpp #include "coord.hpp" #ifndef grille_hpp #define grille_hpp using namespace std; /** Initialise une grille vide: brandille=0, idTermite=-1 * @param[out] Griile g: la grille a vider **/ void initialiseGrilleVide(Grille &g); /** Initialise le tableau de termites: idT=-1 * @param[out] Termite tab[]: tableau de termites **/ void initialiseTabIdVide(Termite (&tab)[tailleTabIdT]); /** Verifie si les coordonnees donnes sont dans la grille * @param[in] Grille g: la grille * @param[in] Coord c: les coordonnees a verifier * @return vrai si les coordonnees sont dans la grille, faux sinon **/ bool dansGrille(Grille g, Coord c); /** Verifie si la case donnee est vide * @param[in] Grille g: la grille * @param[in] Coord c: les coordonnees a verifier * @return vrai si la case estVide, faux sinon **/ bool estVide (Grille g, Coord c); /** Verifie si la case donnee contient une brindille * @param[in] Grille g: la grille * @param[in] Coord c: les coordonnees a verifier * @return vrai si oui, faux sinon **/ bool contientBrindille(Grille g, Coord c); /** Renvoie le numero du termite sur la case donnee * @param[in] Grille g: la grille * @param[in] Coord c: la case a verifier * @return le numero du termite(idT), -1 s'il n'y en a pas **/ short numeroTermite(Grille g, Coord c); /** Pose une brindille sur la case donne si celle-ci estVide * @param[in/out] Grille g: la grille * @param[in] Coord c: la case ou l'on met une brindille **/ void poseBrindille(Grille &g, Coord c); /** Enleve la brindille d'une case si elle en contient une * @param[in/out] Grille g: la grille * @param[in] Coord c: la case a modifier (ou pas) **/ void enleveBrindille(Grille &g, Coord c); /*Pose une termite dans la case donnee si celle-ci estVide * @param[in/out] Grille g: la grille * @param[in] Coord c: la case a verifier * @param[in] short idT: le numero du termite a poser **/ void poseTermite(Grille &g, Coord c, short); /** Enleve le termite de la case donnee * @param[in/out] Grille g: la grille * @param[in] Coord c: la case a modifier (ou pas) **/ void enleveTermite(Grille &g, Coord c); ``` ## termite.cpp ```cpp #include <iostream> #include "termite.hpp" using namespace std; Direction directionTermite(Termite t){ return t.d; } Coord devantTermite(Termite t){ return devantCoord(t.c, t.d); } void tourneAGauche(Termite &t){ t.d=aGauche(t.d); } void tourneADroite(Termite &t){ t.d=aDroite(t.d); } void tourneAleat(Termite &t){ short n=rand()%2; if (n==0){ tourneAGauche(t); return; } tourneADroite(t); } bool laVoieEstLibre(Grille g, Termite t){ if(dansGrille(g,devantTermite(t))) return (estVide(g, devantTermite(t))); return 0; } bool brindilleEnFace(Grille g, Termite t){ if(dansGrille(g,devantTermite(t))) return (contientBrindille(g, devantTermite(t))); return 0; } bool pasEnferme(Grille g, Coord c){ int vide = 0; Direction dirPE; //DirectionPasEnferme for(short i = 0; i < 8; i++){ dirPE = Direction(i); if(dansGrille(g, devantCoord(c, dirPE)) and estVide(g, devantCoord(c, dirPE))) //compte le nombre de cases vides vide++; } return vide >= 2; } void avanceTermite(Grille &g, Termite &t){ if(laVoieEstLibre(g, t)){ enleveTermite(g, t.c); t.c = devantTermite(t); poseTermite(g, t.c, t.idT); } else tourneAleat(t); } bool dechargeTermite(Grille &g, Termite &t){ Coord devant = devantTermite(t); if(dansGrille(g, devant) and estVide(g, devant) and t.sablier <= 0 and t.brin){ t.brin = 0; poseBrindille(g, devant); return 1; } return 0; } bool chargeTermite(Grille &g, Termite &t){ Coord devant = devantTermite(t); if(dansGrille(g, devant) and contientBrindille(g, devant) and t.sablier <= 0){ t.brin = 1; t.sablier = tempsVide; enleveBrindille(g, devant); return 1; } return 0; } void marcheAleatoire(Grille &g, Termite &t) { int proba = rand()%10; if (laVoieEstLibre(g, t) and proba < 8) avanceTermite(g, t); else tourneAleat(t); } void creeTermite(Coord c1, Termite (&tab)[tailleTabIdT], int id){ for(short i=0; i<tailleTabIdT; i++) if(tab[i].idT==-1){ tab[i].idT=id; tab[i].c=c1; tab[i].sablier=0; tab[i].brin=0; tab[i].d=Direction(rand()%7); tab[i].tourneSurPlace=0; id=tab[i].idT; return; } } void afficheTermite(Termite tab[tailleTabIdT], short nT){ for(int i=0; i< tailleTabIdT; i++){ if(tab[i].idT==nT){ cout<<"id: "<<tab[i].idT<<endl; cout<<"coord: ";afficheCoord(tab[i].c); cout<<"sablier: "<<tab[i].sablier<<endl; cout<<"brin: "<<tab[i].brin<<endl; cout<<"direction: ";afficheDirection(tab[i].d);cout<<endl; cout<<"tourne"<<tab[i].tourneSurPlace<<endl; return; } } } void memeCoord(Grille &g, Coord c, Termite &t){ if(egalCoord(c, t.c) and estVide(g, devantTermite(t))){ t.tourneSurPlace=1; tourneAGauche(t); } else if(t.tourneSurPlace){ t.tourneSurPlace=0; avanceTermite(g, t); } } ``` - ## Termite.hpp ```cpp #include "grille.hpp" #ifndef termite_hpp #define termite_hpp using namespace std; /** Renvoie la direction du termite * @param[in] Termite t: le termite * @return t.d: la direction du type abstrait Termite **/ Direction directionTermite(Termite t); /** Renvoie les coordonnees de la case d'en face du termite * @param[in] Termite t: le termite * @return Coord **/ Coord devantTermite(Termite t); /** Tourne le termite a gauche * @param[out] Termite t **/ void tourneAGauche(Termite &t); /** Tourne le termite a droite * @param[out] Termite T **/ void tourneADroite(Termite &t); /** Tourne le termite a gauche ou a droite * @param[out] Termite **/ void tourneAleat(Termite &t); /** Verifie s'il n'y a ni de brindilles ni de termite en face * @param[in] Grille * @param[in] Termite * @return vrai si la voie est libre, faux sinon **/ bool laVoieEstLibre(Grille g, Termite t); /** Verifie s'il y a une brindille devant * @param[in] Grille * @param[in] Termite: celui qui avance * @return vrai s'il y en a une, faux sinon : **/ bool brindilleEnFace(Grille g, Termite t); /** Verifie que le termite ne soit pas enferme s'il pose sa brindille dans la case donnee * @param[in] Grille * @param[in] Coord: les coordonnees da la case d'en face * @return vrai si le termite ne s'enferme pas, faux sinon **/ bool pasEnferme(Grille g, Coord c); /** Fait avancer le termite si possible, sinon le fait tourner * @param[in/out] Grille * @param[in/out] Termite **/ void avanceTermite(Grille &g, Termite &t); /** Fait le termite deposer la brindille devant lui * @param[in/out] Grille * @param[in/out] Termite **/ bool dechargeTermite(Grille &g, Termite &t); /** Fait le termite prendre la brindille d'en face * @param[in/out] Grille * @param[in/out] Termite **/ bool chargeTermite(Grille &g, Termite &t); /** Fait avancer le termite avec une probabilite de 90% et tourne aleatoirement avec celle de 10% * @param[in/out] Grille * @param[in/out] Termite **/ void marcheAleatoire(Grille &g, Termite &t); /** Cree un termite avec les donnees d'initialisation choisies * @param[in] int id: le numero (id) du termite * @param[in] Coord c1: les coordonnees de la case ou le termite se trouve * @param[out] Termite tab[]: le tableau des termites **/ void creeTermite(Coord c1, Termite (&tab)[tailleTabIdT], int id); /** * @param[in/out] : * @param[in] : * @param[out] : **/ void afficheTermite(Termite tab[tailleTabIdT], short nT); ``` void testTermite(); ## testtermite ```cpp #include <iostream> #include "termite.hpp" using namespace std; #define ASSERT(test) if (!(test)) cout << "Test failed in file " << __FILE__ \ << " line " << __LINE__ << ": " #test << endl void testTermite(){ Termite tab[tailleTabIdT]; initialiseTabIdVide(tab); creeTermite(creeCoord(0, 0), tab, 2); Grille g; initialiseGrilleVide(g); poseBrindille(g, {LON-1, 1}); poseTermite(g, {0, 0}, 2); Termite t = tab[0]; t.d = E; ASSERT (egalCoord(devantTermite(t), creeCoord(1, 0))); for (int i=1; i<LON; i++){ avanceTermite(g, t); } ASSERT (!(dansGrille(g, devantTermite(t)))); ASSERT (!(laVoieEstLibre(g, t))); ASSERT (!(brindilleEnFace(g, t))); ASSERT (!(porteBrindille(t))); tourneADroite(t); tourneADroite(t); ASSERT (dansGrille(g, devantTermite(t))); ASSERT (!(laVoieEstLibre(g, t))); ASSERT (brindilleEnFace(g, t)); ASSERT (pasEnferme(g, t)); chargeTermite(g, t); ASSERT (porteBrindille(t)); ASSERT (laVoieEstLibre(g, t)); ASSERT (!(brindilleEnFace(g, t))); poseBrindille(g, {LON-1, 1}); poseBrindille(g, {LON-2, 1}); tourneADroite(t); tourneADroite(t); ASSERT (laVoieEstLibre(g, t)); ASSERT (!(pasEnferme(g, t))); tourneAGauche(t); ASSERT (!(laVoieEstLibre(g, t))); ASSERT (brindilleEnFace(g, t)); enleveBrindille(g, {LON-2, 1}); creeTermite(creeCoord(LON-2, 1), tab, 22); poseTermite(g, creeCoord(LON-2, 1), 22); Termite t2 = tab[1]; t2.d = t.d; tourneAGauche(t);tourneAGauche(t);tourneAGauche(t);tourneAGauche(t); tourneAGauche(t);tourneAGauche(t);tourneAGauche(t);tourneAGauche(t); ASSERT (directionTermite(t)==directionTermite(t2)); ASSERT (!(laVoieEstLibre(g, t))); tourneAleat(t2); ASSERT (!(directionTermite(t)==directionTermite(t2))); marcheAleatoire(g, t2); ASSERT (laVoieEstLibre(g, t)); dechargeTermite(g, t); ASSERT (!(pasEnferme(g, t))); tourneADroite(t); marcheAleatoire(g, t); ASSERT (pasEnferme(g, t)); poseBrindille(g, creeCoord(LON-3, 0)); ASSERT (!(pasEnferme(g, t))); Coord c1 = devantTermite(t); Coord c2 = t.c; marcheAleatoire(g, t); ASSERT (!(egalCoord(c1, devantTermite(t)))); ASSERT (egalCoord(c2, t.c)); } int main(){ testTermite(); return 0; } ``` ## projet.cpp ```cpp /#include <iostream> //#include <SFML/Graphics.hpp> #include "termite.hpp" #define RED "\033[31m" #define WHITE "\033[0m" using namespace std; unsigned short t=0; void initialiseRand(Grille &g, Termite (&tab)[tailleTabIdT]){ short id = 0; for(int i(0); i < LON; i++){ for(int j(0); j < LAR;j++){ short ranNum = rand()%100+1; Coord c = creeCoord(i,j); if(ranNum < proba_b) poseBrindille(g, c); else if(ranNum < proba_t){ id++; creeTermite(c, tab, id); poseTermite(g, c, id); } } } } void afficheCase(int i, int j, Grille g, Termite (&tab)[tailleTabIdT], int &v, int &b){ Case c=g.tab[i][j]; if(c.brindille == 0 and c.idT == -1){ v++; cout<<' ';} else if(c.brindille == 1){ b++; cout << '*';} else { if(tab[t].brin) cout << RED << afficheDirectionT(tab[t].d) << WHITE; else cout << afficheDirectionT(tab[t].d); t++; } } void afficheGrille(Grille g, Termite (&tab)[tailleTabIdT]){ int v = 0, b = 0, numTerm = 1; t = 0; for(int i(0); i < LON; i++){ for(int j(0); j < LAR;j++) afficheCase(i, j, g, tab, v, b); cout << endl; } cout << "brindilles: " << b << endl; cout << "termites: " << t << endl; afficheTermite(tab, numTerm); } void sim(Grille &g, Termite (&tab)[tailleTabIdT]){ for(int i = 0; i < t; i++){ tab[i].sablier--; Coord coordAvant=tab[i].c; //if(!tab[i].tourneSurPlace) marcheAleatoire(g, tab[i]); // memeCoord(g, coordAvant, tab[i]); if(chargeTermite(g, tab[i]) or dechargeTermite(g, tab[i])) tab[i].sablier = tempsVide; chargeTermite(g, tab[i]); dechargeTermite(g, tab[i]); } } int main(){ unsigned long i=1;unsigned short m=1;//iteration, multiplicateur srand (time(NULL)); int c; //test(); Grille g; Termite tabIdT[tailleTabIdT]; initialiseGrilleVide(g); initialiseTabIdVide(tabIdT); initialiseRand(g, tabIdT); while(1){ cout << "Nombre d'iterations: " << i << endl; cout << "Vitesse: " << m << endl; for(int k=0; k != m; k++) sim(g, tabIdT); afficheGrille(g, tabIdT); c = getchar(); if(c == '*'){ //cout << "1"; m *= 2; } else if(c == '/'){ cout << "2"; m /= 2; if(m == 0) m = 1; } else if(c == '.'){ cout << "3"; break; } i += m; //sleep(1); system("clear"); } cout << "Vous avez mis fin a la simulation." << endl; return 0; } ``` ## testcoord.cpp ```cpp #include <iostream> #include <string> #include "coord.hpp" using namespace std; #define ASSERT(test) if (!(test)) cout << "Test failed in file " << __FILE__ \ << " line " << __LINE__ << ": " #test << endl void testEgalCoord(){ ASSERT( egalCoord(creeCoord(5, 6), creeCoord(5, 6))); ASSERT(!egalCoord(creeCoord(3, 5), creeCoord(4, 5))); ASSERT(!egalCoord(creeCoord(1, 19), creeCoord(19, 1))); } void testEgalDirection(){ ASSERT(aGauche(aDroite(O))== O); ASSERT(aGauche(aDroite(NO))== NO); ASSERT(aGauche(aDroite(S))== S); ASSERT(aGauche(aGauche(aGauche(aGauche(aGauche(aGauche(aGauche(aGauche(N))))))))==N); ASSERT(aDroite(aDroite(aDroite(aDroite(aDroite(aDroite(aDroite(aDroite(SE))))))))==SE); } void testDevantCoord(){ ASSERT(egalCoord(devantCoord(creeCoord(2,1), N), creeCoord(2,0))); ASSERT(egalCoord(devantCoord(creeCoord(5,5), NE), creeCoord(6,4))); ASSERT(egalCoord(devantCoord(creeCoord(100,100), SO), creeCoord(99,101))); ASSERT(egalCoord(devantCoord(creeCoord(0,0), S), creeCoord(0,1))); ASSERT(egalCoord(creeCoord(7,11),devantCoord(devantCoord(creeCoord(7,11),NE),aDroite(aDroite(aDroite(aDroite(NE))))))); //Question 3.20 } int main(){ Coord c1 = creeCoord(2,1); afficheCoord(c1); cout << endl; testEgalCoord(); testEgalDirection(); testDevantCoord(); return 0; } ``` ## testgrille.cpp ```cpp #include <iostream> #include "coord.hpp" #include "grille.hpp" using namespace std; #define ASSERT(test) if (!(test)) cout << "Test failed in file " << __FILE__ \ << " line " << __LINE__ << ": " #test << endl void testGrille1(){ // marche sous la condition que LON et LAR soient toujours >1 Grille g; initialiseGrilleVide(g); for (int i=0; i<LON; i++){ for (int j=0; j<LAR; j++){ ASSERT (estVide(g, {i, j})); } } ASSERT (!(dansGrille(g, {LON, LAR-1}))); ASSERT (!(dansGrille(g, {LON-1, LAR}))); ASSERT (!(dansGrille(g, {-1, LAR-1}))); ASSERT (!(dansGrille(g, {LON-1, -1}))); ASSERT (dansGrille(g, {0, 0})); ASSERT (dansGrille(g, {1, 1})); ASSERT (dansGrille(g, {0, LAR-2})); ASSERT (dansGrille(g, {LON-1, LAR-1})); } void testGrille2(){ // marche sous la condition que LON et LAR soient toujours >1 Grille g; initialiseGrilleVide(g); poseBrindille(g, {LON-1, LAR-1}); poseTermite(g, {0, 0}, 5); ASSERT (!(estVide(g, {0, 0}))); ASSERT (!(estVide(g, {LON-1, LAR-1}))); ASSERT (numeroTermite(g, {LON-1, LAR-1})==-1); ASSERT (numeroTermite(g, {0, 0})==5); ASSERT (!(contientBrindille(g, {0, 0}))); ASSERT (contientBrindille(g, {LON-1, LAR-1})); enleveBrindille(g, {LON-1, LAR-1}); enleveTermite(g, {0, 0}); for (int i=0; i<LON; i++){ for (int j=0; j<LAR; j++){ ASSERT (estVide(g, {i, j})); } } ASSERT (numeroTermite(g, {0, 0})==-1); ASSERT (!(contientBrindille(g, {LON-1, LAR-1}))); poseBrindille(g, {0, 1}); poseTermite(g, {0, 1}, 0); poseTermite(g, {1, 0}, 0); poseBrindille(g, {1, 0}); ASSERT (numeroTermite(g, {0, 1})==-1); ASSERT (contientBrindille(g, {0, 1})); ASSERT (!(contientBrindille(g, {1, 0}))); ASSERT (numeroTermite(g, {1, 0})==0); enleveBrindille(g, {0, 1}); enleveTermite(g, {1, 0}); ASSERT (estVide(g, {0, 1})); ASSERT (estVide(g, {1, 0})); } int main(){ testGrille1(); testGrille2(); return 0; } ``` # Omar ## coord.cpp ```cpp #include <iostream> #include "coord.hpp" using namespace std; Coord creeCoord(int lig, int col){ Coord c; c.x = lig; c.y = col; return c; } void afficheCoord(Coord c){ cout << "(" << c.x << "," << c.y << ")" << endl; } int getX(Coord c){ return c.x; } int getY(Coord c){ return c.y; } bool egalCoord(Coord c1, Coord c2) { return (c1.x==c2.x and c1.y==c2.y); } void afficheDirection(Direction d){ if (d==0) cout << "nord-ouest" << endl; if (d==1) cout << "nord" << endl; if (d==2) cout << "nord-est" << endl; if (d==3) cout << "est" << endl; if (d==4) cout << "sud-est" << endl; if (d==5) cout << "sud" << endl; if (d==6) cout << "sud-ouest" << endl; if (d==7) cout << "ouest" << endl; } Direction aGauche(Direction d){ if (d>=1 and d<=7){ return Direction(d-1); } if (d==0) { return Direction(7); }else{ cout << "ERROR : Direction doit être entre 0 et 7" << endl; } return d; } Direction aDroite(Direction d){ if (d>=0 and d<=6){ return Direction(d+1); } if (d==7) { return Direction(0); }else{ cout << "ERROR : Direction doit être entre 0 et 7" << endl; } return d; } Coord devantCoord(Coord c, Direction d){ switch(d){ case N : c.y--; break; case S : c.y++; break; case E : c.x++; break; case O : c.x--; break; case SE: c.x++; c.y++; break; case NO: c.x--; c.y--; break; case NE: c.x++; c.y--; break; case SO: c.x--; c.y++; break; } return c; } char afficheDirectionT(Direction d){ switch(d){ case nordOuest : return '\\'; break; case nord : return '^'; break; case nordEst : return '/'; break; case est : return '>'; break; case sudEst : return '\\'; break; case sud : return 'v'; break; case sudOuest : return '/'; break; case ouest : return '<'; break; } return '?'; } ``` ## coord.hpp ```cpp #ifndef coord_hpp #define coord_hpp //////////////////////////////////////////////////// struct Coord { int x; int y; }; //enum Direction {NO, N, NE, E, SE, S, SO, O}; enum Direction { nordOuest=0, nord = 1, nordEst = 2, est = 3, sudEst = 4, sud = 5, sudOuest = 6, ouest = 7, NO = 0, N = 1, NE = 2, E = 3, SE = 4, S = 5, SO = 6, O = 7 }; //////////////////////////////////////////////////// Coord creeCoord(int lig, int col); void afficheCoord(Coord c); int getX(Coord c); int getY(Coord c); bool egalCoord(Coord c1, Coord c2); void testEgalCoord(); void afficheDirection(Direction d); Direction aGauche(Direction d); Direction aDroite(Direction d); Coord devantCoord(Coord c, Direction d); char afficheDirectionT(Direction d); #endif ``` ## testcoord.cpp ```cpp #include <iostream> #include <string> #include "coord.hpp" using namespace std; #define ASSERT(test) if (!(test)) cout << "Test failed in file " << __FILE__ \ << " line " << __LINE__ << ": " #test << endl void testEgalCoord(){ ASSERT( egalCoord(creeCoord(5, 6), creeCoord(5, 6))); ASSERT(!egalCoord(creeCoord(3, 5), creeCoord(4, 5))); ASSERT(!egalCoord(creeCoord(1, 19), creeCoord(19, 1))); } void testEgalDirection(){ ASSERT(aGauche(aDroite(O))== O); ASSERT(aGauche(aDroite(NO))== NO); ASSERT(aGauche(aDroite(S))== S); ASSERT(aGauche(aGauche(aGauche(aGauche(aGauche(aGauche(aGauche(aGauche(N))))))))==N); ASSERT(aDroite(aDroite(aDroite(aDroite(aDroite(aDroite(aDroite(aDroite(SE))))))))==SE); } void testDevantCoord(){ ASSERT(egalCoord(devantCoord(creeCoord(2,1), N), creeCoord(2,0))); ASSERT(egalCoord(devantCoord(creeCoord(5,5), NE), creeCoord(6,4))); ASSERT(egalCoord(devantCoord(creeCoord(100,100), SO), creeCoord(99,101))); ASSERT(egalCoord(devantCoord(creeCoord(0,0), S), creeCoord(0,1))); ASSERT(egalCoord(creeCoord(7,11),devantCoord(devantCoord(creeCoord(7,11),NE),aDroite(aDroite(aDroite(aDroite(NE))))))); //Question 3.20 } int main(){ Coord c1 = creeCoord(2,1); afficheCoord(c1); cout << endl; testEgalCoord(); testEgalDirection(); testDevantCoord(); return 0; } ``` ## grille.cpp ```cpp #include <iostream> #include "grille.hpp" #include "coord.hpp" using namespace std; void initialiseGrilleVide(Grille &g){ for (int i=0; i<LON; i++){ for (int j=0; j<LAR; j++){ g.tab[i][j].brindille = 0; g.tab[i][j].idT = -1; } } } bool estVide(Grille g, Coord c){ return (!g.tab[c.x][c.y].brindille) and (g.tab[c.x][c.y].idT == -1); } bool dansGrille(Grille g, Coord c){ return (c.x < LON) and (c.y < LAR) and (c.x >= 0) and (c.y >= 0); } bool contientBrindille(Grille g, Coord c){ return g.tab[c.x][c.y].brindille; } int numeroTermite(Grille g, Coord c){ return g.tab[c.x][c.y].idT; } void poseBrindille(Grille &g, Coord c){ if (estVide(g, c)) g.tab[c.x][c.y].brindille = 1; } void enleveBrindille(Grille &g, Coord c){ if (g.tab[c.x][c.y].brindille==1) g.tab[c.x][c.y].brindille = 0; } void poseTermite(Grille &g, Coord c, int idT){ if (estVide(g, c)) g.tab[c.x][c.y].idT = idT; } void enleveTermite(Grille &g, Coord c){ g.tab[c.x][c.y].idT = -1; } ``` ## grille.hpp ```cpp #ifndef grille_hpp #define grille_hpp #include "coord.hpp" const int LON = 25; const int LAR = 15; struct Case { bool brindille; int idT; }; struct Grille { Case tab[LON][LAR]; }; //////////////////////////////////////////////////// // Constructeur : void initialiseGrilleVide(Grille &g); // Accès à une case de la grille : bool estVide(Grille g, Coord c); bool dansGrille(Grille g, Coord c); bool contientBrindille(Grille g, Coord c); int numeroTermite(Grille g, Coord c); // Modifications d’une case de la grille : void poseBrindille(Grille &g, Coord c); void enleveBrindille(Grille &g, Coord c); void poseTermite(Grille &g, Coord c, int idT); void enleveTermite(Grille &g, Coord c); // Des tests : void testGrille1(); void testGrille2(); #endif ``` ## testgrille.cpp ```cpp #include <iostream> #include "coord.hpp" #include "grille.hpp" using namespace std; #define ASSERT(test) if (!(test)) cout << "Test failed in file " << __FILE__ \ << " line " << __LINE__ << ": " #test << endl void testGrille1(){ // marche sous la condition que X et Y soient toujours >1 Grille g; initialiseGrilleVide(g); for (int i=0; i<LON; i++){ for (int j=0; j<LAR; j++){ ASSERT (estVide(g, {i, j})); } } ASSERT (!(dansGrille(g, {LON, LAR-1}))); ASSERT (!(dansGrille(g, {LON-1, LAR}))); ASSERT (!(dansGrille(g, {-1, LAR-1}))); ASSERT (!(dansGrille(g, {LON-1, -1}))); ASSERT (dansGrille(g, {0, 0})); ASSERT (dansGrille(g, {1, 1})); ASSERT (dansGrille(g, {0, LAR-2})); ASSERT (dansGrille(g, {LON-1, LAR-1})); } void testGrille2(){ // marche sous la condition que LON et LAR soient toujours >1 Grille g; initialiseGrilleVide(g); poseBrindille(g, {LON-1, LAR-1}); poseTermite(g, {0, 0}, 5); ASSERT (!(estVide(g, {0, 0}))); ASSERT (!(estVide(g, {LON-1, LAR-1}))); ASSERT (numeroTermite(g, {LON-1, LAR-1})==-1); ASSERT (numeroTermite(g, {0, 0})==5); ASSERT (!(contientBrindille(g, {0, 0}))); ASSERT (contientBrindille(g, {LON-1, LAR-1})); enleveBrindille(g, {LON-1, LAR-1}); enleveTermite(g, {0, 0}); for (int i=0; i<LON; i++){ for (int j=0; j<LAR; j++){ ASSERT (estVide(g, {i, j})); } } ASSERT (numeroTermite(g, {0, 0})==-1); ASSERT (!(contientBrindille(g, {LON-1, LAR-1}))); poseBrindille(g, {0, 1}); poseTermite(g, {0, 1}, 0); poseTermite(g, {1, 0}, 0); poseBrindille(g, {1, 0}); ASSERT (numeroTermite(g, {0, 1})==-1); ASSERT (contientBrindille(g, {0, 1})); ASSERT (!(contientBrindille(g, {1, 0}))); ASSERT (numeroTermite(g, {1, 0})==0); enleveBrindille(g, {0, 1}); enleveTermite(g, {1, 0}); ASSERT (estVide(g, {0, 1})); ASSERT (estVide(g, {1, 0})); } int main(){ testGrille1(); testGrille2(); return 0; } ``` ## termite.cpp ```cpp #include <iostream> #include "termite.hpp" using namespace std; Direction directionTermite(Termite t){ return t.d; } Coord devantTermite(Termite t){ return devantCoord(t.c, t.d); } bool porteBrindille(Termite t){ return t.brin; } void tourneADroite(Termite &t){ t.d = aDroite(t.d); } void tourneAGauche(Termite &t){ t.d = aGauche(t.d); } void tourneAleat(Termite &t){ short n = rand()%2; if (n==0){ tourneAGauche(t); } else { tourneADroite(t); } } bool laVoieEstLibre(Grille g, Termite t){ return (dansGrille(g, devantTermite(t))) and (estVide(g, devantTermite(t))); } bool brindilleEnFace(Grille g, Termite t){ return (dansGrille(g, devantTermite(t))) and (contientBrindille(g, devantTermite(t))); } bool pasEnferme(Grille g, Termite t){ int vide = 0; for(short i = 0; i < 8; i++){ if (dansGrille(g, devantCoord(t.c, Direction(i))) and estVide(g, devantCoord(t.c, Direction(i)))){ vide++; //compte le nombre de cases vides } } return vide >= 2; } void avanceTermite(Grille &g, Termite &t){ if (laVoieEstLibre(g, t)){ enleveTermite(g, t.c); t.c = devantTermite(t); poseTermite(g, t.c, t.idT); } } bool pretADecharger(Grille g, Termite t){ return (brindilleEnFace(g, t)) and (t.sablier <= 0) and (t.brin); } bool dechargePossible(Grille g, Termite t){ return (laVoieEstLibre(g, t) and (t.sablier <= 0) and (t.brin) and t.tourneSurPlace and pasEnferme(g, t)); } bool chargePossible(Grille g, Termite t){ return (brindilleEnFace(g, t)) and (t.sablier <= 0) and (!t.brin); } void dechargeTermite(Grille &g, Termite &t){ poseBrindille(g, devantTermite(t)); t.brin = 0; t.sablier = tempsVide; } void chargeTermite(Grille &g, Termite &t){ t.brin = 1; enleveBrindille(g, devantTermite(t)); t.sablier = tempsVide; } void marcheAleatoire(Grille &g, Termite &t){ int proba = rand()%10; if (laVoieEstLibre(g, t) and proba < 8){ avanceTermite(g, t); } else if (laVoieEstLibre(g, t) and proba >= 8){ tourneAleat(t); avanceTermite(g, t); } else { tourneAleat(t); } } void initialiseTabIdVide(Termite (&tab)[tailleTabIdT]){ for(int i=0; i<tailleTabIdT; i++) //initialise les id des termites a 0 tab[i].idT=-1; } void afficheTermite(Termite tab[tailleTabIdT], short nT){ for(int i=0; i< tailleTabIdT; i++){ if(tab[i].idT==nT){ cout<<"id: "<<tab[i].idT<<endl; cout<<"coord: ";afficheCoord(tab[i].c); cout<<"sablier: "<<tab[i].sablier<<endl; cout<<"brin: "<<tab[i].brin<<endl; cout<<"direction: ";afficheDirection(tab[i].d);cout<<endl; cout<<"tourne"<<tab[i].tourneSurPlace<<endl; return; } } } void creeTermite(Coord c1, Termite (&tab)[tailleTabIdT], short id){ for(int i=0; i<tailleTabIdT; i++){ if(tab[i].idT==-1){ tab[i].idT=id; tab[i].c=c1; tab[i].sablier=0; tab[i].brin=0; tab[i].d=Direction(rand()%7); tab[i].tourneSurPlace=0; return; } } } ``` ## termite.hpp ```cpp #ifndef termite_hpp #define termite_hpp #include "grille.hpp" //////////////////////////////////////////////////// Direction directionTermite(Termite t); Coord devantTermite(Termite t); bool porteBrindille(Termite t); void tourneADroite(Termite &t); void tourneAGauche(Termite &t); void tourneAleat(Termite &t); bool laVoieEstLibre(Grille g, Termite t); bool brindilleEnFace(Grille g, Termite t); bool pasEnferme(Grille g, Termite t); void avanceTermite(Grille &g, Termite &t); bool pretADecharger(Grille g, Termite t); void dechargeTermite(Grille &g, Termite &t); void chargeTermite(Grille &g, Termite &t); bool dechargePossible(Grille g, Termite t); bool chargePossible(Grille g, Termite t); void marcheAleatoire(Grille &g, Termite &t); void initialiseTabIdVide(Termite (&tab)[tailleTabIdT]); void afficheTermite(Termite tab[tailleTabIdT], short nT); void creeTermite(Coord c1, Termite (&tab)[tailleTabIdT], short id); void testTermite(); #endif ``` ## testtermite.cpp ```cpp #include <iostream> #include "termite.hpp" using namespace std; #define ASSERT(test) if (!(test)) cout << "Test failed in file " << __FILE__ \ << " line " << __LINE__ << ": " #test << endl void testTermite(){ Termite tab[tailleTabIdT]; initialiseTabIdVide(tab); creeTermite(creeCoord(0, 0), tab, 2); Grille g; initialiseGrilleVide(g); poseBrindille(g, {LON-1, 1}); poseTermite(g, {0, 0}, 2); Termite t = tab[0]; t.d = E; ASSERT (egalCoord(devantTermite(t), creeCoord(1, 0))); for (int i=1; i<LON; i++){ avanceTermite(g, t); } afficheGrille(g, tab); ASSERT (!(dansGrille(g, devantTermite(t)))); ASSERT (!(laVoieEstLibre(g, t))); ASSERT (!(brindilleEnFace(g, t))); ASSERT (!(porteBrindille(t))); tourneADroite(t); tourneADroite(t); ASSERT (dansGrille(g, devantTermite(t))); ASSERT (!(laVoieEstLibre(g, t))); ASSERT (brindilleEnFace(g, t)); ASSERT (pasEnferme(g, t)); chargeTermite(g, t); ASSERT (porteBrindille(t)); ASSERT (laVoieEstLibre(g, t)); ASSERT (!(brindilleEnFace(g, t))); poseBrindille(g, {LON-1, 1}); poseBrindille(g, {LON-2, 1}); tourneADroite(t); tourneADroite(t); ASSERT (laVoieEstLibre(g, t)); ASSERT (!(pasEnferme(g, t))); tourneAGauche(t); ASSERT (!(laVoieEstLibre(g, t))); ASSERT (brindilleEnFace(g, t)); enleveBrindille(g, {LON-2, 1}); creeTermite(creeCoord(LON-2, 1), tab, 22); poseTermite(g, creeCoord(LON-2, 1), 22); Termite t2 = tab[1]; t2.d = t.d; tourneAGauche(t);tourneAGauche(t);tourneAGauche(t);tourneAGauche(t); tourneAGauche(t);tourneAGauche(t);tourneAGauche(t);tourneAGauche(t); ASSERT (directionTermite(t)==directionTermite(t2)); ASSERT (!(laVoieEstLibre(g, t))); tourneAleat(t2); ASSERT (!(directionTermite(t)==directionTermite(t2))); marcheAleatoire(g, t2); ASSERT (laVoieEstLibre(g, t)); dechargeTermite(g, t); ASSERT (!(pasEnferme(g, t))); tourneADroite(t); marcheAleatoire(g, t); ASSERT (pasEnferme(g, t)); poseBrindille(g, creeCoord(LON-3, 0)); ASSERT (!(pasEnferme(g, t))); Coord c1 = devantTermite(t); Coord c2 = t.c; marcheAleatoire(g, t); ASSERT (!(egalCoord(c1, devantTermite(t)))); ASSERT (egalCoord(c2, t.c)); } int main(){ testTermite(); return 0; } ``` ## testtermite.cpp modifié ```cpp #include <iostream> #include "termite.hpp" using namespace std; #define ASSERT(test) if (!(test)) cout << "Test failed in file " << __FILE__ \ << " line " << __LINE__ << ": " #test << endl #define RED "\033[31m" #define WHITE "\033[0m" short t=0; void afficheCase(Grille g, Termite (&tab)[tailleTabIdT], int i, int j){ Case c = g.tab[i][j]; if (estVide(g, {i, j})){ cout << " "; } else if (c.brindille){ cout << "*"; } else { if (tab[t].brin){ cout << RED << afficheDirectionT(tab[t].d) << WHITE; } else { cout << afficheDirectionT(tab[t].d); } t++; } } void afficheGrille(Grille g, Termite (&tab)[tailleTabIdT]){ t=0; for (int j=0; j<LAR; j++){ for (int i=0; i<LON; i++){ afficheCase(g, tab, i, j); cout << " "; } cout << endl; } } void testTermite(){ Termite tab[tailleTabIdT]; initialiseTabIdVide(tab); creeTermite(creeCoord(0, 0), tab, 0); Grille g; initialiseGrilleVide(g); poseBrindille(g, {X-1, 1}); poseTermite(g, {0, 0}, 0); // afficheGrille(g, tab); tab[0].d = E; // afficheGrille(g, tab); ASSERT (egalCoord(devantTermite(tab[0]), creeCoord(1, 0))); for (int i=1; i<X; i++){ avanceTermite(g, tab[0]); } // afficheGrille(g, tab); ASSERT (!(dansGrille(g, devantTermite(tab[0])))); ASSERT (!(laVoieEstLibre(g, tab[0]))); ASSERT (!(brindilleEnFace(g, tab[0]))); ASSERT (!(porteBrindille(tab[0]))); tourneADroite(tab[0]); tourneADroite(tab[0]); // afficheGrille(g, tab); ASSERT (dansGrille(g, devantTermite(tab[0]))); ASSERT (!(laVoieEstLibre(g, tab[0]))); ASSERT (brindilleEnFace(g, tab[0])); ASSERT (pasEnferme(g, tab[0])); chargeTermite(g, tab[0]); // afficheGrille(g, tab); ASSERT (porteBrindille(tab[0])); ASSERT (laVoieEstLibre(g, tab[0])); ASSERT (!(brindilleEnFace(g, tab[0]))); poseBrindille(g, {X-1, 1}); poseBrindille(g, {X-2, 1}); tourneADroite(tab[0]); tourneADroite(tab[0]); // afficheGrille(g, tab); ASSERT (laVoieEstLibre(g, tab[0])); ASSERT (!(pasEnferme(g, tab[0]))); tourneAGauche(tab[0]); ASSERT (!(laVoieEstLibre(g, tab[0]))); ASSERT (brindilleEnFace(g, tab[0])); // afficheGrille(g, tab); enleveBrindille(g, {X-2, 1}); creeTermite(creeCoord(X-2, 1), tab, 1); poseTermite(g, creeCoord(X-2, 1), 1); tab[1].d = tab[0].d; // afficheGrille(g, tab); tourneAGauche(tab[0]);tourneAGauche(tab[0]);tourneAGauche(tab[0]);tourneAGauche(tab[0]); tourneAGauche(tab[0]);tourneAGauche(tab[0]);tourneAGauche(tab[0]);tourneAGauche(tab[0]); ASSERT (directionTermite(tab[0])==directionTermite(tab[1])); ASSERT (!(laVoieEstLibre(g, tab[0]))); tourneAleat(tab[1]); // afficheGrille(g, tab); ASSERT (!(directionTermite(tab[0])==directionTermite(tab[1]))); marcheAleatoire(g, tab[1]); ASSERT (laVoieEstLibre(g, tab[0])); // afficheGrille(g, tab); tab[0].sablier = 0; dechargeTermite(g, tab[0]); // afficheGrille(g, tab); ASSERT (!(pasEnferme(g, tab[0]))); tourneADroite(tab[0]); // afficheGrille(g, tab); marcheAleatoire(g, tab[0]); // afficheGrille(g, tab); ASSERT (pasEnferme(g, tab[0])); ASSERT (!(pasEnferme(g, tab[0]))); // afficheGrille(g, tab); Coord c1 = devantTermite(tab[0]); Coord c2 = tab[0].c; marcheAleatoire(g, tab[0]); // afficheGrille(g, tab); ASSERT (!(egalCoord(c1, devantTermite(tab[0])))); ASSERT (egalCoord(c2, tab[0].c)); } int main(){ testTermite(); cout << X << " " << Y << endl; return 0; } ``` ```cpp #include <iostream> //#include <SFML/Graphics.hpp> #include "termite.hpp" #define RED "\033[1;31m" #define WHITE "\033[1;0m" #define YELLOW "\033[1;33m" #define GREEN "\033[1;32m" using namespace std; void initialiseRand(Grille &g, Termite (&tab)[tailleTabIdT], short &nbTermite, short &nbBrindille){ initialiseGrilleVide(g); initialiseTabIdVide(tab); nbTermite = 0; nbBrindille = 0; for (int j=0; j < Y; j++){ for (int i=0; i < X; i++){ short ranNum = rand()%100 + 1; Coord c = creeCoord(i, j); if(ranNum <= proba_b){ poseBrindille(g, c); } else if (ranNum > 100-proba_t){ creeTermite(c, tab, nbTermite); poseTermite(g, c, nbTermite); nbTermite++; } } } } void afficheCase(Grille g, Termite (tab)[tailleTabIdT], int i, int j){ Case c = g.tab[i][j]; if (estVide(g, {i, j})){ cout << " "; } else if (c.brindille == 1){ cout << YELLOW << "*" << WHITE; } else { if (tab[c.idT].brin){ cout << RED << afficheDirectionT(tab[c.idT].d) << WHITE; } else { cout << GREEN <<afficheDirectionT(tab[c.idT].d) << WHITE; } } } void afficheGrille(Grille g, Termite (tab)[tailleTabIdT]){ cout << "@"; for(int i=0; i < X; i++){ cout << WHITE << "--" ; } cout << "@" << endl; for (int j=0; j<Y; j++){ cout << "|"; for (int i=0; i<X; i++){ afficheCase(g, tab, i, j); cout << " "; } cout << "|" << endl; } cout << "@"; for(int i=0; i < X; i++){ cout << WHITE << "--" ; } cout << "@" << endl; } void sim(Grille &g, Termite (&tab)[tailleTabIdT], short nbTermite){ for (int i = 0; i < nbTermite; i++){ tab[i].sablier--; if (chargePossible(g, tab[i])){ chargeTermite(g, tab[i]); } else if (pretADecharger(g, tab[i])){ tourneADroite(tab[i]); tab[i].tourneSurPlace = 1; } else if (dechargePossible(g, tab[i])){ dechargeTermite(g, tab[i]); tab[i].tourneSurPlace = 0; } else if (tab[i].tourneSurPlace){ tourneAleat(tab[i]); } else { marcheAleatoire(g, tab[i]); } } } int main(){ unsigned long iter = 1; unsigned short m = 1; //iteration, multiplicateur srand (time(NULL)); char c; short nbTermite = 0; short nbBrindille = 0; Grille g; Termite tabIdT[tailleTabIdT]; initialiseRand(g, tabIdT, nbTermite, nbBrindille); while(1){ cout << "Nombre d'iterations : " << iter << endl; cout << "Vitesse : " << m << endl; cout << "nbTermite : " << nbTermite << endl; for(int k=0; k != m; k++){ sim(g, tabIdT, nbTermite); } afficheGrille(g, tabIdT); cout << "d : "<< tabIdT[0].d << endl; cout << "c : "; afficheCoord(tabIdT[0].c); c = getchar(); if(c == '*'){ m *= 2; } else if(c == '/'){ m /= 2; if(m == 0) m = 1; } else if(c == '.'){ break; } iter += m; system("clear"); } cout << "Vous avez mis fin a la simulation." << endl; return 0; } ``` des choses : ҉ ҈ ֎ ֍ ֻ ꙰ ꙮ ῞ ΅ ᷾ \ֱ poseBrindille(g, creeCoord(LON-3, 0)); // afficheGrille(g, tab);

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully