3838
3939SCARED_TIME = 40
4040
41+ # The number of moves shared between all agents.
42+ DEFAULT_MAX_MOVES = 1200
43+
44+ # Total time allowed for an agent,
45+ # move limits generally stop this from happening.
46+ DEFAULT_MAX_TOTAL_AGENT_TIME_SECS = 900
47+
48+ # Time allowed for registerInitialState().
49+ DEFAULT_MAX_STARTUP_TIME_SECS = 15
50+
51+ # Some number of warnings will cause a forfeit.
52+ DEFAULT_MOVE_WARNING_TIME_SECS = 1
53+
54+ # Any violation of this is an instant forfeit.
55+ DEFAULT_MOVE_TIMEOUT_TIME_SECS = 3
56+
57+ # Next violation causes a forfeit.
58+ DEFAULT_MAX_MOVE_WARNINGS = 2
59+
4160class CaptureGameState (AbstractGameState ):
4261 """
4362 A game state specific to capture.
@@ -246,7 +265,13 @@ class CaptureRules:
246265 and how the game starts and ends.
247266 """
248267
249- def newGame (self , layout , agents , display , length , catchExceptions ):
268+ def newGame (self , layout , agents , display , length , catchExceptions ,
269+ maxTotalAgentTimeSecs = DEFAULT_MAX_TOTAL_AGENT_TIME_SECS ,
270+ maxStartupTimeSecs = DEFAULT_MAX_STARTUP_TIME_SECS ,
271+ moveWarningTimeSecs = DEFAULT_MOVE_WARNING_TIME_SECS ,
272+ moveTimeoutTimeSecs = DEFAULT_MOVE_TIMEOUT_TIME_SECS ,
273+ maxMoveWarnings = DEFAULT_MAX_MOVE_WARNINGS ,
274+ ** kwargs ):
250275 initState = CaptureGameState (layout , length )
251276 starter = random .randint (0 , 1 )
252277 logging .info ('%s team starts' % ['Red' , 'Blue' ][starter ])
@@ -258,6 +283,12 @@ def newGame(self, layout, agents, display, length, catchExceptions):
258283 self ._totalBlueFood = initState .getBlueFood ().count ()
259284 self ._totalRedFood = initState .getRedFood ().count ()
260285
286+ self ._maxTotalAgentTimeSecs = maxTotalAgentTimeSecs
287+ self ._maxStartupTimeSecs = maxStartupTimeSecs
288+ self ._moveWarningTimeSecs = moveWarningTimeSecs
289+ self ._moveTimeoutTimeSecs = moveTimeoutTimeSecs
290+ self ._maxMoveWarnings = maxMoveWarnings
291+
261292 return game
262293
263294 def process (self , state , game ):
@@ -313,20 +344,20 @@ def agentCrash(self, game, agentIndex):
313344 logging .error ("Blue agent crashed." )
314345 game .state .setScore (1 )
315346
316- def getMaxTotalTime (self , agentIndex ):
317- return 900 # Move limits should prevent this from ever happening
347+ def getMaxTotalAgentTime (self , agentIndex ):
348+ return self . _maxTotalAgentTimeSecs
318349
319350 def getMaxStartupTime (self , agentIndex ):
320- return 15 # 15 seconds for registerInitialState
351+ return self . _maxStartupTimeSecs
321352
322353 def getMoveWarningTime (self , agentIndex ):
323- return 1 # One second per move
354+ return self . _moveWarningTimeSecs
324355
325356 def getMoveTimeout (self , agentIndex ):
326- return 3 # Three seconds results in instant forfeit
357+ return self . _moveTimeoutTimeSecs
327358
328359 def getMaxTimeWarnings (self , agentIndex ):
329- return 2 # Third violation loses the game
360+ return self . _maxMoveWarnings
330361
331362class AgentRules :
332363 """
@@ -536,14 +567,34 @@ def readCommand(argv):
536567 help = 'make agent 3 (second blue player) a keyboard agent (default: %(default)s)' )
537568
538569 parser .add_argument ('--max-moves' , dest = 'maxMoves' ,
539- action = 'store' , type = int , default = 1200 ,
540- help = 'set maximum number of moves in a game (default: %(default)s)' )
570+ action = 'store' , type = int , default = DEFAULT_MAX_MOVES ,
571+ help = 'set maximum number of moves between all agents in a game (default: %(default)s)' )
541572
542573 parser .add_argument ('--red-args' , dest = 'redArgs' ,
543574 action = 'store' , type = str , default = None ,
544575 help = 'comma separated arguments to be passed to red team (e.g. \' opt1=val1,opt2\' ) '
545576 + '(default: %(default)s)' )
546577
578+ parser .add_argument ('--max-total-agent-time' , dest = 'maxTotalAgentTimeSecs' ,
579+ action = 'store' , type = float , default = DEFAULT_MAX_TOTAL_AGENT_TIME_SECS ,
580+ help = 'set maximum number of seconds a game can run (default: %(default)s)' )
581+
582+ parser .add_argument ('--max-startup-time' , dest = 'maxStartupTimeSecs' ,
583+ action = 'store' , type = float , default = DEFAULT_MAX_STARTUP_TIME_SECS ,
584+ help = 'set maximum number of seconds allowed for registerInitialState() (default: %(default)s)' )
585+
586+ parser .add_argument ('--move-warning-time' , dest = 'moveWarningTimeSecs' ,
587+ action = 'store' , type = float , default = DEFAULT_MOVE_WARNING_TIME_SECS ,
588+ help = 'set maximum number of seconds an agent can take on a move before a warning is issued (default: %(default)s)' )
589+
590+ parser .add_argument ('--move-timeout-time' , dest = 'moveTimeoutTimeSecs' ,
591+ action = 'store' , type = float , default = DEFAULT_MOVE_TIMEOUT_TIME_SECS ,
592+ help = 'set maximum number of seconds an agent can take on a move before forfeiting (default: %(default)s)' )
593+
594+ parser .add_argument ('--max-move-warnings' , dest = 'maxMoveWarnings' ,
595+ action = 'store' , type = int , default = DEFAULT_MAX_MOVE_WARNINGS ,
596+ help = 'set maximum number of warnings issued to an agent before that agent is disqualified (default: %(default)s)' )
597+
547598 options , otherjunk = parser .parse_known_args (argv )
548599 args = dict ()
549600
@@ -639,6 +690,11 @@ def readCommand(argv):
639690 args ['record' ] = options .record
640691 args ['catchExceptions' ] = options .catchExceptions
641692 args ['replay' ] = options .replay
693+ args ['maxTotalAgentTimeSecs' ] = options .maxTotalAgentTimeSecs
694+ args ['maxStartupTimeSecs' ] = options .maxStartupTimeSecs
695+ args ['moveWarningTimeSecs' ] = options .moveWarningTimeSecs
696+ args ['moveTimeoutTimeSecs' ] = options .moveTimeoutTimeSecs
697+ args ['maxMoveWarnings' ] = options .maxMoveWarnings
642698
643699 return args
644700
@@ -660,10 +716,10 @@ def loadAgents(isRed, agentModule, textgraphics, args):
660716
661717 return createTeamFunction (indices [0 ], indices [1 ], isRed , ** args )
662718
663- def replayGame (layout , agents , actions , display , length , redTeamName , blueTeamName ):
719+ def replayGame (layout , agents , actions , display , length , redTeamName , blueTeamName , ** kwargs ):
664720 agents = [DummyAgent (index ) for index in range (len (agents ))]
665721 rules = CaptureRules ()
666- game = rules .newGame (layout , agents , display , length , False )
722+ game = rules .newGame (layout , agents , display , length , False , ** kwargs )
667723 state = game .state
668724 display .redTeam = redTeamName
669725 display .blueTeam = blueTeamName
@@ -698,7 +754,7 @@ def runGames(layout, agents, display, length, numGames, record, numTraining,
698754 else :
699755 gameDisplay = display
700756
701- g = rules .newGame (layout , agents , gameDisplay , length , catchExceptions )
757+ g = rules .newGame (layout , agents , gameDisplay , length , catchExceptions , ** kwargs )
702758 g .run ()
703759
704760 if (not isTraining ):
0 commit comments