def trade_menu(player,app):
    """Runs trading screen for player"""
    from catan_logic import perform_trade, evaluate_port_trade

    app.pieces.turn_phase = "trade"

    app.button_chosen.set(-1)
    while app.button_chosen.get()!=0:
        clear_resource_panel(app)
        draw_resource_panel(player,app)

        app.board_canvas.wait_variable(app.button_chosen)

        if app.button_chosen.get()==1:
            given_resource = app.displays.port_give_text.get()
            gotten_resource = app.displays.port_get_text.get()

            sell_resource_type = given_resource.split()[1]
            sell_resource_copies = int(given_resource.split()[0])
            buy_resource_type = gotten_resource.split()[1]
            buy_resource_copies = int(gotten_resource.split()[0])

            trade_successful, mul, trade_ratio = evaluate_port_trade(player,sell_resource_copies,
                sell_resource_type,buy_resource_copies,buy_resource_type)

            if trade_successful:
                perform_trade(player,sell_resource_type,buy_resource_type,
                    app,mul,False,trade_ratio)

    app.button_chosen.set(-1)
def take_turn(computer,available_settlement_points,available_roads,
        available_city_points,app):
    from catan_logic import perform_trade
    # Function is called to determine what action the computer should take

    # Should return "build settlement", "build road", "build city", or "ended turn"

    # Check current wheat trade ratio
    wheat_trade_ratio = 4
    if "any" in computer.ports or "?" in computer.ports:
        wheat_trade_ratio = 3
    if "wheat" in computer.ports:
        wheat_trade_ratio = 2

    # If building a road or settlement is possible and no wood is available,
    #  trade for wood if possible
    if (len(available_roads)!=0 or len(available_settlement_points)!=0) and \
        len(computer.roads)<computer.road_max and \
        len(computer.settlements)<computer.settlement_max and \
        computer.wood==0 and computer.wheat>=wheat_trade_ratio+1:
        perform_trade(computer,"wheat","wood",app)
    # If building a road or settlement is possible and no bricks are available,
    #  trade for brick if possible
    if (len(available_roads)!=0 or len(available_settlement_points)!=0) and \
        len(computer.roads)<computer.road_max and \
        len(computer.settlements)<computer.settlement_max and \
        computer.brick==0 and computer.wheat>=wheat_trade_ratio+1:
        perform_trade(computer,"wheat","brick",app)
    # If building a settlement is possible and no sheep are available,
    #  trade for sheep if possible
    if len(available_settlement_points)!=0 and \
        len(computer.settlements)<computer.settlement_max and \
        computer.sheep==0 and computer.wheat>=wheat_trade_ratio+1:
        perform_trade(computer,"wheat","sheep",app)
    # If building a road isn't possible but building a settlement is
    #  and no wood is available, trade for wood if possible
    if (len(computer.roads)==computer.road_max or len(available_roads)==0) and \
        len(available_settlement_points)!=0 and \
        len(computer.settlements)<computer.settlement_max and \
        computer.wood==0 and computer.wheat>=wheat_trade_ratio+1:
        perform_trade(computer,"wheat","wood",app)
    # If building a road isn't possible but building a settlement is
    #  and no brick is available, trade for brick if possible
    if (len(computer.roads)==computer.road_max or len(available_roads)==0) and \
        len(available_settlement_points)!=0 and \
        len(computer.settlements)<computer.settlement_max and \
        computer.brick==0 and computer.wheat>=wheat_trade_ratio+1:
        perform_trade(computer,"wheat","brick",app)
    # If building a city is possible and too little stone is available,
    #  trade for stone while it's possible
    if len(available_city_points)!=0 and \
        len(computer.cities)<computer.city_max:
        while computer.stone<3 and computer.wheat>=wheat_trade_ratio+1:
            perform_trade(computer,"wheat","stone",app)

    # Assume the computer can't do anything and just passes
    action_string = "ended turn"
    # If the computer can place a road, do that
    if len(available_roads)>0 and \
        len(computer.roads)<computer.road_max and \
        computer.wood>=1 and computer.brick>=1:
        action_string = "build road"
    # If the computer can place a settlement, do that instead
    if len(available_settlement_points)>0 and \
        len(computer.settlements)<computer.settlement_max and \
        computer.wood>=1 and computer.brick>=1 and computer.sheep>=1 and \
        computer.wheat>=1:
        action_string = "build settlement"
    # If the computer can place a city, do that instead
    if len(available_city_points)>0 and \
        len(computer.cities)<computer.city_max and \
        computer.wheat>=2 and computer.stone>=3:
        action_string = "build city"

    return action_string