1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NB_MAX 10000
typedef int tableau[NB_MAX];
struct tirage{
int resultat[NB_MAX];
int blancs;
int noirs;
};
typedef struct tirage tirage;
void usage (char *s){
printf("Usage : %s <entier1 infrieur 100> <n entiers inf entier1> \n",s);
exit(-1);
}
char pluriel(int n){
if ((n == 1) || (n == 0))
return ' ';
else
return 's';
}
/*compte le nombre de noirs et le nombre de blancs pour chaque proposition*/
void comptage(int code[NB_MAX],tirage tableau,int nb_pions){
int temp1[NB_MAX];
int temp2[NB_MAX];
int i,k,j; /*nb_pions correspondra argc-2*/
int blanc = 0;
int noir = 0;
for (i=0; i<(nb_pions) ; i++){
temp1[i] = code[i]; /*on met le code dans temp1*/
temp2[i] = tableau.resultat[i]; /*on met la proposition dans temp2*/
}
for (k=0; k < (nb_pions);k++) /*on compte les noirs*/
if(temp1[k] == temp2[k]){
noir++;
temp1[k] = 0;
temp2[k] = 0;
}
for (k=0; k < (nb_pions);k++) /*on compte les blancs*/
if(temp2[k] != 0){
j=0;
while((j<nb_pions) && (temp2[k] != temp1[j]))
j++;
if(j != nb_pions){
blanc++;
temp1[k] = 0;
temp2[k] = 0;
}
}
tableau.blancs = blanc;
tableau.noirs = noir;
}
/*donne la ligne suivante sur le modle d'un compteur de voiture*/
void suivante(int prop[NB_MAX],int nb_pions,int nb_coul){
int i;
i = 0;
while((prop[i] == nb_coul) && (i < nb_pions)){
prop[i] = 1;
i++;
}
prop[i]++ ;
}
/*teste si la ligne propose est cohrente par rapport aux propositions prcdentes return 1 si est cohrente et 0 sinon*/
int est_coherente(tirage tab[NB_MAX],int nb_pions,int nb_coul,int indice){
int i, bool;
tirage temp; /*on definit une structure temporaire*/
for (i=0;i<nb_pions;i++)
temp.resultat[i] = tab[indice].resultat[i];
i=(indice -1);
bool = 1;
while((i >= 0) && (bool == 1)){
comptage( tab[i].resultat, temp, nb_pions);
if((temp.noirs != tab[i].noirs) || (temp.blancs != tab[i].blancs))
bool = 0;
i--;
}
if (i<0)
return 1 ;
else
return 0;
}
void affichage(int nb_pions, tirage t){
int i,n,b;
printf("Je vous propose : ");
for(i=0;i<nb_pions;i++){
printf(" %d ",tirage.t[i])
}
n=t.noirs;
b=t.blancs;
printf("\tReponse: %d noir%c",n,pluriel(n));
printf(" %d blanc%c \n",b,pluriel(b));
}
int main (int argc, char *argv[]){
int nb_coul,i;
int blanc,noir,nb_pions,essais,a,couleurs,j;
/*on dcode la ligne de commande*/
if (argc<3)
usage(argv[0]);
nb_coul = atoi(argv[1]);
if ((nb_coul <= 0) || (nb_coul >= 100))
usage(argv[0]);
for(i=2;i<argc;i++){
a = atoi(argv[i]);
if ((a > nb_coul) || (a<1))
usage(argv[0]);
exit(-1);
}
exit(0);
} |
Partager