Skip to content

Instantly share code, notes, and snippets.

@azusa
Created Apr 9, 2011
Embed
What would you like to do?
ボーリングのスコア計算
#!/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