-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcbSolve.m
55 lines (50 loc) · 1.66 KB
/
cbSolve.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function cbSolve(src, event,h)
%% Read my Sudokugui
sudokugui = get(h,'UserData');
for ii = 1:9
for jj= 1:9
string_ij = get(sudokugui.X(ii,jj),'string');
if(isempty(string_ij))
A(ii,jj) = 0;
else
A(ii,jj) = str2double(string_ij);
end
end
end
%% handle any sort of invalid cells
for ii = 1:9
for jj = 1:9
if(A(ii,jj)>9 || A(ii,jj)<0 || isnan(A(ii,jj)))
set(sudokugui.status,'string','Invalid entry',...
'foregroundcolor',[0.69,0,0.125],...
'fontsize',14);
set(sudokugui.X(ii,jj),'backgroundcolor',[0.69,0,0.125]);
return
end
end
end
%% Handle any type of conflics
for ii = 1:9
for jj = 1:9
if (A(ii,jj)~=0)
if ConflictAdvanceTest(A,ii,jj)
set(sudokugui.status,'string','Conflicts',...
'foregroundcolor',[0.69,0,0.125],...
'fontsize',14);
set(sudokugui.X(ii,jj),'backgroundcolor',[0.69,0,0.125]);
return
end
end
end
end
%% solve my sudoku
A_sol = SudokuBacktracker(A);
%% Promt the user that sudoku is solved
set(sudokugui.status,'string','SOLVED',...
'foregroundcolor',[0,0.39,0],...
'fontsize',15)
for ii = 1:9
for jj = 1:9
set(sudokugui.X(ii,jj),'string',num2str(A_sol(ii,jj)));
end
end