75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
from collections import namedtuple
|
|
from random import randint
|
|
from keres import *
|
|
from seged import *
|
|
|
|
Act = namedtuple('Act', ['korong', 'rud']) # [int, str]
|
|
|
|
class Hanoi(Feladat):
|
|
def __init__(self, n): #hany darab korong = n
|
|
super().__init__('1'*n)
|
|
|
|
def célteszt(self, állapot: str):
|
|
if állapot == '2'*len(állapot) or állapot == '3'*len(állapot):
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def mod(self, állapot):
|
|
self.kezdő = állapot
|
|
|
|
def result(self, állapot: str, act: Act): #állapot(1,1,1) -> 0 korong '2' poziciora -> (2,1,1)
|
|
k,r = act #korong, rud kicsomagolasa
|
|
return állapot[:k] + r + állapot[k+1:]
|
|
|
|
def rákövetkező(self, állapot: str) -> list[tuple[str, tuple[int,int,int]]]:
|
|
lepesek = []
|
|
f1 = állapot.find('1') # melyik az legkisebb korong az első rúdon
|
|
f2 = állapot.find('2') # -1 ha nincs korong a rúdon
|
|
f3 = állapot.find('3')
|
|
|
|
if f1 > -1 and (f1 < f2 or f2 == -1):
|
|
text = f'A {f1+1}-es korongot a 2-es rúdra'
|
|
tmp = self.result(állapot, Act(f1, '2'))
|
|
lepesek.append((text, tmp))
|
|
|
|
if f1 > -1 and (f1 < f3 or f3 == -1):
|
|
text = f'A {f1+1}-es korongot a 3-as rúdra'
|
|
tmp = self.result(állapot, Act(f1, '3'))
|
|
lepesek.append((text, tmp))
|
|
|
|
if f2 > -1 and (f2 < f3 or f3 == -1):
|
|
text = f'A {f2+1}-es korongot a 3-as rúdra'
|
|
tmp = self.result(állapot, Act(f2, '3'))
|
|
lepesek.append((text, tmp))
|
|
|
|
if f2 > -1 and (f2 < f1 or f1 == -1):
|
|
text = f'A {f2+1}-es korongot a 1-es rúdra'
|
|
tmp = self.result(állapot, Act(f2, '1'))
|
|
lepesek.append((text, tmp))
|
|
|
|
if f3 > -1 and (f3 < f2 or f2 == -1):
|
|
text = f'A {f3+1}-es korongot a 2-es rúdra'
|
|
tmp = self.result(állapot, Act(f3, '2'))
|
|
lepesek.append((text, tmp))
|
|
|
|
if f3 > -1 and (f3 < f1 or f1 == -1):
|
|
text = f'A {f3+1}-es korongot a 1-es rúdra'
|
|
tmp = self.result(állapot, Act(f3, '1'))
|
|
lepesek.append((text, tmp))
|
|
|
|
return lepesek
|
|
|
|
h = Hanoi(8)
|
|
#print(h.result('123', Act(0,'2')))
|
|
#print(h.rákövetkező('321'))
|
|
|
|
k = 0
|
|
while h.célteszt(h.kezdő)==False:
|
|
#print(h.rákövetkező(h.kezdő))
|
|
i=randint(0,len(h.rákövetkező(h.kezdő))-1)
|
|
lepes=h.rákövetkező(h.kezdő)[i][1]
|
|
h.mod(lepes)
|
|
#print(lepes)
|
|
k+=1
|
|
print(k) |