Created
April 9, 2011 09:51
-
-
Save azusa/911283 to your computer and use it in GitHub Desktop.
ボーリングのスコア計算
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
#!/usr/bin/env ruby | |
#! coding: utf-8 | |
class Bowling | |
def initialize() | |
@pins = [] | |
end | |
def bowl(pin) | |
@pins.push(pin) | |
score(@pins) | |
end | |
def score(pins) | |
pins = Marshal.load(Marshal.dump(pins)) | |
pins.push(0) | |
score = 0 | |
strike = false; | |
spare = false; | |
double = false; | |
pins.every do |first,second| | |
if (second == nil ) then | |
#第10フレーム三投目 | |
score = score + first | |
break | |
else | |
score = score + first + second | |
end | |
if (strike) then | |
score = score + first + second | |
end | |
if (spare||double) then | |
score = score + first | |
end | |
if (strike && first == 10) then | |
double = true; | |
else | |
double = false | |
end | |
if (first == 10) then | |
strike = true | |
spare = false | |
elsif ((first + second ) == 10) then | |
strike = false | |
spare = true | |
else | |
strike =false | |
spare = false | |
end | |
end | |
return score | |
end | |
end | |
class Array | |
def every(&block) | |
arity = block.arity | |
return self.each(&block) if arity <= 0 | |
i = 0 | |
while i < self.size | |
yield(*self[i, arity]) | |
i += arity | |
end | |
self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment