Last active
April 17, 2019 18:47
-
-
Save dgnsrekt/a19da29dcf81e13d19732f9e30a936a9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from time import sleep | |
import plotly.offline as py | |
import plotly.figure_factory as ff | |
import random | |
def dice(): | |
return random.randint(1, 6), random.randint(1, 6) | |
class Game: | |
PLAY_MAP = { | |
"11": "double", | |
"12": "single", | |
"13": "single", | |
"14": "single", | |
"15": "base on error", | |
"16": "base on balls", | |
"22": "strike", | |
"23": "strike", | |
"24": "strike", | |
"25": "strike", | |
"26": "foul out", | |
"33": "out at 1st", | |
"34": "out at 1st", | |
"35": "out at 1st", | |
"36": "out at 1st", | |
"44": "fly out", | |
"45": "fly out", | |
"46": "fly out", | |
"55": "double play", | |
"56": "triple", | |
"66": "home run", | |
} | |
def __init__(self): | |
self.score = [] | |
self.inning = 0 | |
self.strikes = 0 | |
self.outs = 0 | |
self.plays = 0 | |
def map_action(self, dice_roll): | |
""" | |
returns string name of the action | |
""" | |
x, y = sorted(dice_roll) | |
return self.PLAY_MAP[str(x) + str(y)] | |
def step(self, action): | |
self.plays += 1 | |
if action == "single": | |
self.score.append(1) | |
elif action == "double": | |
self.score.append(2) | |
elif action == "base on error": | |
self.score.append(1) | |
elif action == "base on balls": | |
self.score.append(1) | |
elif action == "home run": | |
self.score.append(4) | |
elif action == "foul out": | |
self.outs += 1 | |
elif action == "fly out": | |
self.outs += 1 | |
elif action == "out at first": | |
self.outs += 1 | |
elif action == "double play": | |
self.outs += 2 | |
self.score.append(-1) | |
elif action == "triple": | |
self.outs += 3 | |
self.score.append(-2) | |
elif action == "strike": | |
self.strikes += 1 | |
if self.strikes > 2: | |
self.strikes = 0 | |
self.outs += 1 | |
return self.done, self.sum_score() | |
@property | |
def done(self): | |
if self.outs > 54: | |
return True | |
return False | |
def sum_score(self): | |
runs = sum(self.score) - 3 | |
if runs < 1: | |
runs = 0 | |
return runs | |
def __repr__(self): | |
output = "--BASEBALL--\n" | |
output += f"SCORE:{self.sum_score()}\n" | |
output += f"INNING:{self.inning}\n" | |
output += f"STRIKES:{self.strikes}\n" | |
output += f"OUTS:{self.outs}\n" | |
output += f"PLAYS:{self.plays}\n" | |
output += f"ACTIONS:{self.score}" | |
return output | |
def compute_average(list_of_scores): | |
return sum(list_of_scores) / len(list_of_scores) | |
def render_plot(list_of_scores): | |
hist_data = [list_of_scores] | |
group_labels = ["distplot"] | |
fig = ff.create_distplot(hist_data, group_labels) | |
py.plot(fig, filename="Distplot(runs scored)") | |
def main(): | |
scores = [] | |
for _ in range(1000): | |
baseball = Game() | |
while True: | |
roll = dice() | |
action = baseball.map_action(roll) | |
done, score = baseball.step(action) | |
if done: | |
scores.append(score) | |
break | |
print(".", end="", flush=True) | |
avg = compute_average(scores) | |
print(avg) | |
render_plot(scores) | |
if __name__ == "__main__": | |
main() |
REQUIRES:
Plotly
Scipy
Numpy
This will not work properly.
Why my scoring system wont work.
https://docs.google.com/spreadsheets/d/14FAGCJab8tDQcpDKh-yZOM4auwkfp5xef7Xtz_F_OqY/edit?usp=sharing
move sorted from mapped action to dice roll.
test 1 <= 2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think I have triple wrong.