Ridiculous infinite loop

This is a discussion on Ridiculous infinite loop within the C Programming forums, part of the General Programming Boards category; Hi Everyone, I'm writing a blackjack game for c and my users turn works fine. The array I made has ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30

    Ridiculous infinite loop

    Hi Everyone,

    I'm writing a blackjack game for c and my users turn works fine. The array I made has an index of 52. I'm "shuffling" the deck with srand and everything seems to work other than the computer's turn. I'm making a limit that the computer will only draw cards up until it gets an accumulated sum larger than 15.

    When I run the program, the computer's turn keeps going and going and going. I've tried several different loop structures and condition statements but it keeps doing the same thing. Can anyone else see what error there is that is causing this?? I'd really really appreciate any input at all!

    here's the code for the user and computer:
    Code:
     
    }
      int Users_Turn(int deck[])
      {
           int card;
           int totalscore=0;
           int score;
           int choice;
           
           choice=Continue();
            
           while(totalscore<21 && choice==1)       
              {
               card=Draw_One_Card(deck);
               Display_Card(card);
               score=Score_Card(card);
               totalscore=totalscore+score;
               printf("\n\nYour total score so far in this hand is: %i\n\n", totalscore);
               
               if (totalscore>21)
                  printf("Sorry you lose this round you owe me a peanut!HAHAHA");
               else
                   choice=Continue();
               }   
            while(totalscore<21 && choice==2)
                {
                Computer_Turn(deck);           
                }
            
            
      return totalscore;
      }      
      
       int Computer_Turn(int deck[]) 
        {
           int card;
           int totalscore=0;
           int score;
           int finalscore;
            
           if(totalscore<15)       
              {
               card=Draw_One_Card(deck);
               Display_Card(card);
               score=Score_Card(card);
               totalscore=totalscore+score;
               printf("\n\n My total score so far in this hand is: %i\n\n", totalscore);
               }
            else   
               finalscore=totalscore;
          
           return finalscore;
    }
    Thanks a ton!!

    -Melodia

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One thing wrong is that you have no variable to "catch" the return from the computer's turn function.

    So that number bytes the dust.

    The while loop just keeps sending the program back to the computers turn function, since nothing is being incremented or saved.

    You don't need a "totalscore", affecting the computers turn. If the game is over from the human going bust, then just call the endGame() function, after you break out of the main game loop. Many ways to do this.

    You need a flag like "gameOn", and as long as gameOn is 1, then keep playing. When it's set to 0, the game is over.

    In rough pseudo-code, the main game loop is like:

    Code:
    int gameOn = 1;
    
    do {
       player(s) ante up an inital bet
       player(s) and dealer, are dealt two cards
       initial round of betting
       players given extra cards if they desire
       any player going bust is out of that hand
       if any players left, another round of betting
       dealer gets extra cards or holds
       best hand wins, tie goes to the dealer
       query user if they want to play another hand
       if no, gameOn = 0;
    }while(gameOn == 1)
    Whether any player goes bust has nothing to do with whether the dealer plays on or not. The dealer always finishes a hand.
    Last edited by Adak; 11-15-2009 at 10:40 PM.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30

    Tried a thousand things still doesn't work

    Thank you Adak for replying to my post. I'm still having ridiculous troubles with this program. I've tried what feels like a million different loop structures and still it doesn't work. I don't want to annoy with a long post of code, but I'm starting to get desperate for help. Thank you again to anyone who responds I really appreciate it!

    There are two of my tries in user_turn which is still in the code one is commented out. I've tried numerous different bracket placements and as I said loop structures but cannot seem to get it. I keep getting an error in User_Turn that says " [Warning] control reaches end of non-void function"

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h> 
    
    #define true 1
    #define false 0
    
    /* function prototypes */
    void clean (void);
    char GetChoice();			
    void Initialize_Deck(int deck[]);
    int Random_Number();
    void Shuffle_Deck(int deck[]);
    int Draw_One_Card(int deck[]);
    void Display_Card(int card);
    int Score_Card(int card);
    int Users_Turn(int deck[]);
    int Continue();
    int Computer_Turn(int deck[]);
    
    #define QUIT  'Q'	
    #define PLAY_GAME 'P'
    
    
    
    #define DECK_SIZE  53   
    int deck[DECK_SIZE];
    /*********************************************************************
     * main() 
     *		
     *********************************************************************/
    
    int main()
    {
        
    	int choice;			
        
    	printf(" Welcome! I'm happy to have someone to play blackjack with!\n\n");
    
    	do 
    	{
    		choice = GetChoice();
    		
    		switch (choice)
    		{
               case PLAY_GAME:
                     printf("Here are the rules of the game:\n 1. Ace is worth 1 point\n 2. Cards 2 through 10 are worth their face value.\n");
                     printf(" 3. Jack, Queens and Kings are worth 10 points each\n 4. Your goal is to obtain 21 points, you get one turn only to draw cards\n");
                     printf(" 5. You get a total of 10 peanuts, each player bets one peanut per game\n 6. Whoever scores closer to 21 gets the others peanut\n");
     
                     int users_peanuts=10;
                     int computers_peanuts=10;
                     int users_score;
                     int dealers_score;
                     
                     
                     Initialize_Deck(deck);
                     printf("\n\nLet me grab the deck and then we can play!\n\n");
                     Shuffle_Deck(deck);
                     printf("The deck has been shuffled.\n\nLet's begin, you go first\n\n\n");
                     
                     users_score=Users_Turn(deck);
                     dealers_score=Computer_Turn(deck);
                     
                     if(dealers_score>users_score)
                        {
                         users_peanuts--;
                         computers_peanuts++;
                         }     
                               if (users_peanuts==0)
                                  {
                                   printf("\nYou have no more peanuts! I have them all! YOU LOSE!\n");
                                   GetChoice();
                                   }
                     else
                         {
                         computers_peanuts--;
                         users_peanuts++;
                         }
                                if (computers_peanuts==0)
                                {
                                   printf("\n I can't believe I lost....I demand a rematch!\n");
                                   GetChoice();
                                }  
                    /*    
                     users_score=Users_Turn(int deck[]);
                  dealers_score
               */
                     break;
                     
    			
    
                 default:
    				if (choice != QUIT)
    					printf("\n That is NOT a valid choice. Please try again...\n");
    				break;
    
    		}  /* end switch  */
    
    	} while (choice != QUIT);
        printf("Finishing the program.\n");
        system("PAUSE");
        return 0;
        
    }
    
    void clean (void)
    {
         char dummy;
         do 
         {
             scanf("%c",&dummy); /* read a single char */
         } while(dummy != '\n'); /* if it was the NEWLINE we are done */
    }
    
    
    
    char GetChoice()
    {	
    	char choice;
    	
    	printf("\n\n Would you like to continue?\n");
    
    	printf("%c: Quit the game\n", QUIT);
    	printf("%c: Play the game\n", PLAY_GAME);
    
    	scanf("\n%c", &choice);
    	clean(); /* empty the input buffer */
    	printf("\n\n"); 
    
    	return choice;
    }
    
      void Initialize_Deck(int deck[])
      {
           int i;
           for (i=0; i < DECK_SIZE; i++)
               {
               deck[i]=i;
               }
      }
    
    
      void Shuffle_Deck(int deck[])
      {
           int i;
           int num;
           int temp;
           
           for (i=51; i > 0; i--)
           {
               srand ( (unsigned)time ( NULL ) );
               num=Random_Number(i);
               temp=deck[num];
               deck[num]=deck[i];
               deck[i]=temp;
           }
      }
      int Users_Turn(int deck[])
     { 
           int card;
           int totalscore=0;
           int score;
           int choice;
           
           choice=Continue();
          /*  
           while(totalscore<21 && choice==1)       
              
               card=Draw_One_Card(deck);
               Display_Card(card);
               score=Score_Card(card);
               totalscore=totalscore+score;
               printf("\n\nYour total score so far in this hand is: %i\n\n", totalscore);
               
               if (totalscore>21)
                  
                  printf("Sorry you lose this round you owe me a peanut!HAHAHA");
                  totalscore=0;
                  return totalscore;
                  
               else
                  choice=Continue();
                  
           
           while(choice==2)
               
                return totalscore;
            */
            if (choice==2)
               
               return totalscore;
               
            if (choice==1&&totalscore<21)
               {
                card=Draw_One_Card(deck);
                Display_Card(card);
                score=Score_Card(card);
                totalscore=totalscore+score;
                printf("/n/nYour total score so far this hand is:%i\n\n",totalscore);
                
                if(totalscore>21)
                   totalscore=0;
                   return totalscore;   
    }              
    }        
    
       int Computer_Turn(int deck[]) 
        {
           int card;
           int totalscore=0;
           int score;
           
            
           if(totalscore<21)       
              
               card=Draw_One_Card(deck);
               Display_Card(card);
               score=Score_Card(card);
               totalscore=totalscore+score;
               printf("\n\n My total score so far in this hand is: %i\n\n", totalscore);
                
                if (totalscore>21)
                  printf("I lost this round here's your peanut!\n");
           
          
           return totalscore;
    }           
           
      int Random_Number(int upper)
      {
          return rand()%(upper+1);  
      }          
    
      int Draw_One_Card(int deck[])
      {
          if (deck[52]==0)
             { 
              Shuffle_Deck(deck);
              }
              deck[52]--;
              
      return (deck[deck[52]]);
    }
    
     void Display_Card(int card)
     {    
         
         int suit = card/13;
         int rank = card%13;
         rank=rank+1;
         
         switch(rank)
         {
                case 1:
                     printf("The card drawn is the Ace");
                     break;
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                     printf("\n The card drawn is the %i", rank);
                     break;
                case 11:
                     printf("\n The card drawn is the J");
                     break;
                case 12:
                     printf("\n The card drawn is the Q");
                     break;
                case 13:
                     printf("\n The card drawn is the K");
                     break;
         }            
                
         switch(suit)
         {      
                case 0:
                     printf(" of SPADES\n");
                     break;
                case 1:
                     printf(" of HEARTS\n");
                     break;
                case 2:
                     printf(" of DIAMONDS\n");
                     break;
                case 3:
                     printf(" of CLUBS\n");
                     break;
         }      
     }
     int Score_Card(int card)
     {
         
         int rank=card%13 + 1;
         
         switch(rank)
         {
          case 1:
          case 2:
          case 3:
          case 4:
          case 5:
          case 6:
          case 7:
          case 8:
          case 9:
          case 10:
               return rank;
               break;
          
          case 11:
          case 12:      
          case 13:
               return 10;
               break;
          
          default:
    	  if (rank>10)
    	  printf("\n That is NOT a valid score....\n");
          return 0;		
            	break;
               }
         
    }
     int Continue()
     {
      int choice;
      printf("Would you like to draw a card?\n Enter 1 to draw:\n Enter 2 to not draw:\n");
      scanf("%i", &choice);
      clean();
      return choice;
     }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Unfortunately, what you're stuck on, is the flow of the program, itself.

    All the details in your program shouldn't be there, until the flow of the program is correct. THEN you start adding in the details - and things not related to program flow ARE details, in this context.

    Best thing to do is to STOP trying to make the program run right - instead, sit down with a deck of cards, and play a few games of blackjack.

    Then play another game, and have the computer program set up with getchar() lines of code, to help you play the game by hand at the table, but also hit a key and see what your program is doing, that you're NOT doing, as you play.

    IMO this program should be largely thrown into a comment section reserve, like this:

    Code:
    /*
    code limbo:
    
    code you might want parts of, etc.
    
    */
    I can't follow it, because the logic leaves me twisting in the wind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-15-2009, 12:24 AM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 01:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 12:17 PM

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