From 79a1077fb7b2b4b978e9dec687ea86b195a0c407 Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Thu, 12 Nov 2009 10:27:10 +1100 Subject: Added average script. --- average.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 average.py diff --git a/average.py b/average.py new file mode 100644 index 0000000..01a98dc --- /dev/null +++ b/average.py @@ -0,0 +1,44 @@ +from collections import defaultdict +from itertools import imap as map +from operator import itemgetter + +RESULTS_FILE = 'results.csv' +results = defaultdict(list) + +for line in open(RESULTS_FILE, 'rU'): + game, name, length, time = line[:-1].split(',') + length = int(length) + time = int(time) + results[name].append((length, time)) + +def average(items): + s = 0.0 + l = 0 + for item in items: + s += item + l += 1 + return s / l + +lengths = [] +times = [] + +for name, series in results.items(): + length_average = average(map(itemgetter(0), series)) + lengths.append((length_average, name)) + time_average = average(map(itemgetter(1), series)) + times.append((time_average, name)) + +lengths.sort(reverse=True) +times.sort(reverse=True) + +print 'Lengths' +print '=======' +for length, name in lengths: + print name, length +print + +print 'Times' +print '=====' +for time, name in times: + print name, time + -- cgit v1.2.3 From 83f0fd9d9006934463b9237e6e6ef9a2a795b988 Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Sun, 8 Sep 2013 17:02:03 +1000 Subject: dual support for python 3 --- setup.py | 3 +++ snakegame/colour.py | 9 ++++----- snakegame/engine.py | 21 +++++++++++---------- snakegame/utils.py | 4 +++- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/setup.py b/setup.py index 0dea571..6357f28 100644 --- a/setup.py +++ b/setup.py @@ -8,6 +8,9 @@ setup( author_email='peteraward@gmail.com', packages=['snakegame'], zip_safe=False, + install_requires=[ + 'six', + ], package_data={ 'snakegame': 'images/*.png', }, diff --git a/snakegame/colour.py b/snakegame/colour.py index fa24f2b..f8e4aec 100644 --- a/snakegame/colour.py +++ b/snakegame/colour.py @@ -1,8 +1,7 @@ import hashlib +from random import Random def hash_colour(data): - data = map(ord, hashlib.md5(data).digest()) - colour = data[::3], data[1::3], data[2::3] - colour = map(sum, colour) - return (colour[0] % 255, colour[1] % 255, colour[2] % 255) - + n = int(hashlib.md5(data.encode('utf-8')).hexdigest(), 16) + r = Random(n) + return r.randrange(256), r.randrange(256), r.randrange(256) diff --git a/snakegame/engine.py b/snakegame/engine.py index 1ec3757..155e9bf 100644 --- a/snakegame/engine.py +++ b/snakegame/engine.py @@ -2,10 +2,12 @@ from collections import defaultdict, deque from copy import deepcopy from random import Random from string import ascii_lowercase as lowercase -import sys import time import traceback +import six +from six.moves import xrange + from snakegame.colour import hash_colour from snakegame import common @@ -63,7 +65,6 @@ class Engine(object): n_ice_creams, n_shrink_potions, n_walls, ): self.game_ticks = 0 - self.game_id = self.random.randint(0, sys.maxint) self.letters = list(lowercase) self.letters.reverse() @@ -100,7 +101,7 @@ class Engine(object): position = self.replace_random(common.EMPTY, letter.upper()) if position is None: - raise KeyError, "Could not insert snake into the board." + raise KeyError("Could not insert snake into the board.") self.bots[letter] = [bot, colour, deque([position]), team] return letter @@ -118,7 +119,7 @@ class Engine(object): def update_snakes(self): self.game_ticks += 1 - for letter, (bot, colour, path, team) in self.bots.items(): + for letter, (bot, colour, path, team) in list(self.bots.items()): board = deepcopy(self.board) try: x, y = path[-1] @@ -142,10 +143,10 @@ class Engine(object): 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) + print('Bot %s (%r) exceeded soft time limit.' % (letter.upper(), bot)) # Sanity checking... - assert isinstance(d, basestring), \ + assert isinstance(d, six.string_types), \ "Return value should be a string." d = d.upper() assert d in common.directions, "Return value should be 'U', 'D', 'L' or 'R'." @@ -177,7 +178,7 @@ class Engine(object): path.appendleft(tail) self.replace_random(common.EMPTY, common.APPLE) elif oldcell == common.ICE_CREAM: - for i in range(3): + for i in xrange(3): path.appendleft(tail) self.replace_random(common.EMPTY, common.ICE_CREAM) elif oldcell == common.SHRINK_POTION: @@ -190,10 +191,10 @@ class Engine(object): self.remove_bot(letter) except: - print "Exception in bot %s (%s):" % (letter.upper(), bot) - print '-'*60 + print("Exception in bot %s (%s):" % (letter.upper(), bot)) + print('-'*60) traceback.print_exc() - print '-'*60 + print('-'*60) self.remove_bot(letter) def __iter__(self): diff --git a/snakegame/utils.py b/snakegame/utils.py index 162c0b2..0339518 100644 --- a/snakegame/utils.py +++ b/snakegame/utils.py @@ -3,7 +3,9 @@ try: except ImportError: MaybeOrderedDict = dict -def scale_aspect((source_width, source_height), (target_width, target_height)): +def scale_aspect(source_size, target_size): + source_width, source_height = source_size + target_width, target_height = target_size source_aspect = float(source_width) / source_height target_aspect = float(target_width) / target_height if source_aspect > target_aspect: -- cgit v1.2.3 From fe9b989f1836499bb8fada3ea99476834d75a9dd Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Fri, 27 Jun 2014 23:45:13 +1000 Subject: update minted version --- docs/tutorial.tex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorial.tex b/docs/tutorial.tex index a76760b..907052d 100644 --- a/docs/tutorial.tex +++ b/docs/tutorial.tex @@ -16,7 +16,7 @@ \usepackage{minted} \usemintedstyle{tango} -\newmint[py]{python}{} +\newmintinline[py]{python}{} \newminted{python}{} \newmintedfile{python}{} -- cgit v1.2.3