summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Ward <peteraward@gmail.com>2012-07-21 11:27:35 +1000
committerPeter Ward <peteraward@gmail.com>2012-07-21 11:27:35 +1000
commit38ad46d7ce7cb965726d2af5dee8f90261b4a44a (patch)
treebc9bcab43543de8f26a248f1f7455085e9d83d5c
parent4f42f0a86e4ba649ae2e2ebaa66ed73f91aa799b (diff)
Quick and dirty time limiting.
-rw-r--r--snakegame/engines/base.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/snakegame/engines/base.py b/snakegame/engines/base.py
index 74659c4..2797aa1 100644
--- a/snakegame/engines/base.py
+++ b/snakegame/engines/base.py
@@ -3,11 +3,15 @@ from copy import deepcopy
from random import Random
from string import ascii_lowercase as lowercase
import sys
+import time
import traceback
from snakegame.colour import hash_colour
from snakegame import common
+SOFT_TIME_LIMIT = 0.5
+HARD_TIME_LIMIT = 1.0
+
class Engine(object):
def __init__(
self,
@@ -118,6 +122,8 @@ class Engine(object):
try:
x, y = path[-1]
+ start = time.time()
+
if team is None:
d = bot(board, (x, y))
else:
@@ -131,6 +137,12 @@ class Engine(object):
)
messages[letter] = message
+ end = time.time()
+ delta = end - start
+ assert delta < HARD_TIME_LIMIT, 'Exceeded hard time limit.'
+ if delta >= SOFT_TIME_LIMIT:
+ print 'Bot %s (%r) exceeded soft time limit.' % (letter.upper(), bot)
+
# Sanity checking...
assert isinstance(d, basestring), \
"Return value should be a string."