Пример #1
0
 def __init__(self, home, away):
     self.ct = CreateTeam()
     self.teams = self.ct.createTeams(home, away)
     self.bat = Batting()
     self.cf = CurrentField()
     self.f = Field(self.cf)
     self.amountOfBasesToMove = 0
     self.currentBattingPlayer = None
     self.battingTeam = 1
     self.pitchingTeam = 0
     self.initOrder = False
     self.innings = 1
     self.inningEventList = []
     self.gameEventList = []
Пример #2
0
 def __init__(self, home, away):
    self.ct = CreateTeam()
    self.teams = self.ct.createTeams(home, away)
    self.bat = Batting()
    self.cf = CurrentField()
    self.f = Field(self.cf)
    self.amountOfBasesToMove = 0
    self.currentBattingPlayer = None
    self.battingTeam = 0
    self.pitchingTeam = 1
    self.initOrder = True
    self.innings = 1
Пример #3
0
class Game(object):

 def __init__(self, home, away):
    self.ct = CreateTeam()
    self.teams = self.ct.createTeams(home, away)
    self.bat = Batting()
    self.cf = CurrentField()
    self.f = Field(self.cf)
    self.amountOfBasesToMove = 0
    self.currentBattingPlayer = None
    self.battingTeam = 0
    self.pitchingTeam = 1
    self.initOrder = True
    self.innings = 1

 def switchTeams(self):
    if self.initOrder == True:
        self.battingTeam = 1
        self.pitchingTeam = 0
        self.initOrder = False

    else:
        self.battingTeam = 0
        self.pitchingTeam = 1
        self.initOrder = True

 def teamAtBat(self):
  self.cf.start(self.teams[self.pitchingTeam])
  while self.teams[self.battingTeam].getOuts() < 3:
   self.currentBattingPlayer = self.teams[self.battingTeam].getNextPlayerAtBat()
   cb = CurrentBatting(self.teams[self.pitchingTeam].get_Pitcher(), self.currentBattingPlayer)
   self.amountOfBasesToMove = self.bat.startBatting(cb)
   if self.amountOfBasesToMove > 0:
    outsToBeAdded = self.f.newPlayerOnBases(self.amountOfBasesToMove, self.currentBattingPlayer, self.teams[self.battingTeam].getOuts(), cb.getHomerunOrWalk())
    if outsToBeAdded > 0:
      self.teams[self.battingTeam].addNumToOuts(outsToBeAdded)
   else:
    self.teams[self.battingTeam].addOneToOuts()
    print "OUT HAS HAPPENED " + str(self.teams[self.battingTeam].getOuts())
  self.teams[self.battingTeam].addNumToScore(self.cf.getScore())
  self.teams[self.battingTeam].setOutsToZero()
  self.cf.reset()


 def inning(self):
    self.teamAtBat()
    print "NEW TEAM AT BAT"
    print
    self.switchTeams()
    self.teamAtBat()
    self.switchTeams()
    print "NEW TEAM AT BAT"
    print
    self.innings = self.innings + 1

 def playGame(self):
    while self.innings < 9:
        self.inning()
        print "score at end of inning " + str(self.innings) + " is: " + "\n" + "Home Team: " + str(self.teams[0].getScore()) + "\n" + "Away Team: " + str(self.teams[1].getScore())

    while self.teams[0].getScore() == self.teams[1].getScore():
        self.inning()
        print "score at end of inning " + str(self.innings) + " is: " + "\n" + "Home Team: " + str(self.teams[0].getScore()) + "\n" + "Away Team: " + str(self.teams[1].getScore())

    temp = [self.teams[0].getScore(), self.teams[1].getScore()]
    return temp
Пример #4
0
class Game(object):

 def __init__(self, home, away):
    self.ct = CreateTeam()
    self.teams = self.ct.createTeams(home, away)
    self.bat = Batting()
    self.cf = CurrentField()
    self.f = Field(self.cf)
    self.amountOfBasesToMove = 0
    self.currentBattingPlayer = None
    self.battingTeam = 1
    self.pitchingTeam = 0
    self.initOrder = False
    self.innings = 1
    self.inningEventList = []
    self.gameEventList = []

 def switchTeams(self):
    if self.initOrder == True:
        self.battingTeam = 1
        self.pitchingTeam = 0
        self.initOrder = False

    else:
        self.battingTeam = 0
        self.pitchingTeam = 1
        self.initOrder = True

 def teamAtBat(self):
  self.cf.start(self.teams[self.pitchingTeam])
  self.createInningData([{"code" : "START-HALF-INNING", "description" : "The " + self.teams[self.battingTeam].getTeamName() + " are up at bat!"}])
  while self.teams[self.battingTeam].getOuts() < 3:
   self.currentBattingPlayer = self.teams[self.battingTeam].getNextPlayerAtBat()
   cb = CurrentBatting(self.teams[self.pitchingTeam].getPitcher(), self.currentBattingPlayer)
   self.amountOfBasesToMove = self.bat.startBatting(cb)
   print self.bat.toString()
   self.createInningData(self.bat.getGameString())
   if self.amountOfBasesToMove > 0:
    outsToBeAdded = self.f.newPlayerOnBases(self.amountOfBasesToMove, self.currentBattingPlayer, self.teams[self.battingTeam].getOuts(), cb.getHomerunOrWalk())
    self.createInningData(self.f.getGameString())
    if outsToBeAdded > 0:
      self.teams[self.battingTeam].addNumToOuts(outsToBeAdded)
   else:
    self.teams[self.battingTeam].addOneToOuts()
    print "OUT HAS HAPPENED " + str(self.teams[self.battingTeam].getOuts())
  self.teams[self.battingTeam].addNumToScore(self.cf.getScore())
  self.teams[self.battingTeam].setOutsToZero()
  self.cf.reset()

 def inning(self):
    self.teamAtBat()
    print "NEW TEAM AT BAT"
    print
    if self.innings < 9 or self.teams[0].getScore() <= self.teams[1].getScore():
    	self.switchTeams()
    	self.teamAtBat()
    	self.switchTeams()
    	print "NEW TEAM AT BAT"
    	print
    self.innings = self.innings + 1

 def playGame(self):
    while self.innings < 10:
        self.inning()
        print "score at end of inning " + str(self.innings-1) + " is: " + "\n" + "Home Team: " + str(self.teams[0].getScore()) + "\n" + "Away Team: " + str(self.teams[1].getScore())
        self.createInningData([{"code" : "END-INNING-SCORE", "description" : "Home Team: " + str(self.teams[0].getScore()) + "  " + "Away Team: " + str(self.teams[1].getScore())}])
        self.addGameEvents()

    while self.teams[0].getScore() == self.teams[1].getScore():
        self.inning()
        print "score at end of inning " + str(self.innings - 1) + " is: " + "\n" + "Home Team: " + str(self.teams[0].getScore()) + "\n" + "Away Team: " + str(self.teams[1].getScore())
        self.createInningData([{"code" : "END-INNING-SCORE", "description" : "Home Team: " + str(self.teams[0].getScore()) + "  " + "Away Team: " + str(self.teams[1].getScore())}])
        self.addGameEvents()

    temp = [self.teams[0].getScore(), self.teams[1].getScore()]
    return temp

 def addGameEvents(self):
  self.gameEventList.append(self.inningEventList)
  self.inningEventList = []

 def createInningData(self, aList):
  self.inningEventList.extend(aList)

 def createJSON(self):
  with open('data.json' , 'w') as outfile:
	  json.dump(self.gameEventList, outfile, sort_keys = True, indent = 4, ensure_ascii = False)
  test = open('data.json')
  son = json.load(test)
  print son[0][0]["description"]


 def getJSONData(self):
  ts = time.time()
  ts = datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d-%H-%M-%S")

  # The website looks for the game files located in static/simulations/
  game_file_location = "static/simulations/"

  with open(game_file_location + ts+ "_" + self.teams[0].getTeamName() + "_" + self.teams[1].getTeamName() +'_.json' , 'w') as outfile:
	  json.dump(self.gameEventList, outfile, sort_keys = True, indent = 4, ensure_ascii = False)
  return ts+ "_" + self.teams[0].getTeamName() + "_" + self.teams[1].getTeamName() +'_.json'
Пример #5
0
class Game(object):
    def __init__(self, home, away):
        self.ct = CreateTeam()
        self.teams = self.ct.createTeams(home, away)
        self.bat = Batting()
        self.cf = CurrentField()
        self.f = Field(self.cf)
        self.amountOfBasesToMove = 0
        self.currentBattingPlayer = None
        self.battingTeam = 1
        self.pitchingTeam = 0
        self.initOrder = False
        self.innings = 1
        self.inningEventList = []
        self.gameEventList = []

    def switchTeams(self):
        if self.initOrder == True:
            self.battingTeam = 1
            self.pitchingTeam = 0
            self.initOrder = False

        else:
            self.battingTeam = 0
            self.pitchingTeam = 1
            self.initOrder = True

    def teamAtBat(self):
        self.cf.start(self.teams[self.pitchingTeam])
        self.createInningData([{
            "code":
            "START-HALF-INNING",
            "description":
            "The " + self.teams[self.battingTeam].getTeamName() +
            " are up at bat!"
        }])
        while self.teams[self.battingTeam].getOuts() < 3:
            self.currentBattingPlayer = self.teams[
                self.battingTeam].getNextPlayerAtBat()
            cb = CurrentBatting(self.teams[self.pitchingTeam].getPitcher(),
                                self.currentBattingPlayer)
            self.amountOfBasesToMove = self.bat.startBatting(cb)
            print self.bat.toString()
            self.createInningData(self.bat.getGameString())
            if self.amountOfBasesToMove > 0:
                outsToBeAdded = self.f.newPlayerOnBases(
                    self.amountOfBasesToMove, self.currentBattingPlayer,
                    self.teams[self.battingTeam].getOuts(),
                    cb.getHomerunOrWalk())
                self.createInningData(self.f.getGameString())
                if outsToBeAdded > 0:
                    self.teams[self.battingTeam].addNumToOuts(outsToBeAdded)
            else:
                self.teams[self.battingTeam].addOneToOuts()
                print "OUT HAS HAPPENED " + str(
                    self.teams[self.battingTeam].getOuts())
        self.teams[self.battingTeam].addNumToScore(self.cf.getScore())
        self.teams[self.battingTeam].setOutsToZero()
        self.cf.reset()

    def inning(self):
        self.teamAtBat()
        print "NEW TEAM AT BAT"
        print
        if self.innings < 9 or self.teams[0].getScore(
        ) <= self.teams[1].getScore():
            self.switchTeams()
            self.teamAtBat()
            self.switchTeams()
            print "NEW TEAM AT BAT"
            print
        self.innings = self.innings + 1

    def playGame(self):
        while self.innings < 10:
            self.inning()
            print "score at end of inning " + str(
                self.innings - 1) + " is: " + "\n" + "Home Team: " + str(
                    self.teams[0].getScore()) + "\n" + "Away Team: " + str(
                        self.teams[1].getScore())
            self.createInningData([{
                "code":
                "END-INNING-SCORE",
                "description":
                "Home Team: " + str(self.teams[0].getScore()) + "  " +
                "Away Team: " + str(self.teams[1].getScore())
            }])
            self.addGameEvents()

        while self.teams[0].getScore() == self.teams[1].getScore():
            self.inning()
            print "score at end of inning " + str(
                self.innings - 1) + " is: " + "\n" + "Home Team: " + str(
                    self.teams[0].getScore()) + "\n" + "Away Team: " + str(
                        self.teams[1].getScore())
            self.createInningData([{
                "code":
                "END-INNING-SCORE",
                "description":
                "Home Team: " + str(self.teams[0].getScore()) + "  " +
                "Away Team: " + str(self.teams[1].getScore())
            }])
            self.addGameEvents()

        temp = [self.teams[0].getScore(), self.teams[1].getScore()]
        return temp

    def addGameEvents(self):
        self.gameEventList.append(self.inningEventList)
        self.inningEventList = []

    def createInningData(self, aList):
        self.inningEventList.extend(aList)

    def createJSON(self):
        with open('data.json', 'w') as outfile:
            json.dump(self.gameEventList,
                      outfile,
                      sort_keys=True,
                      indent=4,
                      ensure_ascii=False)
        test = open('data.json')
        son = json.load(test)
        print son[0][0]["description"]

    def getJSONData(self):
        ts = time.time()
        ts = datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d-%H-%M-%S")

        # The website looks for the game files located in static/simulations/
        game_file_location = "static/simulations/"

        with open(
                game_file_location + ts + "_" + self.teams[0].getTeamName() +
                "_" + self.teams[1].getTeamName() + '_.json', 'w') as outfile:
            json.dump(self.gameEventList,
                      outfile,
                      sort_keys=True,
                      indent=4,
                      ensure_ascii=False)
        return ts + "_" + self.teams[0].getTeamName(
        ) + "_" + self.teams[1].getTeamName() + '_.json'