function v = encode(hand, table)
%  ENCODE  Encodes the current game state as an input vector for the network.
%  ENCODE(HAND, TABLE), where HAND is the player's hand, and TABLE represents
%  the current table configuration of Seven, returns an encoding of this
%  game state to be used as an input vector for the network.
%  THIS FUNCTION UPDATES GLOBAL VARIABLES.


global N;


%
%  Create the encoded vector.
%
v = zeros(N + 1, 1);  % +1 for the bias node

%
%  Encode the player's hand using a 52 bit localist representation,
%  one bit for each of the 52 possible cards.
%
if ~isempty(hand)
    for i = 1:52
        if (find(hand == i) > 0)
            v(i, 1) = 1;
	end
    end    
end

%
%  Encode the table state using eight 7 bit localist representations,
%  one bit for each of the low and high cards of the four suits.
%
for suit = 1:4
    if (table(suit, :) ~= [8 6])
        v(52 + (suit - 1) * 7 + table(suit, 1)) = 1;
        v(52 + (suit - 1) * 7 + table(suit, 2) + 1) = 1;
    end
end

%
%  For the bias node.
%
v(N + 1) = 1;
