generated from BattlesnakeOfficial/starter-snake-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_board.py
43 lines (33 loc) · 1.54 KB
/
find_board.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from algoliasearch.search_client import SearchClient
from typing import List
ALGOLIA_APP_ID = environ.get('ALGOLIA_APP_ID')
ALGOLIA_API_KEY = environ.get('ALGOLIA_API_KEY')
client = SearchClient.create(ALGOLIA_APP_ID, ALGOLIA_API_KEY)
def vision_to_string(vision: List[str]) -> str:
return ''.join(vision)
def remove_move(move: str, possible_moves: List[str]) -> List[str]:
if move in possible_moves:
possible_moves.remove(move)
return possible_moves
def avoid_walls(vision: List[str], possible_moves: List[str]) -> List[str]:
index = client.init_index('boards-walls')
print("Searching for walls in " + vision_to_string(vision))
results = index.search(vision_to_string(vision))
if results['hits']:
print("Search detected " + results['hits'][0]['description'])
for move in results['hits'][0]['remove_moves']:
possible_moves = remove_move(move, possible_moves)
return possible_moves
def find_food(size: int, vision: str, possible_moves: List[str]) -> List[str]:
best_moves = []
board_index = 'boards-apples-' + str(size) + 'x' + str(size)
index = client.init_index(board_index)
print("Searching for food in " + vision)
results = index.search(vision)
if results['hits']:
print("Search detected " + results['hits'][0]['description'])
for move in results['hits'][0]['best_move']:
if move in possible_moves:
best_moves.append(move)
print("Added " + move + " to best moves [" + str(best_moves) + "]")
return best_moves