Thread: blackjack program

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    blackjack program

    hi all, next im working on a blackjack program. cards are just 2-10, giving the user 2 cards and asking 1 for another card or 2 for no.

    if they say no, hit 21, or go over they are asked if they want to play again, this is where im struggling. it doesnt start over and read two new cards like i thought it would, it just takes the same two cards. sorry about the indentation and format im working on it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
        
        int i;
        int total;
        int cardchoice, gamechoice;
        int card1, card2, newcard;
        
        srand(time(NULL));
        
        for(i = 0; i < 2; i++) {
              card1 = rand() % 10 + 1;
                if (card1 == 1) {
                          card1 = rand() % 10 + 1;
                                }
              card2 = rand() % 10 + 1;
                if (card2 == 1) {
                          card2 = rand()% 10 + 1;}
        }
        
        printf("You have the following cards: %d, %d\n", card1, card2);
       
        total = card1 + card2;
       
        printf("Your total is currently %d\n",total);
        
        for (i = 0; i < 100000000; i++) {
        printf("Would you like another card? Enter 1 for y or 2 for n: ");
        scanf("%d",&cardchoice);
        
             if (cardchoice == 1) {
        
        
              newcard = rand() % 10 + 1;
                  if (newcard == 1) {
                              newcard = rand()% 10 + 1; }
              total = total + newcard;
              
              
              
              printf("\nYou got a %d\n", newcard);
              printf("Your total is: %d\n",total);
              
              if (total > 21) {
                        printf("You lose, you went over 21.\n");
                        printf("Would you like to play again? Enter 1 for y or 2 for n: ");
                        scanf("%d", &gamechoice);
                        if (gamechoice == 1) {
                                          for(i = 0; i < 2; i++) {
                                                   card1 = rand() % 10 + 1;
                                                   card2 = rand() % 10 + 1;
                                                              }   
                                          }
                        if (gamechoice == 2) {
                                       break;
                                       }
              }
              else if (total == 21) {
                        printf("Congratulations!! You win. You got blackjack!!\n");
                        printf("Would you like to play again? Enter 1 for y or 2 for n: ");
                        scanf("%d", &gamechoice);
                                            if (gamechoice == 1) {
                                                      for(i = 0; i < 2; i++) {
                                                             card1 = rand() % 10 + 1;
                                                             card2 = rand() % 10 + 1;
                                                       }
                                              }
                                            if (gamechoice == 2) {
                                                        break;
                                            }
                                               
                        }
            }
              if (cardchoice == 2) {
                             printf("Would you like to play again? Enter 1 for y or 2 for n: ");
                             scanf("%d", &gamechoice);
                                                 if (gamechoice == 1) {
                                                     for(i = 0; i < 2; i++) {
                                                            card1 = rand() % 10 + 1;
                                                            card2 = rand() % 10 + 1;
                                                     }
                                                 }
                                       if (gamechoice == 2) {
                                                       break;
                                       }
              }    
        
    }    
    system("PAUSE");
    return 0;
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    It looks like you are dealing the player starting hand outside of you main loop. I think you might need to put it inside for it to deal new cards each game.

    also here:
    Code:
    for (i = 0; i < 100000000; i++)
    You could jsut do:
    Code:
    for(;;)
    To loop infinitely.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    yeah i tried to put that into the loop but then it puts another set of cards after you get the total from new cards.

    so if i have 2 and 10 for 12 and ask for another card im returned

    you got a 5
    your new total is 17
    your cards are 6,2
    youre total is 8
    do you want another card?

    id like to make it so the first cards you get are outside of the loop and taken in, and when those are done with (you hit 21, go over, or want to start a new game) the gamechoice of 1 gives you two new cards but it doesnt work quite right. thanks for the help though mike g. if it hits 21 or goes over after the first draw it works well

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i would put everything you have in a do-while loop, except for the first few statements (the variable declarations and srand).

    at the beginning of the do, you determine the 2 cards to be 'dealt'. then have another loop (another do-while loop) looping over the choice of hitting or staying, and exiting the inner do loop once they want to stay. you then have the 'computer' play (if you are including that, its quite easy). finally you print out the results of the game, and ask to play again or not. this is what controls your main/outer do-while loop: if they want to play again, it starts over, otherwise exit the loop and the program finishes.

    once you have it better structured you can follow it easier, hopefully, and find out where your going wrong.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    ive never done a do while loop before, do you mind showing me what you mean?

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    got it to work!!!!

    i just made a goto label, didnt even think of that. thanks for the help again yall i really appreciate it like always

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
        
        int i;
        int total;
        int cardchoice, gamechoice;
        int card1, card2, newcard;
        
        srand(time(NULL));
        
        start:
     
            for(i = 0; i < 2; i++) {
              card1 = rand() &#37; 10 + 1;
                if (card1 == 1) {
                          card1 = rand() % 10 + 1;
                                }
              card2 = rand() % 10 + 1;
                if (card2 == 1) {
                          card2 = rand()% 10 + 1;}
        }
        
        printf("\n\nYou have the following cards: %d, %d\n", card1, card2);
        
        total = card1 + card2;
       
        printf("Your total is currently %d\n",total);
            
            
        
    
        
        for (i = 0; i < 100000000; i++) {
        printf("Would you like another card? Enter 1 for y or 2 for n: ");
        scanf("%d",&cardchoice);
        
             if (cardchoice == 1) {
        
        
              newcard = rand() % 10 + 1;
                  if (newcard == 1) {
                              newcard = rand()% 10 + 1; }
              total = total + newcard;
              
              
              
              printf("\nYou got a %d\n", newcard);
              printf("Your total is: %d\n",total);
              
              if (total > 21) {
                        printf("You lose, you went over 21.\n");
                        printf("Would you like to play again? Enter 1 for y or 2 for n: ");
                        scanf("%d", &gamechoice);
                        if (gamechoice == 1) {
                                          goto start; 
                                          }
                        if (gamechoice == 2) {
                                       break;
                                       }
              }
              else if (total == 21) {
                        printf("Congratulations!! You win. You got blackjack!!\n");
                        printf("Would you like to play again? Enter 1 for y or 2 for n: ");
                        scanf("%d", &gamechoice);
                                            if (gamechoice == 1) {
                                                      goto start; 
                                              }
                                            if (gamechoice == 2) {
                                                        break;
                                            }
                                               
                        }
            }
              if (cardchoice == 2) {
                             printf("Would you like to play again? Enter 1 for y or 2 for n: ");
                             scanf("%d", &gamechoice);
                                                 if (gamechoice == 1) {
                                                     goto start; 
                                                 }
                                       if (gamechoice == 2) {
                                                       break;
                                       }
              }    
        
    }    
    system("PAUSE");
    return 0;
    }

  7. #7
    Registered User
    Join Date
    Sep 2007
    Location
    South Africa
    Posts
    20
    You should learn to use while, for and do while loops.
    You really, really should learn to indent your code properly. I did not even try looking at your code because it is too ugly and difficult to understand.
    Try never to use goto especially in the case where it is a quick fix to a problem you do not understand. Here is a working example with proper indenting that does not use goto:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
        int total, gamechoice;
        int card1, card2, newcard;
        int looping=1;
           
        do 
        {
            /* initialize random seed: */
            srand ( time(NULL) );
            total = 0;
            newcard = 0;
            card1 = rand() % 10 + 1;  
            card2 = rand() % 10 + 1;  
            printf("\nyour cards are: %d and %d\n", card1, card2);
            total = card1 + card2;
            while (1)
            {
                printf("your total is: %d\n", total);
                printf("would you like another? 1=yes 2=no\n");
                scanf("%d", &gamechoice);
                if (1 == gamechoice)
                {
                    newcard = rand() % 10 + 1;
                    printf("new card is: %d\n", newcard);
                    total += newcard;
                    if (total == 21)
                    {
                        printf("21 you win! play again? 1=yes 2=no\n");
                        scanf("%d", &gamechoice);
                        if (gamechoice == 2)
                        {
                            looping = 0;
                            break;            
                        }
                        break;
                    }
                    else if (total > 21)
                    {
                        printf("you have %d\n", total);
                        printf("you lose! Play again? 1=yes 2=no\n");
                        scanf("%d", &gamechoice);
                        if (gamechoice == 2)
                        {
                            looping = 0;
                            break;            
                        }
                        break; 
                    }
                }
                else 
                {
                    printf("play again? 1=yes 2=no\n");
                    scanf("%d", &gamechoice);
                    if (gamechoice == 2)
                    {
                        looping = 0;
                        break;            
                    }
                    break;
                }
            }
        } while (looping);
        
        system("PAUSE");
        return 0;
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A few colour-coded comments:
    Red = not needed.
    Blue = Consistency, do you do "1 == x" or "x == 1"? Decide on one and use it conistently.
    Green = move outside the loop, you shouldn't "srand" more than once.
    Quote Originally Posted by wyliek View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
        int total, gamechoice;
        int card1, card2, newcard;
        int looping=1;
           
        do 
        {
             /* initialize random seed: */
           srand ( time(NULL) );
            total = 0;
            newcard = 0;
            card1 = rand() % 10 + 1;  
            card2 = rand() % 10 + 1;  
            printf("\nyour cards are: %d and %d\n", card1, card2);
            total = card1 + card2;
            while (1)
            {
                printf("your total is: %d\n", total);
                printf("would you like another? 1=yes 2=no\n");
                scanf("%d", &gamechoice);
                if (1 == gamechoice)
                {
                    newcard = rand() % 10 + 1;
                    printf("new card is: %d\n", newcard);
                    total += newcard;
                    if (total == 21)
                    {
                        printf("21 you win! play again? 1=yes 2=no\n");
                        scanf("%d", &gamechoice);
                        if (gamechoice == 2)
                        {
                            looping = 0;
                            break;            
                        }
                        break;
                    }
                    else if (total > 21)
                    {
                        printf("you have %d\n", total);
                        printf("you lose! Play again? 1=yes 2=no\n");
                        scanf("%d", &gamechoice);
                        if (gamechoice == 2)
                        {
                            looping = 0;
                            break;            
                        }
                        break; 
                    }
                }
                else 
                {
                    printf("play again? 1=yes 2=no\n");
                    scanf("%d", &gamechoice);
                    if (gamechoice == 2)
                    {
                        looping = 0;
                        break;            
                    }
                    break;
                }
            }
        } while (looping);
        
        system("PAUSE");
        return 0;
    }
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Location
    South Africa
    Posts
    20
    thanks for pointing those out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM