elof
10/26/2008 4:03:00 PM
fre, 24 10 2008 kl. 23:25 +0900, skrev Matthew Moss:
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>
> ## Bowling Scores (#181)
# a simple no-frills tail-recursive implementation
class Bowling
def initialize
playBowling(ARGV[0], ARGV[1..ARGV.length].collect { |n| n.to_i })
end
def playBowling(player, pins)
printf("\nFrame Roll Roll Score\n")
printf("\n%s's final score: %d\n\n", player, doFrame(pins))
end
def doFrame(pins, frame = 1, subtotal = 0)
dSubtotal = 0
if pins.length > 0
if frame > 10
printFrame("*", pins[0], pins[1], "")
pins = []
elsif pins[0] == 10
dSubtotal = + pins[0] + pins[1] + pins[2]
printFrame(frame, "X", " ", subtotal + dSubtotal)
pins = pins[1..pins.length]
elsif pins[0] + pins[1] == 10
dSubtotal = pins[0] + pins[1] + pins[2]
printFrame(frame, pins[0], "/", subtotal + dSubtotal)
pins = pins[2..pins.length]
else
dSubtotal = pins[0] + pins[1]
printFrame(frame, pins[0], pins[1], subtotal + dSubtotal)
pins = pins[2..pins.length]
end
doFrame(pins, frame + 1, subtotal + dSubtotal)
else
subtotal
end
end
def printFrame(frameNr, ball0, ball1, subtotal)
printf(" %2s %s %s %3s\n", frameNr, ball0, ball1, subtotal)
end
end
Bowling.new() if $0 == __FILE__