-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChess_Horse.py
177 lines (126 loc) · 3.88 KB
/
Chess_Horse.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
import copy
#import graphviz
import os
from colorama import Fore
os.system('del test-output /q')
moves = [(-1,2),(1,2),(2,1),(2,-1),(1,-2),(-1,-2),(-2,-1),(-2,1)]
# # # # #
# # # #
class Node:
def __init__(self,x,y,depth):
self.x = x
self.y = y
self.depth = depth
self.h_n = 7-self.x if 7-self.x > 7-self.y else 7-self.y
self.g_n = depth*3
self.f_n = self.h_n + self.g_n
self.symmetry = (None,None)
self.leaves=[]
self.parent = None
def get_information(self):
info = ''
info += Fore.RED+'({0},{1})'.format(self.x,self.y) + '\n'
info +=Fore.WHITE
info += 'depth = {0}'.format(self.depth) + '\n'
info += f"{'f(n)':<7}={'g(n)':^10}+{'h(n)':^10}\n"
info += '{0:<7}={1:^10}+{2:^10}'.format(self.f_n , self.g_n , self.h_n) + '\n'
return info
def show_output(self,final_leaves,min_obj):
k=1
color = Fore.WHITE
for i in final_leaves:
if i in self.leaves:
color = Fore.LIGHTGREEN_EX
if min_obj==i:
color = Fore.LIGHTRED_EX
else:
if min_obj==i:
color = Fore.LIGHTRED_EX
else:
color = Fore.WHITE
print(color +'(x={0:^2d}, y={1:^2d}, f={2:d})'.format(i.x,i.y,i.f_n),end=' '*3)
if i==final_leaves[-1]:
print(Fore.WHITE)
break
if k%3==0:
print()
k+=1
print(k)
print(Fore.LIGHTMAGENTA_EX+'-'*60,'\n')
final_leaves=[]
count = 0
def move_horse(self):
global moves,board,count
#graph = graphviz.Digraph(comment='The Round Table',node_attr={'shape':'square'},format = 'png')
count+=1
print(self.get_information())
#graph.node(str(self),self.get_information())
for i in moves:
if 8 > self.x+i[0] >= 0 and 8 > self.y+i[1] >= 0 and i!=self.symmetry and not((self.x+i[0],self.y+i[1]) in barriers):
self.leaves.append(Node(self.x+i[0],self.y+i[1],self.depth+1))
#graph.node(str(self.leaves[-1]),self.leaves[-1].get_information())
#graph.edge(str(self),str(self.leaves[-1]))
self.leaves[-1].symmetry = (-i[0],-i[1])
self.leaves[-1].parent = self
if self.leaves[-1].x==7 and self.leaves[-1].y==7 :
print(self.leaves[-1].x,self.leaves[-1].y,self.leaves[-1].parent.x , self.leaves[-1].parent.y)
return self.leaves[-1]
else:
continue
self_index = final_leaves.index(self)
final_leaves.remove(self)
for i in self.leaves[::-1]:
final_leaves.insert(self_index,i)
min_value = 1000000000
min_obj=None
for i in final_leaves:
if i.f_n < min_value:
min_value = i.f_n
min_obj = i
show_output(self,final_leaves,min_obj)
#graph.node(str(min_obj),min_obj.get_information(),color = 'red')
#graph.render('test-output/'+str(count))
return move_horse(min_obj)
finishing_char = ''
barrier_x = 0
barrier_y = 0
barriers = []
while finishing_char not in['no','No','n','N']:
try:
barrier_x,barrier_y = map(int,input('Enter x and y barrier:').split(','))
except:
print('invalid input')
continue
if not(8>barrier_x>-1 and 8>barrier_y>-1) or (barrier_x,barrier_y) in [(0,0),(7,7)]:
print('invalid input')
continue
barriers.append((barrier_x,barrier_y))
finishing_char = input('Do you want to countinue?(yes/no) ')
print('barriers = ',barriers)
tree = Node(0,0,0)
final_leaves.append(tree)
last_leaf = move_horse(tree)
#print(last_leaf.x , last_leaf.y , last_leaf.f_n)
goal_path = []
loop_finish = False
while True:
goal_path.append(last_leaf)
last_leaf = last_leaf.parent
if loop_finish:
break
if last_leaf.parent == None:
loop_finish = True
print(Fore.RED +'\n'+'-'*60)
print(Fore.CYAN+ 'number of leaves : '+str(len(final_leaves)))
print('\nanswer is:')
for i in goal_path[::-1]:
print('(x={0:^2d}, y={1:^2d}, f={2:d})'.format(i.x,i.y,i.f_n))
#Cgraph= graphviz.Digraph(comment='The Round Table',node_attr={'shape':'square'},format = 'pdf')
#for i in goal_path[::-1]:
# Cgraph.node(str(i),i.get_information(),color = 'red')
# for j in i.leaves:
# Cgraph.node(str(j),j.get_information())
# Cgraph.edge(str(i),str(j))
#
#Cgraph.render('test-ouput/output')
#Cgraph.view()