function table = playcard(turn, table)
%  PLAYCARD  The player makes a move.
%  PLAYCARD(TURN, TABLE), where TURN specifies the current player, and
%  TABLE represents the current table configuration of Seven, returns
%  an updated table after the current player makes a move.
%  THIS FUNCTION UPDATES GLOBAL VARIABLES.


global PLAYER HISTORY;


%
%  Get all possible moves for the given hand and table state.
%
moves = findmoves(PLAYER{turn}, table);


%
%  If no moves can be made, the other two players pass a card to this player.
%
if isempty(moves)
    PLAYER{turn}(end + 1) = givecard(mod(turn, 3) + 1, table);
    PLAYER{turn}(end + 1) = givecard(mod(turn + 1, 3) + 1, table);

%
%  Otherwise, find the best possible move and make it.
%
else
    [X, Y] = size(moves);
    scores = [];
    for i = 1:Y
        [tmphand, tmptable] = makeplay(PLAYER{turn}, table, moves(i));
        scores = [scores evaluate(encode(tmphand, tmptable))];
    end

    [score, index] = max(scores);

    [PLAYER{turn}, table] = makeplay(PLAYER{turn}, table, moves(index));

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