生成不同的随机数

时间:2016-11-24 14:34:00

标签: c++ visual-c++

我正在尝试为Blackjack游戏构建代码,我遇到了以下问题:

我希望代码在重新洗牌之前不要多次使用同一张卡(再次重新加载所有52张卡)。

#include<iostream>
#include<string>
#include <ctime>

#include <stdio.h>     
#include <stdlib.h>     
#include <time.h> 
using namespace std;
class play_cards

{
public:
    play_cards::play_cards();
    void play_cards::load();
    void play_cards::printdeck();
    void play_cards::startGame();
    void play_cards::reshuffle();
    void play_cards::check();
private:
struct deck
{
    string card_name;
    string card_type;
    int card_value;
    bool dealt;
};
deck card[52];
int i,random, counter ;
double Total;
};
int main()
{ string start="";

    cout<<endl<<endl;
    cout<<"The Rules are as Following:"<<endl<<endl;
    cout<<"1.  The dealer gives one card face up to each player,  "<<endl;
    cout<<"    and then one card face up to himself."<<endl;
    cout<<"2.  Another round of cards is then dealt face up to each player, "<<endl;
    cout<<"    but the dealer takes his second card face down. "<<endl;
    cout<<"3.  Each player must decide whether to "<<endl;
    cout<<"    stand (not ask for another card) "<<endl;
    cout<<"                 OR                  "<<endl;
    cout<<"    hit (ask for another card ) "<<endl;
    cout<<"4.  Each player attempts to beat the dealer by getting a count  closer to 21, "<<endl;


cout<<"    without going over 21. "<<endl;
    cout<<"5.  When the dealer has served the player, his face-down card is turned up. "<<endl;
    cout<<"    If the total is 17 or more, he must stand. 16 or under, he takes a card. "<<endl;
    cout<<"6.  If the dealer has an ace, and counting it as 11 would bring his total to 17"<<endl;
    cout<<"    or more (but not over 21), he must count the ace as 11 and stand. "<<endl;
    cout<<"7.  The dealer's decisions, then, are automatic on all plays,  "<<endl;
    cout<<"    whereas the player always has the option of taking one or more cards. "<<endl;
    cout<<endl<<endl;

    play_cards value;
    cout<< "Do you want to Start Playing ? (yes / no)"<<endl<<endl;
    cin>>start;
    if(start=="yes"){
    value.startGame();
    }
    //value.printdeck();
    system("pause");
   return 0;
}
play_cards::play_cards()
{
    random=0;
    counter=0;
    Total=0;
}

 void play_cards::load()
    {
    card[0].card_name="TWO";
    card[0].card_type="heart";
    card[0].card_value=2;
    card[0].dealt=false;
    card[1].card_name="THREE";
    card[1].card_type="heart";
    card[1].card_value=3;
    card[1].dealt=false;
    card[2].card_name="FOUR";
    card[2].card_type="heart";
    card[2].card_value=4;
    card[2].dealt=false;
    card[3].card_name="FIVE";
    card[3].card_type="heart";
    card[3].card_value=5;
    card[3].dealt=false;
    card[4].card_name="SIX";
    card[4].card_type="heart";
    card[4].card_value=6;
    card[4].dealt=false;
    card[5].card_name="SEVEN";
    card[5].card_type="heart";
    card[5].card_value=7;
    card[5].dealt=false;
    card[6].card_name="EIGHT";
    card[6].card_type="heart";
    card[6].card_value=8;
    card[6].dealt=false;
    card[7].card_name="NINE";
    card[7].card_type="heart";
    card[7].card_value=9;
    card[7].dealt=false;
    card[8].card_name="TEN";
    card[8].card_type="heart";
    card[8].card_value=10;
    card[8].dealt=false;
    card[9].card_name="ACE";
    card[9].card_type="heart";
    card[9].card_value=1;
    card[9].dealt=false;
    card[10].card_name="JACK";
    card[10].card_type="heart";
    card[10].card_value=10;
    card[10].dealt=false;
    card[11].card_name="QUEEN";
    card[11].card_type="heart";
    card[11].card_value=10;
    card[11].dealt=false;
    card[12].card_name="KING";
    card[12].card_type="heart";
    card[12].card_value=10;
    card[12].dealt=false;

    card[13].card_name="TWO";
    card[13].card_type="diamond";
    card[13].card_value=2;
    card[13].dealt=false;
    card[14].card_name="THREE";
    card[14].card_type="diamond";
    card[14].card_value=3;
    card[14].dealt=false;
    card[15].card_name="FOUR";
    card[15].card_type="diamond";
    card[15].card_value=4;
    card[15].dealt=false;
    card[16].card_name="FIVE";
    card[16].card_type="diamond";
    card[16].card_value=5;
    card[16].dealt=false;
    card[17].card_name="SIX";
    card[17].card_type="diamond";
    card[17].card_value=6;
    card[17].dealt=false;
    card[18].card_name="SEVEN";
    card[18].card_type="diamond";
    card[18].card_value=7;
    card[18].dealt=false;
    card[19].card_name="EIGHT";
    card[19].card_type="diamond";
    card[19].card_value=8;
    card[19].dealt=false;
    card[20].card_name="NINE";
    card[20].card_type="diamond";
    card[20].card_value=9;
    card[20].dealt=false;
    card[21].card_name="TEN";
    card[21].card_type="diamond";
    card[21].card_value=10;
    card[21].dealt=false;
    card[22].card_name="ACE";
    card[22].card_type="diamond";
    card[22].card_value=1;
    card[22].dealt=false;
    card[23].card_name="JACK";
    card[23].card_type="diamond";
    card[23].card_value=10;
    card[23].dealt=false;
    card[24].card_name="QUEEN";
    card[24].card_type="diamond";
    card[24].card_value=10;
    card[24].dealt=false;
    card[25].card_name="KING";
    card[25].card_type="diamond";
    card[25].card_value=10;
    card[25].dealt=false;

    card[26].card_name="TWO";
    card[26].card_type="spade";
    card[26].card_value=2;
    card[26].dealt=false;
    card[27].card_name="THREE";
    card[27].card_type="spade";
    card[27].card_value=3;
    card[27].dealt=false;
    card[28].card_name="FOUR";
    card[28].card_type="spade";
    card[28].card_value=4;
    card[28].dealt=false;
    card[29].card_name="FIVE";
    card[29].card_type="spade";
    card[29].card_value=5;
    card[29].dealt=false;
    card[30].card_name="SIX";
    card[30].card_type="spade";
    card[30].card_value=6;
    card[30].dealt=false;
    card[31].card_name="SEVEN";
    card[31].card_type="spade";
    card[31].card_value=7;
    card[31].dealt=false;
    card[32].card_name="EIGHT";
    card[32].card_type="spade";
    card[32].card_value=8;
    card[32].dealt=false;
    card[33].card_name="NINE";
    card[33].card_type="spade";
    card[33].card_value=9;
    card[33].dealt=false;
    card[34].card_name="TEN";
    card[34].card_type="spade";
    card[34].card_value=10;
    card[34].dealt=false;
    card[35].card_name="ACE";
    card[35].card_type="spade";
    card[35].card_value=1;
    card[35].dealt=false;
    card[36].card_name="JACK";
    card[36].card_type="spade";
    card[36].card_value=10;
    card[36].dealt=false;
    card[37].card_name="QUEEN";
    card[37].card_type="spade";
    card[37].card_value=10;
    card[37].dealt=false;
    card[38].card_name="KING";
    card[38].card_type="spade";
    card[38].card_value=10;
    card[38].dealt=false;

    card[39].card_name="TWO";
    card[39].card_type="club";
    card[39].card_value=2;
    card[39].dealt=false;
    card[40].card_name="THREE";
    card[40].card_type="club";
    card[40].card_value=3;
    card[40].dealt=false;
    card[41].card_name="FOUR";
    card[41].card_type="club";
    card[41].card_value=4;
    card[41].dealt=false;
    card[42].card_name="FIVE";
    card[42].card_type="club";
    card[42].card_value=5;
    card[42].dealt=false;
    card[43].card_name="SIX";
    card[43].card_type="club";
    card[43].card_value=6;
    card[43].dealt=false;
    card[44].card_name="SEVEN";
    card[44].card_type="club";
    card[44].card_value=7;
    card[44].dealt=false;
    card[45].card_name="EIGHT";
    card[45].card_type="club";
    card[45].card_value=8;
    card[45].dealt=false;
    card[46].card_name="NINE";
    card[46].card_type="club";
    card[46].card_value=9;
    card[46].dealt=false;
    card[47].card_name="TEN";
    card[47].card_type="club";
    card[47].card_value=10;
    card[47].dealt=false;
    card[48].card_name="ACE";
    card[48].card_type="club";
    card[48].card_value=1;
    card[48].dealt=false;
    card[49].card_name="JACK";
    card[49].card_type="club";
    card[49].card_value=10;
    card[49].dealt=false;
    card[50].card_name="QUEEN";
    card[50].card_type="club";
    card[50].card_value=10;
    card[50].dealt=false;
    card[51].card_name="KING";
    card[51].card_type="club";
    card[51].card_value=10;
    card[51].dealt=false;
    }
void play_cards::printdeck()
    {
         //loop to print all cards
           for (i=0; i<=51; ++i)
            {
            if((i==13)||(i==26)||(i==39))
                cout<<endl;
              cout<<card[i].card_name<<" "<<card[i].card_type<<" " <<card[i].card_value<<" " +card[i].dealt<<endl;
            }//for loop
    }

void play_cards::reshuffle()
{
    cout<<"**********"<<endl<<
    "The Cards has been reshuffled"<<endl<<
    "**********"<<endl;

    for (i=0; i<=51; ++i)
    {
        card[i].dealt=false;
    }
    counter =0;
}
  

你可以查看这个功能吗

void play_cards::check()
{

srand(time(NULL));
     random= rand() %53;  //number between 0 and 53



    while(card[random].dealt!=0)
    {
        srand(time(NULL));
        random= rand() %53; 
    }

     if(!card[random].dealt)
    {
        counter =counter+1;
    }

    if(counter==37)
    {
        reshuffle();
    }


}

void play_cards::startGame()
{


        cout<<endl<<endl;
            int player_wins = 0;
            int computer_wins = 0;
            string choice = "";
            string playerOption = "";
            while(choice!="No")
            {
            bool player_flag = false;
            int player_score = 0;
            int computer_score = 0;
            load();


            check();
            player_score = player_score + card[random].card_value;
            cout<<"Player draws: "<< card[random].card_name<<" "<<card[random].card_type <<endl<<"Score becomes: "<<player_score<<endl<<endl;
            card[random].dealt=1;


            check();
            computer_score = computer_score + card[random].card_value;
            cout<<"Dealer draws: "<< card[random].card_name<<" "<<card[random].card_type<<endl<< "Score becomes: "<<computer_score<<endl<<endl;
            card[random].dealt=1;


            check();
            player_score = player_score + card[random].card_value;
            cout<<"Player draws: "<< card[random].card_name<<" "<<card[random].card_type <<endl<<"Score becomes: "<<player_score<<endl<<endl;
            card[random].dealt=1;


            check();
            computer_score = computer_score + card[random].card_value;
            int hidden = random;
            cout<<"Dealer draws but the value will be shown later " <<endl<<endl;
            card[random].dealt=1;

            cout<<"You want to hit or stand? "<<endl<<endl;
            cin>>playerOption;
             while(playerOption!="stand")
            {
              if(playerOption=="hit")
                {

                    check();
                    player_score = player_score + card[random].card_value;
                    cout<<"Player draws: "<< card[random].card_name<<" "<<card[random].card_type <<endl<<" Score becomes: "<<player_score<<endl<<endl;
                    card[random].dealt=1;
                    if(player_score>21)
                    {
                        cout<<"Player Loses"<<endl;
                        computer_wins=computer_wins+1;
                        player_flag = true;
                        break;

                    }
                    if(player_score==21)
                    {
                        cout<<"Player Wins!!"<<endl;
                        player_wins=player_wins+1;
                        player_flag=true;
                        break;
                    }
                }
              cout<<"You want to hit or stand? "<<endl;
            cin>>playerOption;
             }
              if(!player_flag)
            {
                cout<<"Dealer hidden card is: "<< card[hidden].card_name<<" "<<card[hidden].card_type<< "\nScore becomes: "<<computer_score<<endl<<endl;
                bool computer_flag = false;
                    while(computer_score<17)
                    {


                        check();
                        if(card[random].card_name=="ACE")
                        {
                            if(((computer_score+11) >= 17 ) && ((computer_score+11)<=21))
                            {
                                computer_score = computer_score+11;
                                break;
                            }
                            else
                            {
                                computer_score= computer_score+1;
                            }
                        }
                        else
                        {
                            computer_score = computer_score+card[random].card_value;
                        }
                         cout<<"Dealer draws: "<< card[random].card_name<<" "<<card[random].card_type <<endl<<"Score becomes: "<<computer_score<<endl<<endl;
                         card[random].dealt=1;
                        if(computer_score>21)
                        {
                            cout<<"Dealer loses!!"<<endl;
                            player_wins=player_wins+1;
                            computer_flag= true;
                            break;
                        }
                         if(computer_score==21)
                        {
                            cout<<"Dealer Wins!!"<<endl;
                            computer_wins=computer_wins+1;
                            computer_flag = true;
                            break;
                        }
                         }
                    if(!computer_flag)
                    {
                    if(computer_score>player_score)
                    {
                        cout<<"Dealer Wins!!"<<endl;
                        computer_wins=computer_wins+1;
                    }
                    else
                    {
                        if(computer_score<player_score)
                        {
                            cout<<"Player Wins!!"<<endl;
                            player_wins=player_wins+1;
                        }
                        else
                        {
                            cout<<"Nobody wins or loses"<<endl;
                        }
                    }
                    }
            }
              Total=Total+1;
              cout<<"counter ="<<counter<<endl;
              cout<<" Do you want to play again? (Yes / No)"<<endl;
              cin>>choice;

    }
    cout<<endl<<endl;
    cout<<"Final Summary:"<<endl<<endl;
    cout<<"Total Number of played games: "<<Total<<endl;
    cout<<"Player Wins: "<<player_wins<<endl;
    cout<<"Computer Wins: "<<computer_wins<<endl;
    cout<<"The percentage of wins for the playes: "<<(player_wins/Total)*100<<"%"<<endl;

    }

1 个答案:

答案 0 :(得分:1)

你在这里弄乱了一些概率。一开始你有52张牌,所以你使用rand()%52得到0到51之间的数字(http://www.cplusplus.com/reference/cstdlib/rand/)。这是正确的,因为每张卡的提取概率是1/52。

然而,随着卡越来越少,提取单张卡的可能性必须相应提高。此外,您的解决方案效率低下,因为当只剩下一张卡时,您将继续提取,直到您无法获得正确的价值浪费大量时间。

您可以将此问题重新考虑为随机置换有限集,可以使用 Knuth shuffle 解决,这很容易实现。链接到维基百科:https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle