Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IT 212 -- Project 2 Goal: write an application that computes bowling scores. Bowling terminology: Bowling is a game in which the player attempts to

IT 212 -- Project 2

Goal: write an application that computes bowling scores.

Bowling terminology:

Bowling is a game in which the player attempts to knock down ten ten bowling pins with a bowling ball. A bowling game consists of ten frames. In a frame, a player rolls one or two balls to knock down as many pins as possible. A player bowls a strike in a frame if he or she knocks down all ten pins with one ball. A strike is denoted by X. A player bowls a spare in a frame if he or she knocks down all ten pins with two balls. A spare is denoted by /. An open frame results if a player fails to knock down all ten pins with two balls in that frame.

Bowling scoring: The score for an open frame is the number of balls knocked down in that frame. Here is a bowling game consisting of only open frames (no strikes or spares): Game 1

Frame Number 1 2 3 4 5 6 7 8 9 10 Bonus +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ Ball Scores | 8 | 0 | 7 | 2 | 6 | 0 | 6 | 2 | 0 | 8 | 6 | 1 | 9 | 0 | 8 | 0 | 8 | 1 | 5 | 2 | | | +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ Frame Scores | 8 | 9 | 6 | 8 | 8 | 7 | 9 | 8 | 9 | 7 | | +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ Running Total | 8 | 17 | 23 | 31 | 39 | 46 | 55 | 63 | 72 | 79 | | +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ 

The total score for Game 1 is 79. The score for a spare is 10 plus the bonus ball in the next frame. Game 2

Frame Number 1 2 3 4 5 6 7 8 9 10 Bonus +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ Ball Scores | 8 | 1 | 7 | / | 5 | 3 | 0 | / | 7 | / | 9 | / | 9 | 0 | 8 | / | 8 | 1 | 5 | / | 1 | | +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ Frame Scores | 9 | 15 | 8 | 17 | 19 | 19 | 9 | 18 | 9 | 11 | | +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ Running Total | 9 | 24 | 32 | 39 | 68 | 87 | 96 | 114 | 123 | 134 | | +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ 

In Game 2, Frame 2, the score is 10 for the spare plus the bonus ball 5, which is the first ball in Frame 3: 10 + 5 = 15. If a player scores a spare in Frame 10, he or she is allowed one more bonus ball. In Game 2, Frame 10, the score is 10 + 1 = 11. The score for a strike is 10 for the strike plus the sum of the next two balls. Game 3

Frame Number 1 2 3 4 5 6 7 8 9 10 Bonus +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ Ball Scores | 8 | 1 | X | | 6 | / | 8 | 1 | 9 | / | X | | X | | X | | 9 | / | X | | X | 8 | +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ Frame Scores | 9 | 20 | 18 | 9 | 20 | 30 | 29 | 20 | 20 | 28 | | +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ Running Total | 9 | 29 | 47 | 56 | 76 | 106 | 135 | 155 | 175 | 203 | | +-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+ 

In Game 3, Frame 2, the score is 10 for the strike plus the next two balls, which are 6 and 4: 10 + 6 + 4 = 20. In Frame 6, the score is 10 for the strike plus the next two balls , which are 10 and 10: 10 + 10 + 10 = 30. In Frame 7, the score is 10 + 10 + 9 = 29. In Frame 10, if the player rolls a strike, he or she gets two more bonus balls. The score for Frame 10 is 10 + 10 + 8 = 28. Writing the script to compute bowling scores:

A bowling frame is represented by a Fixnum array of length 2 if the frame is a spare or an open frame, for example [6, 4] or [5, 3]. A bowling frame is represented by an array of length 1 if the frame is a strike: [10].

A bowling game is represented by a ragged array containing its frames, including the bonus balls if the last frame is a strike or a spare. For example, here are the representations of Games 1, 2, and 3:

Game Number File Name Ragged Array
Game 1 game1.txt [ nil, [8,0],[7,2],[6,0],[6,2], [0,8],[6,1],[9,0],[8,0],[8,1], [5,2]]
Game 2 game2.txt [ nil, [8,1],[7,3],[5,3],[0,10], [7,3],[9,1],[9,0],[8,2],[8,1], [5,5],[8]]
Game 3 game3.txt [ nil, [8,1],[10],[6,4],[8,1], [9,1],[10],[10],[10], [9,1], [10],[10],[8]]

Write a method named frame_score in the file frame-score.rb with this header:

def frame_score(the_frame, bonus1, bonus2) 

Use an if..else statement with three cases: Case 1: the_frame[0] == 10 (frame is a strike) Action: return 10 + bonus1 + bonus2 Case 2: the_frame[0] + the_frame[1] == 10 (frame is a spare) Action: return 10 + bonus1 Case 3: the_frame[0] + the_frame[1] < 10 (frame is an open frame) Action: return the_frame[0] + the_frame[1] Use this unit test script to test your frame_score method:

# Filename: unit-test.rb require "./frame-score" require "minitest/autorun" class TestClass < MiniTest::Test def test_1 assert_equal 9, frame_score([7, 2], 0, 0) assert_equal 18, frame_score([9, 1], 8, 0) assert_equal 26, frame_score([10], 10, 6) end end 

Write a main script in the file bowling.rb that computes the bowling score from the scores for a game in an input file:

Prompt the user for the name of the input file, for example, game1.txt, game2.txt, or game3.txt.

Use the read_ragged_array method from the RaggedArray Example to read the game scores from the input file into a ragged array (see the table above). Make these changes to the read_ragged_array method before using it:

Change the delimiter in the split method to be " " instead of ",".

Store the values in the ragged array as Fixnum values instead of Float.

Store the frames in an array named frames. Also, instead of initializing the frames array to [ ], initialize it to [nil] so that the first actual bowling frame in frames is at index 1. nil is a placeholder at index 0.

Loop over each frame in the frames array to compute the sum of all the return values of the frame_score method calls.

Here is suggested pseudocode:

Copy the read_ragged_array method from the RaggedArray Example or use a require statement to load the method from another .rb file. Define the frame_score method as in Item 3. 
prompt user for name of input file input_file = get.chomp Call the read_ragged_array method to create a ragged array of the data in the input_file like game1.txt, game2.txt, or game3.txt. Assign the ragged array to the variable frames. Initialize score to 0. For i in the range 1..10 if frames[i][0] equals 10 and frames[i+1][0] equals 10 assign 10 to bonus1. assign frames[i+2][0] to bonus2. elsif frames[i][0] equals 10 and frames[i+1][0] < 10 assign frames[i+1][0] to bonus1. assign frames[i+1][1] to bonus2. elsif frames[i][0] + frames[i][1] equals 10 assign frames[i+1][0] to bonus1. assign zero to bonus2. else assign zero to bonus1. assign zero to bonus2. end Use frame_score method to compute the score of frames[i] using bonus1 and bonus2. Update score with the current frame score computed from frame_score. end loop. Print score. 

Grading Breakdown: Functionality for frame-score.rb: 15%; Functionality for bowling.rb: 55%; Indentation: 10%; Source Code Comments: 10%; Input files game1.txt, game2.txt, and game3.txtincluded: 5%; Header with name, project number, and submission date: 5%.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

Students also viewed these Databases questions