function [mapout, possibleout, done] = sudokutrial(map,possible)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function solves the sudoku by filling in all the easy locations,
% then it makes assumptions and checks for problems, if none then done, if
% it runs into a logical flaw it goes back to an earlier assumption and
% tries again.  (Tested in 3 difficult puzzles and then the fun wore off)
%
% MAP is the sudoku puzzle at the current state of completion
% POSSIBLE is the array of possible 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/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
maparray          = cell(2,1);
maparray{1}       = map;
possiblearray    = cell(2,1);
possiblearray{1} = possible;

% index the assumption choice that it makes
spot = ones(50,1);
% index which of the possibilities to use (although this is incremented
% it's always the mod that's taken to keep it binary
use = ones(50,1);
% index the previously sudoku board
failed = 0;
done = 0;
iter = 0;
moved = zeros(50,5);
% movetype = [];
while failed  == 0  && iter < 50   && done == 0
    iter = iter+1;
    % find the #spot assumption choice and choose the first option
    % therein
    [maparray{iter+1},possiblearray{iter+1},moved(iter,:)] = sudokutry(maparray{iter},possiblearray{iter},use(iter),spot(iter));
    [maparray{iter+1},possiblearray{iter+1}] = sudoku(maparray{iter+1},possiblearray{iter+1});
    %check for logical flaws, if they exist go for
    failed = sudokucheck(maparray{iter+1},possiblearray{iter+1});
%     movetype = [movetype;[1,failed]];
    if failed>0
        % find the #spot assumption choice and choose the alternative 'use' index
        % therein since the first didn't work out.
        use(iter) = mod(use(iter),2)+1;
        [maparray{iter+1},possiblearray{iter+1},moved(iter,:)] = sudokutry(maparray{iter},possiblearray{iter},use(iter),spot(iter));
        [maparray{iter+1},possiblearray{iter+1}] = sudoku(maparray{iter+1},possiblearray{iter+1});
        %check for logical flaws, if they exist go for
        failed = sudokucheck(maparray{iter+1},possiblearray{iter+1});
%         movetype = [movetype;[2,failed]];
    end
    
    if failed>0
        % since both 'use' optiopns lead to failures the start of the
        % failing path must be ther previous iteration
        spot(iter-1) = spot(iter-1)+1;
        [maparray{iter+1},possiblearray{iter+1},moved(iter,:)] = sudokutry(maparray{iter-1},possiblearray{iter-1},use(iter-1),spot(iter-1));
        [maparray{iter+1},possiblearray{iter+1}] = sudoku(maparray{iter+1},possiblearray{iter+1});
        %check for logical flaws, if they exist go for
        failed = sudokucheck(maparray{iter+1},possiblearray{iter+1});
%         movetype = [movetype;[3,failed]];

    end
    
    % Check if the algorithm is done and there are no errors
    if sum(sum(isnan(maparray{iter+1})))==0  && failed==0
        % The map is full and there are no errors!
        % Stop doing stuff because the algorithm is finished
        disp('done!')
        mapout = maparray{iter+1};
        possibleout = possiblearray{iter+1};
        done = 1;
    elseif sum(sum(isnan(maparray{iter+1}))) > 0 &&( sum(sum(sum(~isnan(possiblearray{iter+1})))) == 0  ||failed == 1 )
        % The algorithm is not done, but the possible array has nowhere
        % left to go :^(
        % OR 
        % The algorithm is not done, but there is a found error.
        % Either way there is an error
        % so don't accept this iteration and try again
        maparray{iter+1} = maparray{max(1,iter-1)};
        possiblearray{iter+1} = possiblearray{max(1,iter-1)};
        use(iter+1) = use(max(1,iter-1))+1;
disp('possible died')        
    elseif sum(sum(isnan(maparray{iter+1}))) > 0 && ( sum(sum(isnan(maparray{iter+1})))  == sum(sum(isnan(maparray{iter}))))
        % The algorithm has stopped progressing
        maparray{iter+1} = maparray{max(1,iter-1)};
        possiblearray{iter+1} = possiblearray{max(1,iter-1)};
        use(iter+1) = use(max(1,iter-1))+1;
disp('loop back')
disp('====')
    else 
        % The algorithm is not done, and there are no errors, but we need
        % to make another assumption to progress
disp('go ahead')
        %         use = use+1;
    end
end