From 6941f7192fe34add95bd444491f399408eaac014 Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Wed, 28 Oct 2009 17:40:37 +1100 Subject: Added game id to allow stats. --- pygame_snake.py | 22 +++++++++++++--------- snake.py | 7 ++++++- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/pygame_snake.py b/pygame_snake.py index dece4a4..c2ac323 100755 --- a/pygame_snake.py +++ b/pygame_snake.py @@ -40,7 +40,9 @@ class PygameSnakeEngine(SnakeEngine): EDGE_COLOR = (255, 255, 255) EDGE_WIDTH = 1 - def __init__(self, rows, columns, n_apples, width=800, height=600, fullscreen=False): + def __init__(self, rows, columns, n_apples, + width=800, height=600, fullscreen=False, + **kwargs): flags = 0 if fullscreen: flags |= pygame.FULLSCREEN @@ -49,7 +51,8 @@ class PygameSnakeEngine(SnakeEngine): self.width = width self.height = height - super(PygameSnakeEngine, self).__init__(rows, columns, n_apples) + super(PygameSnakeEngine, self).__init__(rows, columns, n_apples, + **kwargs) def new_game(self, rows, columns, n_apples): super(PygameSnakeEngine, self).new_game(rows, columns, n_apples) @@ -140,11 +143,12 @@ if __name__ == '__main__': from bots import * from oldbot import BotWrapper - game = PygameSnakeEngine(25, 25, 50) - game.add_bot(right_bot) - game.add_bot(random_bot) - game.add_bot(random_bounds_bot) - game.add_bot(random_square_bot) - game.add_bot(BotWrapper('oldbots/peter.py')) - game.run() + while True: + game = PygameSnakeEngine(25, 25, 50, results=True) + game.add_bot(right_bot) + game.add_bot(random_bot) + game.add_bot(random_bounds_bot) + game.add_bot(random_square_bot) + game.add_bot(BotWrapper('oldbots/peter.py')) + game.run() diff --git a/snake.py b/snake.py index 022f897..3a286e7 100644 --- a/snake.py +++ b/snake.py @@ -2,8 +2,10 @@ from __future__ import division +import sys import time import string +import random from random import randint from collections import deque from copy import deepcopy @@ -18,6 +20,8 @@ class SnakeEngine(object): self.letters = list(string.lowercase) self.letters.reverse() + self.game_id = random.randint(0, sys.maxint) + self.bots = {} self.results = None if results: @@ -91,7 +95,8 @@ class SnakeEngine(object): pass else: apple_score = len(bot[2]) - self.results.write('%s,%s,%s\n' % (name, apple_score, time_score)) + self.results.write('%s,%s,%s,%s\n' % \ + (self.game_id, name, apple_score, time_score)) self.results.flush() def update_snakes(self, directions_id=id(directions)): -- cgit v1.2.3