【问题标题】:Issue with getting random number from Array in C从 C 中的数组获取随机数的问题
【发布时间】:2021-05-05 02:23:32
【问题描述】:

我有一些代码可以选择两个赌场游戏。我对第一个游戏选择有问题,二十一点(它只是一个非常简单的版本)。我有一系列卡号。我随机生成一个索引号,并将玩家或庄家的牌设置为索引号所代表的数字。

我可以选择第三张卡。如果玩家说是,则为玩家和庄家随机生成第三张牌(以保持平衡)。起初,无论我做什么,第三张牌都只会等于零。

我现在有一个为第三张牌生成的数字,除了第三张牌号总是太大而且不是数组中的任何数字。即使我选择否,第三个数字也会变成奇怪的、过大的值。我尝试过改变事物,使用 if 语句和其他方法,但到目前为止没有任何效果。

我也为代码的混乱道歉,除非它导致问题/错误,否则我不打算真正更改它。

代码

int main(){
//seeding
srand(time(0));

// variables
int credits = 500;
int choice, bet, rNum, guess, winnings;
int pCard1, pCard2, pCard3, dCard1, dCard2, dCard3, pHand, dHand, ace, randomIndex;
char pAgain, third;

    while(pAgain != 'n' && pAgain != 'N' && credits > 0){

        printf("\nWhat you you like to play?");
        printf("\n1: Blackjack\n2: Roulette\n");
        printf("\nEnter 1 or 2 here: ");
        scanf( "%d", &choice);

        switch(choice){
            //blackjack
            case 1:
                printf("You chose Blackjack.\n\n");

                //bet set
                printf("\nHow much would you like to bet?: ");
                scanf( "%d", &bet);


                //card array
                int cardNums[14] = { 1,2,3,4,5,6,7,8,9,10,10,10,10,11 };

                //dealer cards
                randomIndex = rand() % 14;
                dCard1 = cardNums[randomIndex];

                randomIndex = rand() % 14;
                dCard2 = cardNums[randomIndex];

                //player cards

                //card 1
                randomIndex = rand() % 14;
                pCard1 = cardNums[randomIndex];
                if(pCard1 == 1 && pCard1 == 11){
                    printf("\nThis is an Ace! Do you want this to be an eleven or one?: ");
                    scanf( "%d", &ace);
                        if(ace == 1){
                            pCard1 = 1;
                        } //inner if end
                        else if(ace == 11){
                            pCard1 = 11;
                        } //inner if end
                } //pcard1 outer if end

                //card 2
                randomIndex = rand() % 14;
                pCard2 = cardNums[randomIndex];
                if(pCard2 == 1 && pCard2 == 11){
                    printf("\nThis is an Ace! Do you want this to be an eleven or one?: ");
                    scanf( "%d", &ace);
                        if(ace == 1){
                            pCard2 = 1;
                        } //inner if end
                        else if(ace == 11){
                            pCard2 = 11;
                        } //inner if end
                } //pcard2 outer if end

                printf("\nYour first card is a %d. Your second card is a %d.\n", pCard1, pCard2);

                printf("\nDo you want to pick a third card? (y/n): ");
                scanf( " %c", &third);

                if(third == 'y' && third == 'Y'){
                    randomIndex = rand() % 14;
                    pCard3 = cardNums[randomIndex];

                    randomIndex = rand() % 14;
                    dCard3 = cardNums[randomIndex];

                    if(pCard3 == 1 && pCard3 == 11){
                        printf("\nThis is an Ace! Do you want this to be an eleven or one?: ");
                        scanf( "%d", &ace);

                        if(ace == 1){
                            pCard3 = 1;
                        } //inner if end
                        else if(ace == 11){
                            pCard3 = 11;
                        } //inner if end
                        else{} //else end

                    } //middle if end

                } //outer if end
                else if(third == 'y' && third == 'Y'){
                    pCard3 = 0;
                    dCard3 = 0;
                } //else if end
                else{} //else end

                pHand = pCard1 + pCard2 + pCard3;
                dHand = dCard1 + dCard2 + dCard3;

                printf("Your cards are a %d, a %d, and a %d.", pCard1, pCard2, pCard3);
                printf("\nYour hand is: %d", pHand);
                printf("\nThe Dealer's hand is: %d", dHand);

                if(pHand > dHand && pHand <= 21){
                    printf("\n\nYou win!");
                } //if end
                else if(pHand == dHand){
                    printf("\n\nIt's a draw.");
                } //else if end
                else if(pHand > 21 && dHand > 21){
                    printf("\n\nIt's a draw.");
                }
                else if(pHand < dHand || pHand > 21){
                    printf("\n\nYou lost.");
                } //else if end
                else{} //else end

                break;

            //roulette
            case 2:
                printf("You chose Roulette.\n\n");

                //bet set
                printf("\nHow much would you like to bet?: ");
                scanf("%d", &bet);

                //generate random num
                rNum = 1 + (rand() % 36);

                //guess
                printf("Choose a number between 1-36: ");
                scanf("%d", &guess);

                //win lose
                if(guess == rNum){
                    printf("You won!\n");

                    winnings = bet + winnings;
                    credits = bet + winnings;

                    printf("\nYour current credits: %d", credits);
                } //if end
                else{
                    printf("You lost. The number was %d.\n", rNum);
                    credits = credits - bet;

                    printf("\nYour current credits: %d", credits);
                } //else end
                break;

            } //switch end

         //play again
        if(credits != 0 && credits > 0){
            printf("\nWould you like to play again? (y/n): ");
            scanf(" %c", &pAgain);
        } //if end
        else{
            printf("\nYou have no more credits");
        } //else end

    } //while end
} //main end

【问题讨论】:

    标签: arrays c random


    【解决方案1】:

    if(third == 'y' &amp;&amp; third == 'Y')

    这总是错误的,因为third 不能同时等于'y''Y'。我认为您的意思是 || 而不是 &amp;&amp;。您还有其他几个类似的错误,例如if(pCard1 == 1 &amp;&amp; pCard1 == 11).

    【讨论】:

    • 哦!谢谢!我总是对 && 和 || 感到困惑
    • 虽然有一个快速的问题,暂时(pAgain != 'n' && pAgain != 'N' && credits > 0),我需要这样做||也?我试过了,但它没有把 n 读成 no。
    • @Kaiju:那是正确的。只要满足所有条件,您就想继续前进:pAgain 既不是n 也不是Ncredits 是肯定的。您还可以应用 De Morgan's law 来查看当 either pAgain 等于 nNcredits 变为 &lt;= 0 时循环将终止。
    猜你喜欢
    • 1970-01-01
    • 2012-04-13
    • 2011-05-21
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多