Skip to content

Instantly share code, notes, and snippets.

@dgnsrekt
Last active April 12, 2019 03:02
Show Gist options
  • Save dgnsrekt/ec410fdab4ba7c47d9b089401f6711d8 to your computer and use it in GitHub Desktop.
Save dgnsrekt/ec410fdab4ba7c47d9b089401f6711d8 to your computer and use it in GitHub Desktop.
baseball idea
import random
def dice():
return random.randint(1, 6), random.randint(1, 6)
class Game:
def __init__(self):
self.team = 0
self.score = {0: [], 1: []}
self.inning = 0
self.strikes = 0
self.outs = 0
def map_action(self, dice_roll):
"""
returns string name of the action
"""
# Mikes idea of mapping to a dict using strings could work here.
x, y = sorted(dice_roll)
mapped_plays = {"11": "double"}
action = str(x) + str(y)
return mapped_plays[action]
def step(self, roll):
# should ouput if the inning is complete and when the game is done
action = self.map_action(roll)
if action == "single":
self.score[self.team].append(1)
elif action == "strike":
self.strikes += 1
if strikes > 2:
self.strikes = 0
self.outs += 1
# rest of logic which will have singles, doubles, strikes, etc.
if self.outs > 2:
self.innings += 1
# increas innings by one each half inning so
# so 18 innings really equals 9 innings
# also you can have a property which
# converts the innings // 2 to get the real inning.
self.team = self.innings % 2 # Could remove this if we wanted to speed up games that take only 9 innings. see comments
# to check if the game is done you could have a function like this
def check_gameover(self):
if self.innings // 2 > 8:
if self.score[0] != self.score[1]: # you would wrap these in a sum_runs(self.score[0]) function
return True
return False
"""
I'm pictures something like this where you keep appending singles and
double actions to a list and keeping track of the strikes and outs
separately. When team 0 gets 3 outs you will start appending the
actions to the next teams list until you have 3 outs. At the end of the inning you can
build a function which will sum the score from the list of runs.
Not sure if this would work. This is a concept i've been thinking about.
Single = 1
Double = append two 1's to the list or append a 2. not sure yet.
[1, 1, 1, 1, 1, 1]
which equals 6
[2, 1, 1, 1, 2]
which equals 7
4 singles == 1 run
5 singles == 2 run
6 singles == 3 run
7 singles == 4 run
8 singles == 5 run
9 singles == 6 run
so basically the
runs = sum - 3
if runs < 1:
runs = 0
also if we wanted to keep track of the number of rolls that resulted in non hits we could
append 0's to the list to reference the true length of the inning.
"""
@dgnsrekt
Copy link
Author

dgnsrekt commented Apr 12, 2019

Also, one last concept you could technically wait till after at least 9 innings before you start trying to find the score of the entire game. Instead of storing only team zero and team one you could store up to team 17(team 0) and team 18(team 1). This would add the ability to run a bit faster for most games that take only 9 innings. You would start losing efficiency when you have to sum every game after the 9th inning to find out if the game is complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment