-
Notifications
You must be signed in to change notification settings - Fork 0
/
8x8_puzzel_BFS.py
196 lines (169 loc) · 5.03 KB
/
8x8_puzzel_BFS.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
class Puzzle:
State = []
Path = []
Ci = -1
Ri = -1
P = Puzzle()
Goal=[[1,2,3],[4,5,6],[7,8,-1]]
Qeuee=[]
ExploredSet=[]
def PrintPuzzle(State):
print()
i=0
while i < 3:
if State[i][0] == -1:
print(" ",State[i][1],State[i][2])
elif State[i][1] == -1:
print(State[i][0]," ",State[i][2])
elif State[i][2] == -1:
print(State[i][0],State[i][1]," ")
else:
print(State[i][0],State[i][1],State[i][2])
i+=1
print()
def CheckInput(P,num):
x = len(P.State)
i=0
while i < x:
y = len(P.State[i])
j=0
while j < y:
if P.State[i][j] == num:
return False
j+=1
i+=1
return True
def TakeInput(P):
i=0
while i < 3:
j=0
P.State.append([])
while j < 3:
while True:
X = int((input("Enter Number Range(1-8): " )))
if (X < 0 or X > 8) and X != -1:
print("Invalid Range")
else:
if CheckInput(P,X)==False:
print("Already Entered")
else:
print("Entered Successfully")
if X == -1:
P.Ri = i
P.Ci = j
P.State[i].append(X)
break
j+=1
i+=1
def CheckGoal(Goal,State):
if Goal != State:
return False
else:
return True
def MoveRight(Temp):
P = Puzzle()
import copy
P.State = copy.deepcopy(Temp.State)
P.Path = copy.deepcopy(Temp.Path)
P.Ci=Temp.Ci
P.Ri=Temp.Ri
if P.Ci != 2:
P.State[P.Ri][P.Ci],P.State[P.Ri][P.Ci+1] = P.State[P.Ri][P.Ci+1] , P.State[P.Ri][P.Ci]
P.Path.append("Right")
P.Ci+=1
return P
else:
return 0
def MoveLeft(Temp):
P = Puzzle()
import copy
P.State = copy.deepcopy(Temp.State)
P.Path = copy.deepcopy(Temp.Path)
P.Ci=Temp.Ci
P.Ri=Temp.Ri
if P.Ci != 0:
P.State[P.Ri][P.Ci],P.State[P.Ri][P.Ci-1] = P.State[P.Ri][P.Ci-1] , P.State[P.Ri][P.Ci]
P.Path.append("Left")
P.Ci-=1
return P
else:
return 0
def MoveUp(Temp):
P = Puzzle()
import copy
P.State = copy.deepcopy(Temp.State)
P.Path = copy.deepcopy(Temp.Path)
P.Ci=Temp.Ci
P.Ri=Temp.Ri
if P.Ri != 0:
P.State[P.Ri][P.Ci],P.State[P.Ri-1][P.Ci] = P.State[P.Ri-1][P.Ci] , P.State[P.Ri][P.Ci]
P.Path.append("Up")
P.Ri-=1
return P
else:
return 0
def MoveDown(Temp):
P = Puzzle()
import copy
P.State = copy.deepcopy(Temp.State)
P.Path = copy.deepcopy(Temp.Path)
P.Ci=Temp.Ci
P.Ri=Temp.Ri
if P.Ri != 2:
P.State[P.Ri][P.Ci],P.State[P.Ri+1][P.Ci] = P.State[P.Ri+1][P.Ci] , P.State[P.Ri][P.Ci]
P.Path.append("Down")
P.Ri+=1
return P
else:
return 0
def CheckExploredSet(State,ExploredSet):
c = len(ExploredSet)
i = 0
while i < c:
if ExploredSet[i] == State:
return False
i+=1
return True
def BreakLoop(Check,Goal,ExploredSet,Qeuee):
if Check != 0:
if CheckExploredSet(Check.State,ExploredSet) == False and CheckGoal(Goal,Check.State) == False:
return False
elif CheckExploredSet(Check.State,ExploredSet) == True and CheckGoal(Goal,Check.State) == False:
Qeuee.append(Check)
return False
else:
print("After Goal")
PrintPuzzle(Check.State)
print("Path :: ")
print(Check.Path)
return True
else:
return False
TakeInput(P)
print("Before Goal")
PrintPuzzle(P.State)
if CheckGoal(Goal,P.State) == False:
Qeuee.append(P)
while True:
if len(Qeuee)!= 0:
Temp = Qeuee.pop(0)
if CheckExploredSet(Temp.State,ExploredSet) == True:
ExploredSet.append(Temp.State)
Check = MoveLeft(Temp)
if BreakLoop(Check,Goal,ExploredSet,Qeuee) == True:
break
Check = MoveRight(Temp)
if BreakLoop(Check,Goal,ExploredSet,Qeuee) == True:
break
Check = MoveUp(Temp)
if BreakLoop(Check,Goal,ExploredSet,Qeuee) == True:
break
Check = MoveDown(Temp)
if BreakLoop(Check,Goal,ExploredSet,Qeuee) == True:
break
else:
print("After Goal")
PrintPuzzle(P.State)
print("Path :: ")
print(P.Path)
input("Press Enter to close!!!")