This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJake The Snake.py
111 lines (90 loc) · 3.12 KB
/
Jake The 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
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
s = [['+', '-', '+', '-', '+', '-', '+'], #One Way With No DeadEnd/ Case used
['|', ' ', ' ', ' ', ' ', ' ', '|'],
['+', ' ', '+', '-', '+', ' ', '+'],
['|', ' ', ' ', 'F', '|', ' ', '|'],
['+', '-', '+', '-', '+', ' ', '+'],
['|', '$', ' ', ' ', ' ', ' ', '|'],
['+', '-', '+', '-', '+', '-', '+']]
case2 = [['+', '-', '+', '-', '+', '-', '+'], #Two Way With 1 DeadEnd
['|', ' ', ' ', ' ', ' ', ' ', '|'],
['+', ' ', '+', '-', '+', ' ', '+'],
['|', ' ', ' ', 'F', '|', ' ', '|'],
['+', ' ', '+', '-', '+', '-', '+'],
['|', '$', ' ', ' ', ' ', ' ', '|'],
['+', '-', '+', '-', '+', '-', '+']]
t = 7
pos = []
path = ["=>"]
def check(s, pm, pn): #Check Around Snake's Pos Surroundings
print ("DOWN:",s[pm+1][pn], s[pm+1][pn] == " ")
print ("UP:",s[pm-1][pn])
print ("R:",s[pm][pn+1])
print ("L:",s[pm][pn-1])
print(pm, pn)
def map(s, m, n): #Map out current in checkEmptySpace
for m in range(len(s)):
for n in range(t):
print(s[m][n], end="")
print("\n")
def checkFood(s, path, pm, pn):
if s[pm+1][pn] == "F":
return path.append("down")
elif s[pm-1][pn] == "F":
return path.append("up")
elif s[pm][pn+1] == "F":
return path.append("right")
elif s[pm][pn-1] == "F":
return path.append("left")
else:
return 0
def checkEmptySpace(s, path, pm, pn):
# s[pm][pn] = "$" //Snake Current Position
# map(s, pm, pn) //Snake Current Position on Map
if checkFood(s, path, pm, pn) != 0:
return path
elif(s[pm][pn+1]) == " " and path[-1] != "left":
s[pm][pn] = " "
path.append("right")
checkEmptySpace(s, path, pm, pn+1)
elif(s[pm][pn-1]) == " " and path[-1] != "right":
s[pm][pn] = " "
path.append("left")
checkEmptySpace(s, path, pm, pn-1)
elif(s[pm-1][pn]) == " " and path[-1] != "down":
s[pm][pn] = " "
path.append("up")
checkEmptySpace(s, path, pm-1, pn)
elif(s[pm+1][pn]) == " " and path[-1] != "up":
s[pm][pn] = " "
path.append("down")
checkEmptySpace(s, path, pm+1, pn)
else:
if path[-1] == "up":
s[pm][pn] = " "
path.append("down")
checkEmptySpace(s, path, pm+1, pn)
elif path[-1] == "down":
s[pm][pn] = " "
path.append("up")
checkEmptySpace(s, path, pm-1, pn)
elif path[-1] == "right":
s[pm][pn] = " "
path.append("left")
checkEmptySpace(s, path, pm, pn-1)
elif path[-1] == "left":
s[pm][pn] = " "
path.append("right")
checkEmptySpace(s, path, pm, pn+1)
else:
# check(s, pm, pn) //Check Surrounding Elements
exit()
# --------------------------------- Driver Code
for m in range(len(s)):
for n in range(t):
if(s[m][n] == "$"):
pos.append(m)
pos.append(n)
print(s[m][n], end="") #Initial map
print("\n")
checkEmptySpace(s, path, pos[0], pos[1])
print(*path)