Пример #1
0
class TradeFrame(tk.Frame):
    """
    class TradeFrame is a tkinter Frame containing a dynamic trading interface

    TradeFrame contains a swappable frame. The contents of the frame are responsible for
    swapping themselves out when user input dictates it (eg click button -> swap frame)

    TradeFrame is used inside the larger GameToolbarFrame.

    TradeFrame observes the game object. On notify, it:
    - calls the swappable frame's notify() method
    - sets the state of its buttons
    """
    def __init__(self, master, game):
        print('hit init method of tradeframe')
        super(TradeFrame, self).__init__(master)
        self.master = master
        self.game = game
        self.game.observers.add(self)

        self.trade = CatanTrade(giver=self.game.get_cur_player())

        self.title = tk.Label(self, text="Trade")
        self.frame = WithWhoFrame(self)
        self.cancel = tk.Button(self, text='Cancel', state=tk.DISABLED, command=self.on_cancel)
        self.make_trade = tk.Button(self, text='Make Trade', state=tk.DISABLED, command=self.on_make_trade)

        self.title.grid(sticky=tk.W)
        self.frame.grid()
        self.cancel.grid(row=2, column=0, sticky=tk.EW)
        self.make_trade.grid(row=2, column=1, sticky=tk.EW)

        self.set_states()

    def notify(self, observable):
        self.frame.notify(self)
        self.set_states()

    def set_states(self):
        self.make_trade.configure(state=can_do[self.can_make_trade()])
        self.cancel.configure(state=can_do[self.can_cancel()])

    def set_frame(self, frame):
        self.frame.grid_remove()
        self.frame = frame
        self.frame.grid(row=1)
        self.notify(None)

    def can_make_trade(self):
        return self.frame.can_make_trade()

    def can_cancel(self):
        return self.frame.can_cancel()

    def on_make_trade(self):
        self.trade.set_giver(self.game.get_cur_player())
        logging.debug('trade MAKE: {} {} {} {}'.format(self.trade.giver(),
                                                       self.trade.giving(),
                                                       self.trade.getter(),
                                                       self.trade.getting()))
        self.game.trade(self.trade)

        self.on_cancel()
        self.game.notify(None)

    def on_cancel(self):
        self.trade = CatanTrade(giver=self.game.get_cur_player())
        self.set_frame(WithWhoFrame(self))
Пример #2
0
class TradeFrame(tk.Frame):
    """
    class TradeFrame is a tkinter Frame containing a dynamic trading interface

    TradeFrame contains a swappable frame. The contents of the frame are responsible for
    swapping themselves out when user input dictates it (eg click button -> swap frame)

    TradeFrame is used inside the larger GameToolbarFrame.

    TradeFrame observes the game object. On notify, it:
    - calls the swappable frame's notify() method
    - sets the state of its buttons
    """

    def __init__(self, master, game):
        super(TradeFrame, self).__init__(master)
        self.master = master
        self.game = game
        self.game.observers.add(self)

        self.trade = CatanTrade(giver=self.game.get_cur_player())

        self.title = tk.Label(self, text="Trade")
        self.frame = WithWhoFrame(self)
        self.cancel = tk.Button(self, text="Cancel", state=tk.DISABLED, command=self.on_cancel)
        self.make_trade = tk.Button(self, text="Make Trade", state=tk.DISABLED, command=self.on_make_trade)

        self.title.grid(sticky=tk.W)
        self.frame.grid()
        self.cancel.grid(row=2, column=0, sticky=tk.EW)
        self.make_trade.grid(row=2, column=1, sticky=tk.EW)

        self.set_states()

    def notify(self, observable):
        self.frame.notify(self)
        self.set_states()

    def set_states(self):
        self.make_trade.configure(state=can_do[self.can_make_trade()])
        self.cancel.configure(state=can_do[self.can_cancel()])

    def set_frame(self, frame):
        self.frame.grid_remove()
        self.frame = frame
        self.frame.grid(row=1)
        self.notify(None)

    def can_make_trade(self):
        return self.frame.can_make_trade()

    def can_cancel(self):
        return self.frame.can_cancel()

    def on_make_trade(self):
        self.trade.set_giver(self.game.get_cur_player())
        logging.debug(
            "trade MAKE: {} {} {} {}".format(
                self.trade.giver(), self.trade.giving(), self.trade.getter(), self.trade.getting()
            )
        )
        self.game.trade(self.trade)

        self.on_cancel()
        self.game.notify(None)

    def on_cancel(self):
        self.trade = CatanTrade(giver=self.game.get_cur_player())
        self.set_frame(WithWhoFrame(self))
Пример #3
0
class TradeFrame(tk.Frame):
    """
    class TradeFrame is a tkinter Frame containing a dynamic trading interface

    TradeFrame contains a swappable frame. The contents of the frame are responsible for
    swapping themselves out when user input dictates it (eg click button -> swap frame)

    TradeFrame is used inside the larger GameToolbarFrame.

    TradeFrame observes the game object. On notify, it:
    - calls the swappable frame's notify() method
    - sets the state of its buttons
    """
    def __init__(self, master, game):
        super(TradeFrame, self).__init__(master)
        self.master = master
        self.game = game
        self.game.observers.add(self)

        self.trade = CatanTrade(giver=self.game.get_cur_player())

        self.title = tk.Label(self, text="Trade")
        self.frame = WithWhoFrame(self)
        self.cancel = tk.Button(self,
                                text='Cancel',
                                state=tk.DISABLED,
                                command=self.on_cancel)
        self.make_trade = tk.Button(self,
                                    text='Make Trade',
                                    state=tk.DISABLED,
                                    command=self.on_make_trade)

        self.title.grid(sticky=tk.W)
        self.frame.grid()
        self.cancel.grid(row=2, column=0, sticky=tk.EW)
        self.make_trade.grid(row=2, column=1, sticky=tk.EW)

        self.set_states()

    def notify(self, observable):
        self.frame.notify(self)
        self.set_states()

    def set_states(self):
        self.make_trade.configure(state=can_do[self.can_make_trade()])
        self.cancel.configure(state=can_do[self.can_cancel()])

    def set_frame(self, frame):
        self.frame.grid_remove()
        self.frame = frame
        self.frame.grid(row=1)
        self.notify(None)

    def can_make_trade(self):
        return self.frame.can_make_trade()

    def can_cancel(self):
        return self.frame.can_cancel()

    def on_make_trade(self):
        self.trade.set_giver(self.game.get_cur_player())
        logging.debug('trade MAKE: {} {} {} {}'.format(self.trade.giver(),
                                                       self.trade.giving(),
                                                       self.trade.getter(),
                                                       self.trade.getting()))
        if type(self.trade.getter()) == Port or self.try_trade():
            self.game.trade(self.trade)
            print('Trade accepted!')
        else:
            print('Trade declined.')

        self.on_cancel()
        self.game.notify(None)

    def on_cancel(self):
        self.trade = CatanTrade(giver=self.game.get_cur_player())
        self.set_frame(WithWhoFrame(self))

    def try_trade(self):
        partner_next_moves = droid_behavior.best_win_condition(
            self.master.board_frame,
            self.master.game.board,
            player=self.trade.getter())
        if partner_next_moves[0] == 'road':
            partner_needs = droid_behavior.road_needs
        elif partner_next_moves[0] == 'sett':
            partner_needs = droid_behavior.sett_needs
        elif partner_next_moves[0] == 'devc':
            partner_needs = droid_behavior.devc_needs
        elif partner_next_moves[0] == 'city':
            partner_needs = droid_behavior.city_needs
        helpful = False
        print(partner_needs)
        for item in self.trade.giving():
            if item[1] in partner_needs:
                helpful = True
        if not helpful:
            return False
        harmful = False
        if len(self.trade.getting()) > 1:
            print('Try asking for only one card.')
            return False
        for item in self.trade.getting():
            if item[0] > 1 or (item[1] in partner_needs and (self.master.game.hands[self.trade.getter()].count(item[1]) < 2 or \
                (item[1] == Terrain.ore and self.master.game.hands[self.trade.getter()].count(item[1]) < 4))):
                harmful = True
        if harmful:
            return False
        return True