-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnakePathfindingSim.py
173 lines (141 loc) · 4.59 KB
/
SnakePathfindingSim.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import numpy as np
import random as r
import time, os
# default values, snake length = 4, bait/snake initial position
SNAKE_LENGTH = 10
base_board = [[0 for x in range(30)] for y in range(15)]
head = (len(base_board) // 2, len(base_board[0]) // 3)
snake = [(head[0], head[1] - x) for x in range(1, SNAKE_LENGTH)]
snake.insert(0, head)
bait = [len(base_board) // 2, int(head[1] * 2.5)]
foods = ["♥"]
bait_logo = foods[0]
# =============VISUALS=================#
def display(board):
print(" ┏" + "━━️" * (len(base_board) + 3) + "┓")
for row in board:
print(" |", end="")
for column in row:
if column == 1:
print("◼️", end="")
elif column == 2:
print(bait_logo, end="")
else:
print(" ", end=" ")
print("|")
print(" ┗" + "━️━" * (len(base_board) + 3) + "┛")
def add_objects():
for segment in snake:
base_board[segment[0]][segment[1]] = 1
base_board[bait[0]][bait[1]] = 2
def clear_board():
for row in range(len(base_board)):
for column in range(len(base_board[row])):
base_board[row][column] = 0
# ==============LOGIC==================#
def pathfind():
open = {}
closed = {}
trav = snake[0]
while trav[0] != bait[0] or trav[1] != bait[1]:
valids = get_valid_adjacent(trav)
for valid in valids:
fcost = calc_fcost(valid)
if valid not in closed:
open[valid] = fcost
if open == {}:
return -1
lowest = min(open, key=open.get)
closed[lowest] = open[lowest]
del open[lowest]
trav = lowest
path = find_path(closed, [min(closed, key=closed.get)], 20, max(closed, key=closed.get))
return path
def find_path(path, final, trav, first):
path_list = [(k, v) for k, v in path.items()] # list version
batch = [x for x, y in path.items() if y == trav] # traversal
adjacent = get_adjacent(final[0]) # adjacent to the last
if len(path_list) == 1:
return path_list[0]
if batch == []: # wrong path / out of bounds
return False
adj = [x for x in batch if x in adjacent]
if adj == []:
find_path(path, final, trav - 20, first)
else:
for coord in adj:
final.insert(0, coord)
temp_path = path.copy()
del temp_path[coord]
result = find_path(temp_path, final, trav + 10, first)
if result != False:
break
return final
def get_adjacent(coord):
adjacents = [(coord[0] - 1, coord[1]), (coord[0], coord[1] + 1), (coord[0] + 1, coord[1]), (coord[0], coord[1] - 1)]
return adjacents
def get_valid_adjacent(coord):
valid = []
corners = get_adjacent(coord)
for corner in corners:
try:
if corner[0] < 0 or corner[1] < 0 or corner in snake:
pass
elif base_board[corner[0]][corner[1]] == 0 or base_board[corner[0]][corner[1]] == 2:
valid.append(corner)
except IndexError:
pass
return valid
def calc_fcost(coord):
abs_x, abs_y = abs(bait[0] - coord[0]), abs(bait[1] - coord[1])
gcost = 10 # 4-way direction only
hcost = (abs_x + abs_y) * gcost
fcost = gcost + hcost
return fcost
def move(coord):
global bait_logo
if coord == -1:
return coord
if coord[0] == bait[0] and coord[1] == bait[1]:
generate_bait()
bait_logo = r.choice(foods)
snake.append(snake[len(snake) - 1])
for segment in range(1, len(snake)):
snake[len(snake) - segment] = snake[len(snake) - segment - 1]
snake[0] = coord
def generate_bait():
while True:
x, y = r.randint(0, len(base_board) - 1), r.randint(0, len(base_board[0]) - 1)
if base_board[x][y] == 0:
bait[0], bait[1] = x, y
break
# ==============TESTS==================#
def test():
while True:
os.system("cls")
display(base_board)
result = move(generate_move())
if result == -1:
break
clear_board()
add_objects()
time.sleep(0.05) # 0.5 = SLOW, 0.3 = MEDIUM, 0.1 = FAST FPS
def generate_move():
valid = get_valid_adjacent(snake[0])
if valid == []:
return -1
return r.choice(valid)
# ============MAIN=METHOD==============#
def main():
while True:
os.system("cls")
display(base_board)
path = pathfind()
if path == -1:
break
move(path[0])
clear_board()
add_objects()
time.sleep(0.07)
if __name__ == "__main__":
main()