Created
April 9, 2011 11:27
-
-
Save blueplanet/911327 to your computer and use it in GitHub Desktop.
yokohamarb第7回 ボウリングスコア計算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bol | |
def getPin(pins, index) | |
if index > pins.length - 1 | |
0 | |
elsif pins[index] == 'G' | |
0 | |
elsif pins[index] == '/' || pins[index] == 'x' | |
getPin(pins, index + 1) | |
else | |
pins[index] | |
end | |
end | |
def score(pins) | |
post = 2 | |
count = 0 | |
for index in 0..(pins.length - 1) | |
if pins[index] == '/' | |
count += 10 - getPin(pins, index - 1) | |
count += getPin(pins, index + 1) | |
elsif pins[index] == 'x' | |
count += 10 | |
if index > 17 | |
post = 1 | |
end | |
count += getPin(pins, index + post) | |
count += getPin(pins, index + post + 1) | |
elsif pins[index] == 'G' | |
else | |
count = count + getPin(pins, index) | |
end | |
end | |
count | |
end | |
def bowl(pin) | |
if @pins == nil | |
@pins = [pin] | |
else | |
@pins << pin | |
end | |
score @pins | |
end | |
end | |
#puts score([1,4,2,8,5,0,10,0,0,4,5,5,2,0,6,1,10,0,10,5,5]) | |
#puts score([1,4,2,'/',5,0,'x',0,'G',4,5,'/',2,0,6,1,'x',0,'x',5,'/']) | |
bol = Bol.new | |
puts bol.bowl(1) | |
puts bol.bowl(4) | |
puts bol.bowl(2) | |
puts bol.bowl('/') | |
puts bol.bowl(5) | |
puts bol.bowl(0) | |
puts bol.bowl('x') | |
puts bol.bowl(0) | |
puts bol.bowl('G') | |
puts bol.bowl(4) | |
puts bol.bowl(5) | |
puts bol.bowl('/') | |
puts bol.bowl(2) | |
puts bol.bowl(0) | |
puts bol.bowl(6) | |
puts bol.bowl(1) | |
puts bol.bowl('x') | |
puts bol.bowl(0) | |
puts bol.bowl('x') | |
puts bol.bowl(5) | |
puts bol.bowl('/') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment