Last active
December 16, 2015 21:49
-
-
Save sawamur/5502455 to your computer and use it in GitHub Desktop.
Rule of baseball in ruby - Last weekend, I went to see baseball game with my six years old kid. I found that the rule of baseball is quite difficult. For example, treating foul ball as strike if the count of strike is under two. There are lots of conditional procedures. So, I write the rule in ruby. Looks nice,isn't it?
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
tesms = [ Team.new("baystars"), Team.new("swallows") ] | |
9.times do |inning| | |
2.times do |half| | |
if inning == 8 and half == 1 and teams[0].score > teams[1].score | |
break | |
end | |
pitcher = teams.first.pitcher | |
out = 0 | |
runners = [] | |
while out < 3 | |
batter = teams.last.next_batter | |
s,b = 0,0 | |
loop do | |
ball = pitcher.throw_ball | |
if ball.strike? | |
s += 1 | |
if s == 3 | |
out += 1 | |
break | |
end | |
elsif ball.ball? | |
b += 1 | |
if b == 4 | |
runners << true | |
break | |
end | |
elsif ball.faul? | |
if s <= 2 | |
s += 1 | |
end | |
elsif batter.homerun? | |
teams.last.score += runners.select{ |r| !r.nil? }.size + 1 | |
runners = [] | |
break; | |
elsif batter.hit? | |
runners << true | |
if runners.size > 3 | |
teams.last.score += 1 | |
runners.shift | |
end | |
break; | |
elsif batter.fly? | |
out += 1 | |
runnners.each do |r| | |
if r.success_touchup? | |
# ... | |
end | |
end | |
break | |
end | |
end | |
end | |
teams.reverse! | |
end | |
end | |
while teams.first.score == teams.last.score | |
#... | |
end | |
class Team | |
attr_accessor :score,:name | |
def initialize(name) | |
@name = name | |
@score = 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment