samedi 13 avril 2019

Winning condition with if else structure

I wrote this simple exercise to start learning c++. I'm able to execute the code and to get some random numbers and strings from arrays. My question is about the if() and else control structure. I'm testing the code and it's impossible to get the you win message. This because the extracted winning cards are always random. Is it possible to make a better control structure to improve the code and to have a chance to win?


#include <iostream>
#include <string>
#include <random>

int main(){
    std::string name;
    std::string input;
    int carta_estratta1;
    int carta_estratta2;
    std::string seme1;
    std::string seme2;
    int carta_vincente1;
    int carta_vincente2;
    std::string seme_vincente1;
    std::string seme_vincente2;
    int bet;
    int fish = 100;
    std::string semi[4] = {"hearts","clubs","spades","diamonds"};
    int carte[9] = {2,3,4,5,6,7,8,9,10};

    std::random_device rd;
    std::mt19937_64 engine(rd());
    std::uniform_int_distribution<int> distribution(0, 8);


    std::cout << "Welcome in PokerBash! Please enter your name:" <<std::endl;
    std::getline(std::cin, name);

    std::cout << "Your name is " << name <<std::endl;
    std::cout << "You have a credit of:" << fish <<std::endl;

    do{
        std::cout << "Please enter your bet:" <<std::endl;

        std::cin >> bet;

        fish = fish - bet;

        std::cout << "Your credits after this bet:" << fish <<std::endl;

        carta_estratta1 = carte[distribution(engine)];
        seme1 = semi[rand() % 4];
        carta_estratta2 = carte[distribution(engine)];
        seme2 = semi[rand() % 4];
        carta_vincente1 = carte[distribution(engine)];
        carta_vincente2 = carte[distribution(engine)];
        seme_vincente1 = semi[rand() % 4];
        seme_vincente2 = semi[rand() % 4];

        std::cout << "Your cards are "<< carta_estratta1 <<" "<< seme1 <<" "<< carta_estratta2 <<" "<< seme2 <<std::endl;
        std::cout << "Show winning card? y/n" << std::endl;
        std::cin >> input;

            if(input == "y" || input == "Y"){
                std::cout << "Winning cards are: "<< carta_vincente1 <<" "<< seme_vincente1 <<" "<< carta_vincente2 <<" "<< seme_vincente2  <<std::endl;
                if( carta_estratta1 == carta_vincente1 && carta_estratta2 == carta_vincente2 && seme1 == seme_vincente1 && seme2 == seme_vincente2){
                    std::cout << "You win! "<< bet <<" fish will be added to your credits!";
                    fish = fish + bet;
                }
                else{
                    std::cout << "You loose!"<<std::endl;
                }
            }
    }
    while( fish != 0 );
        std::cout << "Game over" <<std::endl;
        return 0;

}

Aucun commentaire:

Enregistrer un commentaire