Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/ecole_etude_fac_de_pau/licence_2/poo/jeu.cc

(Deskargatu)
/* tp 2
 * Jeu ?
 *
 * Utilisation de :
 *    - tableau de cartes pour les solutions
 *    - tableau de cartes retournees pour les reponses
 *******************************************************/


#include <iostream.h>




void afficher_jeu(char *tab,int n)
{

    int i;
    cout << "\e[0;0m\e[K\t\t";
    for (i=1;i<n+1;i++)
    {
        cout << i << "\t";
    }
    cout << "\n\e[32;5m\e[K\t\t";    

    for (i=0;i<n;i++)
    {
        cout << tab[i] << "\t";
    }
    cout << "\e[0;0m\n";
}

void effacer()
{
      cout << "\e[2J";
}


void creer_jeu(char *tab,int n)
{
    char c='a';
    for (int i=0;i<n;i++)
    {
    tab[i] = c;
    tab[i+1] = c;
    i=i+1;
    c=c+1;
    }
}

void random_jeu(char *tab,int n)
{
    int pos;
    char temp;
    for (int i=0;i<n;i++)
    {
	pos = rand()%n;
	cout << pos;
	temp = tab[i];
	tab[i] = tab[pos];
	tab[pos] = temp;
	//	cout << endl;
	//	cout << endl;
	//	cout << tab[i];
    }
    
    
}

void creer_reponses(char *tab,int n)
{
    for (int i=0;i<n;i++)
    {
	tab[i] = 'X';
    }
}


int demander(char *reponses)
{
    int pos;
    do
    {
    cout << "\e[3AEntrer le numero de la carte a retourner : ";
    cin >> pos;
    }
    while (reponses[pos-1] != 'X' );
    return pos;
}

void retourner(char *cartes, char *reponses, int n)
{
    reponses[n-1] = cartes[n-1];
}


int verifier(char *cartes,char *reponses,int a,int b)
{
    if (cartes[a-1]==cartes[b-1])
    {
	reponses[a-1] = ' ';
	reponses[b-1]= ' ';
    }
    else {
	reponses[a-1] = 'X';
	reponses[b-1]= 'X';
    }    
}

int fin_jeu(char *reponses,int n)
{
    int ok=1;
    for (int i=0;i<n;i++)
    {
	if (reponses[i]	!= ' ')
	{
	    ok = 0;
	}
    }
    return ok;
}



int main(void)
{
    char *cartes;
    char *reponses;
    int nblm;
    int carte_a;
    int carte_b;
    int k=0;
    
    cout << "Entrez le nombre de paires de cartes a trouver : ";
    cin >> nblm;
    nblm*=2;
    
    cartes = new char[nblm];
    reponses = new char[nblm];


    
    creer_jeu(cartes,nblm);
    random_jeu(cartes,nblm);
    creer_reponses(reponses,nblm);
    
    effacer();        
    afficher_jeu(reponses,nblm);        
    
    for (k=1;! fin_jeu(reponses,nblm);k++)    
    {
    carte_a = demander(reponses);
    retourner(cartes,reponses,carte_a);
    afficher_jeu(reponses,nblm);        

    carte_b = demander(reponses);
    retourner(cartes,reponses,carte_b);

    afficher_jeu(reponses,nblm);        


    verifier(cartes,reponses,carte_a,carte_b);

//    afficher_jeu(reponses,nblm);            
    }
    effacer();
    afficher_jeu(cartes,nblm);
    cout << "\n\nVous avez gagne en " << (k) << " essais !\n\n";

}