begin process at 2013 05 21 22:21:39
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Jeux

 > PROGRAMME DE JEU DE MPT

PROGRAMME DE JEU DE MPT


 Information sur la source

 Description

Tout vient de moi, et vous pouvez vous en servir comme bon vous le semble pour vos programmes. Fonction de calcul de moyenne, ouverture de fichier..

Source

  • #include <iostream>
  • #include <string>
  • #include <fstream>
  • #include <stdlib.h>
  • #include <cmath>
  • #include <vector>
  • #include <ctime>
  • #include <cstdlib>
  • #include <time.h>
  • using namespace std;
  • void statDeLaPartie(int decompteur)
  • {
  • ofstream statFlux("bla/stat.txt", ios::app);
  • statFlux << decompteur * 4 << endl;
  • }
  • void afficherMoyenne ()
  • {
  • double moyenne(0);
  • int b;
  • vector <int> notes;
  • ifstream fichier("bla/stat.txt");
  • string line;
  • if(fichier)
  • {
  • while(getline(fichier, line))
  • {
  • b = atof(line.c_str());
  • notes.push_back(b);
  • }
  • }
  • else
  • {
  • cout << "impossible d'ouvrir le fichier " << endl;
  • }
  • for (int i(0); notes.size() > i; i++)
  • {
  • moyenne += notes[i];
  • }
  • moyenne /= notes.size();
  • cout << "Votre moyenne est de:" << moyenne << endl << endl;
  • }
  • void motIndice (string motChoisi)
  • {
  • for (int indice(0); indice != 3; indice++)
  • {
  • cout << motChoisi[indice];
  • }
  • }
  • unsigned int nbAleatoire(int range)
  • {
  • unsigned int aleatoire = (rand() & 0xFFFF) | ((rand() & 0xFFFF) << 16);
  • if (range == 0)
  • {
  • range = 2;
  • }
  • return aleatoire % range;
  • }
  • string choixMot(vector <string> &dico)
  • {
  • string motDict;
  • if (dico.size() != 0)
  • {
  • motDict = dico[nbAleatoire(dico.size())];
  • }
  • else if (dico.size() == 0)
  • {
  • motDict = "AUCUN";
  • }
  • return motDict;
  • }
  • string melanger(string mot)
  • {
  • string melange;
  • int position(0);
  • while (mot.size() != 0)
  • {
  • position = rand() % mot.size();
  • melange += mot[position];
  • mot.erase(position, 1);
  • }
  • return melange;
  • }
  • vector <string> dictionnaire(string nomFichier)
  • {
  • ifstream fichier(nomFichier.c_str());
  • vector <string> dico;
  • string line;
  • cout << "Chargement du dictionnaire..." << endl;
  • if(fichier)
  • {
  • while(getline(fichier, line)) dico.push_back(line);
  • return dico;
  • }
  • else cout << "impossible d'ouvrir le fichier " << nomFichier << endl;
  • }
  • int main()
  • {
  • srand(time(NULL));
  • bool quitter(1), continuer(1), continuerDuDico(1);
  • int choixMenuPrincip(0), choixMenuDic(0), choixMenuStat(0),
  • choixMenuDuDico(0), choixMenuDuDico2(0), decompteur(5);
  • string const dicoFr("personnalise.txt"), dicoFr2("francais.txt");
  • string motEntreDicoFr, dictionnaireChoisi("personnalise.txt"), motChoisi, motMelanger, motPense;
  • vector <string> dico;
  • do
  • {
  • cout << " /----------------------------!" << endl;
  • cout << "! BIEVENUE DANS LE !" << endl;
  • cout << "! PROGRAMMES D'ENTRAINEMENT !" << endl;
  • cout << "! DES LANGUES DE !" << endl;
  • cout << "! !" << endl;
  • cout << "! !" << endl;
  • cout << "! KERIZ !" << endl;
  • cout << "!___________________________/ " << endl << endl;
  • do
  • {
  • cout << "Que voulez vous faire ?" << endl;
  • cout << "1.Entrer des mots dans le dictionnaire" << endl;
  • cout << "2.Choix du dictionnaire" << endl;
  • cout << "3.Statistiques" << endl;
  • cout << "4.Commencer une partie" << endl;
  • cout << "5.Quittez le jeu" << endl << endl;
  • cout << "Votre choix ? ";
  • cin >> choixMenuPrincip;
  • }
  • while (choixMenuPrincip >= 6 || choixMenuPrincip == 0);
  • switch (choixMenuPrincip)
  • {
  • // DEBUT CASE 1 //Case 1 fini
  • case 1:
  • {
  • system("cls");
  • do
  • {
  • cout << "Entrez le mot en francais: " << endl;
  • ofstream dicoFrancais(dicoFr.c_str(), ios::app); //ios::app
  • cin.get();
  • getline(cin, motEntreDicoFr);
  • if (dicoFrancais)
  • {
  • dicoFrancais << motEntreDicoFr << endl;
  • }
  • else
  • {
  • cout << "ERREUR DANS LA CREATION DU FICHIER " << dicoFr << endl;
  • }
  • do
  • {
  • cout << endl << "Que voulez vous faire ?" << endl;
  • cout << "1.Continuer a entrer des mots" << endl;
  • cout << "2.Remettre le dictionnaire a 0" << endl;
  • cout << "3.Retourner au menu" << endl << endl;
  • cout << "Votre choix ? ";
  • cin >> choixMenuDic;
  • }
  • while (choixMenuDic >= 4 || choixMenuDic == 0);
  • switch (choixMenuDic)
  • {
  • case 1:
  • {
  • continuer = 1;
  • break;
  • }
  • case 2:
  • {
  • ofstream dicoFrancais(dicoFr.c_str());
  • continuer = 0;
  • break;
  • }
  • case 3:
  • {
  • continuer = 0;
  • break;
  • }
  • }
  • }
  • while (continuer);
  • system("cls");
  • break;
  • }
  • //CASE 2
  • case 2:
  • {
  • system("cls");
  • do
  • {
  • cout << "Que voulez vous faire ?" << endl;
  • cout << "1.Regarder les mots de mon dictionnaire" << endl;
  • cout << "2.Choisir le dictionnaire avec lequel je joue" << endl;
  • cout << "3.Retourner au menu"<< endl << endl;
  • cout << "Votre choix: " << endl;
  • cin >> choixMenuDuDico;
  • system("cls");
  • }
  • while (choixMenuDuDico >= 4);
  • switch (choixMenuDuDico)
  • {
  • case 1:
  • {
  • system("cls");
  • ifstream dicoALire(dicoFr.c_str());
  • if (dicoALire)
  • {
  • cout << "Dictionnaire :" << endl << endl;
  • string ligne;
  • while (getline(dicoALire, ligne))
  • {
  • cout << ligne << endl;
  • }
  • }
  • else
  • {
  • cout << "ERREUR DANS LA LECTURE DU DICO" << endl;
  • }
  • cout << endl << "Fin du dictionnaire" << endl << endl;
  • system("Pause");
  • system("cls");
  • break;
  • }
  • case 2:
  • {
  • do
  • {
  • cout << "Quel dictionnaire veux tu choisir ?" << endl;
  • cout << "1.Le mien" << endl;
  • cout << "2.Langue francais (environ 360000 mots)" << endl << endl;
  • cout << "Votre choix: ";
  • cin >> choixMenuDuDico2;
  • system("cls");
  • }
  • while (choixMenuDuDico2 >= 3);
  • switch (choixMenuDuDico2)
  • {
  • case 1:
  • {
  • dictionnaireChoisi = "personnalise.txt";
  • system("cls");
  • break;
  • }
  • case 2:
  • {
  • dictionnaireChoisi = "francais.txt";
  • system("cls");
  • break;
  • }
  • }
  • continuerDuDico = 0;
  • break;
  • }
  • case 3:
  • {
  • system("cls");
  • continuerDuDico = 0;
  • break;
  • }
  • }
  • break;
  • }
  • case 3:
  • {
  • do
  • {
  • system("cls");
  • cout << "Que veux tu faire ?" << endl;
  • cout << "1.Voir ma moyenne (/20)" << endl;
  • cout << "2.Remettre a 0 les scores" << endl << endl;
  • cout << "Votre choix: " << endl;
  • cin >> choixMenuStat;
  • }
  • while (choixMenuStat >= 3 || choixMenuStat == 0);
  • switch (choixMenuStat)
  • {
  • case 1:
  • {
  • afficherMoyenne();
  • system("pause");
  • system("cls");
  • break;
  • }
  • case 2:
  • {
  • ofstream fichier("stat.txt");
  • system("CLS");
  • }
  • }
  • break;
  • }
  • case 4:
  • {
  • system("cls");
  • dico = dictionnaire(dictionnaireChoisi);
  • cout << "Tirage du mot en cours.." << endl << endl << endl;
  • motChoisi = choixMot(dico);
  • motMelanger = motChoisi;
  • motMelanger = melanger(motMelanger);
  • cout << "Quel est le mot mystere ? " << motMelanger << endl << endl;
  • while (decompteur != 0)
  • {
  • if (decompteur - 2 >> 0)
  • {
  • if (decompteur == 1)
  • {
  • cout << "Tu as encore " << decompteur << " essaies, voici l'indice (debut du mot): ";
  • motIndice(motChoisi);
  • cout << endl;
  • cout << "Je pense: ";
  • }
  • if (decompteur != 1)
  • {
  • cout << "Tu as encore " << decompteur << " essaies, il te reste maintenant "
  • << decompteur - 2 << " fautes avant un indice !" << endl
  • << "Je pense: ";
  • }
  • }
  • else
  • {
  • cout << "Tu as encore " << decompteur << " essaies, voici l'indice (debut du mot): ";
  • motIndice(motChoisi);
  • cout << endl;
  • cout << "Je pense: ";
  • }
  • cin >> motPense;
  • cout << endl << endl;
  • if (motPense == motChoisi)
  • {
  • statDeLaPartie(decompteur);
  • decompteur = 1;
  • cout << "Tu as trouver le mot (" << motChoisi << ") !" << endl << endl;
  • system("pause");
  • dico = dictionnaire(dictionnaireChoisi);
  • motChoisi = "";
  • motMelanger = "";
  • system("cls");
  • }
  • decompteur = decompteur - 1;
  • }
  • if (decompteur == 0 && motChoisi != "")
  • {
  • statDeLaPartie(0);
  • cout << "Tu as perdu... Le mot a trouve etait " << motChoisi << ". Retente ta chance !" << endl;
  • system("pause");
  • dico = dictionnaire(dictionnaireChoisi);
  • motChoisi = "";
  • motMelanger = "";
  • system("cls");
  • }
  • decompteur = 5;
  • break;
  • }
  • case 5:
  • {
  • quitter = 0;
  • break;
  • }
  • }
  • }
  • while (quitter);
  • return 0;
  • }
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <cmath>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <time.h>

using namespace std;

void statDeLaPartie(int decompteur)
{
    ofstream statFlux("bla/stat.txt", ios::app);

    statFlux << decompteur * 4 << endl;

}

void afficherMoyenne ()
{
    double  moyenne(0);

    int b;

    vector <int> notes;

    ifstream fichier("bla/stat.txt");

    string line;

    if(fichier)
    {
        while(getline(fichier, line))
          {
              b = atof(line.c_str());
              notes.push_back(b);
          }
    }
    else
    {
        cout << "impossible d'ouvrir le fichier " << endl;
    }



    for (int i(0); notes.size() > i; i++)
    {
        moyenne += notes[i];
    }

    moyenne /= notes.size();


    cout << "Votre moyenne est de:" << moyenne << endl << endl;
}

void motIndice (string motChoisi)
{
    for (int indice(0); indice != 3; indice++)
    {
        cout << motChoisi[indice];
    }
}

unsigned int nbAleatoire(int range)
{
    unsigned int aleatoire = (rand() & 0xFFFF) | ((rand() & 0xFFFF) << 16);

    if (range == 0)
    {
        range = 2;
    }
    return aleatoire % range;
}

string choixMot(vector <string> &dico)
{
    string motDict;

    if (dico.size() != 0)
    {

    motDict = dico[nbAleatoire(dico.size())];

    }

    else if (dico.size() == 0)
    {
        motDict = "AUCUN";
    }

    return motDict;
}

string melanger(string mot)
{
    string melange;
    int position(0);

    while (mot.size() != 0)
    {

        position = rand() % mot.size();
        melange += mot[position];
        mot.erase(position, 1);
    }

    return melange;
}

vector <string> dictionnaire(string nomFichier)
{
    ifstream fichier(nomFichier.c_str());
    vector <string> dico;
    string line;

    cout << "Chargement du dictionnaire..." << endl;
    if(fichier)
    {
        while(getline(fichier, line)) dico.push_back(line);
        return dico;
    }
    else cout << "impossible d'ouvrir le fichier " << nomFichier << endl;
}

int main()
{
    srand(time(NULL));

    bool quitter(1), continuer(1), continuerDuDico(1);

    int choixMenuPrincip(0), choixMenuDic(0), choixMenuStat(0),
    choixMenuDuDico(0), choixMenuDuDico2(0), decompteur(5);

    string const dicoFr("personnalise.txt"), dicoFr2("francais.txt");
    string motEntreDicoFr, dictionnaireChoisi("personnalise.txt"), motChoisi, motMelanger, motPense;

    vector <string> dico;


    do
    {
        cout << " /----------------------------!" << endl;
        cout << "!      BIEVENUE DANS LE       !" << endl;
        cout << "!  PROGRAMMES D'ENTRAINEMENT  !" << endl;
        cout << "!      DES LANGUES DE         !" << endl;
        cout << "!                             !" << endl;
        cout << "!                             !" << endl;
        cout << "!         KERIZ               !" << endl;
        cout << "!___________________________/ " << endl << endl;





        do
        {


            cout << "Que voulez vous faire ?" << endl;
            cout << "1.Entrer des mots dans le dictionnaire" << endl;
            cout << "2.Choix du dictionnaire" << endl;
            cout << "3.Statistiques" << endl;
            cout << "4.Commencer une partie" << endl;
            cout << "5.Quittez le jeu" << endl << endl;
            cout << "Votre choix ? ";

            cin >> choixMenuPrincip;

        }

        while (choixMenuPrincip >= 6 || choixMenuPrincip == 0);



        switch (choixMenuPrincip)
        {
            // DEBUT CASE 1 //Case 1 fini
        case 1:
        {
            system("cls");
            do
            {
                cout << "Entrez le mot en francais: " << endl;

                ofstream dicoFrancais(dicoFr.c_str(), ios::app); //ios::app
                cin.get();
                getline(cin, motEntreDicoFr);

                if (dicoFrancais)
                {
                    dicoFrancais << motEntreDicoFr << endl;
                }


                else
                {
                    cout << "ERREUR DANS LA CREATION DU FICHIER " << dicoFr << endl;
                }





                do
                {


                    cout << endl << "Que voulez vous faire ?" << endl;
                    cout << "1.Continuer a entrer des mots" << endl;
                    cout << "2.Remettre le dictionnaire a 0" << endl;
                    cout << "3.Retourner au menu" << endl << endl;
                    cout << "Votre choix ? ";

                    cin >> choixMenuDic;

                }

                while (choixMenuDic >= 4 || choixMenuDic == 0);

                switch (choixMenuDic)
                {
                case 1:
                {
                    continuer = 1;
                    break;
                }

                case 2:
                {
                    ofstream dicoFrancais(dicoFr.c_str());
                    continuer = 0;
                    break;
                }

                case 3:
                {
                    continuer = 0;
                    break;
                }

                }
            }

            while (continuer);

            system("cls");

            break;

        }
        //CASE 2
        case 2:
        {
            system("cls");

            do
            {
                cout << "Que voulez vous faire ?" << endl;
                cout << "1.Regarder les mots de mon dictionnaire" << endl;
                cout << "2.Choisir le dictionnaire avec lequel je joue" << endl;
                cout << "3.Retourner au menu"<< endl << endl;
                cout << "Votre choix: " << endl;

                cin >> choixMenuDuDico;

                system("cls");

            }
            while (choixMenuDuDico >= 4);



            switch (choixMenuDuDico)
            {
            case 1:
            {
                system("cls");

                ifstream dicoALire(dicoFr.c_str());

                if (dicoALire)
                {
                    cout << "Dictionnaire :" << endl << endl;
                    string ligne;

                    while (getline(dicoALire, ligne))
                    {
                        cout << ligne << endl;
                    }
                }

                else
                {
                    cout << "ERREUR DANS LA LECTURE DU DICO" << endl;
                }

                cout << endl << "Fin du dictionnaire" << endl << endl;

                system("Pause");

                system("cls");
                break;
            }

            case 2:
            {
                do
                {


                    cout << "Quel dictionnaire veux tu choisir ?" << endl;
                    cout << "1.Le mien" << endl;
                    cout << "2.Langue francais (environ 360000 mots)" << endl << endl;
                    cout << "Votre choix: ";

                    cin >> choixMenuDuDico2;

                    system("cls");

                }
                while (choixMenuDuDico2 >= 3);

                switch (choixMenuDuDico2)
                {
                case 1:
                {
                    dictionnaireChoisi = "personnalise.txt";
                    system("cls");
                    break;
                }

                case 2:
                {
                    dictionnaireChoisi = "francais.txt";
                    system("cls");
                    break;
                }

                }

                continuerDuDico = 0;
                break;
            }

            case 3:
            {
                system("cls");
                continuerDuDico = 0;
                break;
            }
            }




            break;
        }

        case 3:
        {
            do
            {


                system("cls");

                cout << "Que veux tu faire ?" << endl;
                cout << "1.Voir ma moyenne (/20)" << endl;
                cout << "2.Remettre a 0 les scores" << endl << endl;
                cout << "Votre choix: " << endl;

                cin >> choixMenuStat;

            }
            while (choixMenuStat >= 3 || choixMenuStat == 0);

            switch (choixMenuStat)
            {
                case 1:
                {
                    afficherMoyenne();

                    system("pause");
                    system("cls");

                    break;
                }

                case 2:
                {
                    ofstream fichier("stat.txt");
                    system("CLS");
                }
            }
                break;
        }

        case 4:
        {
            system("cls");

            dico = dictionnaire(dictionnaireChoisi);

            cout << "Tirage du mot en cours.." << endl << endl << endl;

            motChoisi = choixMot(dico);

            motMelanger = motChoisi;

            motMelanger = melanger(motMelanger);

            cout << "Quel est le mot mystere ? " << motMelanger << endl << endl;

            while (decompteur != 0)
            {
                if (decompteur - 2 >> 0)
                {
                    if (decompteur == 1)
                    {
                        cout << "Tu as encore " << decompteur << " essaies, voici l'indice (debut du mot): ";
                        motIndice(motChoisi);
                        cout << endl;
                        cout << "Je pense: ";
                    }

                    if (decompteur != 1)
                    {
                        cout << "Tu as encore " << decompteur << " essaies, il te reste maintenant "
                             << decompteur - 2 << " fautes avant un indice !" << endl
                             << "Je pense: ";
                    }

                }

                else
                {
                    cout << "Tu as encore " << decompteur << " essaies, voici l'indice (debut du mot): ";
                    motIndice(motChoisi);
                    cout << endl;
                    cout << "Je pense: ";

                }





                cin >> motPense;

                cout << endl << endl;


                if (motPense == motChoisi)
                {
                    statDeLaPartie(decompteur);

                    decompteur = 1;

                    cout << "Tu as trouver le mot (" << motChoisi << ") !" << endl << endl;

                    system("pause");

                    dico = dictionnaire(dictionnaireChoisi);

                    motChoisi = "";

                    motMelanger = "";

                    system("cls");

                }


                decompteur = decompteur - 1;

            }

            if (decompteur == 0 &&  motChoisi != "")

            {
                statDeLaPartie(0);
                cout << "Tu as perdu... Le mot a trouve etait " << motChoisi << ". Retente ta chance !" << endl;

                system("pause");

                dico = dictionnaire(dictionnaireChoisi);

                motChoisi = "";

                motMelanger = "";

                system("cls");
            }

            decompteur = 5;

            break;
        }

        case 5:
        {
            quitter = 0;
            break;
        }

        }



    }
    while (quitter);
    return 0;
}



 Sources de la même categorie

Source avec Zip ASCII_PHYSIX par BuuG63
Source avec Zip EAT GAME - AFFRONTEZ VOS AMIS par Nebur97232
Source avec Zip Source avec une capture FRUTISNAKE 1.0 par ringo73
Source avec Zip Source avec une capture BRIQUES GAME par xtreen
RÉSOLUTION DU JEU RUSH HOUR PARFOIS APPELÉ UNBLOCK par pabbati

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture BM-TRON EN SDL par parc12345

Commentaires et avis

Commentaire de CptPingu le 29/02/2012 15:15:08 administrateur CS

Bonjour.

Quelques conseils:
- Évite les using namespace std, voir: http://0217021.free.fr/portfolio/axel.berardino/articles/bon-usage-using-namespace
- Évite d'utiliser "system", tu perds en portabilité pour une fonctionnalité à l'intérêt très limité.
- Découpe ton code en petite fonction. Là c'est assez indigeste. Une fonction ne devrait pas dépasser 50 lignes.
- Si tu utilises des booléens, utilise les pour de vrai ! bool quitter(1) => bool quitter = true;
- Lorsque cela est possible, préfère les iterator. "for (int i(0); notes.size() > i; i++)" => for (std::vector<int>::const_iterator it = notes.begin(); it != notes.end(); ++it)
- On évite de retourner de gros objet par copie ! Ex: vector <string> dictionnaire(string nomFichier) devrait être: void dictionnaire(std::vector<string>& res, const std::string& nomFichier)
- Pour gérer des erreurs, on utilise soit: Des exceptions, un code de retour, ou des asssert. Mais on ne fait pas de std::cout à l'arrache dans une fonction "outil".
- La fonction: string melanger(string mot) est inutilement compliqué et provoque une copie avec ajouts successifs, désastreux en terme de performance. Tu peux largement l'améliorer en ne modifiant que la chaîne en cours.
- La fonction "unsigned int nbAleatoire(int range)" est elle aussi trop compliqué pour ce que ça fait. Un seul appel à rand() suffit. Faire un appel à rand par tranche de 16 bits ne fera pas un meilleur aléatoire... Si tu veux du "meilleur" aléatoire, voir les std::mersenne_twister du C++0x.
- En C++, on inclut cmath et non math.h. cstdlib et non stdlib.h, etc...
- Au lieu de "dico.size() == 0" préfère "dico.empty()"

Il y a aussi un manque flagrant de commentaire. N'oublie pas que tu destines ton code à être relu par des débutants. Voir: http://0217021.free.fr/portfolio/axel.berardino/articles/ecrire-de-bons-commentaires

Pour conclure: Le programme est correcte, mais la forme est à revoir. Il y a pas mal de choses à réécrire au propre. Si vous êtes débutant et que vous lisez ceci: "Attention ce programme n'est pas encore au niveau et il contient des pratiques à éviter absolument !"

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Personnalisation console windows [ par fredd49 ] Salut à tous, J'aurrai voulu savoir :1 - comment faire pour mettre de la couleur sur le texte dans la console windows en C++2 - Comment mettre


Nos sponsors


Sondage...

CalendriCode

Mai 2013
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
2728293031  

Consulter la suite du CalendriCode

Photothèque

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 3,494 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales