function card = givecard(turn, table)
%  GIVECARD  The player passes his/her worst card.
%  GIVECARD(TURN, TABLE), where TURN specifies the current player, and
%  TABLE represents the current table configuration of Seven, returns
%  the worst card in the player's hand as decided by the neural network.
%  THIS FUNCTION UPDATES GLOBAL VARIABLES.


global PLAYER HISTORY;


%
%  If no moves can be made (the hand is empty), return nothing (0).
%
if isempty(PLAYER{turn})
    card = 0;

%
%  Otherwise, evaluate every possible hand reached by removing just one card.
%
else
    %
    %  Find the best possible move reached by removing one card,
    %  then return that card which should be the worst.
    %
    moves = [];
    [X, Y] = size(PLAYER{turn});
    for i = 1:Y
        card = PLAYER{turn}(i);
        PLAYER{turn}(i) = 0;
        score = evaluate(encode(PLAYER{turn}, table));
        PLAYER{turn}(i) = card;

        moves = [moves [score card]'];
    end

    [score, index] = max(moves(1, :));
    card = moves(2, index);

    index = find(PLAYER{turn} == card);
    PLAYER{turn}(index) = [];

    HISTORY{turn} = [encode(PLAYER{turn}, table) HISTORY{turn}];
    train(turn, score);
end
