예제 #1
0
    def __init__(self, parent, size, mode):

        self.size = size
        self.mode = mode

        parent.title('Under the Sea')
        sensor_button = tkinter.Button(parent,
                                       text='SONAR',
                                       command=self.sensor_mode)
        sensor_button.grid()
        action_button = tkinter.Button(parent,
                                       text='DIVE',
                                       command=self.diving_mode)
        action_button.grid()
        self.canvas = tkinter.Canvas(parent,
                                     width=self.size * self.square_size,
                                     height=self.size * self.square_size)
        self.canvas.grid()
        self.text = [[None for row in range(self.size)]
                     for col in range(self.size)]
        # create the squares on the canvas
        for row in range(self.size):
            for column in range(self.size):
                self.canvas.create_rectangle(column * self.square_size,
                                             row * self.square_size,
                                             (column + 1) * self.square_size,
                                             (row + 1) * self.square_size,
                                             fill='blue',
                                             outline='white')
                # ???Why text[col][row]
                self.text[column][row] = self.canvas.create_text(
                    (column + 0.5) * self.square_size,
                    (row + 0.5) * self.square_size,
                    text='',
                    fill='black')
        self.message = tkinter.Label(parent)
        self.message.grid()
        self.treasure_img = tkinter.PhotoImage(file="treasure.gif")
        self.oct = tkinter.PhotoImage(file="octopus.gif")

        self.model = Model()
        self.problem = Problem(size)
        self.belief = Belief(size)
        if self.mode == 'guided':
            self.showbeliefs()
예제 #2
0
class Game(object):
    """
    Game class for the treasure hunt

    Arguments:
    parent: the root window object
    size (int): the number of rows/columns in the game
    mode (string): discovery or guided?  No probabilities are shown in
        discovery mode and no sensor recommendations are shown.

    Attributes
    size (int): the number of rows/columns in the game
    mode (string): discovery or guided?
    canvas (Canvas): tkinter widget
    model (Model object) our world model of how the location of the treasure
            affects the sensor readings
    problem (Problem object) the specific problem (with random treasure
            location that we are trying to solve
    belief (Belief object) belief distribution based on the sensing
            evidence we have so far
    text (canvas text object): used to display the probability of each location
    message (Label) tkinter widget
    """
    square_size = 70  # length in pixels of the side of a grid square

    def __init__(self, parent, size, mode):

        self.size = size
        self.mode = mode

        parent.title('Under the Sea')
        sensor_button = tkinter.Button(parent,
                                       text='SONAR',
                                       command=self.sensor_mode)
        sensor_button.grid()
        action_button = tkinter.Button(parent,
                                       text='DIVE',
                                       command=self.diving_mode)
        action_button.grid()
        self.canvas = tkinter.Canvas(parent,
                                     width=self.size * self.square_size,
                                     height=self.size * self.square_size)
        self.canvas.grid()
        self.text = [[None for row in range(self.size)]
                     for col in range(self.size)]
        # create the squares on the canvas
        for row in range(self.size):
            for column in range(self.size):
                self.canvas.create_rectangle(column * self.square_size,
                                             row * self.square_size,
                                             (column + 1) * self.square_size,
                                             (row + 1) * self.square_size,
                                             fill='blue',
                                             outline='white')
                # ???Why text[col][row]
                self.text[column][row] = self.canvas.create_text(
                    (column + 0.5) * self.square_size,
                    (row + 0.5) * self.square_size,
                    text='',
                    fill='black')
        self.message = tkinter.Label(parent)
        self.message.grid()
        self.treasure_img = tkinter.PhotoImage(file="treasure.gif")
        self.oct = tkinter.PhotoImage(file="octopus.gif")

        self.model = Model()
        self.problem = Problem(size)
        self.belief = Belief(size)
        if self.mode == 'guided':
            self.showbeliefs()

    def sensor_mode(self):
        """
        Set the game to sensor mode - the method is invoked when the "SONAR"
        button is clicked.
        Once the game is in sensor mode, clicks on the grid result in sensor
        readings.
        """
        self.canvas.bind("<Button-1>", self.sense)
        self.message.configure(text='SONAR MODE')

    def diving_mode(self):
        """
        Set the game to diving mode - the method is invoked when the "SONAR"
        button is clicked.
        Once the game is in diving mode, clicks on the grid reveal whether the
        treasure is found at that location.
        """
        self.canvas.bind("<Button-1>", self.dive)
        self.message.configure(text='DIVING MODE')

    def dive(self, event):
        """
        A click at a given location represents a dive.
        Check if the dive is successful and update the GUI
        """
        y = event.y // self.square_size
        x = event.x // self.square_size
        square = self.canvas.find_closest((x + 0.1) * self.square_size,
                                          (y + 0.1) * self.square_size)[0]
        self.canvas.itemconfigure(square, fill="black")
        if self.problem.treasure_found((x, y)):
            self.canvas.create_image((x + 0.5) * self.square_size,
                                     (y + 0.5) * self.square_size,
                                     image=self.treasure_img)
            self.message.configure(text='Treasure chest found!!!')
        else:
            self.canvas.create_image((x + 0.5) * self.square_size,
                                     (y + 0.5) * self.square_size,
                                     image=self.oct)
            self.message.configure(text='Nope.  Try again')

    def sense(self, event):
        """
        A click at a given location represents a sonar reading.
        """
        x = event.x // self.square_size
        y = event.y // self.square_size
        square = self.canvas.find_closest((x + 0.1) * self.square_size,
                                          (y + 0.1) * self.square_size)[0]
        sensing_position = (x, y)
        color = self.model.observe(sensing_position, self.problem)
        self.belief.update(sensing_position, color, self.model)
        self.mark(square, color)
        if self.mode == 'guided':
            self.showbeliefs()
            self.show_recommendation()

    def showbeliefs(self):
        """
        Show the current belief distribution on the GUI
        """
        b = self.belief.get_beliefs()
        for i in range(self.size):
            for j in range(self.size):
                message = f'{b[(i, j)]:4.1}'
                self.canvas.itemconfigure(self.text[i][j],
                                          text=message,
                                          fill='black')

    def show_recommendation(self):
        """
        Show the recommendation returned by the belief module in purple
        on the GUI.
        """
        next_position = self.belief.recommend_sensing()
        if next_position != NotImplemented:
            nx, ny = next_position
            square = self.canvas.find_closest((nx + 0.1) * self.square_size,
                                              (ny + 0.1) * self.square_size)[0]
            self.canvas.itemconfigure(square, fill='purple')

    def mark(self, square, color):
        """
        Mark a given square with the specified color.

        :param square: tkinter id of the square to be marked
        :param color: (string)  'red', 'green' or 'yellow'
        """
        self.canvas.itemconfigure(square, fill=color)