class Hanoi(object): def __init__(self, n=3, start="A", workspace="B", destination="C"): self.startp = Pole(start, 0, 0) self.workspacep = Pole(workspace, 150, 0) self.destinationp = Pole(destination, 300, 0) self.startp.showpole() self.workspacep.showpole() self.destinationp.showpole() for i in range(n): self.startp.pushdisk( Disk("d" + str(i), 0, i * 150, 20, (n - i) * 30)) def move_disk(self, start, destination): disk = start.popdisk() destination.pushdisk(disk) def move_tower(self, n, s, d, w): if n == 1: self.move_disk(s, d) else: self.move_tower(n - 1, s, w, d) self.move_disk(s, d) self.move_tower(n - 1, w, d, s) def solve(self): self.move_tower(3, self.startp, self.destinationp, self.workspacep)
def __init__(self, x, y): self.x = x self.y = y self.Pola = [[0 for m in range(y)] for n in range(x)] for m in range(0, y): for n in range(0, x): self.Pola[n][m] = Pole.Pole(n, m, self)
def WykonajRuch(self): self.plansza.Pola[self.x][self.y] = Pole.Pole(self.x, self.y, self.plansza) nextX = self.x nextY = self.y if self.kierunek == 0: nextY = self.y - 1 if nextY < 0: nextY = self.plansza.y - 1 elif self.kierunek == 1: nextY = self.y + 1 if nextY > self.plansza.y - 1: nextY = 0 elif self.kierunek == 2: nextX = self.x - 1 if nextX < 0: nextX = self.plansza.x - 1 elif self.kierunek == 3: nextX = self.x + 1 if nextX > self.plansza.x - 1: nextX = 0 self.Kolizja(nextX, nextY)
def loadPoles( self, root ): poleDesc = root.find( 'Poles' ) for poleEntry in poleDesc: if( poleEntry.tag == 'PoleSingle' ): pole = modulPole.Pole( True ) pole.positionY = float( poleEntry.get( 'PositionY' ) ) else: pole = modulPole.Pole( False ) #pole.spacing = float( poleEntry.get( 'Spacing' ) ) pole.spacing = poleEntry.get( 'Spacing' ) if isinstance( pole.spacing, str ): pole.spacing = float( pole.spacing ) isStaggered = poleEntry.get( 'IsStaggered' ) if isStaggered == 'False': pole.IsStaggered = False else: pole.IsStaggered = True pole.side = poleEntry.get( 'Side' ) # pole.height = float( poleEntry.get( 'Height' ) ) pole.height = poleEntry.get( 'Height' ) if isinstance( pole.height, str ): pole.height = float( pole.height ) pole.lidc = poleEntry.get( 'LIDC' ) # pole.overhang = float( poleEntry.get( 'Overhang' ) ) pole.overhang = poleEntry.get( 'Overhang' ) if isinstance( pole.overhang, str ): pole.overhang = float( pole.overhang ) self.poles.append( pole ) print ' poles loaded ...'
def __init__(self, n=3, start="A", workspace="B", destination="C"): self.startp = Pole(start, 0, 0) self.workspacep = Pole(workspace, 150, 0) self.destinationp = Pole(destination, 300, 0) self.startp.showpole() self.workspacep.showpole() self.destinationp.showpole() for i in range(n): self.startp.pushdisk( Disk("d" + str(i), 0, i * 150, 20, (n - i) * 30))
def Kolizja(self, nextX, nextY): if len(self.ogonweza) != 0: self.plansza.Pola[self.ogonweza[-1].x][ self.ogonweza[-1].y] = Pole.Pole(self.ogonweza[-1].x, self.ogonweza[-1].y, self.plansza) if self.wydluz == True: self.wydluzWezaWNastTurze = True self.wydluz = False self.ogonweza.append( OgonWeza.OgonWeza(self.koniecX, self.koniecY, self.plansza)) for l in range(len(self.ogonweza) - 1, -1, -1): if l - 1 >= 0: self.ogonweza[l].x = self.ogonweza[l - 1].x self.ogonweza[l].y = self.ogonweza[l - 1].y else: self.ogonweza[l].x = self.x self.ogonweza[l].y = self.y self.plansza.Pola[self.ogonweza[l].x][ self.ogonweza[l].y] = self.ogonweza[l] if type(self.plansza.Pola[nextX][nextY]) == Pole.Pole: if self.dlugosc == 0: self.koniecX = nextX self.koniecY = nextY elif self.dlugosc == 1: self.koniecX = self.x self.koniecY = self.y else: self.koniecX = self.ogonweza[-1].x self.koniecY = self.ogonweza[-1].y self.x = nextX self.y = nextY self.plansza.Pola[nextX][nextY] = self elif type(self.plansza.Pola[nextX][nextY]) == Smakolyk.Smakolyk: if self.dlugosc == 0: self.koniecX = nextX self.koniecY = nextY elif self.dlugosc == 1: self.koniecX = self.x self.koniecY = self.y else: self.koniecX = self.ogonweza[-2].x self.koniecY = self.ogonweza[-2].y self.wydluz = True if self.gra.predkosc != 0.98: self.gra.predkosc += 0.002 self.dlugosc += 1 self.gra.score += 10 self.x = nextX self.y = nextY self.plansza.Pola[nextX][nextY] = self self.gra.smakolyk = Smakolyk.Smakolyk(self.plansza.x, self.plansza.y, self.plansza) elif type(self.plansza.Pola[nextX][nextY]) == OgonWeza.OgonWeza: self.gra.GrajDalej = False