def __init__(self: 'GUIController', number_of_cheeses: int,
                 number_of_stools: int, content_width: float,
                 content_height: float, cheese_scale: float):
        """
        Initialize a new GUIView.

        number_of_cheeses - number of cheese to tower on the first stool
        number_of_stools - number of stools
        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._model = TOAHModel(number_of_stools)
        self._stools = []
        self._cheese_to_move = None
        self._blinking = False
        self._number_of_stools = number_of_stools
        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()

        # the dimensions of a stool are the same as a cheese that's
        # one size bigger than the biggest of the number_of_cheeses cheeses.
        for stool_ind in range(number_of_stools):
            width = self.cheese_scale * (number_of_cheeses + 1)
            x_cent = content_width * (stool_ind + 1) / (number_of_stools + 1.0)
            y_cent = content_height - cheese_scale / 2
            stool = StoolView(width, lambda s: self.stoolClicked(s), canvas,
                              self.cheese_scale, x_cent, y_cent)
            self._stools.append(stool)

        # Can't use self._model.fill_first_stool because we need to
        # use CheeseView objects instead of just Cheese objects.
        total_size = self.cheese_scale
        for sizeparam in range(1, number_of_cheeses + 1):
            size = (number_of_cheeses + 1 - sizeparam)
            width = self.cheese_scale * size
            x_cent = content_width / (number_of_stools + 1.0)
            y_cent = content_height - cheese_scale / 2 - total_size
            cheese = CheeseView(size, width, lambda c: self.cheeseClicked(c),
                                canvas, self.cheese_scale, x_cent, y_cent)
            self._model.add(0, cheese)
            total_size += self.cheese_scale
    def __init__(self: 'ConsoleController', number_of_cheeses: int,
                 number_of_stools: int):
        """
        Initialize a new 'ConsoleController'.

        number_of_cheeses - number of cheese to tower on the first stool
        number_of_stools - number of stools
        """
        self.model = TOAHModel(number_of_stools)
        self.model.fill_first_stool(number_of_cheeses)
        self.has_seen_instructions = False
示例#3
0
    def __init__(self: 'ConsoleController', number_of_cheeses: int,
                 number_of_stools: int):
        """
        Initialize a new 'ConsoleController'.

        number_of_cheeses - number of cheese to tower on the first stool                            
        number_of_stools - number of stools
        """
        self.number_of_stools = TOAHModel(number_of_stools)
        self.number_of_cheeses = \
            self.number_of_stools.fill_first_stool(number_of_cheeses)
示例#4
0
    def __init__(self: 'ConsoleController', number_of_cheeses: int,
                 number_of_stools: int) -> None:
        """
        Initialize a new 'ConsoleController'.
        """

        TOAHModel.__init__(self, number_of_stools)
        TOAHModel.fill_first_stool(self, number_of_cheeses)
        self.cheese_model = TOAHModel(number_of_stools)
        self.number_of_stools = number_of_stools
        self.cheese_model.fill_first_stool(number_of_cheeses)
示例#5
0
def tour_of_four_stools(model: 'TOAHModel',
                        delay_btw_moves: float = 0.5,
                        console_animate: bool = False):
    """Move a tower of cheeses from the first stool in model to the fourth.

       model - a TOAHModel with a tower of cheese on the first stool
                and three other empty stools
       console_animate - whether to animate the tour in the console
       delay_btw_moves - time delay between moves in seconds IF 
                         console_animate == True
                         no effect if console_animate == False
    
    >>> m = TOAHModel(4)
    >>> m.fill_first_stool(8)
    >>> tour_of_four_stools(m)
    >>> m.get_move_seq().length()
    33
    """

    # Use helper function to solve the problem
    four_stool_move(
        model,
        model.number_of_cheeses(),
        SRC_INDEX,  # src_stool
        DEST_INDEX,  # dest_stool
        (TEMP0_INDEX, TEMP1_INDEX))  # temp_stools

    # If animation is requested
    if (console_animate):
        # Get the MoveSequence from model
        move_seq = model.get_move_seq()

        # Create and set up a new 4-stool TOAHModel to animate
        anim_model = TOAHModel(4)
        anim_model.fill_first_stool(model.number_of_cheeses())
        # Print the initial state of anim_model
        print(anim_model)

        # Go through each move in move_seq
        for i in range(move_seq.length()):
            # Get the source and destination stool indices
            (src_stool, dest_stool) = move_seq.get_move(i)

            # Delay the next move
            time.sleep(delay_btw_moves)

            # Apply the move
            anim_model.move(src_stool, dest_stool)

            # Print the result
            print(anim_model)

    return
示例#6
0
    def __init__(self: 'ConsoleController',
                 number_of_cheeses: int, number_of_stools: int):
        """
        Initialize a new 'ConsoleController'.

        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
        """
        # REPRESENTATION INVARIANT
        # self._number_of_cheeses is an integer which represents the number
        # of cheeses in the game
        # self._number_of_stools is an integer which represents the number
        # of stools in the game
        # self._model is a TOAHModel, which represents the game as a whole
        self._number_of_cheeses = number_of_cheeses
        self._number_of_stools = number_of_stools
        self._model = TOAHModel(self._number_of_stools)
        # Fill the first stool with the amount of cheese entered
        self._model.fill_first_stool(self._number_of_cheeses)
    def __init__(self: 'ConsoleController', number_of_cheeses: int,
                 number_of_stools: int):
        """
        Initialize a new 'ConsoleController'.

        number_of_cheeses - number of cheese to tower on the first stool
        number_of_stools - number of stools
        """
        # Initialize dict for converting the user command strings
        # to their methods
        self._cmd_to_method = {
            "MOVE": self.move,
            "HELP": self.help,
            "QUIT": self.quit,
            "EXIT": self.quit
        }

        # Create a TOAHModel with the specified number of stools
        self._model = TOAHModel(number_of_stools)

        # Put the TOAHModel in the starting config with the number of cheeses
        self._model.fill_first_stool(number_of_cheeses)

        return
示例#8
0
    moves = MoveSequence([])
    four_stool_solution(moves, cheese_num, 0, 1, 2, 3)

    #Then we apply it to our existing model, animating with delay if
    #requested.
    if console_animate:
        for move in moves._moves:
            print(model)
            model.move(move[0], move[1])
            time.sleep(delay_btw_moves)
        print(model)
    else:
        for move in moves._moves:
            model.move(move[0], move[1])


if __name__ == '__main__':
    NUM_CHEESES = 25
    DELAY_BETWEEN_MOVES = .1
    CONSOLE_ANIMATE = True

    # DO NOT MODIFY THE CODE BELOW.
    four_stools = TOAHModel(4)
    four_stools.fill_first_stool(number_of_cheeses=NUM_CHEESES)

    tour_of_four_stools(four_stools,
                        console_animate=CONSOLE_ANIMATE,
                        delay_btw_moves=DELAY_BETWEEN_MOVES)

    print(four_stools.number_of_moves())
示例#9
0
def tour_of_four_stools(model: TOAHModel,
                        delay_btw_moves: float = 0.5,
                        console_animate: bool = False) -> None:
    """Move a tower of cheeses from the first stool in model to the fourth.

       model - a TOAHModel with a tower of cheese on the first stool
                and three other empty stools
       console_animate - whether to animate the tour in the console
       delay_btw_moves - time delay between moves in seconds IF 
                         console_animate == True
                         no effect if console_animate == False
    """
    def move_three_stools(n: int, source: int, intermediate: int,
                          destination: int) -> None:
        """
        Print moves to get n cheeses from source to destination, possibly
        using intermediate
        """
        if n > 1:
            move_three_stools(n - 1, source, destination, intermediate)
            move_three_stools(1, source, intermediate, destination)
            move_three_stools(n - 1, intermediate, source, destination)
        else:
            model.move(source, destination)

    def move_four_stools(model: TOAHModel, n: int, source: int,
                         intermediate1: int, intermediate2: int,
                         destination: int) -> None:
        """
        Print moves to get n cheeses from source to destination, possibly
        using intermediates
        """
        def M(n) -> int:
            """
            return the minimum moves of moving n Cheeses on four stools
            >>> M(4)
            9
            """
            if n == 1:
                return 1
            else:
                moves = []
                for i in range(1, n):
                    moves.append(2 * M(n - i) + 2**i - 1)
                global mo
                mo = moves.copy()

            return min(moves)

        def find_i(n) -> int:
            """
            return the index of a in the global variable mo
            >>> find_i(9)
            1
            """
            return mo.index(n)

        def find(n):
            """
            return the index of M(n) in the global variable mo + 1
            since the index has a range from 1 to n(exclusive)
            >>>find(9)
            3
            """
            return find_i(M(n)) + 1

        if n > 1:
            move_four_stools(model, n - find(n), source, intermediate2,
                             destination, intermediate1)
            move_three_stools(find(n), source, intermediate2, destination)
            move_four_stools(model, n - find(n), intermediate1, source,
                             intermediate2, destination)
        else:
            model.move(source, destination)

    move_four_stools(model, NUM_CHEESES, 0, 1, 2, 3)
    model2 = TOAHModel(4)
    model2.fill_first_stool(NUM_CHEESES)
    print(model2)
    for moves in model.get_move_seq()._moves:
        if console_animate:
            time.sleep(delay_btw_moves)
            model2.move(moves[0], moves[1])
            print(model2)
    if number_of_cheeses == 'quit':
        print('Good Game!')

    else:
        # check if it's the right input!
        number_of_cheeses = input_checker(number_of_cheeses)
        print('How many stools?')
        number_of_stools = input()

        # if user wants to leave, let them!
        if number_of_stools == 'quit':
            print('Good Game!')

        else:

            number_of_stools = input_checker(number_of_stools)
            # Create a new CC object and initialize with user's inputs
            new_cc = ConsoleController(int(number_of_cheeses),
                                       int(number_of_stools))
            # Also create a new TOAHModel which will be modified
            new_model = TOAHModel(int(number_of_stools))
            # Fill the first stool so the game can be played
            new_model.fill_first_stool(int(number_of_cheeses))
            # Print a string representation so user can visually see which
            # he/she plays the game
            print(new_model)
            print(new_model.number_of_moves())
            # keep looping until user wants to exit by typing 'quit'
            new_cc.play_loop()