function [newS,won,invalid]=ttt_addO(Sk,slot)
% Computes new states that result from adding one O 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 O
%   won  [Nx9]    - true if the corresponding state in newS leads
%                   to O winning 
%   invalid [Nx9] - false if the corresponding state in newS is
%                   invalid due to placing O on nonempty slot.
    
    OplayMasks=int32([bin2dec('100000 000000 000000');
                      bin2dec('001000 000000 000000');
                      bin2dec('000010 000000 000000');
                      bin2dec('000000 100000 000000');
                      bin2dec('000000 001000 000000');
                      bin2dec('000000 000010 000000');
                      bin2dec('000000 000000 100000');
                      bin2dec('000000 000000 001000');
                      bin2dec('000000 000000 000010')]);
    % compute new state and test whether move is valid
    newS=zeros(size(Sk,1),length(OplayMasks),'int32');
    invalid=false(size(newS));
    for slot=1:length(OplayMasks)
        mask=OplayMasks(slot);
        newS(:,slot)=bitor(Sk,mask);
        invalid(bitand(Sk,mask+mask/2)~=0,slot)=true;
    end
    
    OwinMasks=int32([bin2dec('101010 000000 000000');   % top horizontal
                     bin2dec('000000 101010 000000');   % mid horizontal
                     bin2dec('000000 000000 101010');   % bottom horizontal
                     bin2dec('100000 100000 100000');   % left vertical
                     bin2dec('001000 001000 001000');   % center vertical
                     bin2dec('000010 000010 000010');   % right vertical
                     bin2dec('100000 001000 000010');   % descend diagonal
                     bin2dec('000010 001000 100000')]); % ascend diagonal
    % check if O won
    won=false(size(newS)); 
    for i=1:length(OwinMasks)
        won=bitor(won,bitand(newS,OwinMasks(i))==OwinMasks(i));
    end

end
