-
Notifications
You must be signed in to change notification settings - Fork 0
/
movement_notes.py
120 lines (95 loc) · 3.65 KB
/
movement_notes.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Snake Game movement interface
# To be used in an interval as the game is running to perpetuate movement
direction = 'n'
cur_matrix = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 2, 0],
[0, 0, 0, 3, 0],
[0, 0, 0, 4, 5],
[0, 0, 0, 0, 0]
]
expected_matrix = [
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 2, 3, 0],
[0, 0, 0, 4, 0],
[0, 0, 0, 5, 0],
[0, 0, 0, 0, 0]
]
# Validating input types being sent in the useEffect
def check_input(matrix, direction, pos, length):
if type(length) != int:
return False
if type(pos[0]) != int or type(pos[1]) != int:
return False
if direction not in ['n', 'e', 's', 'w']:
return False
if type(matrix) != list or type(matrix[0]) != list:
return False
return True
# Movement Functions [pop_head, cw_search]
def pop_head(matrix, direction, pos):
if direction == 'n':
if pos[0] == 0 or matrix[ pos[0] - 1 ][ pos[1] ] == 2:
return 'snek_dead'
matrix[ pos[0] - 1 ][ pos[1] ] = 1
if direction == 'e':
if pos[1] == len(matrix[0])-1 or matrix[ pos[0] ][ pos[1]+1 ] == 2:
return 'snek_dead'
matrix[ pos[0] ][ pos[1]+1 ] = 1
if direction == 's':
if pos[0] == len(matrix)-1 or matrix[ pos[0]+1 ][ pos[1] ] == 2:
return 'snek_dead'
matrix[ pos[0]+1 ][ pos[1] ] = 1
if direction == 'w':
if pos[1] == 0 or matrix[ pos[0] ][ pos[1]-1 ] == 2:
return 'snek_dead'
matrix[ pos[0] ][ pos[1]-1 ] = 1
return True
def search_and_update(matrix, pos, body_num):
# check 'n' for next body part
if pos[0] > 0 and matrix[ pos[0]-1 ][ pos[1] ] == body_num:
matrix[pos[0]-1][pos[1]] = body_num
return [ pos[0]-1, [pos[1]] ]
# and 'e'
if pos[1] < len(matrix[0])-1 and matrix[ pos[0] ][ pos[1]+1 ] == body_num:
matrix[pos[0]][pos[1]+1] = body_num
return [ pos[0], pos[1]+1 ]
# 's' too
if pos[0] < len(matrix)-1 and matrix[ pos[0]+1 ][ pos[1] ] == body_num:
matrix[ pos[0]+1 ][ pos[1] ] = body_num
return [ pos[0]+1, pos[1] ]
# can't forget about 'w'
if pos[1] > 0 and matrix[ pos[0] ][ pos[1]-1 ] == body_num:
matrix[ pos[0] ][ pos[1]-1 ] = body_num
return [ pos[0], pos[1]-1 ]
return False
def destroy(matrix, pos, body_num):
# check 'n' for next body part
if pos[0] > 0 and matrix[ pos[0]-1 ][ pos[1] ] == body_num:
matrix[pos[0]-1][pos[1]] = 0
# and 'e'
if pos[1] < len(matrix[0])-1 and matrix[ pos[0] ][ pos[1]+1 ] == body_num:
matrix[pos[0]][pos[1]+1] = 0
# 's' too
if pos[0] < len(matrix)-1 and matrix[ pos[0]+1 ][ pos[1] ] == body_num:
matrix[ pos[0]+1 ][ pos[1] ] = 0
# can't forget about 'w'
if pos[1] > 0 and matrix[ pos[0] ][ pos[1]-1 ] == body_num:
matrix[ pos[0] ][ pos[1]-1 ] = 0
def move_snake(matrix, direction, pos, length):
if not check_input(matrix, direction, pos, length):
return 'input not recognized, check args types and structure'
working_matrix = [row[:] for row in matrix]
if pop_head(working_matrix, direction, pos) == 'snek_dead':
return 'snek_dead'
# Starting at the previous head, we begin updating the values of the body so matrix reflects the snake's next position
body_num = 2
running_pos = [ pos[0], pos[1] ]
while not body_num > length:
running_pos = search_and_update(working_matrix, running_pos, body_num)
body_num += 1
# destroy should work to not break any pieces if snake's current move also consumes a food
destroy(working_matrix, running_pos, body_num)
return working_matrix