Ejemplo n.º 1
0
class Model:
    def __init__(self, win, game_view, game_controller) -> None:

        self.win = win
        self.game_view = game_view
        self.game_controller = game_controller
        self.curr_disk = None
        self.curr_colour = red
        self.game_board = [[None, None, None, None, None, None],[None, None, None, None, None, None],\
                           [None, None, None, None, None, None],[None, None, None, None, None, None],\
                           [None, None, None, None, None, None],[None, None, None, None, None, None],\
                           [None, None, None, None, None, None]]

    def restart_model(self) -> None:
        """
        Restarts the model to the default
        """
        self.curr_disk = None
        self.curr_colour = red
        self.game_board = [[None, None, None, None, None, None],[None, None, None, None, None, None],\
                           [None, None, None, None, None, None],[None, None, None, None, None, None],\
                           [None, None, None, None, None, None],[None, None, None, None, None, None],\
                           [None, None, None, None, None, None]]

    def update_view(self, game_over) -> None:
        """
        Send the game view the game board to display.
        """
        self.game_view.display(self.game_board, game_over, self.curr_colour,
                               self.game_controller)

    def get_colour(self):
        return self.curr_colour

    def change_colour(self) -> None:
        """
        Changes the current colour between red and blue to alternate between players.
        Also sets the current disk to None if a switch is made.
        """
        if (self.curr_disk != None and self.curr_disk.get_set()):
            if (self.curr_colour == red):
                self.curr_colour = blue
            else:
                self.curr_colour = red
            self.curr_disk = None

    def add_disk(self, disk_pos_x) -> bool:
        """
        Add new disk to the game board using the horizontal position
        of the mouse click. Then check if that new disk has won the game.

        returns True if the new disk has won the game.
        """
        col = (disk_pos_x // 100)  # the colomn index on the board list
        row = find_row(self.game_board[col]
                       )  #find the first non empty row in the given column

        if row < 6:
            self.curr_disk = Disk(self.win, disk_pos_x, row, col,
                                  self.curr_colour)  #create disk object
            self.game_board[col][
                row] = self.curr_disk  #insert disk in the proper location on the board
            self.curr_disk.set_up_disk(
                self.game_board
            )  #connect the disk to all surrounding disks on the board
            while (self.curr_disk.get_set() != True):
                self.game_view.time_delay()
                self.update_view(False)
            if (self.curr_disk.check_win()
                ):  #check if the disk has won the game
                return True
            return False
        return False