Simple blackjack program

This is a discussion on Simple blackjack program within the C Programming forums, part of the General Programming Boards category; Hello, I'm writing a simple blackjack program and I can get the user's cards to display but I can not ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Simple blackjack program

    Hello,

    I'm writing a simple blackjack program and I can get the user's cards to display but I can not get the dealer's cards to display I tried inserting the pause function on every line in my code but I can not get the cards to display, also after pressing y or Y to play again the program terminates and will not restart. Any direction would be greatly appreciated, here is my code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char *argv[] ) {
    
    int cards[52] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                     2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                     2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                     2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
    
    int deal( int cards[] ); 
    void shuffle( int wDeck[][ 13 ] );
    srand(time(NULL)); 
    int showCard( int card ); 
    int totalPlayerHand = 0;
    int totalDealerHand = 0;
    char hit = 'H';
    char playAgain = 'Y';
    void pause( int secs );
    
    
    int balance = 0;
    int wager = 0;
    int newBalance = 0;
    
    puts( "Enter your balance: ");
    scanf( "%d", &balance );
    while( balance <= 0 ){
          puts( "You entered an invalid balance!!!" );
          puts( "Enter your balance: ");
          scanf( "%d", &balance );
    }
    puts( "Enter your wager: ");
    scanf( "%d", &wager );
    while( wager > balance || wager < 10 ){
           puts( "Enter a valid wager: ");
           scanf( "%d", &wager );
    }
    
    while ( toupper(playAgain) == 'Y' && balance > 0 ){     
    
          int card1 = deal( cards );  
          int card2 = deal( cards );
    
          totalPlayerHand = showCard( card1 ); 
          totalPlayerHand += showCard( card2 );
    
          printf( "You have a %d.\n", totalPlayerHand );
                  while ( totalPlayerHand < 21 && toupper(hit) == 'H' ){
                        getchar();
    
                        printf( "Do you want to hit or stay?\n" );
                        printf( "Press H for hit or S for stay.\n" );
                        scanf( "%c", &hit );
    
                        if( toupper(hit) == 'H' ){
                            int card = deal( cards );
                            totalPlayerHand += showCard( card );
                            printf( "You have %d.\n", totalPlayerHand );
                        }
    
                        else if( toupper(hit) == 'S' ){
                             printf( "You have %d.\n", totalPlayerHand );
                        }       
                 }
    
                 if( totalPlayerHand > 21 || totalPlayerHand < totalDealerHand ){
                     printf( "You lose.\n" );
                     
                     newBalance = balance - wager;
                     printf( "Your balance is $%i.\n", newBalance );
                     
                     
                 }
                  
         while ( totalDealerHand > 17 || totalDealerHand > totalPlayerHand || totalDealerHand > 21 ){
     
                int card1 = deal( cards );  
                int card2 = deal( cards );
                
                totalDealerHand = showCard( card1 ); 
                totalDealerHand += showCard( card2 );
                        
                printf( "The dealer has %d \n", totalDealerHand );
                      
                if( totalDealerHand < 17 ){
                    int card = deal( cards );
                    totalDealerHand += showCard( card );
                        
                    printf( "The dealer has %d \n", totalDealerHand );       
                }
    
                else if( totalDealerHand > 21 || totalDealerHand < totalPlayerHand ){
                    printf( "You won!!!\n" );
                    newBalance = balance + wager;
                    printf( "Your balance is $%i.\n", newBalance );
                }
         }    
                 
                 
            
    printf( "Do you want to play again press 'Y' or 'N' \n" );
    scanf( "%c", &playAgain );
    getchar();             
    }
    
    
    
    
    system( "PAUSE" );
    return 0;
    }
    
    
    int deal( int cards[] ){
        
        int value;
        int random = 0;
    
        random = rand() % 52 + 1;    
        
        while( cards[random] == 0 ) {
          random = rand() % 52 + 1;
        }
      
        value = cards[random];
        cards[random] = 0;
      
    return value; 
      
    }
    
    void shuffle( int wDeck[][ 13 ] ){
         int row, column, card;
         
         for( card = 1; card <= 52; card++ ){
              do{
                   row = rand() % 4;
                   column = rand() % 13;
              }
              while( wDeck[ row ][ column ] != 0 );
              
              wDeck[ row ][ column ] = card;
         }
    }
    
    int showCard( int card ){
        int value;
        
        if( card <= 10 ) {
               printf( "%d ", card );
               value = card;
        }
          
          else if( card == 11 ) {
               printf( "J " );
               value = 10;
          }
    
          else if( card == 12 ) {
               printf( "Q ");
               value = 10;
          }
           
           else if( card == 13 ) {
                printf( "K " );
                value = 10;
           }
          
          else if( card == 14 ) {
               printf( "A " );
               value = 11;
          }
    
    return value;
    }
    
    void pause( int secs ){
         
       int icurrentTime = 0;
       int iendtime = 0;
    
       icurrentTime = time( NULL );  
      
       while( ( iendtime - icurrentTime ) < secs ){ 
           iendtime = time( NULL );   
       }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't have your program looping. You need something like:
    Code:
    int main( void )
    {
        ...some stuff...
        do
        {
            ...all this stuff...
            prompt for yes / no
        } while( answer == 'y' );
    
        ...possibly more stuff...
    
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 10:13 PM
  2. Homework Help, simple program, unsure newbie :(
    By Aslan14 in forum C Programming
    Replies: 13
    Last Post: 11-14-2010, 04:07 PM
  3. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21