Click here to Skip to main content
14,303,493 members
Rate this:
Please Sign up or sign in to vote.
See more:
I need to write an application that computes bowling scores. This is more for clarification and understanding my mistakes because I really want to understand how to complete this task.

Additionally;

Import the frame_score method from 
the file framescore.py

Import the read_ragged_array method from 
   the framescore.py file.
   (See the RaggedArray Example)
   
read inputfile from keyboard, promping user. 
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 from 1 to 10 (use range(1, 11))
   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 if
   # Next line is actual Python statement.   
   score += framescore(frames[i], bonus1, bonus2)
End loop.

What I have tried:

bowling.py
<pre># import the frame_score method from the file framescore.py
from framescore import frame_score
# import the read_ragged_array method from the framescore.py file.
from framescore import read_ragged_array

# read inputfile from keyboard, promping user.
# 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.
input_file = input("Enter name of input file ('game1.txt, game2.txt, game3.txt'):")

# assign the ragged array to the variable frames.
frames = read_ragged_array(input_file)

# Initialize score to 0.
score = [0]

for i in range(1, 11):
    if frames[i][0] == 10 and frames[i+1][10] == 10:
        bonus1 = 10
        bonus2 = frames[i+2][0]
    elif frames[i][0] == 10 and frames[i+1][0] < 10:
        bonus1 = frames[i+1][0]
        bonus2 = frames[i+1][1]
    elif frames[i][0] + frames[i][1] == 10:
        bonus1 = frames[i+1][1]
        bonus2 = 0
    else:
        bonus1 = 0
        bonus2 = 0
# next line is actual Python statement.
    while True:
        score += frame_score(frames[i], bonus1, bonus2)
#End loop.

print(score)


framescore.py
def frame_score(the_frame, bonus1, bonus2):
    # Case where frame is a strike
    if the_frame[0] == 10: 
        return 10 + bonus1 + bonus2
    # Case where frame is a spare
    elif the_frame[0] + the_frame[1] == 10: 
        return 10 + bonus1
    # Case where frame is an open frame
    else:
        return the_frame[0] + the_frame[1] 

def read_ragged_array(input_file):
    frames = [["Frame0"]]
    f = open(input_file, "r")
    line = f.readline( )
    while line:
        fields = line.split(" ")
        row = [ ]
        for field in fields:
            row.append(int(field.strip( )))
        frames.append(row)
        line = f.readline( )
    return frames
    
frames = read_ragged_array("game1.txt")
print(frames)

frames = read_ragged_array("game2.txt")
print(frames)

frames = read_ragged_array("game3.txt")
print(frames)

# 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
# in frames 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.
Posted
Comments
Richard MacCutchan 30-Jan-19 11:58am
   
What is the question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100