forked from chuyangliu/snake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
greedy.py
51 lines (41 loc) · 1.47 KB
/
greedy.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
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=C0111
from snake.base.pos import Pos
from snake.solver.base import BaseSolver
from snake.solver.path import PathSolver
class GreedySolver(BaseSolver):
def __init__(self, snake):
super().__init__(snake)
self.__path_solver = PathSolver(snake)
def next_direc(self):
# Create a virtual snake
s_copy, m_copy = self.snake.copy()
# Step 1
self.__path_solver.snake = self.snake
path_to_food = self.__path_solver.shortest_path_to_food()
if path_to_food:
# Step 2
s_copy.move_path(path_to_food)
if m_copy.is_full():
return path_to_food[0]
# Step 3
self.__path_solver.snake = s_copy
path_to_tail = self.__path_solver.longest_path_to_tail()
if len(path_to_tail) > 1:
return path_to_food[0]
# Step 4
self.__path_solver.snake = self.snake
path_to_tail = self.__path_solver.longest_path_to_tail()
if len(path_to_tail) > 1:
return path_to_tail[0]
# Step 5
head = self.snake.head()
direc, max_dist = self.snake.direc, -1
for adj in head.all_adj():
if self.map.is_safe(adj):
dist = Pos.manhattan_dist(adj, self.map.food)
if dist > max_dist:
max_dist = dist
direc = head.direc_to(adj)
return direc