Voiçi un projet (assez complet) pour ce programme d'évaluation de fitness. Il manque :
switch..case n'a pas de default, est-ce grave?#include <stdio>
#include <iostreams>
#include <vector>
#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( <std::vector <std::vector < std::vector<int> > > > 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;
// <std::vector <std::vector < std::vector<int> > > > jardins
<std::vector <std::vector<int> > jardinCourant;
std::vector<int> arbreCourant;
// pour chaque jardin
/* niveaux de gris :
* arbre = 0
* herbe non coupé = 182
* herbe coupé = 54
* tondeuse = 19
*/
for (k=0;k<jardins.size();k++){
jardinCourant = jardins[k];
for (i=0;i<1024;i++){
for (j=0;j<768,j++){
map[i][j]= HIGHGRASS_COLOR;
}
}
nbArbres = jardinsCourant.size() - 1; // dernier objet est la tondeuse
for (a=0;a<nbArbres-1;a++){
arbreCourant = jardinCourant[a]
x = arbreCourant[0];
y = arbreCourant[1];
for (i=x;i<x+cote+1;i++){
for (j=y;j<y+cote+1;j++){
map[i][j]=TREE_COLOR;
}
}
} // fin for a
arbreCourant = jardinCourant[nbArbres-1]
x = arbreCourant[0];
y = arbreCourant[1];
xOtto = x;
yOtto = y;
for (i=x;i<x+cote+1;i++){
for (j=y;j<y+cote+1;j++){
map[i][j]=CUTGRASS_COLOR;
cumulHerbe++;
}
}
/*
* le terrain est initialisé avec herbe longue, arbres, tondeuse ayant coupé au point de départ
* tondeuse est du coin (xOtto,yOtto) jusqu'au coin (xOtto+cote,yOtto+cote)
*/
/*
* maintenant, faut faire les 100,000 pas !
*/
ottoEtat = 0; //initialisation
xPas=0;
yPas=0;
boolArbre=false;
boolMur = false;
boolVide = false;
// faut-il vérifier ces booléennes au départ? probablement pas, il n'a pas encore d'orientation
for (p=0;p<maxPas;p++){
/*
* chercher action (mouvement plus état suivant) dans otto
* - cinq cas de mouvement précédent (voir après) mais faisons plus générale
* pour pouvoir changer le nombre d'actions facilement
* on utilise un tableau pour stocker un arbre binaire (sauf les feuilles ne sont pas binaires, mais x5)
* si pas d'arbre, moitié gauche : indices de 0 à cas par etat /2
* si pas de perimetre (mur) : moitié gauche de la moitié "choisie" par arbre -> +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;
}