if else结构的获胜条件

时间:2019-04-13 15:31:24

标签: c++ arrays if-statement random

我写了这个简单的练习来开始学习c ++。我能够执行代码并从数组中获取一些随机数和字符串。我的问题是关于if()else控制结构。我正在测试代码,因此无法获得“赢得胜利”的信息。这是因为提取的中奖卡总是随机的。是否有可能建立一个更好的控制结构来改进代码并有机会获胜?


#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;

}

0 个答案:

没有答案