def tour_of_three_stools(n: int, stools: DomainStools, input: int, aux: int, output: int) -> None: if n == 1: stools.move(stools.select_top_cheese(input), stools.select_top_cheese(output)) else: tour_of_three_stools(n-1, stools, input, output, aux) tour_of_three_stools(1, stools, input, aux, output) tour_of_three_stools(n-1, stools, aux, input, output)
def tour_helper(n: int, stools: DomainStools, input: int, aux1: int, aux2: int, output: int) -> None: if n == 1: stools.move(stools.select_top_cheese(input), stools.select_top_cheese(output)) else: i = math.ceil(n/2) tour_helper(n-i, stools, input, aux2, output, aux1) tour_of_three_stools(i, stools, input, aux2, output) tour_helper(n-i, stools, aux1, input, aux2, output)
def tour_of_three_stools(self: 'SolvingController', n: int, stools: DomainStools, input: int, aux: int, output: int) -> None: if n == 1: self.select(stools.select_top_cheese(input)) #time.sleep(self.time_delay) self.select(stools.select_top_cheese(output)) else: self.tour_of_three_stools(n-1, stools, input, output, aux) self.tour_of_three_stools(1, stools, input, aux, output) self.tour_of_three_stools(n-1, stools, aux, input, output) return None
def solve(self: 'SolvingController', n: int, stools: DomainStools, input: int, aux1: int, aux2: int, output: int) -> None: if n == 1: self.select(stools.select_top_cheese(input)) time.sleep(self.time_delay) self.select(stools.select_top_cheese(output)) else: i = math.ceil(n/2) self.solve(n-i, stools, input, aux2, output, aux1) self.tour_of_three_stools(i, stools, input, aux2, output) self.solve(n-i, stools, aux1, input, aux2, output) return None
def __init__(self: 'SolvingController', number_of_cheeses: int, content_width: float, content_height: float, cheese_scale: float, time_delay: float): """ Initialize a new SolvingController. number_of_cheeses - number of cheese to tower on the first stool, not counting the bottom cheese acting as stool content_width - width in pixels of the working area content_height - height in pixels of the working area cheese_scale - height in pixels for showing cheese thicknesses, and to scale cheese diameters time_delay - number of seconds to pause b/w highlighting a cheese to move and moving it """ self.domain = DomainStools(4) self.cheese_to_move = None self.blinking = False self.cheese_scale = cheese_scale self.time_delay = time_delay self.number_of_cheeses = number_of_cheeses self.root = TI.Tk() #creating variable canvas canvas = TI.Canvas(self.root, background="blue", width=content_width, height=content_height) canvas.pack(expand=True, fill=TI.BOTH) self.moves_label = TI.Label(self.root) self.show_number_of_moves() self.moves_label.pack() for stool in range(self.domain.number_of_stools()): total_size = 0 for size in range(1 + (number_of_cheeses if stool == 0 else 0)): cheese = CheeseView(self.cheese_scale * (number_of_cheeses + 1 - size), lambda cheese: self.clicked(cheese), canvas, self.cheese_scale, content_width * (stool + 1) / (self.domain.number_of_stools() + 1.0), content_height - cheese_scale / 2 - total_size) self.domain.add(stool, cheese) total_size += self.cheese_scale #self.solve(number_of_cheeses, self.domain, 0, 1, 2, 3) self.root.after(0, self.animate()) self.root.mainloop()
def __init__(self: 'ManualController', number_of_cheeses: int, number_of_stools: int, content_width: float, content_height: float, cheese_scale: float): """ Initialize a new ManualController. number_of_cheeses - number of cheese to tower on the first stool, not counting the bottom cheese acting as stool number_of_stools - number of stools, to be shown as large cheeses content_width - width in pixels of the working area content_height - height in pixels of the working area cheese_scale - height in pixels for showing cheese thicknesses, and to scale cheese diameters """ self.domain = DomainStools(number_of_stools) self.cheese_to_move = None self.blinking = False self.cheese_scale = cheese_scale self.root = TI.Tk() #creating variable canvas canvas = TI.Canvas(self.root, background="blue", width=content_width, height=content_height) canvas.pack(expand=True, fill=TI.BOTH) self.moves_label = TI.Label(self.root) self.show_number_of_moves() self.moves_label.pack() for stool in range(self.domain.number_of_stools()): total_size = 0 for size in range(1 + (number_of_cheeses if stool == 0 else 0)): cheese = CheeseView(self.cheese_scale * (number_of_cheeses + 1 - size), lambda cheese: self.clicked(cheese), canvas, self.cheese_scale, content_width * (stool + 1) / (self.domain.number_of_stools() + 1.0), content_height - cheese_scale / 2 - total_size) self.domain.add(stool, cheese) total_size += self.cheese_scale
class SolvingController: def __init__(self: 'SolvingController', number_of_cheeses: int, content_width: float, content_height: float, cheese_scale: float, time_delay: float): """ Initialize a new SolvingController. number_of_cheeses - number of cheese to tower on the first stool, not counting the bottom cheese acting as stool content_width - width in pixels of the working area content_height - height in pixels of the working area cheese_scale - height in pixels for showing cheese thicknesses, and to scale cheese diameters time_delay - number of seconds to pause b/w highlighting a cheese to move and moving it """ self.domain = DomainStools(4) self.cheese_to_move = None self.blinking = False self.cheese_scale = cheese_scale self.time_delay = time_delay self.number_of_cheeses = number_of_cheeses self.root = TI.Tk() #creating variable canvas canvas = TI.Canvas(self.root, background="blue", width=content_width, height=content_height) canvas.pack(expand=True, fill=TI.BOTH) self.moves_label = TI.Label(self.root) self.show_number_of_moves() self.moves_label.pack() for stool in range(self.domain.number_of_stools()): total_size = 0 for size in range(1 + (number_of_cheeses if stool == 0 else 0)): cheese = CheeseView(self.cheese_scale * (number_of_cheeses + 1 - size), lambda cheese: self.clicked(cheese), canvas, self.cheese_scale, content_width * (stool + 1) / (self.domain.number_of_stools() + 1.0), content_height - cheese_scale / 2 - total_size) self.domain.add(stool, cheese) total_size += self.cheese_scale #self.solve(number_of_cheeses, self.domain, 0, 1, 2, 3) self.root.after(0, self.animate()) self.root.mainloop() def show_number_of_moves(self: 'SolvingController'): """Show the number of moves so far.""" self.moves_label.config(text='Number of moves: ' + str(self.domain.number_of_moves())) def clicked(self: 'SolvingController', cheese: CheeseView): """React to cheese being clicked: if not in the middle of blinking then select cheese for moving, or for moving onto. cheese - clicked cheese """ pass def select(self: 'SolvingController', cheese: CheeseView): """If no cheese is selected to move, select cheese and highlight it. Otherwise try moving the currently selected cheese onto cheese: if that's not a valid move, blink the currently selected cheese. cheese - cheese to select for moving, or to try moving onto. """ if self.cheese_to_move is None: self.cheese_to_move = cheese self.cheese_to_move.highlight(True) else: if cheese is not self.cheese_to_move: try: self.domain.move(self.cheese_to_move, cheese) self.cheese_to_move.place(cheese.x_center, cheese.y_center - self.cheese_scale) self.show_number_of_moves() except: self.blinking = True for i in range(10): self.cheese_to_move.highlight(i % 2 != 0) self.root.update() time.sleep(0.1) self.blinking = False self.cheese_to_move.highlight(False) self.cheese_to_move = None def solve(self: 'SolvingController', n: int, stools: DomainStools, input: int, aux1: int, aux2: int, output: int) -> None: if n == 1: self.select(stools.select_top_cheese(input)) time.sleep(self.time_delay) self.select(stools.select_top_cheese(output)) else: i = math.ceil(n/2) self.solve(n-i, stools, input, aux2, output, aux1) self.tour_of_three_stools(i, stools, input, aux2, output) self.solve(n-i, stools, aux1, input, aux2, output) return None def tour_of_three_stools(self: 'SolvingController', n: int, stools: DomainStools, input: int, aux: int, output: int) -> None: if n == 1: self.select(stools.select_top_cheese(input)) #time.sleep(self.time_delay) self.select(stools.select_top_cheese(output)) else: self.tour_of_three_stools(n-1, stools, input, output, aux) self.tour_of_three_stools(1, stools, input, aux, output) self.tour_of_three_stools(n-1, stools, aux, input, output) return None def animate(self: 'SolvingController', event=None): #self.solve(self.number_of_cheeses, self.domain, 0, 1, 2, 3) self.select(self.domain.select_top_cheese(0)) self.select(self.domain.select_top_cheese(1))
class ManualController: def __init__(self: 'ManualController', number_of_cheeses: int, number_of_stools: int, content_width: float, content_height: float, cheese_scale: float): """ Initialize a new ManualController. number_of_cheeses - number of cheese to tower on the first stool, not counting the bottom cheese acting as stool number_of_stools - number of stools, to be shown as large cheeses content_width - width in pixels of the working area content_height - height in pixels of the working area cheese_scale - height in pixels for showing cheese thicknesses, and to scale cheese diameters """ self.domain = DomainStools(number_of_stools) self.cheese_to_move = None self.blinking = False self.cheese_scale = cheese_scale self.root = TI.Tk() canvas = TI.Canvas(self.root, background="blue", width=content_width, height=content_height) canvas.pack(expand=True, fill=TI.BOTH) self.moves_label = TI.Label(self.root) self.show_number_of_moves() self.moves_label.pack() for stool in range(self.domain.number_of_stools): total_size = 0 for size in range(1 + (number_of_cheeses if stool == 0 else 0)): cheese = CheeseView(self.cheese_scale * (number_of_cheeses + 1 - size), lambda cheese: self.clicked(cheese), canvas, self.cheese_scale, content_width * (stool + 1) / (self.domain.number_of_stools + 1.0), content_height - cheese_scale / 2 - total_size) self.domain.add(stool, cheese) total_size += self.cheese_scale def show_number_of_moves(self: 'ManualController'): """Show the number of moves so far.""" self.moves_label.config(text='Number of moves: ' + str(self.domain.number_of_moves)) def clicked(self: 'ManualController', cheese: CheeseView): """React to cheese being clicked: if not in the middle of blinking then select cheese for moving, or for moving onto. cheese - clicked cheese """ if not self.blinking: self.select(cheese) def select(self: 'ManualController', cheese: CheeseView): """If no cheese is selected to move, select cheese and highlight it. Otherwise try moving the currently selected cheese onto cheese: if that's not a valid move, blink the currently selected cheese. cheese - cheese to select for moving, or to try moving onto. """ if self.cheese_to_move is None: self.cheese_to_move = cheese self.cheese_to_move.highlight(True) else: if cheese is not self.cheese_to_move: try: self.domain.move(self.cheese_to_move, cheese) self.cheese_to_move.place(cheese.x_center, cheese.y_center - self.cheese_scale) self.show_number_of_moves() except: self.blinking = True for i in range(10): self.cheese_to_move.highlight(i % 2 != 0) self.root.update() time.sleep(0.1) self.blinking = False self.cheese_to_move.highlight(False) self.cheese_to_move = None
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this file. If not, see <http://www.gnu.org/licenses/>. #Edited by: Matthew Kim from DomainStools import DomainStools from DomainStools import Cheese def tour_of_four_stools(n: int, stools: DomainStools) -> None: """Move an n cheese tower from the first stool in stools to the fourth. n - number of cheeses on the first stool of stools stools - a DomainStools with a tower of cheese on the first stool and three other empty stools """ if __name__ == '__main__': four_stools = DomainStools(4) for s in range(5, 0, -1): four_stools.add(0, Cheese(s)) for s in range(1,3): four_stools.add(s, Cheese(5)) tour_of_four_stools(5, four_stools) print(four_stools.number_of_moves)
if n == 1: stools.move(stools.select_top_cheese(input), stools.select_top_cheese(output)) else: i = math.ceil(n/2) tour_helper(n-i, stools, input, aux2, output, aux1) tour_of_three_stools(i, stools, input, aux2, output) tour_helper(n-i, stools, aux1, input, aux2, output) def tour_of_three_stools(n: int, stools: DomainStools, input: int, aux: int, output: int) -> None: if n == 1: stools.move(stools.select_top_cheese(input), stools.select_top_cheese(output)) else: tour_of_three_stools(n-1, stools, input, output, aux) tour_of_three_stools(1, stools, input, aux, output) tour_of_three_stools(n-1, stools, aux, input, output) if __name__ == '__main__': four_stools = DomainStools(4) for s in range(7, 0, -1): four_stools.add(0, Cheese(s)) tour_of_four_stools(7, four_stools) print(four_stools.number_of_moves()) #three_stools = DomainStools(3) #for s in range(15, 0, -1): # three_stools.add(0, Cheese(s)) #tour_of_three_stools(15, three_stools, 0, 1, 2) #print(three_stools.number_of_moves())