summaryrefslogtreecommitdiff
path: root/docs/random_avoid.py
blob: bf08eef39ef39771bf6f5661e99a163a94a30e6d (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from random import choice

def random_avoid_bot(board, position):
    x, y = position
    height = len(board)
    width = len(board[0])

    valid_moves = []

    left = board[y][(x - 1) % width]
    if left == '.' or left == '*':
        valid_moves.append('L')

    right = board[y][(x + 1) % width]
    if right == '.' or right == '*':
        valid_moves.append('R')

    up = board[(y - 1) % height][x]
    if up == '.' or up == '*':
        valid_moves.append('U')

    down = board[(y + 1) % height][x]
    if down == '.' or down == '*':
        valid_moves.append('D')

    return choice(valid_moves)