Created
December 28, 2015 12:24
-
-
Save wemrekurt/1e9a5ea7e09ff9a38203 to your computer and use it in GitHub Desktop.
Tower of Hanoi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Stack import * | |
class Hanoi: | |
def __init__(self,n): | |
self.move = n | |
self.tower = [0] | |
[self.tower.append(Stack()) for a in range(1,4)] # 3 Stack kule olustur | |
self.tofh() | |
def tofh(self): | |
#Kuleye diskleri yerlestir | |
for i in range(self.move,0,-1): | |
self.tower[1].push(i) | |
print 'K: Kaynak Kule, Y: Yardimci Kule, H: Hedef Kule' | |
self.display() | |
self.movement(self.move,1,2,3) | |
def movement(self,n,a,b,c): | |
if n > 0: | |
self.movement(n-1,a,c,b) | |
d = self.tower[a].pop() | |
self.tower[c].push(d) | |
self.display() | |
self.movement(n-1,b,a,c) | |
def display(self): | |
print ' K | Y | H ' | |
print '--------------' | |
for i in range(self.move-1,-1,-1): | |
try: | |
source = self.tower[1].show()[i] | |
except IndexError: | |
source = " " | |
try: | |
helper = self.tower[2].show()[i] | |
except IndexError: | |
helper = " " | |
try: | |
target = self.tower[3].show()[i] | |
except IndexError: | |
target = " " | |
print source,' | ', helper,' | ',target | |
print '\n' | |
tofhanoi = int(raw_input('Disk Sayisi Giriniz: ')) | |
test = Hanoi(tofhanoi) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Stack: | |
def __init__(self): | |
self.items = [] | |
def isEmpty(self): | |
return self.items == [] | |
def push(self, item): | |
self.items.append(item) | |
def pop(self): | |
return self.items.pop() | |
def peek(self): | |
return self.items[len(self.items)-1] | |
def size(self): | |
return len(self.items) | |
def show(self): | |
return self.items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment