function [map, possible,moved] = sudokutry(map,possible,use,spot )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Look for a location in a value in a block that could be in one of two spots.
%
% SPOT defines the first, second, third,... such location to choose
% USE says attempt to put the value in the first or second possible location
% MAP is the sudoku puzzle at the current state of completion
% Possible is the array of potential lovations for a particular value.
% It's logical but rather than using 0 and 1 it uses the NaN and the actual
% value.
% modified by Dave Campbell dac5@sfu.ca Mar 1, 2014
% 
% 
% This code is released under the GPLv3 license.
% See http://www.gnu.org/licenses/gpl.html for details.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     Find a copy of the GNU General Public License
%     here http://www.gnu.org/licenses/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if nargin <3
    use = 1;
end

if nargin<4
    spot = 1;
end
done = 0;
value = 0;
attempts =0;

moved = NaN(1,5);
while value<9 && done ==0;
    value = value+1;
    for row = 1:9
        if max(map(row,:)== value) == 0  && done == 0%if the value is not already in this row
            for column = 1:2%9
                if ~isnan(map(row,column))  && done == 0% if the cell is already occupied, eliminate the remaining options
                    possible(row,column,:) = NaN;
                end
                if max(map(:,column)== value) == 0 && done == 0%if the value is not already in this column
                    block = (floor((row-1)/3) )*3+ceil(column/3);
                    blockuse = map(floor((block-1)/3)*3+(1:3), mod( (block-1)*3+(1:3)-1,9)+1);
                    blockpossible = possible(floor((block-1)/3)*3+(1:3), mod( (block-1)*3+(1:3)-1,9)+1,:);
                    if  max(blockuse(:) ==value) == 1% value is already in the block, exclude it
                        possible(floor((block-1)/3)*3+(1:3), mod( (block-1)*3+(1:3)-1,9)+1,value) = NaN;
                    else
                        if sum(~isnan(possible(row,column,:)))==1
                            map(row,column) = value;
                        elseif sum(sum(~isnan(blockpossible(:,:,value))))==2 && done == 0 % only 2 possible places for a given value
                            attempts = attempts+1;
                            if attempts == (spot-1)*2+1 % go up by 2 to avoid duplicate block attempts
                                blockrow = floor((block-1)/3)*3+(1:3);
                                blockcol = mod( (block-1)*3+(1:3)-1,9)+1;
                                
                                temp = ~isnan(blockpossible(:,:,value)); % matrix of logical to place the point that belongs there
                                [x,y]=find(temp);
                                map(blockrow(x(use)),blockcol(y(use))) = value;
                                disp('attempts, value, row, column, block')
                                disp(num2str([attempts,value,row,column,block]))
                                moved = [attempts,value,row,column,block];
                            break
                            elseif attempts > (spot-1)*2+1 % silly spot, you're just too big.
                                spot = max(1,spot-1);
                                attempts = 1;
                            end
                        end
                    end
                    
                else% value is already in this column
                    possible(:,column,value) = NaN;
                end
            end
        else% if the value is already in this row
            possible(row,:,value) = NaN;
        end
    end
end

