function table = playcard_rand(turn, table)
%  PLAYCARD_RAND  The player makes a move (possibly random).
%  PLAYCARD_RAND(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 randomly selected move.
%  THIS FUNCTION UPDATES GLOBAL VARIABLES.


global PLAYER;


%
%  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_rand(mod(turn, 3) + 1, table);
    PLAYER{turn}(end + 1) = givecard_rand(mod(turn + 1, 3) + 1, table);

%
%  Otherwise, make a move.
%
else
    [X, Y] = size(moves);

    %
    %  If it is the AI, find the best possible move and make it.
    %
    if (turn == 1)
        scores = [];
	for i = 1:Y
            [tmphand, tmptable] = makeplay(PLAYER{turn}, table, moves(i));
            scores = [scores evaluate(encode(tmphand, tmptable))];
        end
	[score, index] = max(scores);

    %
    %  Otherwise, pick a valid move at random.
    %
    else
        index = ceil(rand * Y);
    end

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