Thread: Bowling Game

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Question Bowling Game

    I am having a problem with my bowling program. Everything works fine until a strike is bowled. Once a strike is bowled the scores for all the next inputs come out incorrectly. Any help would be good.
    Code:
    #include <stdio.h>
    #define True 1
    #define False 0
    
       int main()
       {
          int roll_1, roll_2, roll_3, sum, count,true;
          int strike, spare;
          count = 1;
          sum = 0;
          roll_1 = 0;
          roll_2 = 0;
         
          do
          { 
             printf("Enter roll 1 of frame ");
             scanf("%d",&roll_1);
             if (roll_1 == 10)
             {
                printf("Nice strike! ");
                strike = False;
                sum = sum + 10;
                printf("%d",sum);
             
             }
             else if (roll_1 <= 10)
             {
                printf("Enter roll 2 of frame ");
                scanf("%d",&roll_2);
                if (roll_1 + roll_2 == 10)
                {
                   printf("Nice spare!");
                   spare = False;
                   sum = sum + roll_1 + roll_2;
                   printf("%d",sum);
    
                }     
                else if (roll_1 + roll_2 > 10)
                {
                   do
                   {
                      printf("Enter roll 2 of frame\n\n ");
                      scanf("%d",&roll_2);
                   }
                   while (roll_1 + roll_2 > 10);
                }
                else
                {
                   sum = sum + roll_1 + roll_2;
                    printf("%d",sum);
    
                }
             }
             else
             {
                printf("Enter roll 2 of frame ");
                scanf("%d",&roll_2);
                if (roll_1 + roll_2 == 10)
                {
                   printf("Nice spare!");
                }
                sum = sum + roll_1 + roll_2;
                 printf("%d",sum);
    
             }
          
             if(strike == False)
             {
                printf("%d",sum);
                sum = sum + 10 + roll_1;
             }
             
             else if(spare == False)
             {
                printf("%d",sum);
                sum = sum + roll_1; //+ throw_2;
             }
                           
          
             if(sum == 300)
             {
                printf("You bowled a perfect game!");
             } 
          
             if (count >= 10)
             {
                printf(" %d", sum);
             }
             ++count;
          }
          while (count <= 10);
       
       
          return(0);
       }

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Here's some code I wrote...Use it for reference.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    
    {
    
    FILE *fp;
    
    int N, x;
    char fname[21];
    
    printf("Enter the filename: ");
    
    scanf("%s", fname);
    
    //Open
    
    fp = fopen(fname, "r");
    
    //If no file
    
    if (ifp == NULL)
    
    {
    
      printf("Error, %s not available\n", fname);
    
      exit(1);
    }
    
     
       int scores[22];
       int i, cc;
       char ss;
       
       i = 0;
    
    //Array storage
      
    
       fscanf(ifp, "%c", &ss);
    
       while(ss != '-')
       {
       
       if(ss == 'X')
       {
       cc = 10;
       scores[i] = cc;
       i = i + 1;
       }
       
       else if(ss == '/')
       {
       cc = 10 - scores[i-1];
       scores[i] = cc;
       i = i + 1;
       }
       
       else if(ss >= '0' && ss <= '9')
       {
       cc = ss - '0';
       scores[i] = cc;
       i = i + 1;
       }
    
      
       fscanf(ifp, "%c", &ss);   
       
       
       
       }
      scores[i] = -1;
    
    int howmany,sum;
    howmany = i;
    sum = 0;
    
    //Prints SUM
    
    
    
       
    
    printf("N = %d\n", howmany);
    
    i = 0;
    while(scores[i] != -1)
    {
    sum = sum + scores[i];
    i++;
    }
    printf("SUM: %d\n",sum);
    
    
    
    printf("----------------------\n");
    
    
    
    int f,e,total,y;
    
    y = 0;
    e = 0;
    total = 0;
    
    //Scores total with strike, spare rules
    
    for (y=0;y<22;y++)
       {
    
    if(scores[e] == 10)
       {
       total = total + 10 + scores[e+1] + scores[e+2];
       printf("Frame %2d: Total %2d\n",y+1, total);
       e = e + 1;
       printf("STRIKE!\n");
       }
    else if(scores[e] + scores[e+1] == 10)
       {
       total = total + 10 + scores[e+2];
       printf("Frame %2d: Total %2d\n",y+1, total);
       e = e + 2;
       printf("SPARE!\n");
       }
    else if(scores[e] == -1)
       { 
       printf("----------------------\n");
       printf("Your final score is: %2d", total);
       break;
       }
    else
       {
       total = total + scores[e] + scores[e+1];
       printf("Frame %2d: Total %2d\n",y+1, total);
       e = e + 2;
       }
       }
    
    return 0;
    
    }
    Here is the file it reads from...Use it for formatting references.
    Code:
    X X X X X X 9 / 9 / 9 / 9 / -1
    Let me know if this helps...
    Last edited by DuckCowMooQuack; 04-18-2011 at 09:23 PM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Are you helping, DuckCowMooQuack ?

    studalicious16 the variables strike and spare are not initialized. If they're not explicitly assigned 'False' they may have any random value.

    I have no idea how the rules of bowling scoring go. I've played the game but could not figure out what the hell the screen was showing me. Everyone else played like they understood it. But I doubt anyone did.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Quote Originally Posted by nonoob View Post
    Are you helping, DuckCowMooQuack ?

    studalicious16 the variables strike and spare are not initialized. If they're not explicitly assigned 'False' they may have any random value.

    I have no idea how the rules of bowling scoring go. I've played the game but could not figure out what the hell the screen was showing me. Everyone else played like they understood it. But I doubt anyone did.
    How to score a game of Bowling...How to Score a Game of Bowling - Bowling Scoring

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Thanks, DuckCowMooQuack.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Quote Originally Posted by nonoob View Post
    Thanks, DuckCowMooQuack.
    No problem...Quite honestly, the knoledge of knowing how to score a bowling game could be the difference between life and death! I wouldn't want anyone to die

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a bowling program...
    By fuze in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2008, 04:06 AM
  2. bowling games in c
    By zarith in forum C Programming
    Replies: 2
    Last Post: 10-09-2007, 06:41 AM
  3. A Different Bowling Program
    By Mike56p in forum C Programming
    Replies: 3
    Last Post: 12-08-2005, 09:47 AM
  4. Bowling again.
    By fatty acid in forum C Programming
    Replies: 4
    Last Post: 11-30-2005, 02:12 AM
  5. Bowling for Columbine
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 97
    Last Post: 09-09-2003, 07:48 PM