function [newS,won,invalid]=ttt_addX(Sk)
% Computes new states that result from adding one X to each
% possible slot
%
% Input: 
%   Sk    [Nx1]    - array with N current states
%
% Outputs:
%   newS [Nx9]    - Nx9 arrays with next states. Each row
%                   corresponds  to one of the original states in Sk
%                   and each column corresponds to a possible
%                   placement for X
%   won  [Nx9]    - true if the corresponding state in newS leads
%                   to X winning 
%   invalid [Nx9] - false if the corresponding state in newS is
%                   invalid due to placing X on nonempty slot.
    
    XplayMasks=int32([bin2dec('010000 000000 000000');
                      bin2dec('000100 000000 000000');
                      bin2dec('000001 000000 000000');
                      bin2dec('000000 010000 000000');
                      bin2dec('000000 000100 000000');
                      bin2dec('000000 000001 000000');
                      bin2dec('000000 000000 010000');
                      bin2dec('000000 000000 000100');
                      bin2dec('000000 000000 000001')]);
    % compute new state and test whether move is valid
    newS=zeros(size(Sk,1),length(XplayMasks),'int32');
    invalid=false(size(newS));
    for slot=1:length(XplayMasks)
        mask=XplayMasks(slot);
        newS(:,slot)=bitor(Sk,mask);
        invalid(bitand(Sk,mask+2*mask)~=0,slot)=true;
    end

    XwinMasks=int32([bin2dec('010101 000000 000000');   % top horizontal
                     bin2dec('000000 010101 000000');   % mid horizontal
                     bin2dec('000000 000000 010101');   % bottom horizontal
                     bin2dec('010000 010000 010000');   % left vertical
                     bin2dec('000100 000100 000100');   % center vertical
                     bin2dec('000001 000001 000001');   % right vertical
                     bin2dec('010000 000100 000001');   % descend diagonal
                     bin2dec('000001 000100 010000')]); % ascend diagonal
    % check if X won
    won=false(size(newS));
    for i=1:length(XwinMasks)
        won=bitor(won,bitand(newS,XwinMasks(i))==XwinMasks(i));
    end

end
