/******************************************************************* Ce code n'a pas ete commente car il a ete donne en examen http://www-sop.inria.fr/oasis/personnel/Carine.Courbis/c/ *********************************************************************/ #include #include #include #define MAX_CODE 4 #define NB_COULEUR 8 enum {DIFFICILE, FACILE = 4}; typedef enum {FALSE, TRUE} Boolean; typedef enum {ROUGE, VERT, BLEU, JAUNE, MARRON, GRIS, FUSHIA, LAVANDE} Couleur; typedef enum {JUSTE, COULEUR_OK, RIEN} Resultat; typedef struct { Couleur code[MAX_CODE]; Resultat resultat[MAX_CODE]; } Tentative; #define MAX_TENTATIVES 9 void initMasterMind(Couleur code[]) { int i, graine; printf("Donner un nombre de depart : "); graine = getc(stdin); fflush(stdin); srand(graine); for (i=0; i 1) { printf("Precedentes Tentatives\n"); for (i=0; i= 0) { tentatives[nbTentatives-1].code[i++] = couleur; } else { i = 0; printf("%c n'est pas une couleur. Recommencez la saisie des 4 pions\n", car); } } fflush(stdin); } void compareCodes(Couleur secret[], Tentative * tentative) { Boolean utilise[MAX_CODE]; int i, j; for(i=0; icode[i]) { tentative->resultat[i] = JUSTE; utilise[i] = TRUE; } else { j =0; while (jcode[i] == secret[j]) && (utilise[j] == FALSE) && (tentative->code[j] != secret[j])) { utilise[j] = TRUE; tentative->resultat[i] = COULEUR_OK; break; } j++; } if (j >= MAX_CODE) tentative->resultat[i] = RIEN; } } } void main(int argc, char * argv[]) { int i; int nbTentatives = 1; int niveau = DIFFICILE; Couleur code[MAX_CODE]; Tentative tentatives[MAX_TENTATIVES]; Boolean aGagne = FALSE; if (argc > 1) { if (strcmp(argv[1], "f") == 0) niveau = FACILE; } initMasterMind(code); do { saisieTentative(tentatives, nbTentatives, niveau); compareCodes(code, &(tentatives[nbTentatives-1])); aGagne = afficheResultat(tentatives[nbTentatives-1].resultat, niveau); nbTentatives++; } while ((nbTentatives <= MAX_TENTATIVES) && (!aGagne)); if (!aGagne) { printf("Perdu!!!!"); printf("Voila la combinaison gagnante : "); for(i=0; i