%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This script puts imports and solves a Sudoku puzzle:
% 
% 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/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
map = NaN(9);
% Import the original puzzle
map(1,6) = 2;
map(1,9) = 5;
map(2,2) = 9;
map(2,5) = 3;
map(2,7) = 2;
map(2,9) = 4;
map(3,1) = 7;
map(3,8) = 8;
map(4,2) = 5;
map(4,5) = 2;
map(4,6) = 4;
map(5,3) = 2;
map(5,7) = 9;
map(6,4) = 8;
map(6,5) = 6;
map(6,8) = 1;
map(7,2) = 8;
map(7,9) = 6;
map(8,1) = 9;
map(8,3) = 7;
map(8,5) = 8;
map(8,8) = 4;
map(9,1) = 5;
map(9,4) = 1


% Set up an array of possible values.
possible = NaN(9,9,9);

possible(:,:,1)=1;
possible(:,:,2)=2;
possible(:,:,3)=3;
possible(:,:,4)=4;
possible(:,:,5)=5;
possible(:,:,6)=6;
possible(:,:,7)=7;
possible(:,:,8)=8;
possible(:,:,9)=9;
% Then we will eliminate the potential as they break down the logic
% statements

    % first do the easy steps.  This is what you can do quickly without any guessing and assumption checking
    [map,possible] = sudoku(map,possible);
    map
    % now we have exhausted the easy steps and we need to make an
    % assumption and see if it leads to logic flaws.  
    % Look for a block where a value could go in 1 of 2 spots, try the first position.  If
    % it fails try the other position.  
    % If the puzzle is not resolved, try another second assumption, if it
    % works great!, if it fails...
    [mapout, potentialout, done] = sudokutrial(map,possible);
    done
    mapout
    
    
    