%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This script puts imports and solves a Sudoku puzzle from:
% http://www.websudoku.com/
% and uses Puzzle # 1130357764 
% (Rated 'Evil')
% 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/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
map = NaN(9);
% Import the original puzzle
map(1,5) = 7;
map(1,7) = 9;
map(2,1) = 6;
map(2,3) = 7;
map(2,4) = 1;
map(2,7) = 3;
map(3,2) = 2;
map(3,4) = 3;
map(3,8) = 1;
map(4,6) = 1;
map(4,9) = 8;
map(5,1) = 5;
map(5,2) = 7;
map(5,8) = 9;
map(5,9) = 4;
map(6,1) = 1;
map(6,4) = 9;
map(7,2) = 5;
map(7,6) = 8;
map(7,8) = 4;
map(8,3) = 9;
map(8,6) = 4;
map(8,7) = 6;
map(8,9) = 5;
map(9,3) = 6;
map(9,5) = 5


% 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
    
    
    