בציור למטה מופיע חלק מלוח שחמט, אנו צריכים לבצע מהלכים של פרש ולנסות להחליף את המיקומים של הפרשים. כך שבסוף המהלכים הפרשים הלבנים יהיו למעלה והשחורים למטה (בפינות כמובן). אתם נדרשים לבנות מודל בפייתון שיניב את דרך הפתרון עד לתוצאה המיוחלת.

רמז - למשבצת האמצעית אין שום תפקיד משום שהפרשים לא יכולים להגיע אליה בשום מקרה. בסדר אז זה לא רמז גדול. הפרשים לא צריכים לשחק לפי התור, כלי יכול לנוע פעמיים ברצף.
פתרון הכולל 16 מהלכים - המודל שלנו מסתמך על רשימה שהתא הראשון שלה למטה משמאל, והמספרים נעים במעגל עד לתא השמיני שהוא מימין לתא הראשון. (האמצע לא חשוב לחידה הזאת).
class Night: def __init__(self,color,location,grid): self.color=color self.location=location self.grid=grid self.grid[self.location]=f"{self.color}{self.location}" def __str__(self): return f"{self.color}{self.location}" def posibleClockeiseMoves(self): if self.location == 1: return 4 if self.location == 2: return 5 if self.location == 3: return 6 if self.location == 4: return 7 if self.location == 5: return 8 if self.location == 6: return 1 if self.location == 7: return 2 if self.location == 8: return 3 def posible_moves(self): lista=[] if self.location==1: for i in (4,6): if self.grid[i]==0: lista.append(i) return lista if self.location==2: for i in (5,7): if self.grid[i] == 0: lista.append(i) return lista if self.location==3: for i in (8,6): if self.grid[i] == 0: lista.append(i) return lista if self.location==4: for i in (1,7): if self.grid[i] == 0: lista.append(i) return lista if self.location==5: for i in (2,8): if self.grid[i] == 0: lista.append(i) return lista if self.location==6: for i in (1,3): if self.grid[i] == 0: lista.append(i) return lista if self.location==7: for i in (2,4): if self.grid[i] == 0: lista.append(i) return lista if self.location==8: for i in (3,5): if self.grid[i] == 0: lista.append(i) return lista def move_to(self,destiny): if grid[destiny] !=0: print(f"location {destiny} is occupide") else: self.grid[self.location]=0 self.location=destiny self.grid[self.location] = f"{self.color}{self.location}" grid=['Grid',0,0,0,0,0,0,0,0] w1=Night('w',1,grid) w2=Night('w',7,grid) b1=Night('b',3,grid) b2=Night('b',5,grid) allNights=[w1,w2,b1,b2] for night in allNights: n=night.location grid[n]=night.__str__() nightDict={'w1':w1.__str__(),'w2':w2.__str__(),'b1':b1.__str__(),'b2':b2.__str__()} n=0 for i in range(4): for night in allNights: p=night.posibleClockeiseMoves() grid=night.grid if grid[p]==0: night.move_to(p) print(grid) n+=1 if grid[3]=='w3' and grid[5]=='w5' and grid[1]=='b1' and grid[7]=="b7": print(f"works!!! {n} moves") break הפתרון לאחר הרצת התוכנית נראה כך -
['Grid', 0, 0, 'b3', 'w4', 'b5', 0, 'w7', 0]
['Grid', 0, 'w2', 'b3', 'w4', 'b5', 0, 0, 0]
['Grid', 0, 'w2', 0, 'w4', 'b5', 'b6', 0, 0]
['Grid', 0, 'w2', 0, 'w4', 0, 'b6', 0, 'b8']
['Grid', 0, 'w2', 0, 0, 0, 'b6', 'w7', 'b8']
['Grid', 0, 0, 0, 0, 'w5', 'b6', 'w7', 'b8']
['Grid', 'b1', 0, 0, 0, 'w5', 0, 'w7', 'b8']
['Grid', 'b1', 0, 'b3', 0, 'w5', 0, 'w7', 0]
['Grid', 'b1', 'w2', 'b3', 0, 'w5', 0, 0, 0]
['Grid', 'b1', 'w2', 'b3', 0, 0, 0, 0, 'w8']
['Grid', 0, 'w2', 'b3', 'b4', 0, 0, 0, 'w8']
['Grid', 0, 'w2', 0, 'b4', 0, 'b6', 0, 'w8']
['Grid', 0, 0, 0, 'b4', 'w5', 'b6', 0, 'w8']
['Grid', 0, 0, 'w3', 'b4', 'w5', 'b6', 0, 0]
['Grid', 0, 0, 'w3', 0, 'w5', 'b6', 'b7', 0]
['Grid', 'b1', 0, 'w3', 0, 'w5', 0, 'b7', 0]
works!!! 16 moves