为什么数字不会保持随机?



我一直在编写一个二十一点游戏,它几乎完成了它的第一阶段开发。 我快完成了,唯一的问题是这部分生成的随机数:

if(x.hit == 1) {
            if (x.userShowCardThree == 0) {
                x.userShowCardThree = 1 + (rand()%11);
                int dealerHit1 = x.userShowCardThree;
                x.userTotal += dealerHit1;
                x.cardCount++;
            }
            else {
                if (x.userShowCardFour == 0) {
                    x.userShowCardFour = 1 + (rand()%11);
                    int dealerHit2 = x.userShowCardFour;
                    x.userTotal += dealerHit2;
                    x.cardCount++;
                }
                else {
                    if (x.userShowCardFive ==0) {
                        x.userShowCardFive = 1 + (rand()%11);
                        int dealerHit3 = x.userShowCardFive;
                        x.userTotal += dealerHit3;
                        x.cardCount++;
                    }
                }
            }

与最后一部分中生成的数字不同:

        cout << "You had a total of: " << x.userTotal << endl;

因为我不断得到不同的数字。 我将在下面粘贴我的整个代码。

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstring>

using namespace std;
void setRand() {
srand(time(0));
}
class Game {
 public:
int userCards[11] = {1,2,3,4,5,6,7,8,9,10,11};
int dealerCards[11] = {1,2,3,4,5,6,7,8,9,10,11};
int userShowCardOne = userCards[1 + (rand()%11)];
int userShowCardTwo = userCards[1 + (rand()%11)];
int dealerShowCard = dealerCards[1 + (rand()%11)];
int dealerHiddenCard = dealerCards[1 + (rand()%11)];
int userShowCards[5] = {userShowCardOne, userShowCardTwo, userShowCardThree,       userShowCardFour, userShowCardFive};
int userShowCardThree = 0;
int userShowCardFour = 0;
int userShowCardFive = 0;
int fresh = 1;
int beginningInput;
int hit = 1;
int dealerTotal = dealerShowCard + dealerHiddenCard;
int userTotal = userShowCardOne + userShowCardTwo;
int cardCount = 2;
int runGame = 1;
private:
};

// int a = 1 + rand()%11;
/*int b = 1 + rand()%11;
int c = 1 + rand()%11;
int d = 1 + rand()%11;
int e = 1 + rand()%11;
int f = 1 + rand()%11;
int g = 1 + rand()%11;
int h = 1 + rand()%11;
*/



int startGame();
int main() {
srand(time(0));
Game x;
cout << "Welcome to BlackJack 1.0.0" << endl;
cout << "Press: " <<endl;
cout << "1 ----- New Game" << endl;
cout << "2 ----- Help" << endl;
cin >> x.beginningInput;
if(x.beginningInput == 1){
    startGame();
    cout << "The dealer had: " << endl;
    cout << x.dealerHiddenCard << endl;
    cout << x.dealerShowCard << endl;
    while(x.dealerTotal <= 16) {
        cout << "The dealer has decided to hit" << endl;
        int dealerHit = 1 + (rand()%11);
        cout << "The dealer has gotten " << dealerHit << endl;
        x.dealerTotal += dealerHit;
    }
    cout << "The dealer has decided to stay" << endl;
    cout << "The dealer had a total of " << x.dealerTotal << endl;
    cout << "You had a total of: " << x.userTotal << endl;
    if (x.dealerTotal > 21 && x.userTotal <= 21) {
        cout << "The dealer busted. You won!" << endl;
    }
    else if (x.userTotal > 21 && x.dealerTotal <= 21) {
        cout << "You busted.  The Dealer Won" << endl;
    }
    else if (x.userTotal > 21 && x.dealerTotal > 21) {
        cout << "You and the dealer both busted. Tie" << endl;
    }
    else {
        if (x.dealerTotal > x.userTotal) {
            cout << "Sorry, you lost to the dealer" << endl;
        }
    else if (x.dealerTotal < x.userTotal) {
        cout << "Congrats, you won!" << endl;
    }
    else {
        cout << "You and the dealer tied. " << endl;
    }
    }
    cout << "Would you like to play again? 1 to play again, 0 to quit" << endl;
    cin  >> x.beginningInput;
    if (x.beginningInput == 1){
        startGame();
    }
    else if (x.beginningInput == 0){
        return 0;
    }
}
else if (x.beginningInput == 0) {
    cout << "Thanks for playing!" << endl;
}
else {
    cout << "Here's the help section" << endl;
}
}
int startGame() {
Game x;
srand(time(0));
if (x.fresh != 1){
    cout << "NEW GAMEn n "  << endl;
}
while (x.runGame == 1) {
        cout <<  "Dealer: n" << endl;
        cout << "X" << endl;
        cout << x.dealerShowCard << endl;
        cout << "You: n" << endl;
        cout << x.userShowCardOne << endl;
        cout << x.userShowCardTwo << endl;
        x.userTotal = x.userShowCardOne + x.userShowCardTwo;
        if (x.userShowCardThree != 0) {
            cout << x.userShowCardThree << endl;
        }
        if (x.userShowCardFour != 0) {
            cout << x.userShowCardFour << endl;
            cout << "You can only hit one more time!" << endl;
        }
        if (x.userShowCardFive != 0) {
            cout << x.userShowCardFive << endl;
        }
    if(x.cardCount > 5) {
        cout << "sorry, there is a 5 card limit.";
    }
        cout << "Would you like to hit or stay? (1 for hit or 2 for stay)" << endl;
        cin >> x.hit;
        x.fresh = 2;
        if(x.hit == 1) {
            if (x.userShowCardThree == 0) {
                x.userShowCardThree = 1 + (rand()%11);
                int dealerHit1 = x.userShowCardThree;
                x.userTotal += dealerHit1;
                x.cardCount++;
            }
            else {
                if (x.userShowCardFour == 0) {
                    x.userShowCardFour = 1 + (rand()%11);
                    int dealerHit2 = x.userShowCardFour;
                    x.userTotal += dealerHit2;
                    x.cardCount++;
                }
                else {
                    if (x.userShowCardFive ==0) {
                        x.userShowCardFive = 1 + (rand()%11);
                        int dealerHit3 = x.userShowCardFive;
                        x.userTotal += dealerHit3;
                        x.cardCount++;
                    }
                }
            }
        }
    if (x.hit == 2) {
        x.runGame = 2;
    }
    }
return 0;
}

看起来你有 2 个 Game 对象的实例,并且都命名为 x

int main() {
    srand(time(0));
    Game x;
    ...
    cout << "You had a total of: " << x.userTotal << endl;

然后在开始游戏函数中

int startGame() {
    Game x;
    ...
    if(x.hit == 1) {
        if (x.userShowCardThree == 0) {
            x.userShowCardThree = 1 + (rand()%11);
            int dealerHit1 = x.userShowCardThree;
            x.userTotal += dealerHit1;
            x.cardCount++;
main() 中的 x

与 startGame() 中的 x 不同,因为两者都是独立的游戏对象。

最新更新