===== casesTondues.cpp ===== Voiçi un projet (assez complet) pour ce programme d'évaluation de fitness. Il manque : * specification de otto, un automate : vraisemblablement un tableau int [280][3], mais peut être pareil que jardin : > > * le ''switch..case'' n'a pas de ''default'', est-ce grave? [[project_track|retour page principale]] ---- #include #include #include #define TREE_COLOR 0 #define HIGHGRASS_COLOR 182 #define CUTGRASS_COLOR 54 #define MOWER_COLOR 19 #define MAP_WIDTH 1024 #define MAP_HEIGHT 768 using namespace std; /* * jardins est une liste de jardins, chaque jardin étant une liste * d'obstacles (signifiés par le coin mx-min, y-min), le dernier obstacle est pris pour une tondeuse... pas tres agile mais on est pris par le temps * otto est une AES(FSM) ayant nbEtats * casparEtat réactions */ int casesTondues( > > > jardins, tbd2 otto){ unsigned char map [1024][768]; int cote = 9; int i, j, k, x, y, a; int ottoEtat, xPas, yPas, xOtto, yOtto; int nbArbres; int maxPas = 100000; int casparEtat = 40; int nbEtats = 7; int compteHerbe; int cumulHerbe; int minHerbe = 5; bool boolArbre=false; bool boolMur=false; bool boolVide = false; // > > > jardins > jardinCourant; std::vector arbreCourant; // pour chaque jardin /* niveaux de gris : * arbre = 0 * herbe non coupé = 182 * herbe coupé = 54 * tondeuse = 19 */ for (k=0;k +0 ou +(cas par etat/4 * si peu d'herbe coupée, moitié droite du quart choisi par arbre et perimetre : +(cas par état)/8 ou + 0 */ if (boolArbre) idx = casparEtat/2; // on commence par chercher arbre ou pas if (boolMur) idx = idx + casparEtat/4; // on affine si pres du bord ou pas if (boolVide) idx = idx + casparEtat/8; /* * on finit en tenant compte du dernier mouvement * (-1, 0) -> 1 * (0, -1) -> 2 * (0, 0) -> 3 (2*(0+1)+1*(0+1)) * (0, 1) -> 4 (2*(0+1)+1*(1+1)) * (1, 0) -> 5 (2*(1+1)+1*(0+1)) */ idx = (xPas+1)*2+yPas+1; /* * idx indique maintenant l'indice dans la table d'un état, * il faut ajuster pour prendre dans le bon état (et pas toujours état 0) */ idx = idx + ottoEtat * casparEtat; xPas = otto[idx][0]; yPas = otto[idx][1]; // mettre à jour état de otto ottoEtat = (ottoEtat + otto[idx][2]) % nbEtats; // verifier que ce mouvement est possible : casChoisi=((xPas+1)*2+yPas+1); boolArbre = false; boolMur = false; boolVide = false; compteHerbe = 0; switch (casChoisi){ case 1: // (-1,0) : verifier pour (x-1,y) et (x-1, y+cote) if ((xOtto < 1)||(map[xOtto-1][yOtto]==TREE_COLOR)||(map[xOtto-1][yOtto+cote]==TREE_COLOR)) break; xOtto=xOtto+xPas; if (xOtto==0) boolMur = true; if ((map[xOtto-1][yOtto]==TREE_COLOR)||(map[xOtto-1][yOtto+cote]==TREE_COLOR)) boolArbre=true; for (j = yOtto; j < yOtto + cote; j++) { if(map[xOtto][j]==HIGHGRASS_COLOR){ compteHerbe++; cumulHerbe++; map[xOtto][j]=CUTGRASS_COLOR; } } break; case 2: // (0,-1) : verifier pour (x,y-1) et (x+cote, y-1) if ((yOtto<1)||(map[xOtto][yOtto-1]==TREE_COLOR)||(map[xOtto+cote][yOtto-1]==TREE_COLOR)) break; yOtto=yOtto+yPas; if (yOtto==0) boolMur = true; if ((map[xOtto][yOtto-1]==TREE_COLOR)||(map[xOtto+cote][yOtto-1]==TREE_COLOR)) boolArbre = true; for (i=xOtto; i < xOtto + cote; i++) { if(map[i][yOtto]==HIGHGRASS_COLOR){ compteHerbe++; cumulHerbe++; map[i][yOtto]=CUTGRASS_COLOR; } } break; case 3: break; // (0, 0) : rien verifier case 4: // (0, 1) : verifier pour (x, y+1) et (x+cote, y+1) if ((y+1>=MAP_HEIGHT)||(map[x][y+cote+1]==TREE_COLOR)||(map[x+cote][y+cote+1]==TREE_COLOR)) break; yOtto=yOtto+yPas; if (y+1>=MAP_HEIGHT) boolMur = true; if ((map[x][y+cote+1]==TREE_COLOR)||(map[x+cote][y+cote+1]==TREE_COLOR)) boolArbre = true; for (i=xOtto; i < xOtto + cote; i++) { if(map[i][yOtto+cote]==HIGHGRASS_COLOR){ compteHerbe++; cumulHerbe++; map[i][yOtto+cote]=CUTGRASS_COLOR; } } break; case 5: // (1, 0) : verifier pour (x+1, y) et (x+1, y+cote) if ((x+1>=MAP_WIDTH)||(map[x+cote+1][y]==TREE_COLOR)||(map[x+cote+1][y+cote]==TREE_COLOR)) break; xOtto=xOtto+xPas; if (x+1>=MAP_WIDTH) boolMur = true; if ((map[x+cote+1][y]==TREE_COLOR)||(map[x+cote+1][y+cote]==TREE_COLOR)) boolArbre = true; for (j = yOtto; j < yOtto+cote; j++) { if(map[xOtto+cote][j]==HIGHGRASS_COLOR){ compteHerbe++; cumulHerbe++; map[xOtto+cote][j]=CUTGRASS_COLOR; } } } // fin switch if (compteHerbe < minHerbe) boolVide = true; } // fin for p (pas) } // fin for k return cumulHerbe; }