function [map,possible] = sudoku(map,possible,itermax) 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function solves the sudoku by filling in all the easy locations,
%
% MAP is the sudoku puzzle at the current state of completion
% POSSIBLE is the array of possible lovations for a particular value.
% ITERMAX is the number of sweeps it will make.  81 is more than the theoretical max
% 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
    itermax = 81;
end
iter = 0;
while max(max(isnan(map)))==1  && iter<itermax

iter = iter+1;
    for value = 1:9
        for row = 1:9
            if max(map(row,:)== value) == 0 %if the value is not already in this row 
                for column = 1:9
                    if ~isnan(map(row,column)) % if the cell is already occupies eliminate the remaining options
                        possible(row,column,:) = NaN;
                    end
                    if max(map(:,column)== value) == 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);
                        blockpotential = 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(blockpotential(:,:,value))))==1  % only one possible place for a given value
                                
                                blockrow = floor((block-1)/3)*3+(1:3);
                                blockcol = mod( (block-1)*3+(1:3)-1,9)+1;
                                
                                temp = ~isnan(blockpotential(:,:,value)); % matrix of logical to place teh point that belongs there
                                map(blockrow(sum(temp,2)==1),blockcol(sum(temp,1)==1)) = value;
                                
                            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
    
end








% try one of the test cases and look for a logical flaw
    
    
