From f788bdcf63d14dd97f0802391b0676ca84c900ce Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Tue, 5 Jan 2010 06:56:13 +1100 Subject: Renamed old bots to process bots. --- bots/peter.py | 83 +++++++++++++++++++++++++++++ bots/peter_smart.py | 131 ++++++++++++++++++++++++++++++++++++++++++++++ bots/peter_smart2.py | 135 ++++++++++++++++++++++++++++++++++++++++++++++++ console_snake.py | 4 +- oldbot.py | 30 ----------- oldbots/peter.py | 83 ----------------------------- oldbots/peter_smart.py | 131 ---------------------------------------------- oldbots/peter_smart2.py | 135 ------------------------------------------------ processbot.py | 30 +++++++++++ pygame_snake.py | 4 +- pyglet_snake.py | 13 ++--- 11 files changed, 388 insertions(+), 391 deletions(-) create mode 100755 bots/peter.py create mode 100755 bots/peter_smart.py create mode 100755 bots/peter_smart2.py delete mode 100644 oldbot.py delete mode 100755 oldbots/peter.py delete mode 100755 oldbots/peter_smart.py delete mode 100755 oldbots/peter_smart2.py create mode 100644 processbot.py diff --git a/bots/peter.py b/bots/peter.py new file mode 100755 index 0000000..72bbc95 --- /dev/null +++ b/bots/peter.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +"""New and improved bot, OPTIMISED!!""" + +import random +import sys + +EMPTY_TILE = '.' +APPLE_TILE = '*' + +WIDTH, HEIGHT, SNAKE_BODY = raw_input().split() +WIDTH = int(HEIGHT) +HEIGHT = int(HEIGHT) + +SNAKE_BODY = SNAKE_BODY.lower() +SNAKE_HEAD = SNAKE_BODY.upper() + +HEADX = None +HEADY = None + +def get_cell(board, x, y): + if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT: + raise KeyError, 'out of range.' + return board[y][x] + +BOARD = [] +for y in xrange(HEIGHT): + row = raw_input() + for x, char in enumerate(row): + if char == SNAKE_HEAD: + HEADX = x + HEADY = y + BOARD.append(row) + +md_two = { + (-1, 0, 'l'): ((-2, 0), (-1, 1), (-1, -1)), + (0, -1, 'u'): ((-1, -1), (1, -1), (0, -2)), + (1, 0, 'r'): ((2, 0), (1, 1), (1, -1)), + (0, 1, 'd'): (((0, 2), (-1, 1), (1, 1))), +} + +max_score = 0 +max_moves = [] + +for (dx, dy, move), adj in md_two.items(): + score = 0 + + try: + square = get_cell(BOARD, HEADX + dx, HEADY + dy) + except KeyError: + continue + + if square == APPLE_TILE: + score += 2 + elif square != EMPTY_TILE: + continue # Definitely cannot move here. + + for ddx, ddy in adj: + try: + square = get_cell(BOARD, HEADX + ddx, HEADY + ddy) + except KeyError: + score -= 1 + continue + + if square == APPLE_TILE: + score += 2 + elif square == EMPTY_TILE: + score += 1 + elif square == SNAKE_BODY: + score -= 1 + elif square.isupper(): + score += 3 + + if score == max_score: + max_moves.append(move) + elif score > max_score: + max_score = score + max_moves = [move] + +if max_moves: + print random.choice(max_moves) +else: + print 'U' # Suicide! + diff --git a/bots/peter_smart.py b/bots/peter_smart.py new file mode 100755 index 0000000..b099cdd --- /dev/null +++ b/bots/peter_smart.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python +"""New and improved bot, OPTIMISED!!""" + +import random +import sys + +DEBUG = False + +# Show tracebacks, then pause for debugging. +if DEBUG: + sys_excepthook = sys.excepthook + def excepthook(*args, **kwargs): + sys_excepthook(*args, **kwargs) + import time + time.sleep(10) + sys.excepthook = excepthook + +EMPTY_TILE = '.' +APPLE_TILE = '*' + +WIDTH, HEIGHT, SNAKE_BODY = raw_input().split() +WIDTH = int(WIDTH) +HEIGHT = int(HEIGHT) + +SNAKE_BODY = SNAKE_BODY.lower() +SNAKE_HEAD = SNAKE_BODY.upper() + +HEADX = None +HEADY = None + +SNAKE_LENGTH = 0 + +def get_cell(board, x, y): + if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT: + raise KeyError, 'out of range.' + return board[y][x] + +BOARD = [] +for y in xrange(HEIGHT): + row = raw_input() + for x, char in enumerate(row): + if char == SNAKE_HEAD: + HEADX = x + HEADY = y + elif char == SNAKE_BODY: + SNAKE_LENGTH += 1 + BOARD.append(row) + +MOVES = ( + (-1, 0, 'l'), + (1, 0, 'r'), + (0, -1, 'u'), + (0, 1, 'd') +) + +def get_score(x, y, n, done=None): + if done is None: + done = set() + + done.add((x, y)) + + score = 0 + explore = False + + # See if the cell exists. + try: + square = get_cell(BOARD, x, y) + except KeyError: + return 0 + + # Give some extra points for getting an apple. + if square == APPLE_TILE: + explore = True + score += 50 + + # Yay - it's empty! + elif square == EMPTY_TILE: + explore = True + score += 10 + + elif square.islower(): + score -= 1 + + if explore and n > 0: + # Explore n-1 cells further. + for dx, dy, move in MOVES: + nx = x + dx + ny = y + dy + + if (nx, ny) in done: + continue + + subscore = get_score(nx, ny, n - 1, done) + score += subscore / 10 + + return score * n + +max_score = None +max_moves = [] + +for dx, dy, move in MOVES: + score = 0 + + x = HEADX + dx + y = HEADY + dy + + n = (SNAKE_LENGTH + 4) / 2 + score = get_score(x, y, n) + +# print 'Score for', move, '=', score + + # Suicide protection squad! + try: + square = get_cell(BOARD, x, y) + except KeyError: + continue + else: + if square not in (APPLE_TILE, EMPTY_TILE): + continue + + if score == max_score: + max_moves.append(move) + elif max_score is None or score > max_score: + max_score = score + max_moves = [move] + +if max_moves: + print random.choice(max_moves) +else: + raise Exception, "No suitable moves found!" + diff --git a/bots/peter_smart2.py b/bots/peter_smart2.py new file mode 100755 index 0000000..8f431ca --- /dev/null +++ b/bots/peter_smart2.py @@ -0,0 +1,135 @@ +#!/usr/bin/env python +"""New and improved bot, OPTIMISED!!""" + +import random +import sys + +DEBUG = False + +# Show tracebacks, then pause for debugging. +if DEBUG: + sys_excepthook = sys.excepthook + def excepthook(*args, **kwargs): + sys_excepthook(*args, **kwargs) + import time + time.sleep(10) + sys.excepthook = excepthook + +EMPTY_TILE = '.' +APPLE_TILE = '*' + +WIDTH, HEIGHT, SNAKE_BODY = raw_input().split() +WIDTH = int(WIDTH) +HEIGHT = int(HEIGHT) + +SNAKE_BODY = SNAKE_BODY.lower() +SNAKE_HEAD = SNAKE_BODY.upper() + +HEADX = None +HEADY = None + +SNAKE_LENGTH = 0 + +def get_cell(board, x, y): + if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT: + raise KeyError, 'out of range.' + return board[y][x] + +BOARD = [] +for y in xrange(HEIGHT): + row = raw_input() + for x, char in enumerate(row): + if char == SNAKE_HEAD: + HEADX = x + HEADY = y + elif char == SNAKE_BODY: + SNAKE_LENGTH += 1 + BOARD.append(row) + +MOVES = ( + (-1, 0, 'l'), + (1, 0, 'r'), + (0, -1, 'u'), + (0, 1, 'd') +) + +def get_score(x, y, n, done=None): + if done is None: + done = set() + + done.add((x, y)) + + score = 0 + explore = False + + # See if the cell exists. + try: + square = get_cell(BOARD, x, y) + except KeyError: + return 0 + + # Give some extra points for getting an apple. + if square == APPLE_TILE: + explore = True + score += 100 + + # Yay - it's empty! + elif square == EMPTY_TILE: + explore = True + score += 50 + + elif square.islower(): + score += 2 + + elif square.isupper(): + score += 1 + + if explore and n > 0: + # Explore n-1 cells further. + for dx, dy, move in MOVES: + nx = x + dx + ny = y + dy + + if (nx, ny) in done: + continue + + subscore = get_score(nx, ny, n - 1, done) + score += subscore / 10 + + return score * n + +max_score = None +max_moves = [] + +for dx, dy, move in MOVES: + score = 0 + + x = HEADX + dx + y = HEADY + dy + + n = (SNAKE_LENGTH + 4) / 2 + n = min([n, 10]) + score = get_score(x, y, n) + +# print 'Score for', move, '=', score + + # Suicide protection squad! + try: + square = get_cell(BOARD, x, y) + except KeyError: + continue + else: + if square not in (APPLE_TILE, EMPTY_TILE): + continue + + if score == max_score: + max_moves.append(move) + elif max_score is None or score > max_score: + max_score = score + max_moves = [move] + +if max_moves: + print random.choice(max_moves) +else: + raise Exception, "No suitable moves found!" + diff --git a/console_snake.py b/console_snake.py index 8c327e5..3c40b24 100755 --- a/console_snake.py +++ b/console_snake.py @@ -59,14 +59,14 @@ class ConsoleSnakeEngine(SnakeEngine): def main(*args): from bots import * - from oldbot import BotWrapper + from processbot import BotWrapper game = ConsoleSnakeEngine(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.add_bot(BotWrapper('bots/peter.py')) game.run() if __name__ == '__main__': diff --git a/oldbot.py b/oldbot.py deleted file mode 100644 index 3bd3038..0000000 --- a/oldbot.py +++ /dev/null @@ -1,30 +0,0 @@ -import subprocess - -class BotWrapper(object): - def __init__(self, process): - self.process = process - self.__name__ = process - - def __call__(self, board, (x, y)): - height = len(board) - width = len(board[0]) - - letter = board[y][x].lower() - - proc = subprocess.Popen( - [self.process], - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - ) - - board = '\n'.join([''.join(row) for row in board]) - - print>>proc.stdin, width, height, letter - print>>proc.stdin, board - proc.stdin.close() - proc.wait() - - assert proc.returncode == 0, 'Snake died.' - output = proc.stdout.read() - return output.strip() - diff --git a/oldbots/peter.py b/oldbots/peter.py deleted file mode 100755 index 72bbc95..0000000 --- a/oldbots/peter.py +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/env python -"""New and improved bot, OPTIMISED!!""" - -import random -import sys - -EMPTY_TILE = '.' -APPLE_TILE = '*' - -WIDTH, HEIGHT, SNAKE_BODY = raw_input().split() -WIDTH = int(HEIGHT) -HEIGHT = int(HEIGHT) - -SNAKE_BODY = SNAKE_BODY.lower() -SNAKE_HEAD = SNAKE_BODY.upper() - -HEADX = None -HEADY = None - -def get_cell(board, x, y): - if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT: - raise KeyError, 'out of range.' - return board[y][x] - -BOARD = [] -for y in xrange(HEIGHT): - row = raw_input() - for x, char in enumerate(row): - if char == SNAKE_HEAD: - HEADX = x - HEADY = y - BOARD.append(row) - -md_two = { - (-1, 0, 'l'): ((-2, 0), (-1, 1), (-1, -1)), - (0, -1, 'u'): ((-1, -1), (1, -1), (0, -2)), - (1, 0, 'r'): ((2, 0), (1, 1), (1, -1)), - (0, 1, 'd'): (((0, 2), (-1, 1), (1, 1))), -} - -max_score = 0 -max_moves = [] - -for (dx, dy, move), adj in md_two.items(): - score = 0 - - try: - square = get_cell(BOARD, HEADX + dx, HEADY + dy) - except KeyError: - continue - - if square == APPLE_TILE: - score += 2 - elif square != EMPTY_TILE: - continue # Definitely cannot move here. - - for ddx, ddy in adj: - try: - square = get_cell(BOARD, HEADX + ddx, HEADY + ddy) - except KeyError: - score -= 1 - continue - - if square == APPLE_TILE: - score += 2 - elif square == EMPTY_TILE: - score += 1 - elif square == SNAKE_BODY: - score -= 1 - elif square.isupper(): - score += 3 - - if score == max_score: - max_moves.append(move) - elif score > max_score: - max_score = score - max_moves = [move] - -if max_moves: - print random.choice(max_moves) -else: - print 'U' # Suicide! - diff --git a/oldbots/peter_smart.py b/oldbots/peter_smart.py deleted file mode 100755 index b099cdd..0000000 --- a/oldbots/peter_smart.py +++ /dev/null @@ -1,131 +0,0 @@ -#!/usr/bin/env python -"""New and improved bot, OPTIMISED!!""" - -import random -import sys - -DEBUG = False - -# Show tracebacks, then pause for debugging. -if DEBUG: - sys_excepthook = sys.excepthook - def excepthook(*args, **kwargs): - sys_excepthook(*args, **kwargs) - import time - time.sleep(10) - sys.excepthook = excepthook - -EMPTY_TILE = '.' -APPLE_TILE = '*' - -WIDTH, HEIGHT, SNAKE_BODY = raw_input().split() -WIDTH = int(WIDTH) -HEIGHT = int(HEIGHT) - -SNAKE_BODY = SNAKE_BODY.lower() -SNAKE_HEAD = SNAKE_BODY.upper() - -HEADX = None -HEADY = None - -SNAKE_LENGTH = 0 - -def get_cell(board, x, y): - if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT: - raise KeyError, 'out of range.' - return board[y][x] - -BOARD = [] -for y in xrange(HEIGHT): - row = raw_input() - for x, char in enumerate(row): - if char == SNAKE_HEAD: - HEADX = x - HEADY = y - elif char == SNAKE_BODY: - SNAKE_LENGTH += 1 - BOARD.append(row) - -MOVES = ( - (-1, 0, 'l'), - (1, 0, 'r'), - (0, -1, 'u'), - (0, 1, 'd') -) - -def get_score(x, y, n, done=None): - if done is None: - done = set() - - done.add((x, y)) - - score = 0 - explore = False - - # See if the cell exists. - try: - square = get_cell(BOARD, x, y) - except KeyError: - return 0 - - # Give some extra points for getting an apple. - if square == APPLE_TILE: - explore = True - score += 50 - - # Yay - it's empty! - elif square == EMPTY_TILE: - explore = True - score += 10 - - elif square.islower(): - score -= 1 - - if explore and n > 0: - # Explore n-1 cells further. - for dx, dy, move in MOVES: - nx = x + dx - ny = y + dy - - if (nx, ny) in done: - continue - - subscore = get_score(nx, ny, n - 1, done) - score += subscore / 10 - - return score * n - -max_score = None -max_moves = [] - -for dx, dy, move in MOVES: - score = 0 - - x = HEADX + dx - y = HEADY + dy - - n = (SNAKE_LENGTH + 4) / 2 - score = get_score(x, y, n) - -# print 'Score for', move, '=', score - - # Suicide protection squad! - try: - square = get_cell(BOARD, x, y) - except KeyError: - continue - else: - if square not in (APPLE_TILE, EMPTY_TILE): - continue - - if score == max_score: - max_moves.append(move) - elif max_score is None or score > max_score: - max_score = score - max_moves = [move] - -if max_moves: - print random.choice(max_moves) -else: - raise Exception, "No suitable moves found!" - diff --git a/oldbots/peter_smart2.py b/oldbots/peter_smart2.py deleted file mode 100755 index 8f431ca..0000000 --- a/oldbots/peter_smart2.py +++ /dev/null @@ -1,135 +0,0 @@ -#!/usr/bin/env python -"""New and improved bot, OPTIMISED!!""" - -import random -import sys - -DEBUG = False - -# Show tracebacks, then pause for debugging. -if DEBUG: - sys_excepthook = sys.excepthook - def excepthook(*args, **kwargs): - sys_excepthook(*args, **kwargs) - import time - time.sleep(10) - sys.excepthook = excepthook - -EMPTY_TILE = '.' -APPLE_TILE = '*' - -WIDTH, HEIGHT, SNAKE_BODY = raw_input().split() -WIDTH = int(WIDTH) -HEIGHT = int(HEIGHT) - -SNAKE_BODY = SNAKE_BODY.lower() -SNAKE_HEAD = SNAKE_BODY.upper() - -HEADX = None -HEADY = None - -SNAKE_LENGTH = 0 - -def get_cell(board, x, y): - if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT: - raise KeyError, 'out of range.' - return board[y][x] - -BOARD = [] -for y in xrange(HEIGHT): - row = raw_input() - for x, char in enumerate(row): - if char == SNAKE_HEAD: - HEADX = x - HEADY = y - elif char == SNAKE_BODY: - SNAKE_LENGTH += 1 - BOARD.append(row) - -MOVES = ( - (-1, 0, 'l'), - (1, 0, 'r'), - (0, -1, 'u'), - (0, 1, 'd') -) - -def get_score(x, y, n, done=None): - if done is None: - done = set() - - done.add((x, y)) - - score = 0 - explore = False - - # See if the cell exists. - try: - square = get_cell(BOARD, x, y) - except KeyError: - return 0 - - # Give some extra points for getting an apple. - if square == APPLE_TILE: - explore = True - score += 100 - - # Yay - it's empty! - elif square == EMPTY_TILE: - explore = True - score += 50 - - elif square.islower(): - score += 2 - - elif square.isupper(): - score += 1 - - if explore and n > 0: - # Explore n-1 cells further. - for dx, dy, move in MOVES: - nx = x + dx - ny = y + dy - - if (nx, ny) in done: - continue - - subscore = get_score(nx, ny, n - 1, done) - score += subscore / 10 - - return score * n - -max_score = None -max_moves = [] - -for dx, dy, move in MOVES: - score = 0 - - x = HEADX + dx - y = HEADY + dy - - n = (SNAKE_LENGTH + 4) / 2 - n = min([n, 10]) - score = get_score(x, y, n) - -# print 'Score for', move, '=', score - - # Suicide protection squad! - try: - square = get_cell(BOARD, x, y) - except KeyError: - continue - else: - if square not in (APPLE_TILE, EMPTY_TILE): - continue - - if score == max_score: - max_moves.append(move) - elif max_score is None or score > max_score: - max_score = score - max_moves = [move] - -if max_moves: - print random.choice(max_moves) -else: - raise Exception, "No suitable moves found!" - diff --git a/processbot.py b/processbot.py new file mode 100644 index 0000000..3bd3038 --- /dev/null +++ b/processbot.py @@ -0,0 +1,30 @@ +import subprocess + +class BotWrapper(object): + def __init__(self, process): + self.process = process + self.__name__ = process + + def __call__(self, board, (x, y)): + height = len(board) + width = len(board[0]) + + letter = board[y][x].lower() + + proc = subprocess.Popen( + [self.process], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + ) + + board = '\n'.join([''.join(row) for row in board]) + + print>>proc.stdin, width, height, letter + print>>proc.stdin, board + proc.stdin.close() + proc.wait() + + assert proc.returncode == 0, 'Snake died.' + output = proc.stdout.read() + return output.strip() + diff --git a/pygame_snake.py b/pygame_snake.py index 64faf9b..6ee0eeb 100755 --- a/pygame_snake.py +++ b/pygame_snake.py @@ -138,7 +138,7 @@ class PygameSnakeEngine(SnakeEngine): if __name__ == '__main__': from bots import * - from oldbot import BotWrapper + from processbot import BotWrapper ROWS = 25 COLUMNS = 25 @@ -150,7 +150,7 @@ if __name__ == '__main__': 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.add_bot(BotWrapper('bots/peter.py')) game.run() game.new_game(ROWS, COLUMNS, APPLES) diff --git a/pyglet_snake.py b/pyglet_snake.py index 79ae047..2ef11a8 100755 --- a/pyglet_snake.py +++ b/pyglet_snake.py @@ -109,15 +109,12 @@ class PygletSnakeEngine(SnakeEngine, pyglet.window.Window): if __name__ == '__main__': from bots import random_bounds_bot, random_square_bot - from oldbot import BotWrapper - from peter_bot import peter_bot + from processbot import BotWrapper game = PygletSnakeEngine(25, 25, 50, results=True) -# game.add_bot(random_bounds_bot) -# game.add_bot(random_square_bot) - for i in xrange(0): - game.add_bot(BotWrapper('oldbots/peter.py')) - for i in xrange(1): - game.add_bot(peter_bot) + game.add_bot(random_bounds_bot) + game.add_bot(random_square_bot) + for i in xrange(2): + game.add_bot(BotWrapper('bots/peter.py')) game.run() -- cgit v1.2.3