2
\$\begingroup\$

i created this console application ( it's actually a Mastermind) and i would like to know what can i improve as a beginner. Thanks !

#include <iostream>
#include <cstdlib>
#include <time.h>

std::string colorsRandomizer();
int verif(char couleur);
std::string transform(char couleur);

int main()
{
    std::srand(time(0));
    //Declaration des variables
    std::string color_1;
    std::string color_2;
    std::string color_3;
    std::string color_4;
    std::string combination;

    char color_1_User;
    char color_2_User;
    char color_3_User;
    char color_4_User;
    std::string color_1_trial;
    std::string color_2_trial;
    std::string color_3_trial;
    std::string color_4_trial;
    std::string combinationUser;
    short chances = 0;
    short s = 0; //Compteur pour le boucle du choix utilisateur
    short good;
    short near;

    //Creation aleatoire de la combinaison

    color_1 = colorsRandomizer();
    color_2 = colorsRandomizer();
    color_3 = colorsRandomizer();
    color_4 = colorsRandomizer();


    combination = color_1 + color_2 + color_3 + color_4;
    std::cout << "Bienvenue dans MasterMind ! Le jeu qui va vous soulever    le     cerveau !" << std::endl;
    std::cout << "Le bot a choisi sa combinaison. A vous d'essayer de la trouver. J pour jaune, R pour rouge, B pour Bleu, V pour violet, G pour vert, O pour orange." << std::endl;

    while(chances <= 10){
        ++chances;
        do{
            std::cout << "Couleur une:" << std::endl;
            std::cin >> color_1_User;
            std::cin.ignore(999, '\n');
            s=verif(color_1_User);
        }while(s!=1);
        color_1_trial= transform(color_1_User);
        do{
            s=0;
            std::cout << "Couleur deux:" << std::endl;
            std::cin >> color_2_User;
            std::cin.ignore(999, '\n');
            s=verif(color_2_User);
        }while(s!=1);
        color_2_trial= transform(color_2_User);
        do{
            s=0;
            std::cout << "Couleur trois:" << std::endl;
            std::cin >> color_3_User;
            std::cin.ignore(999, '\n');
            s=verif(color_3_User);
        }while(s!=1);
        color_3_trial= transform(color_3_User);
        do{
            s=0;
            std::cout << "Couleur quatre:" << std::endl;
            std::cin >> color_4_User;
            std::cin.ignore(999, '\n');
            s=verif(color_4_User);
        }while(s!=1);
        color_4_trial= transform(color_4_User);
        s=0;

        combinationUser = color_1_trial + color_2_trial + color_3_trial + color_4_trial;
        std::cout << combinationUser << std::endl;

        good = 0;
        near = 0;

        //Test de la première couleur proposée

        if(color_1_trial == color_1){
             ++good;
        }
         else if(color_1_trial == color_2 && good<1){
             ++near;
        }
         else if(color_1_trial == color_3 && near <1 && good<1){
             ++near;
        }
         else if (color_1_trial == color_4&& near <1 && good<1){
             ++near;
        }

        //Test de la deuxième couleur proposée

        if(color_2_trial == color_2){
            ++good;
        }
        else if(color_2_trial == color_1 && good<2){
            ++near;
        }
        else if(color_2_trial == color_3 && near <2 && good<2){
            ++near;
        }
        else if (color_2_trial == color_4 && near <2 && good<2){
            ++near;
        }

        //Test de la troisième couleur proposée
        if(color_3_trial == color_3){
            ++good;
        }
        else if(color_3_trial == color_1 && good<3){
            ++near;
        }
        else if(color_3_trial == color_2 && near <3 && good<3){
            ++near;
        }
        else if (color_3_trial == color_4 && near <3 && good<3){
            ++near;
        }

        //Test de la quatrième couleur proposée
        if (color_4_trial == color_4){
            ++good;
        }
        else if(color_4_trial == color_1 && good<4){
            ++near;
        }
        else if(color_4_trial == color_2 && near <4 && good<4){
            ++near;
        }
        else if(color_4_trial == color_3 && near <4 && good<4){
            ++near;
        }

        if(near==0){
            std::cout << "Il n'y a aucune couleur dans la combinaison." << std::endl;
        }
        if(near>0){
            std::cout << "Il y a " << near << " couleur(s) qui sont dans la combi, mais pas a la bonne place." << std::endl;
        }
        if(good==0){
            std::cout << "Il n'y a aucune bonne couleur lo. " << std::endl;
        }
        if(good>0){
            std::cout << "Il y a " << good << " couleur(s) justes." << std::endl;
        }
        if(good == 4){
            std::cout << "Bien joué le jo, tu as battu le bot ! J'espere que tu es fier de toi !!!!!!!!!!!!!!!!! \a";
            system("PAUSE");
            return 0;
        }


    }

    std::cout << " PAS DE BOL, tu as perdu ! La combinaison etait " << combination << std::endl;
    system("PAUSE");
    return 0;


}
int verif(char couleur){
    short s = 0; //Initialisation de la variable de sortie de boucle.

    switch(couleur){
    case 'J':
    case 'j':
        std::cout << " Vous avez choisi la couleur jaune." << std::endl;
        s++;
        break;
    case 'B':
    case 'b':
        std::cout << "Vous avez choisi la couleur bleue." << std::endl;
        s++;
        break;
    case 'G':
    case 'g':
        std::cout << "Vous avez choisi la couleur verte." << std::endl;
        s++;
        break;
    case 'R':
    case 'r':
        std::cout << "Vous avez choisi la couleur rouge." << std::endl;
        s++;
        break;
    case 'V':
    case 'v':
        std::cout << "Vous avez choisi la couleur violette." << std::endl;
        s++;
        break;
    case 'O':
    case 'o':
        std::cout << "Vous avez choisi la couleur orange." << std::endl;
        s++;
        break;
    default:
        std::cout << "Veuillez respecter le code couleur." << std::endl;
    }

    return s;
}

std::string transform(char couleur){

    std::string coul;
    switch(couleur){

    case 'J':
    case 'j':
        coul="yellow";
        break;
    case 'B':
    case 'b':
        coul="blue";
        break;
    case 'G':
    case 'g':
        coul="green";
        break;
    case 'R':
    case 'r':
        coul="red";
        break;
    case 'V':
    case 'v':
        coul="violet";
        break;
    case 'O':
    case 'o':
        coul="orange";
        break;
    default:
        std::cout << "ho" << std::endl;
    }

    return coul;
}


std::string colorsRandomizer(){

    short x;
    std::string color;
    x = rand() % 6;

    switch(x){
    case 0:
        color = "red";
        break;
    case 1:
        color = "blue";
        break;
    case 2:
        color = "yellow";
        break;
    case 3:
        color = "violet";
        break;
    case 4:
        color = "green";
        break;
    case 5:
        color = "orange";
        break;
    }

    return color;


}
\$\endgroup\$
  • 1
    \$\begingroup\$ It might be better to define an enum for colors. \$\endgroup\$ – L. F. Feb 13 at 1:46
  • \$\begingroup\$ Okay i'll keep that in a corner of my head. thanks ! \$\endgroup\$ – COGNER Feb 13 at 10:45
1
\$\begingroup\$

There's a lot of things you can improve upon, but let me offer at least a reasonable start.

First, look into arrays (particularly, std::vector). If you have bunch of variables (like color_1 through color_4) that are intimately connected, they should be somehow logically connected. One natural way is to put them into a vector.

Also, does it make sense a color is represented as a string? The answer is no: you are expecting it to be something like "r" or "R" to designate red. So consider switching the type into something that conveys the correct meaning and protects the user from unintentional mistakes, so make it a char or your own color-enum.

Your transform and colorRandomizer should be implemented in terms of a data structure as well. For example, consider:

const std::vector<std::string> cols = { "red", "blue", "yellow", "violet", "green", "orange" };

std::string colorsRandomizer()
{
    int x = rand() % 6;
    return cols.at(x);
}

For the above, you need to include <string> and <vector>. Notice how much more simpler this is for maintenance: you can easily change color names and/or add more colors.

A similar idea extends to transform:

const std::map<char, std::string> col_map = 
{
    { 'j', "yellow" },
    { 'b', "blue" },
    { 'g', "green" },
    { 'r', "red" },
    { 'v', "violet" },
    { 'o', "orange" },
};

std::string transform(char couleur)
{
    return col_map[std::tolower(couleur)];
}

For this, you will need to include <map> and <cctype>. So again this approach decreases your maintenance burden and makes the code more readable. You could also ensure that the argument is actually in the map, and then fail gracefully in case it is not, but I'm skipping that you focus on the idea.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.