The Frame Job

 

Modules - Structured Programming, Procedural Programming, Data Structures, File Streaming

 

Teacher’s Notes:

========================================================================

This assignment can include a lot of things that could make it more difficult than presented here.  In this version, the student is only required to work with String manipulation techniques coupled with clever use of IF statements.  You could however, ask the student to put this into a method/function or read the bowling scoresheets from a file.

The problem can be a little complicated so students may need to take some time to understand how scoring is done in bowling.  It might seem a but daunting but it is a good reminder that programmers might need to spend some time studying the systems that they are modeling before they can model them.

I have included a bowling scoresheet generator at the bottom of the code so that you can generate as many additional scoresheets as you may need.

Another assignment you could make out of this is to have the players make the Bowling Scoresheet Generator.  Or have some students make the Generator and others make the Scoring Program.

 

Assignment:

========================================================================

 

In the game of10-pin bowling, a player plays in 10 rounds called FRAMES.  In each frame, the player has two chances to roll a ball and knock down all 10 pins.  If the player knocks down all 10 pins on the first roll, it is called a STRIKE and the player does not roll again for that frame.  A strike is indicated by an X on the score sheet.  If the player does not knock down all of the pins on the first roll, the number of pins they knocked down is recorded on the score sheet and then they have a second roll.  If they knock down all of the pins on the second roll, it is called a SPARE.  A spare is indicated by a / on the score sheet.  If they fail to knock down all of the pins on the second roll, the number of pins they knocked down on the second roll is recorded on the score sheet. 

Example of ONE frame: 

X - indicates a strike

6/ - indicates that the player knocked down six pins on the first roll and then got the other four pins on the second roll.

63 - indicates that the player knocked down six pins on the first roll and three pins on the second roll.

STRIKES:

If a player gets a strike, they are awarded 10 points, plus the next two rolls.  If the strike is in the 10th frame, the player gets two additional rolls to complete the scoring for that frame but those two rolls are not counted as part of the score outside of the 10th frame.

Example:

XXX indicates three strikes in a row and the player would get 30 points for that frame.

X45 indicates a strike followed by a 4 and 5.  The player would get 19 points for that frame.

x4/ indicates a strike followed by a spare.  The player would get 20 points for that frame.

SPARES:

If a player gets a spare, they are awarded 10 points, plus the next one roll.  If the spare is in the 10th frame, the player gets an additional roll to complete the scoring but that roll is not counted as part of the score outside of the 10th frame.

Example:

4/5 indicates a spare followed by a 5.  The player would get 15 points in this frame.

4/X indicates a spare followed by a strike(10).  The player would get 20 points for this frame.

NOT STRIKE OR SPARE:

The player gets the sum of the two rolls.

Example: 

35 indicates the player would get 8 points for that frame.

09 indicates the player would get 9 points for that frame.

 

ASSIGNMENT:

You have been hired by a bowling alley to write a program that will automatically score a bowling scoresheet.  The program will receive as input a single string representing all 10 frames of bowling scores.  The program output will be a single integer representing the entire score for the game.

Sample Input:

XX801044XXXXX3/

Sample Output:

196

 

Solution (JAVA):

========================================================================

 
import java.util.Scanner;
public class BowlingScore {
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        String frames = input.next();
        
        int framesCounted = 0, currentIndex = 0, score = 0;
        
        while(framesCounted < 10) {
            if(frames.charAt(currentIndex) >='0' && frames.charAt(currentIndex) <= '9') {
                score += frames.charAt(currentIndex)-48;
                currentIndex++;
                if(frames.charAt(currentIndex) >='0' && frames.charAt(currentIndex) <= '9') {
                    score += frames.charAt(currentIndex)-48;
    
                }
                else {
                    score += 10 - (frames.charAt(currentIndex-1)-48);
                    if(frames.charAt(currentIndex+1) == 'X')
                        score += 10;
                    else 
                        score += frames.charAt(currentIndex+1)-48;
                }
            }
            else if (frames.charAt(currentIndex) == 'X')
            {
                score += 10;
                //Strike followed by another strike
                if(frames.charAt(currentIndex+1) == 'X') {
                    score += 10;
                    if(frames.charAt(currentIndex+2) == 'X')//followed by another strike
                        score += 10;
                    else
                        score += frames.charAt(currentIndex+2)-48;//followed by a single
                }
                else {
                    if(frames.charAt(currentIndex+2)=='/')
                        score+=10;
                    else
                        score += frames.charAt(currentIndex+1)-48 + frames.charAt(currentIndex+2)-48;
                }
                        
            }
            framesCounted++;
            currentIndex++;    
            
            
            //System.out.println("CurrentIndex: " + currentIndex);
            //System.out.println("Frames Counted: " + framesCounted);
        }
        System.out.println(score);        
            
    }
}
 
 

Bowling Score Sheet Generator (JAVA):

========================================================================

  
public class BowlingScoreGenerator {
    public static void main(String[] args) {
        
        double event = 0;
        int shot1= 0, shot2 = 0;
        boolean lastStrike = false;
        boolean lastSpare = false;
        String frames = "";
        
        for(int x = 0; x < 10; x++) {
            event = Math.random();
            if(event <= 0.4){//This is a frame where the player did not knock down all 10 pins
                shot1 = (int)(Math.random()*10);//the first shot must be less than 10
                do{
                    shot2 = (int)(Math.random()*10);// the two shots cannot be 10 or more
                }while(shot1 + shot2 >= 10);
                frames += shot1;
                frames += shot2;
            }
            else if(event <= 0.7){//This is a frame where a spare was scored
                frames += (int)(Math.random()*10);//the first shot must be less than 10
                frames += "/"; // indicating the rest of the pins were knocked down
                if(x == 9)
                    lastSpare = true;
            }
            else{ //This is where a strike is scored
                frames += "X";
                if(x == 9)
                    lastStrike = true;
            }
            
            //frames += " ";
            
            if(lastSpare){//if the last shot was a spare, there is one more shot
                shot1 = (int)(Math.random()*11);
                if(shot1 == 11)
                    frames += "X";
                else
                    frames += shot1;
            }
            else if (lastStrike) {//if the last shot was a strike, there are two more shots
                event = Math.random();
                if(event <= 0.4){//This is a frame where the player did not knock down all 10 pins
                    shot1 = (int)(Math.random()*10);//the first shot must be less than 10
                    do{
                        shot2 = (int)(Math.random()*10);// the two shots cannot be 10 or more
                    }while(shot1 + shot2 >= 10);
                    frames += shot1;
                    frames += shot2;
                }
                else if(event <= 0.7){//This is a frame where a spare was scored
                    frames += (int)(Math.random()*10);//the first shot must be less than 10
                    frames += "/"; // indicating the rest of the pins were knocked down
                    if(x == 9)
                        lastSpare = true;
                }
                else{ //This is where a strike is scored
                    frames += "X";
                    shot1 = (int)(Math.random()*11);
                    if(shot1 == 11)
                        frames += "X";
                    else
                        frames += shot1;
                }
            }    
        }
    
        System.out.println(frames);
    }
}