// TD Semaine 3

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>

/*
void badPermut(int a, int b) {
	a += b;
	b = a - b;
	a -= b;
	printf("Dans badPermut apres permutation a = %i b = %i\n", a, b);
}

typedef struct { int x; int y; } DeuxInt;

DeuxInt permutation(int a, int b) {
	DeuxInt d;
	d.x = b;
	d.y = a;
	return d;
}

unsigned int badCompteur() {
	unsigned int cpt = 0;
	++cpt;
	return cpt;
}

unsigned int factorielle(unsigned int n) {
	unsigned int i=0;		// i varie de 0 à n
	unsigned int f=1;		// f == i! 

	while (i !=n) { 
		++i;					
		f *= i;				// pour qu'on ait toujours f == i!
	}
	return f;
}

unsigned int division (unsigned int A, unsigned int B) {
// soient a,b les valeurs initiales de A et B
// on a toujours : D*b +A == a
// et : B == b
	unsigned int D =0;		// 0*b +A == a
	assert(B >0 && A>=0);
	while (A >= B) { // en entrant on a D*b +A == a
		A = A -B;	 // D*b + (A -b) 
		D = D +1;	// (D+1)*b + (A -b) == D*b +A == a
	}
	return D;		
}

unsigned int choisirNombre( unsigned int max) {
	srand((unsigned)time(NULL));
	return rand()%max +1;
}

unsigned int proposerNombre(unsigned int max, int comparaison) {
	return rand()%max +1;
}

int comparer (unsigned int a, unsigned int b) {
	return a - b;
}

unsigned int devinerNombre(unsigned int max) {
	unsigned int x = choisirNombre(max);
	unsigned int y;
	unsigned int cpt =0;
	int comparaison = 0;
	do { 
		y = proposerNombre(max, comparaison);
		comparaison = comparer(y, x);
		++ cpt;
	} while (comparaison != 0);
	return cpt;
}

int main() {
{	int x = 3, y = 5;
	printf("Avant badPermut x = %i y = %i\n", x, y);
	badPermut(x, y);
	printf("Apres badPermut x = %i y = %i\n", x, y);
}
	system("pause");
	system("cls");

{	int x = 3, y = 5;
	DeuxInt di= permutation(x,y);
	x = di.x;
	y = di.y;
	printf("Apres permutation x = %i y = %i\n", x, y);
}
	system("pause");
	system("cls");

{	unsigned int x;
	x= 	badCompteur();
	x = badCompteur();
	printf("x = %i\n", x);
}
	system("pause");
	system("cls");

	printf("0! = %u\n", factorielle(0));
	printf("1! = %u\n", factorielle(1));
	printf("3! = %u\n", factorielle(3));
	printf("5! = %u\n", factorielle(5));

	system("pause");
	system("cls");

	printf("21/4 = %u\n", division(21u, 4u));
	printf("4/21 = %u\n", division(4, 21u));
	printf("8/1 = %u\n", division(8u, 1u));
	printf("7/7 = %u\n", division(7u, 7u));
	printf("12000/1000 = %u\n", division(12000u, 1000u));

	system("pause");
	system("cls");
{
	unsigned int max = 10;
	printf("Nombre < %u decouvert en %u coups.\n", max, devinerNombre(max));
}
	system("pause");
	return 0;
}
*/

// Commencer par la version la plus simple : Ordinateur contre Humain
// la valeur à deviner est choisie aléatoirement par l’ordinateur 
// et la recherche du nombre se fait par lecture au clavier
unsigned int choisirNombre( unsigned int max) { //nombre inférieur à max
srand((unsigned)time(NULL));	//ici car la fonction est appelée une seule
 						//fois dans le programme
	return rand()%max +1;
}

unsigned int proposerNombre() {
	// l’humain propose un nombre par saisie au clavier
unsigned int n ;
printf("\nEntrez un nombre : ") ; 
scanf("%d", &n) ;
return n ;
}

unsigned int devinerNombre(unsigned int max) {
	unsigned int x = choisirNombre(max);
	unsigned int y;
	unsigned int cpt =0;
	int comparaison = 0;
	do { 
		y = proposerNombre(max, comparaison);
		comparaison = comparer(y, x);
		++ cpt;
	} while (comparaison != 0);
	return cpt;
}

int main() {
	// Match ordinateur contre humain
	unsigned int max = 10; // limite du nombre nb à trouver : nb<max
	unsigned int nbCoups=0; // nombre de coups pour la découverte du nombre
	unsigned int n ;
	unsigned int theNombre=choisirNombre(max) ; //Le nombre à trouver
printf("Le nombre à trouver est inférieur à %u.\n", max);

	do { 
		n = proposerNombre();
		++ nbCoups;
		// Réponse de l’ordinateur
	if (n<theNombre) 
printf("Le nombre à trouver est plus grand.\n", max);
	if (n>theNombre) 
printf("Le nombre à trouver est plus petit.\n", max);
	} while (n != theNombre);

printf("Nombre %u decouvert en %u coups.\n", theNombre, nbCoups);
	system("pause");
	return 0;
}

