-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
72 lines (56 loc) · 2.44 KB
/
snake.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from constraints import check_constraints
def number_of_available_different_paths(board, snake, depth):
check_constraints(board, snake, depth)
actual_depth = 0
paths = [snake]
valid_paths = find_paths(board, depth, actual_depth, paths)
return valid_paths
def find_paths(board, depth, actual_depth, paths):
if actual_depth == depth:
valid_paths = len(paths)
return valid_paths
new_movements = []
for path in paths:
new_movements.extend(check_movements(board, path))
paths = new_movements
actual_depth += 1
return find_paths(board, depth, actual_depth, paths)
def check_movements(board, previous_path):
new_paths = []
check_up_movement(previous_path, new_paths)
check_down_movement(previous_path, new_paths, board)
check_left_movement(previous_path, new_paths)
check_right_movement(previous_path, new_paths, board)
return new_paths
def check_up_movement(path, new_paths):
# Check if up movement not crosses board and there is not self-intersection
new_path = path.copy()
new_path.pop(-1)
up_movement = new_path[0][0] - 1
if 0 <= up_movement and [up_movement, new_path[0][1]] not in new_path:
new_path.insert(0, [up_movement, new_path[0][1]])
new_paths.append(new_path)
def check_down_movement(path, new_paths, board):
# Check if down movement not crosses board and there is not self-intersection
new_path = path.copy()
new_path.pop(-1)
down_movement = new_path[0][0] + 1
if down_movement < board[0] and [down_movement, new_path[0][1]] not in new_path:
new_path.insert(0, [down_movement, new_path[0][1]])
new_paths.append(new_path)
def check_left_movement(path, new_paths):
# Check if left movement not crosses board and there is not self-intersection
new_path = path.copy()
new_path.pop(-1)
left_movement = new_path[0][1] - 1
if 0 <= left_movement and [new_path[0][0], left_movement] not in new_path:
new_path.insert(0, [new_path[0][0], left_movement])
new_paths.append(new_path)
def check_right_movement(path, new_paths, board):
# Check if right movement not crosses board and there is not self-intersection
new_path = path.copy()
new_path.pop(-1)
right_movement = new_path[0][1] + 1
if right_movement < board[1] and [new_path[0][0], right_movement] not in new_path:
new_path.insert(0, [new_path[0][0], right_movement])
new_paths.append(new_path)