function card = givecard_rand(turn, table)
%  GIVECARD_RAND  The player passes his/her worst card (possibly random).
%  GIVECARD_RAND(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;


%
%  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
    [X, Y] = size(PLAYER{turn});

    %
    %  If it is the AI, find the best possible move reached by removing
    %  one card, then return that card which should be the worst.
    %
    if (turn == 1)
        moves = [];
        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);

    %
    %  Otherwise, pick a random card from the hand and give it.
    %
    else
        index = ceil(rand * Y);
        card = PLAYER{turn}(index);
    end

    PLAYER{turn}(index) = [];
end
