mardi 4 mai 2021

Issue with getting random number from Array in C

I have some code for choosing two casino games. I'm having an issue with the first game choice, blackjack (it's just a very simple version). I have an array of card numbers. I randomly generate an index number and set the player or dealer's card as the number the index number represents.

I have an option to choose a third card. If the player says yes, a third card is randomly generated for both the player and the dealer (to keep things even). At first, the third card would only equal zero, no matter what I did.

I now have a number generating for card three, except, the third card number is always way too large and is not any of the numbers in the array. Even when I select no, the third number becomes the weird, overly large value. I've tried changing things around, working on the if statement, and other methods but so far nothing works.

also I apologize for the mess of code, I don't intend to really change it unless it causes an issue/error.

Code

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




Aucun commentaire:

Enregistrer un commentaire