function Sudoku
close all
clear all
board=zeros(9);
stat=zeros(9);
ScreenSize=get(0,'ScreenSize');
mainwindow=figure('Name','Sudoku',...
'NumberTitle','Off',...
'Resize','off',...
'Menubar','none',...
'Units','pixels',...
'Position',[0.5*(ScreenSize(3)-384),...
0.5*(ScreenSize(4)-400),...
384,400]);
GUIboard=zeros(9);
for i=1:9
for j=1:9
vc=(ceil(j/3)-2)*0.0125;
hc=(ceil(i/3)-2)*0.0125;
GUIboard(i,j)=uicontrol('Parent',mainwindow,...
'Style','edit',...
'String','',...
'Enable','On',...
'Units','normalized',...
'Position',[0.0925+0.075*i+hc,...
0.975-0.075*j-vc,...
0.075,0.075]);
end
end
uicontrol('Parent',mainwindow,...
'Style','pushbutton',...
'String','PlayGame(Easy)',...
'Units','normalized',...
'Position',[0.1,0.2,0.25,0.05],...
'Callback',@startgameeasyfcn);
uicontrol('Parent',mainwindow,...
'Style','pushbutton',...
'String','PlayGame(Normal)',...
'Units','normalized',...
'Position',[0.375,0.2,0.25,0.05],...
'Callback',@startgamenormalfcn);
uicontrol('Parent',mainwindow,...
'Style','pushbutton',...
'String','PlayGame(Hard)',...
'Units','normalized',...
'Position',[0.65,0.2,0.25,0.05],...
'Callback',@startgamehardfcn);
uicontrol('Parent',mainwindow,...
'Style','pushbutton',...
'String','CheckBoard',...
'Units','normalized',...
'Position',[0.1,0.12,0.25,0.05],...
'Callback',@checkboardfcn);
uicontrol('Parent',mainwindow,...
'Style','pushbutton',...
'String','ResetBoard',...
'Units','normalized',...
'Position',[0.375,0.12,0.25,0.05],...
'Callback',@resetboardfcn);
uicontrol('Parent',mainwindow,...
'Style','pushbutton',...
'String','SolveSudoku',...
'Units','normalized',...
'Position',[0.65,0.12,0.25,0.05],...
'Callback',@solvesudokufcn);
instructionbox=uicontrol('Parent',mainwindow,...
'Style','text',...
'String','Welcome to Sudoku!',...
'Units','normalized',...
'Position',[0.05,0.025,0.9,0.04]);
function board=readGUIboard
board=zeros(9);
for row=1:9
for col=1:9
value=str2double(get(GUIboard(row,col),'String'));
if isnan(value)
board(row,col)=0;
else
board(row,col)=value;
end
end
end
end
function display2GUIboard(board)
for row=1:9
for col=1:9
if board(row,col)~=0
set(GUIboard(row,col),'String',num2str(board(row,col)))
else
set(GUIboard(row,col),'String','')
end
set(GUIboard(row,col),'BackGroundColor','w')
end
end
end
function lockGUIboard(board)
for row=1:9
for col=1:9
if board(row,col)==0
set(GUIboard(row,col),'Enable','On')
else
set(GUIboard(row,col),'Enable','Off')
end
end
end
end
function stat=checkboard(board)
stat=false(9);
for testvalue=1:9
for row=1:9
if sum(sum(board(row,:)==testvalue))>1
errorloc=(board(row,:)==testvalue);
stat(row,:)=stat(row,:)|errorloc;
end
end
for col=1:9
if sum(sum(board(:,col)==testvalue))>1
errorloc=(board(:,col)==testvalue);
stat(:,col)=stat(:,col)|errorloc;
end
end
for gcrow=[1,2,3]
for gccol=[1,2,3]
if sum(sum(board(3*gcrow-2:3*gcrow,3*gccol-2:3*gccol)...
==testvalue))>1
errorloc=(board(3*gcrow-2:3*gcrow,3*gccol-2:3*gccol)...
==testvalue);
stat(3*gcrow-2:3*gcrow,3*gccol-2:3*gccol)=...
(stat(3*gcrow-2:3*gcrow,3*gccol-2:3*gccol)|...
errorloc);
end
end
end
end
end
function markGUIboard(stat)
for row=1:9
for col=1:9
if stat(row,col)==1
set(GUIboard(row,col),'BackGroundColor','r')
else
set(GUIboard(row,col),'BackGroundColor','w')
end
end
end
end
function board=deleteGUIboard(board,delcount)
count=0;
while count<delcount
temp=ceil(rand*81);
if board(temp)~=0
board(temp)=0;
count=count+1;
end
end
end
function startgameeasyfcn(~,~)
board=SudokuSolver;
board=deleteGUIboard(board,20);
display2GUIboard(board)
lockGUIboard(board)
set(instructionbox,'String','Easy Sudoku Puzzle')
end
function startgamenormalfcn(~,~)
board=SudokuSolver;
board=deleteGUIboard(board,40);
display2GUIboard(board)
lockGUIboard(board)
set(instructionbox,'String','Normal Sudoku Puzzle')
end
function startgamehardfcn(~,~)
board=SudokuSolver;
board=deleteGUIboard(board,60);
display2GUIboard(board)
lockGUIboard(board)
set(instructionbox,'String','Hard Sudoku Puzzle')
end
function checkboardfcn(~,~)
tempboard=readGUIboard;
stat=checkboard(tempboard);
markGUIboard(stat)
if sum(sum(stat))==0
if sum(sum(tempboard==0))==0
set(instructionbox,'String','Puzzle Solved!')
else
set(instructionbox,'String',...
'No violation detected! Keep going!')
end
else
set(instructionbox,'String',...
'Violation detected! Recheck the board!')
end
end
function resetboardfcn(~,~)
board=zeros(9);
display2GUIboard(board)
lockGUIboard(board)
set(instructionbox,'String','Board reseted!')
end
function solvesudokufcn(~,~)
tempboard=readGUIboard;
stat=checkboard(tempboard);
if sum(sum(stat))==0
result=SudokuSolver(tempboard);
display2GUIboard(result)
set(instructionbox,'String','Puzzle Solved!')
else
markGUIboard(stat)
set(instructionbox,'String',...
'Puzzle unsolvable due to violation!')
end
end
end