Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bowling match score calculator.
def bowling(input)
scores = []
result = []
scores = input.split.collect { |e| e.to_i }
cf = 0 # current frame
i = 0
while(cf < 10) do
if scores[i] == 10 # For strike
result[cf] = scores[i] + scores[i + 1] + (scores[i + 2] ? scores[i + 2] : 0)
cf += 1
else
if (scores[i] + scores[i + 1] == 10) # For spair
result[cf] = scores[i] + scores[i + 1] + scores[i + 2]
cf += 1
i += 1
else
result[cf] = scores[i] + scores[i + 1] # normal
cf += 1
i += 1
end
end
i += 1
end
cumulative_sum = 0
puts "Scores : #{scores}"
puts "Result : #{result.map { |r| cumulative_sum += r}}"
end
if $PROGRAM_NAME == __FILE__
while(str = gets)
bowling(str.chomp)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment