Last active
September 17, 2015 09:54
-
-
Save kechol/a4a7a75c7c7908f0e883 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 python | |
class Game: | |
def __init__(self, balls): | |
self.balls = list(balls) | |
self.cnt_strike = 0 | |
self.cnt_ball = 0 | |
self.cnt_out = 0 | |
def start(self): | |
for b in self.balls: | |
self.play(b) | |
if self.is_out(): | |
self.count_out() | |
self.next_box() | |
if self.is_four_ball(): | |
self.next_box() | |
if self.is_three_out(): | |
self.next_inning() | |
def play(self, ball): | |
if ball == 'B': | |
self.cnt_ball += 1 | |
elif ball == 'S': | |
self.cnt_strike += 1 | |
def is_out(self): | |
return self.cnt_strike == 3 | |
def is_four_ball(self): | |
return self.cnt_ball == 4 | |
def is_three_out(self): | |
return self.cnt_out == 3 | |
def count_out(self): | |
self.cnt_out += 1 | |
def next_box(self): | |
self.cnt_strike = 0 | |
self.cnt_ball = 0 | |
def next_inning(self): | |
self.cnt_strike = 0 | |
self.cnt_ball = 0 | |
self.cnt_out = 0 | |
if __name__ == "__main__": | |
balls = "BBSSSBSBSSS" | |
g = Game(balls) | |
g.start() | |
print("s:{} b:{} o:{}".format(g.cnt_strike, g.cnt_ball, g.cnt_out)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment