diff options
author | Peter Ward <peteraward@gmail.com> | 2009-10-28 18:28:15 +1100 |
---|---|---|
committer | Peter Ward <peteraward@gmail.com> | 2009-10-28 18:28:15 +1100 |
commit | e46596cb8ba20e05300b676f536baa073df9d971 (patch) | |
tree | cd816f8b6d722510af5b7ad2f7dbcf252c67eacc | |
parent | 16651873f1cc0be9134d00334e9337d09fec2ead (diff) |
Added reliable colours.
-rw-r--r-- | colour.py | 8 | ||||
-rw-r--r-- | pygooglechart.py (renamed from stats/pygooglechart.py) | 0 | ||||
-rw-r--r-- | snake.py | 8 | ||||
-rw-r--r-- | stats.py (renamed from stats/stats.py) | 26 |
4 files changed, 30 insertions, 12 deletions
diff --git a/colour.py b/colour.py new file mode 100644 index 0000000..fa24f2b --- /dev/null +++ b/colour.py @@ -0,0 +1,8 @@ +import hashlib + +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) + diff --git a/stats/pygooglechart.py b/pygooglechart.py index 0c17973..0c17973 100644 --- a/stats/pygooglechart.py +++ b/pygooglechart.py @@ -6,6 +6,7 @@ import sys import time import string import random +from colour import hash_colour from random import randint from collections import deque from copy import deepcopy @@ -52,7 +53,7 @@ class SnakeEngine(object): x, y = self.get_random_position() self.board[y][x] = Squares.APPLE - def add_bot(self, bot, colour=None): + def add_bot(self, bot): """ A bot is a callable object, with this method signature: def bot_callable( @@ -63,12 +64,13 @@ class SnakeEngine(object): """ letter = self.letters.pop() + name = bot.__name__ + colour = hash_colour(name) + position = self.replace_random(Squares.EMPTY, letter.upper()) if position is None: raise KeyError, "Could not insert snake into the board." - if colour is None: - colour = (randint(0, 255), randint(0, 255), randint(0, 255)) self.bots[letter] = [bot, colour, deque([position])] return letter diff --git a/stats/stats.py b/stats.py index 6eada02..cb1037a 100644 --- a/stats/stats.py +++ b/stats.py @@ -1,16 +1,18 @@ #!/usr/bin/env python -from pygooglechart import SimpleLineChart from collections import defaultdict +from pygooglechart import SimpleLineChart +from colour import hash_colour WIDTH = 600 HEIGHT = 200 +RESULTS_FILE = 'results.csv' def main(): data = {} order = [] snakes = [] - for line in open('../results.csv'): + for line in open(RESULTS_FILE): game_id, name, length, life = line[:-1].split(',') game_id = int(game_id) length = int(length) @@ -25,9 +27,9 @@ def main(): data[name][game_id] = (length, life) - length_chart = SimpleLineChart(WIDTH, HEIGHT) - time_chart = SimpleLineChart(WIDTH, HEIGHT) - + length_data = [] + time_data = [] + colours = [] for name in snakes: time_series = [] length_series = [] @@ -37,11 +39,17 @@ def main(): time_series.append(time) length_series.append(length) - time_chart.add_data(time_series) - length_chart.add_data(length_series) + colours.append('%2X%2X%2X' % hash_colour(name)) + + time_data.append(time_series) + length_data.append(length_series) - length_chart.download('length_chart.png') - time_chart.download('time_chart.png') + for filename, data in (('length_chart.png', length_data), + ('time_chart.png', time_data)): + chart = SimpleLineChart(WIDTH, HEIGHT, colours=colours) + for series in data: + chart.add_data(series) + chart.download(filename) print 'Chart update!' |