Code covered by the BSD License  

Highlights from
Sudoku

image thumbnail
from Sudoku by Zhen Qian
This is a Sudoku game that is able to generate new games by itself.

sudoku(varargin)
function varargout = sudoku(varargin)
% SUDOKU M-file for sudoku.fig
%      SUDOKU, by itself, creates a new SUDOKU or raises the existing
%      singleton*.
%
%      H = SUDOKU returns the handle to a new SUDOKU or the handle to
%      the existing singleton*.
%
%      SUDOKU('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SUDOKU.M with the given input arguments.
%
%      SUDOKU('Property','Value',...) creates a new SUDOKU or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before sudoku_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to sudoku_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help sudoku

% Last Modified by GUIDE v2.5 30-May-2009 03:18:49

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @sudoku_OpeningFcn, ...
                   'gui_OutputFcn',  @sudoku_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end

% End initialization code - DO NOT EDIT


% --- Executes just before sudoku is made visible.
function sudoku_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to sudoku (see VARARGIN)

% Choose default command line output for sudoku
global figurehandle;
figurehandle = hObject;

handles.output = hObject;

global S_user
global SpacePanel
S_user = -1;
SpacePanel =-1;


for i=1:10
    if (i==4)||(i==7)
        line ([i,i],[1, 10],'color','k','linewidth',3);
    else
        line ([i,i],[1, 10],'color','k');
    end
end

for i=1:10
     if (i==4)||(i==7)
         line ([1, 10],[i,i],'color','k','linewidth',3);
     else
         line ([1, 10],[i,i],'color','k');
     end
end

line ([0.9,0.9,10.1,10.1,0.9],[0.9,10.1,10.1,0.9,0.9],'color','k');
axis off
axis equal

load sudokulibrary

handles.sudoku = Sudoku;
handles.pt = Sudoku_pter;

global S_user

handles.hard = get(handles.radiobutton1,'Value')+get(handles.radiobutton2,'Value')*2+get(handles.radiobutton3,'Value')*3;

handles.S_complete = squeeze(Sudoku(:,:,handles.pt));
S = generate_sudoku_game(handles.S_complete,handles.hard);
S_user = S;
%global i
%global j
global fillincallback
global SpacePanel


for i = 1:9
    for j=1:9
        if S(i,j)>0
            text(i+0.33,j+0.5,num2str(S(i,j)),'FontSize',21);
        else
            SpacePanel(i,j) = uicontrol('Style','edit','Position',[i+2.69,j+0.44,.8,.8]*38,'tag',['sdkedit',num2str(i),num2str(j)]);
            fillincallback{i,j}=['global fillincallback; global SpacePanel; global S_user;  global figurehandle; tempv = get(SpacePanel(',num2str(i),',',num2str(j),'),''String'');  if ~isempty(str2num(tempv)) S_user(',num2str(i),',',num2str(j),')=str2num(tempv)-floor(str2num(tempv)/10)*10; set(SpacePanel(',num2str(i),',',num2str(j),'),''String'',num2str(str2num(tempv)-floor(str2num(tempv)/10)*10)); else set(SpacePanel(',num2str(i),',',num2str(j),'),''String'',''''); end; Checkifdone(figurehandle);'] ;
            set(SpacePanel(i,j),'string','','FontSize',20,'ForegroundColor',[0 .5 0.5],'BackgroundColor',[1 1 1],'Callback',fillincallback{i,j});
        end
    end
end

handles.hinton = get(handles.checkbox1,'Value');
set(handles.text1,'String',[num2str(handles.pt-1),' of the 10']);
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes sudoku wait for user response (see UIRESUME)
% uiwait(handles.figure1);

    
% --- Outputs from this function are returned to the command line.
function varargout = sudoku_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global fillincallback
global SpacePanel
global S_user

for i = 1:9
    for j=1:9
        if SpacePanel(i,j)>0
            delete(SpacePanel(i,j));
        end
    end
end

fillincallback=[];
SpacePanel=[];
S_user=[];

handles.hard = get(handles.radiobutton1,'Value')+get(handles.radiobutton2,'Value')*2+get(handles.radiobutton3,'Value')*3;
handles.S_complete = squeeze(handles.sudoku(:,:,handles.pt));
S = generate_sudoku_game(handles.S_complete,handles.hard);
S_user = S;

for i = 1:9
    for j=1:9
        if S(i,j)>0
            %text(i+0.33,j+0.5,'','FontSize',22,'Color',[1 1 1]);
            text(i+0.33,j+0.5,num2str(S(i,j)),'FontSize',21,'BackgroundColor',[.925 .914 .847],'Margin',1);
        else
            SpacePanel(i,j) = uicontrol('Style','edit','Position',[i+2.69,j+0.44,.8,.8]*38,'tag',['sdkedit',num2str(i),num2str(j)]);
            fillincallback{i,j}=['global fillincallback; global SpacePanel; global S_user;  global figurehandle; tempv = get(SpacePanel(',num2str(i),',',num2str(j),'),''String'');  if ~isempty(str2num(tempv)) S_user(',num2str(i),',',num2str(j),')=str2num(tempv)-floor(str2num(tempv)/10)*10; set(SpacePanel(',num2str(i),',',num2str(j),'),''String'',num2str(str2num(tempv)-floor(str2num(tempv)/10)*10)); else set(SpacePanel(',num2str(i),',',num2str(j),'),''String'',''''); end; Checkifdone(figurehandle);'] ;
            set(SpacePanel(i,j),'string','','FontSize',20,'ForegroundColor',[0 .5 0.5],'BackgroundColor',[1 1 1],'Callback',fillincallback{i,j});
        end
    end
end

set(handles.text4,'String','');
guidata(hObject, handles);

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.pushbutton1,'Enable','off');
set(handles.pushbutton3,'Enable','off');
handles = generate_complete_sudoku(handles);
set(handles.pushbutton1,'Enable','on');
load sudokulibrary
handles.sudoku = Sudoku;
handles.pt = Sudoku_pter;
set(handles.text1,'String',[num2str(handles.pt-1),' of the 10']);
guidata(hObject, handles);


% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox1
handles.hinton = get(hObject,'Value');
global S_user
global SpacePanel
for i = 1:9
    for j=1:9
        if (S_user(i,j)~=handles.S_complete(i,j))&&(handles.hinton>0)&&(S_user(i,j)~=0)
            set(SpacePanel(i,j),'ForegroundColor',[1 0 0]);
        elseif (SpacePanel(i,j)>0)
            set(SpacePanel(i,j),'ForegroundColor',[0 0.5 0.5]);
        end
    end
end
guidata(hObject, handles);

% --------------------------------------------------------------------
function Help_Callback(hObject, eventdata, handles)
% hObject    handle to Help (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Sdkhelp_Callback(hObject, eventdata, handles)
% hObject    handle to Sdkhelp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
uiwait(msgbox('Visit http://en.wikipedia.org/wiki/Sudoku','Info','modal'));

% --------------------------------------------------------------------
function About_Callback(hObject, eventdata, handles)
% hObject    handle to About (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
uiwait(msgbox('Author: Zhen Qian (zhqian@gmail.com) June 2009','Info','modal'));



% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton3


Contact us at files@mathworks.com