def __init__(self, tower_len, dna_len, initialize=False): self._dna = [] self._tower = Hanoi(tower_len) if dna_len < 2**tower_len - 1: raise Exception("dna_len must be greater or equal than " + str(2**tower_len - 1)) self._dna_len = dna_len if initialize: self._initialize()
def main(): """ function based disks = 3 solve (disks, "A", "B", "C") """ # class based h = Hanoi(3) h.solve("a", "b", "c")
def _init_hanoi(self): # Variables for the animation self._next_move = True self._move = 0 self._starting_left = 0 self._starting_top = 0 self._target_left = 0 self._target_top = 0 self._acceleration = 0 self._time_spent = 0 # Flags indicating the state of the program self._running_flag = False self._step_by_step_flag = False self._over_flag = False # Reset the trigger_button to 'Start' self._trigger_button.text = 'Start' # Hanoi algorithm object self._hanoi = Hanoi(input('Please enter the number of plates\n')) # Initialization of the plate images for each_plate in self._hanoi.get_plates(0): plate_image = TextButton(self) plate_image.rect = (70 + self._hanoi.get_total() * PLATE_BASE_WIDTH / 2) - each_plate.number * PLATE_BASE_WIDTH / 2, \ (WINDOW_HEIGHT - self._hanoi.get_total() * PLATE_HEIGHT) + each_plate.number * PLATE_HEIGHT, \ PLATE_BASE_WIDTH * (each_plate.number + 1), \ PLATE_HEIGHT plate_image.background = r'Images\Root_button.png' self._plate_images.insert(0, plate_image) # Start running the hanoi algorithm and animation self._steps = self._hanoi.move(self._hanoi.get_total(), 0, 2) self._animation = koan.anim.IntervalExecute(TIME_INTERVAL, self._onTimer)
class Individual: def __init__(self, tower_len, dna_len, initialize=False): self._dna = [] self._tower = Hanoi(tower_len) if dna_len < 2**tower_len - 1: raise Exception("dna_len must be greater or equal than " + str(2**tower_len - 1)) self._dna_len = dna_len if initialize: self._initialize() def _initialize(self): for i in range(self._dna_len): selN = randint(0, 2) toN = randint(0, 2) while selN == toN: toN = randint(0, 2) self._dna.append({"sel": selN, "to": toN}) def set_movements(self): self._tower.set_movements(self._dna) def get_fitness(self): tower = self._tower.tower[2] fitness_cont = 0 percent = 100 / len(tower) towerCont = 2 * len(self._tower.tower[2]) - 1 for i in range(len(self._tower.tower[2]) - 1, -1, -1): if self._tower.tower[2][i] == towerCont: fitness_cont += 1 towerCont -= 2 else: break return percent * fitness_cont
def main(): print "Welcome to the card game Hanoi.\nThe object of this game is to move all the cards into a single stack with" \ " 9 being the top card and 1 being the bottom card.\nThe rules are simple: You can only move one card at a" \ " time. You can only move cards from the bottom of each stack. You cannot move a higher value card onto a" \ " lower value card.\nYou can quit the game anytime by entering the word 'quit'.\n" # instructions on how to play play = raw_input("Would you like to play Hanoi (yes or no)? ") win = False playAgain = 'yes' while playAgain == 'yes': if play == "no": print("Goodbye!") sys.exit(0) else: game = Hanoi() game.create_dealingPile() game.deal() while win == False: print "" user_card = raw_input("Which card would you like to move? ") game.checkUserCard(user_card) user_stack = raw_input("Which stack would you like to move " + user_card + " to (1, 2, or 3)? ") game.checkUserStack(user_stack, user_card) game.moveCards(user_card, user_stack) game.redeal() check = game.checkWin() if check: win = True again = raw_input("Would you like to play again (yes or no)? ") if again == "no": print "I hope you enjoyed playing Hanoi." sys.exit(0) elif again == "yes": win = False game.playAgain(again)
from RobotVision import SearchTool from Hanoi import Hanoi import time rospy.init_node("vision") print("Getting robot state... ") rs = baxter_interface.RobotEnable(CHECK_VERSION) init_state = rs.state().enabled rightArm = baxter_interface.Limb("right") leftArm = baxter_interface.Limb("left") leftGripper = baxter_interface.Gripper('left', CHECK_VERSION) rightGripper = baxter_interface.Gripper('right', CHECK_VERSION) camera = CameraSubscriber() hanoi = Hanoi(3) searchTool = SearchTool("camera_img.png") def goToOrigin(): rightJoints = { 'right_s0': 0.0, 'right_s1': .3, 'right_e0': 0, 'right_e1': 1, 'right_w0': 0, 'right_w1': 0.2, 'right_w2': 0 } leftJoints = {
from Hanoi import Hanoi, Stack, turtle def moveTower(height, fromPole, toPole, auxPole): """Solve Tower of Hanoi problem depending on height.""" if height == 1: moveDisk(fromPole, toPole) h.moveDisks(fromPole, toPole) else: moveTower(height - 1, fromPole, auxPole, toPole) moveDisk(fromPole, toPole) h.moveDisks(fromPole, toPole) moveTower(height - 1, auxPole, toPole, fromPole) def moveDisk(fromPole, toPole): """Print the moves made.""" print(f"Move from {fromPole} to {toPole}") h = Hanoi(4) moveTower(4, "A", "C", "B") h.wn.exitonclick()
class HanoiWindow(Window): def __init__(self): Window.__init__(self) self.create(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, caption = True) self._plate_images = [] # Button for 'Start' and 'Pause' self._trigger_button = TextButton(self) self._trigger_button.rect = 0, 0, 50, 30 self._trigger_button.text = 'Start' self._trigger_button.background = r'Images\Root_button.png' self.autoRemove(self._trigger_button.bind('Click', self._on_trigger_button_click)) # Button for 'Restart' self._restart_button = TextButton(self) self._restart_button.rect = 60, 0, 50, 30 self._restart_button.text = 'Restart' self._restart_button.background = r'Images\Root_button.png' self.autoRemove(self._restart_button.bind('Click', self._on_restart_button_click)) # Button for 'Next' self._next_button = TextButton(self) self._next_button.rect = 120, 0, 50, 30 self._next_button.text = 'Next' self._next_button.background = r'Images\Root_button.png' self.autoRemove(self._next_button.bind('Click', self._on_next_button_click)) self._init_hanoi() def _init_hanoi(self): # Variables for the animation self._next_move = True self._move = 0 self._starting_left = 0 self._starting_top = 0 self._target_left = 0 self._target_top = 0 self._acceleration = 0 self._time_spent = 0 # Flags indicating the state of the program self._running_flag = False self._step_by_step_flag = False self._over_flag = False # Reset the trigger_button to 'Start' self._trigger_button.text = 'Start' # Hanoi algorithm object self._hanoi = Hanoi(input('Please enter the number of plates\n')) # Initialization of the plate images for each_plate in self._hanoi.get_plates(0): plate_image = TextButton(self) plate_image.rect = (70 + self._hanoi.get_total() * PLATE_BASE_WIDTH / 2) - each_plate.number * PLATE_BASE_WIDTH / 2, \ (WINDOW_HEIGHT - self._hanoi.get_total() * PLATE_HEIGHT) + each_plate.number * PLATE_HEIGHT, \ PLATE_BASE_WIDTH * (each_plate.number + 1), \ PLATE_HEIGHT plate_image.background = r'Images\Root_button.png' self._plate_images.insert(0, plate_image) # Start running the hanoi algorithm and animation self._steps = self._hanoi.move(self._hanoi.get_total(), 0, 2) self._animation = koan.anim.IntervalExecute(TIME_INTERVAL, self._onTimer) def _on_trigger_button_click(self): if self._over_flag: return if self._trigger_button.text == 'Start': self._running_flag = True self._trigger_button.text = 'Pause' else: self._running_flag = False self._trigger_button.text = 'Start' def _on_restart_button_click(self): if self._running_flag: return # Remove the registered animation and images self._animation.remove() for each_plate_image in self._plate_images: each_plate_image.close() # Re-initialization of the program self._init_hanoi() def _on_next_button_click(self): if not self._running_flag: self._step_by_step_flag = True def _onTimer(self): # No update of the animation when it's in step by step state if not self._running_flag and not self._step_by_step_flag: return try: if self._next_move: self._next_move = False # Generating the next move self._move = self._steps.next() # Calculating the __starting point and __target point of the animation self._time_spent = 0 self._starting_left = self._plate_images[self._move.number].left self._starting_top = self._plate_images[self._move.number].top self._target_left = (70 + self._hanoi.get_total() * PLATE_BASE_WIDTH / 2) + self._move.to_location * (WINDOW_WIDTH / 3) - self._move.number * (PLATE_BASE_WIDTH / 2) self._target_top = WINDOW_HEIGHT - self._hanoi.get_total_in_location(self._move.to_location) * PLATE_HEIGHT # Calculating the total time and __acceleration needed from start to the __target total_time = abs((self._target_left - self._starting_left) / HORIZONTAL_SPEED) self._acceleration = 2 * (self._target_top - self._starting_top - VERTICAL_SPEED * total_time) / (total_time ** 2) # Indicating there's no move left except StopIteration: print '[HanoiWindow::_onTimer] Over!!!' self._running_flag = False self._over_flag = True return # The plate's moved to the __target point if self._plate_images[self._move.number].left == self._target_left: self._plate_images[self._move.number].top = self._target_top self._next_move = True if self._step_by_step_flag: self._step_by_step_flag = False return # Update the new location of the plate if self._target_left > self._plate_images[self._move.number].left: self._plate_images[self._move.number].left += HORIZONTAL_SPEED elif self._target_left < self._plate_images[self._move.number].left: self._plate_images[self._move.number].left -= HORIZONTAL_SPEED self._time_spent += 1 self._plate_images[self._move.number].top = self._starting_top + VERTICAL_SPEED * self._time_spent + self._acceleration * (self._time_spent ** 2) / 2 def close(self): # Remove the registered animation self._animation.remove() super(HanoiWindow, self).close() def onDraw(self, render): render.Clear(255, 0, 0, 0)
class HanoiWindow(Window): def __init__(self): Window.__init__(self) self.create(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, caption=True) self._plate_images = [] # Button for 'Start' and 'Pause' self._trigger_button = TextButton(self) self._trigger_button.rect = 0, 0, 50, 30 self._trigger_button.text = 'Start' self._trigger_button.background = r'Images\Root_button.png' self.autoRemove( self._trigger_button.bind('Click', self._on_trigger_button_click)) # Button for 'Restart' self._restart_button = TextButton(self) self._restart_button.rect = 60, 0, 50, 30 self._restart_button.text = 'Restart' self._restart_button.background = r'Images\Root_button.png' self.autoRemove( self._restart_button.bind('Click', self._on_restart_button_click)) # Button for 'Next' self._next_button = TextButton(self) self._next_button.rect = 120, 0, 50, 30 self._next_button.text = 'Next' self._next_button.background = r'Images\Root_button.png' self.autoRemove( self._next_button.bind('Click', self._on_next_button_click)) self._init_hanoi() def _init_hanoi(self): # Variables for the animation self._next_move = True self._move = 0 self._starting_left = 0 self._starting_top = 0 self._target_left = 0 self._target_top = 0 self._acceleration = 0 self._time_spent = 0 # Flags indicating the state of the program self._running_flag = False self._step_by_step_flag = False self._over_flag = False # Reset the trigger_button to 'Start' self._trigger_button.text = 'Start' # Hanoi algorithm object self._hanoi = Hanoi(input('Please enter the number of plates\n')) # Initialization of the plate images for each_plate in self._hanoi.get_plates(0): plate_image = TextButton(self) plate_image.rect = (70 + self._hanoi.get_total() * PLATE_BASE_WIDTH / 2) - each_plate.number * PLATE_BASE_WIDTH / 2, \ (WINDOW_HEIGHT - self._hanoi.get_total() * PLATE_HEIGHT) + each_plate.number * PLATE_HEIGHT, \ PLATE_BASE_WIDTH * (each_plate.number + 1), \ PLATE_HEIGHT plate_image.background = r'Images\Root_button.png' self._plate_images.insert(0, plate_image) # Start running the hanoi algorithm and animation self._steps = self._hanoi.move(self._hanoi.get_total(), 0, 2) self._animation = koan.anim.IntervalExecute(TIME_INTERVAL, self._onTimer) def _on_trigger_button_click(self): if self._over_flag: return if self._trigger_button.text == 'Start': self._running_flag = True self._trigger_button.text = 'Pause' else: self._running_flag = False self._trigger_button.text = 'Start' def _on_restart_button_click(self): if self._running_flag: return # Remove the registered animation and images self._animation.remove() for each_plate_image in self._plate_images: each_plate_image.close() # Re-initialization of the program self._init_hanoi() def _on_next_button_click(self): if not self._running_flag: self._step_by_step_flag = True def _onTimer(self): # No update of the animation when it's in step by step state if not self._running_flag and not self._step_by_step_flag: return try: if self._next_move: self._next_move = False # Generating the next move self._move = self._steps.next() # Calculating the __starting point and __target point of the animation self._time_spent = 0 self._starting_left = self._plate_images[ self._move.number].left self._starting_top = self._plate_images[self._move.number].top self._target_left = ( 70 + self._hanoi.get_total() * PLATE_BASE_WIDTH / 2) + self._move.to_location * (WINDOW_WIDTH / 3) - self._move.number * ( PLATE_BASE_WIDTH / 2) self._target_top = WINDOW_HEIGHT - self._hanoi.get_total_in_location( self._move.to_location) * PLATE_HEIGHT # Calculating the total time and __acceleration needed from start to the __target total_time = abs((self._target_left - self._starting_left) / HORIZONTAL_SPEED) self._acceleration = 2 * ( self._target_top - self._starting_top - VERTICAL_SPEED * total_time) / (total_time**2) # Indicating there's no move left except StopIteration: print '[HanoiWindow::_onTimer] Over!!!' self._running_flag = False self._over_flag = True return # The plate's moved to the __target point if self._plate_images[self._move.number].left == self._target_left: self._plate_images[self._move.number].top = self._target_top self._next_move = True if self._step_by_step_flag: self._step_by_step_flag = False return # Update the new location of the plate if self._target_left > self._plate_images[self._move.number].left: self._plate_images[self._move.number].left += HORIZONTAL_SPEED elif self._target_left < self._plate_images[self._move.number].left: self._plate_images[self._move.number].left -= HORIZONTAL_SPEED self._time_spent += 1 self._plate_images[ self._move. number].top = self._starting_top + VERTICAL_SPEED * self._time_spent + self._acceleration * ( self._time_spent**2) / 2 def close(self): # Remove the registered animation self._animation.remove() super(HanoiWindow, self).close() def onDraw(self, render): render.Clear(255, 0, 0, 0)