def compute_ghost_pos(pos, level, memory): visits_count = memory.setdefault('visits_count', defaultdict(lambda: 0)) moves = adjacent_moves(pos) for blocked_pos in get_blocked_coords(list(moves), level): moves.remove(blocked_pos) if not moves: return pos if len(moves) == 1: return moves[0] for move in moves: for x, y in iter_coords(pos, move, level): if level[y][x] == 'H': memory['last_pos'] = pos visits_count[pos] += 1 return move if 'last_pos' in memory and memory['last_pos'] in moves: moves.remove(memory['last_pos']) def sort_key(pos): return visits_count.get(pos, 0) new_pos = sorted(moves, key=sort_key)[0] memory['last_pos'] = pos visits_count[pos] += 1 return new_pos
def compute_ghost_pos(pos, level, memory): moves = get_allowed_moves(pos, level) if not moves: return pos memory['last_pos'] = pos if len(moves) == 1: return moves[0] for move in moves: for x, y in iter_coords(pos, move, level): if level[y][x] == 'H': return move if 'last_pos' in memory and memory['last_pos'] in moves: moves.remove(memory['last_pos']) new_pos = random.choice(moves) return new_pos
def compute_ghost_pos(pos, level, memory): moves = adjacent_moves(pos) for blocked_pos in get_blocked_coords(list(moves), level): moves.remove(blocked_pos) if not moves: return pos if len(moves) == 1: return moves[0] for move in moves: for x, y in iter_coords(pos, move, level): if level[y][x] == 'H': memory['last_pos'] = pos return move if 'last_pos' in memory and memory['last_pos'] in moves: moves.remove(memory['last_pos']) new_pos = random.choice(moves) memory['last_pos'] = pos return new_pos
def compute_ghost_pos(pos, level, memory): visits_count = memory.setdefault('visits_count', defaultdict(lambda: 0)) moves = get_allowed_moves(pos, level) if not moves: visits_count[pos] += 1 return pos memory['last_pos'] = pos if len(moves) == 1: visits_count[moves[0]] += 1 return moves[0] for move in moves: for x, y in iter_coords(pos, move, level): if level[y][x] == 'H': visits_count[move] += 1 return move if 'last_pos' in memory and memory['last_pos'] in moves: moves.remove(memory['last_pos']) def sort_key(pos): return visits_count.get(pos, 0) new_pos = sorted(moves, key=sort_key)[0] visits_count[new_pos] += 1 return new_pos