def turn_loop(player,app):
    from catan_logic import build_settlement, build_road, build_city
    from catan_logic import buy_development_card
    from catan_logic import legal_settlement_placements, legal_road_placements
    from catan_AI import computer_take_turn

    app.pieces.turn_phase = "make decisions"

    draw_stats(app)
    clear_resource_panel(app)
    draw_resource_panel(app.pieces.players[app.pieces.turn_index],app)

    # For human players, draw buttons and get button clicks
    if player.AI_code<0:
        app.button_chosen.set(-1)
        while app.button_chosen.get()!=0:
            app.pieces.turn_phase = "make decisions"
            draw_stats(app)
            clear_resource_panel(app)
            draw_resource_panel(app.pieces.players[app.pieces.turn_index],app)

            app.board_canvas.wait_variable(app.button_chosen)
            if app.button_chosen.get()==1:
                build_settlement(player,app)
            elif app.button_chosen.get()==2:
                build_road(player,app)
            elif app.button_chosen.get()==3:
                build_city(player,app)
            elif app.button_chosen.get()==4:
                buy_development_card(player,app)
            elif app.button_chosen.get()==5:
                trade_menu(player,app)
            elif app.button_chosen.get()==6:
                development_menu(player,app)
    # For computer players, reference AI file
    else:
        computer_action = "none"
        while not(computer_action=="ended turn"):
            available_settlement_points = legal_settlement_placements(player,
                app.pieces.players,app.pieces.all_points)
            available_roads = legal_road_placements(player,app.pieces.players,
                app.pieces.all_roads)
            computer_action = computer_take_turn(player,
                available_settlement_points,available_roads,app)
def draw_main_screen(player,app):
    """Draws buttons for player actions on the board window and sets their
    status according to the player's resource availability."""
    from catan_logic import legal_settlement_placements, legal_road_placements

    draw_resources(player,app)

    # Create buttons on board window
    build_settlement_button = Button(app.board_canvas,
        font=(app.style.txt_font,int(.8*app.style.txt_size)),
        text="Build Settlement", command=lambda : app.set_button_chosen(1))
    build_settlement_button.configure(width=13, height=1, padx=0, pady=0)
        #background=inactive_button_color, activebackground=active_button_color)
    app.displays.add_object(build_settlement_button)
    app.board_canvas.create_window(
        int((app.style.hex_x_off-app.style.water_width)*3/10),
        int(app.style.win_height*.4),
        window=build_settlement_button, tags="button")
    app.board_canvas.create_text(
        int((app.style.hex_x_off-app.style.water_width)*3/10),
        int(app.style.win_height*.4+1.25*app.style.txt_size),
        text="(1 wood, 1 brick, 1 sheep, 1 wheat)",
        font=(app.style.txt_font,int(.5*app.style.txt_size)), tags="button")
    app.board_canvas.create_text(
        int((app.style.hex_x_off-app.style.water_width)*3/10),
        int(app.style.win_height*.4+2*app.style.txt_size),
        text=str(player.settlement_max-len(player.settlements))+" remaining",
        font=(app.style.txt_font,int(.7*app.style.txt_size)), tags="button")
    build_road_button = Button(app.board_canvas,
        font=(app.style.txt_font,int(.8*app.style.txt_size)),
        text="Build Road", command=lambda : app.set_button_chosen(2))
    build_road_button.configure(width=13, height=1, padx=0, pady=0)
        #background=inactive_button_color, activebackground=active_button_color)
    app.displays.add_object(build_road_button)
    app.board_canvas.create_window(
        int((app.style.hex_x_off-app.style.water_width)*7/10),
        int(app.style.win_height*.4),
        window=build_road_button, tags="button")
    app.board_canvas.create_text(
        int((app.style.hex_x_off-app.style.water_width)*7/10),
        int(app.style.win_height*.4+1.25*app.style.txt_size),
        text="(1 wood, 1 brick)",
        font=(app.style.txt_font,int(.5*app.style.txt_size)), tags="button")
    app.board_canvas.create_text(
        int((app.style.hex_x_off-app.style.water_width)*7/10),
        int(app.style.win_height*.4+2*app.style.txt_size),
        text=str(player.road_max-len(player.roads))+" remaining",
        font=(app.style.txt_font,int(.7*app.style.txt_size)), tags="button")
    build_city_button = Button(app.board_canvas,
        font=(app.style.txt_font,int(.8*app.style.txt_size)),
        text="Build City", command=lambda : app.set_button_chosen(3))
    build_city_button.configure(width=13, height=1, padx=0, pady=0)
        #background=inactive_button_color, activebackground=active_button_color)
    app.displays.add_object(build_city_button)
    app.board_canvas.create_window(
        int((app.style.hex_x_off-app.style.water_width)*3/10),
        int(app.style.win_height*.4+4*app.style.txt_size),
        window=build_city_button, tags="button")
    app.board_canvas.create_text(
        int((app.style.hex_x_off-app.style.water_width)*3/10),
        int(app.style.win_height*.4+5.25*app.style.txt_size),
        text="(2 wheat, 3 stone)",
        font=(app.style.txt_font,int(.5*app.style.txt_size)), tags="button")
    app.board_canvas.create_text(
        int((app.style.hex_x_off-app.style.water_width)*3/10),
        int(app.style.win_height*.4+6*app.style.txt_size),
        text=str(player.city_max-len(player.cities))+" remaining",
        font=(app.style.txt_font,int(.7*app.style.txt_size)), tags="button")
    buy_dev_button = Button(app.board_canvas,
        font=(app.style.txt_font,int(.8*app.style.txt_size)),
        text="Buy Dev Card", command=lambda : app.set_button_chosen(4))
    buy_dev_button.configure(width=13, height=1, padx=0, pady=0)
        #background=inactive_button_color, activebackground=active_button_color)
    app.displays.add_object(buy_dev_button)
    app.board_canvas.create_window(
        int((app.style.hex_x_off-app.style.water_width)*7/10),
        int(app.style.win_height*.4+4*app.style.txt_size),
        window=buy_dev_button, tags="button")
    app.board_canvas.create_text(
        int((app.style.hex_x_off-app.style.water_width)*7/10),
        int(app.style.win_height*.4+5.25*app.style.txt_size),
        text="(1 sheep, 1 wheat, 1 stone)",
        font=(app.style.txt_font,int(.5*app.style.txt_size)), tags="button")
    app.board_canvas.create_text(
        int((app.style.hex_x_off-app.style.water_width)*7/10),
        int(app.style.win_height*.4+6*app.style.txt_size),
        text=str(len(app.pieces.development_cards))+" remaining",
        font=(app.style.txt_font,int(.7*app.style.txt_size)), tags="button")
    trade_button = Button(app.board_canvas,
        font=(app.style.txt_font,int(.8*app.style.txt_size)),
        text="Trade", command=lambda : app.set_button_chosen(5))
    trade_button.configure(width=13, height=1, padx=0, pady=0)
        #background=inactive_button_color, activebackground=active_button_color)
    app.displays.add_object(trade_button)
    app.board_canvas.create_window(
        int((app.style.hex_x_off-app.style.water_width)*3/10),
        int(app.style.win_height*.4+8*app.style.txt_size),
        window=trade_button, tags="button")
    use_dev_button = Button(app.board_canvas,
        font=(app.style.txt_font,int(.8*app.style.txt_size)),
        text="Use Dev Card", command=lambda : app.set_button_chosen(6))
    use_dev_button.configure(width=13, height=1, padx=0, pady=0)
    if sum(player.development_cards.values())==0:
        use_dev_button.configure(state=DISABLED)
        #background=inactive_button_color, activebackground=active_button_color)
    app.displays.add_object(use_dev_button)
    app.board_canvas.create_window(
        int((app.style.hex_x_off-app.style.water_width)*7/10),
        int(app.style.win_height*.4+8*app.style.txt_size),
        window=use_dev_button, tags="button")
    end_turn_button = Button(app.board_canvas,
        font=(app.style.txt_font,int(.8*app.style.txt_size)),
        text="End Turn", command=lambda : app.set_button_chosen(0))
    end_turn_button.configure(width=10, height=1, padx=0, pady=0)
        #background=inactive_button_color, activebackground=active_button_color)
    app.displays.add_object(end_turn_button)
    app.board_canvas.create_window(
        int((app.style.hex_x_off-app.style.water_width)*5/10),
        int(app.style.win_height*.4+11*app.style.txt_size),
        window=end_turn_button, tags="button")

    # Disable unusable buttons
    if player.wood<1 or player.brick<1 or player.sheep<1 or player.wheat<1 or \
            len(legal_settlement_placements(player,app.pieces.players,
                app.pieces.all_points))==0 or \
            len(player.settlements)>=player.settlement_max:
        build_settlement_button.configure(state=DISABLED)
    # else:
    #     build_settlement_button.configure(state=NORMAL)
    if player.wood<1 or player.brick<1 or \
            len(legal_road_placements(player,app.pieces.players,
                app.pieces.all_roads))==0 or \
            len(player.roads)>=player.road_max:
        build_road_button.configure(state=DISABLED)
    # else:
    #     build_road_button.configure(state=NORMAL)
    if player.wheat<2 or player.stone<3 or \
        len(player.settlements)==0 or \
        len(player.cities)>=player.city_max:
        build_city_button.configure(state=DISABLED)
    # else:
    #     build_city_button.configure(state=NORMAL)
    if player.sheep<1 or player.wheat<1 or player.stone<1:
        buy_dev_button.configure(state=DISABLED)
    # else:
    #     buy_dev_button.configure(state=NORMAL)
    if len(player.settlements)>=player.settlement_max:
        build_settlement_button.configure(text="Max Built")
    # else:
    #     build_settlement_button.configure(text="Build Settlement")
    if len(player.roads)>=player.road_max:
        build_road_button.configure(text="Max Built")
    # else:
    #     build_road_button.configure(text="Build Road")
    if len(player.cities)>=player.city_max:
        build_city_button.configure(text="Max Built")