diff options
author | Peter Ward <peteraward@gmail.com> | 2009-09-28 11:48:20 +1000 |
---|---|---|
committer | Peter Ward <peteraward@gmail.com> | 2009-09-28 11:48:20 +1000 |
commit | 2b3953c0d4af78ba379d49950cba222983d20c8a (patch) | |
tree | e1d3ea7c95c4c1fd4402cf8e314aaa6ac3e80e5e /bots.py | |
parent | b781dfc5059f22d273916df88d9aef2f176358ab (diff) |
Random bot colours, improvements to random bots.
Diffstat (limited to 'bots.py')
-rw-r--r-- | bots.py | 35 |
1 files changed, 32 insertions, 3 deletions
@@ -1,9 +1,14 @@ import random +from common import * + def right_bot(board, (x, y)): return 'R' def random_bot(board, (x, y)): + return random.choice('UDLR') + +def random_bounds_bot(board, (x, y)): height = len(board) width = len(board[0]) moves = [] @@ -15,9 +20,33 @@ def random_bot(board, (x, y)): moves.append('U') if y < height - 1: moves.append('D') - return random.choice(moves) -def random_bot2(board, (x, y)): + move = 'U' + while moves and move not in moves: + move = random_bot(board, (x, y)) + return move + +def random_square_bot(board, (x, y)): + def in_bounds(x, y, w, h): + return x >= 0 and y >= 0 and x < w and y < h + + h = len(board) + w = len(board[0]) + + todo = directions.keys() + move = random_bot(board, (x, y)) - nx, ny = x + dx, dy = directions[move] + nx = x + dx + ny = y + dy + + while todo and in_bounds(nx, ny, w, h) and \ + board[ny][nx] not in (Squares.EMPTY, Squares.APPLE): + if move in todo: + todo.remove(move) + move = random_bot(board, (x, y)) + dx, dy = directions[move] + nx = x + dx + ny = y + dy + return move |