CODING IN PYTHON Goal: write an application that computes bowling scores. Bowlin
ID: 3757429 • Letter: C
Question
CODING IN PYTHON
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
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
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
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: [see below for what text files contain]
Write a method named frame_score in the file framescore.py with this header:
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:
Write a main script in the file bowling.py 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 int values instead of float.
Store the frames in an array named frames. Also, instead of initializing the frames array to [ ], initialize it to [["Frame0"]] so that the first actual bowling frame inframes is at index 1. ["Frame0"] 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. The translated Python code goes in the bowling.py script.
---
game1.txt includes:
game2.txt includes:
game3.txt includes:
Game Number File Name Ragged Array Game 1 game1.txt [["Frame0"], [8,0],[7,2],[6,0],[6,2],[0,8],[6,1],[9,0],[8,0],[8,1],[5,2]] Game 2 game2.txt [["Frame0"], [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 [["Frame0"], [8,1],[10],[6,4],[8,1], [9,1],
[10],[10],[10], [9,1], [10],[10],[8]]
Explanation / Answer
# Number of normal Frame in a game.
LIMIT = 10
# Total Number of pin lined up
COUNT = 10
class BowlingExp < Exception ; end
class Frame_Range
attr_reader :remaining_pin, :shots
def initialize(Frame_Range_num)
@Frame_Range_num = Frame_Range_num
@remaining_pin = COUNT
@shots = []
end
#Call BowlingGame#play first.Do not use this method directly;
def register_shot(pin)
if !played?
@remaining_pin -= pin
end
if !score_finalized?
@shots << pin
end
end
def score
@shots.inject(0, :+)
end
def strike?
shots.first == COUNT
end
def spare?
!strike? && remaining_pin == 0
end
# If a strike or two shots have been played return true
def played?
remaining_pin == 0 || shots.length == 2
end
# If the score has been finalized return true: no more bonus shots
def score_finalized?
if @Frame_Range_num > LIMIT
played? # No bonuses
else
shots.length == ((strike? || spare?) ? 3 : 2 : 1)
end
end
def to_s
strike? ? 'X' :
spare? ? "#{shots[0]}/" :
shots[0...2].join()
end
end
attr_reader :Frame_Ranges
def initialize
@Frame_Ranges = []
new_Frame_Range()
end
def play(&block)
throw BowlingExp.new("Game over") if completed?
pin = yield Frame_Ranges.last.remaining_pin, Frame_Ranges.last.shots.length
throw BowlingExp.new("Shot Impossible") if pin > Frame_Ranges.last.remaining_pin
Frame_Ranges.each { |f| f.register_shot(pin) }
new_Frame_Range() if Frame_Ranges.last.played? && !completed?
end
def completed?
Frame_Ranges.length >= LIMIT && Frame_Ranges[LIMIT - 1].score_finalized?
end
def score
Frame_Ranges[0...LIMIT].collect { |f| f.score }.inject(0, :+)
end
def to_s
Frame_Ranges.collect { |f| f.to_s }.join('-')
end
private
def new_Frame_Range
@Frame_Ranges << Frame_Range.new(@Frame_Ranges.length + 1)
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.