matlab 版 数独小游戏 GUI界面设计

近期,由于各种原因,接触到了matlab版的数独小游戏,需要做GUI界面。由于之前本科的时候自己也做过简单的界面涉及,就以为很简单,结果,piapia打脸。数独中的数字是在table中显示的,为了将题目数字,作答数字、答案数字和解答数字区分开,需要设置不同的颜色,但是网上相关资料太少,所以这个界面设计过程很痛苦,现将个人经验分享于此。

首先是数独游戏的解法,这个网上一搜一大堆,本文所用程序与如下网址中所述一致。

https://www.bilibili.com/video/BV1Yx411V7tE

数独生成部分没使用算法自动生成,而是根据网上资料手动输入了30组数独题,放到了data.txt中,30*81,其中一行为一个数独题目,使用时将其中一行reshape成9*9矩阵即可。将数独题目分成简单、一般、困难三种模式,根据slider的值进行判断,slider的值在0-1之间,0-0.33时,对应简单模式,0.33-0.66对应一般模式,0.66-1对应困难模式。生成数独时,根据slider的值找到对应模式,然后在对应模式中随机抽取题目即可。

然后就是比较关心的不同字号显示问题了,这里用到了html,举一个栗子~

CurCell{row,col}=['

','',num2str(CurMat(row,col)), '

CurCell是cell类型的数据,用于在table中显示,row和col分别对应其行数和列数,bgcolor="white" 用来设置背景颜色,text-align:center设置居中显示,FONT face="Times New Roman"设置新罗马字体,size="25",设置显示的字号为25,color=green"用来显示字体颜色为绿色,这里是手动输入的字体颜色。

其实懂了就很简单了,但是由于网上资料较少,所以找了很久也没有找到,所以,就想分享给有需要的人们呦。

代码如下,因为涉及到UI界面的tag等,所以如果想直接运行也可直接下载我的matlab程序哦

https://download.csdn.net/download/Huihui_Lv/12578477

%主程序,用来运行:

function varargout = ShuduLJY(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ShuduLJY_OpeningFcn, ...
                   'gui_OutputFcn',  @ShuduLJY_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

 

fnction ShuduLJY_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;guidata(hObject, handles);

 

function varargout = ShuduLJY_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;

%slider滑窗程序
function Slider_Callback(hObject, eventdata, handles)
SliderValue = get(handles.Slider,'Value'); % 滑窗取值范围,0-1之间

function Slider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% 创建按钮

function Creat_Callback(hObject, eventdata, handles)
% hObject    handle to Creat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
A = importdata('data.txt');
number = floor(size(A,1)/3);
x=randperm(number,1);

SliderValue = get(handles.Slider,'Value');
if isempty(SliderValue)
    SliderValue = 0;
end
if SliderValue<0.33
    B = A(x,:);
elseif SliderValue<0.66
    B = A(x+number,:);
else
    B = A(x+2*number,:);
end
global matrix CurMat CurCell;
matrix = reshape(B,9,9);
CurMat = matrix;
for i = 1:9
    for j = 1:9
        if matrix(i,j)==0
            CurCell{i,j}=['

','','', '

         else
            CurCell{i,j}=['

','',num2str(matrix(i,j)), '

set(handles.uitable1,'Data',CurCell);


%复位按钮
function Reset_Callback(hObject, eventdata, handles)
% hObject    handle to Reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global matrix CurCell CurMat matrix;
CurMat = matrix;
for i = 1:9
    for j = 1:9
        if matrix(i,j)==0
            CurCell{i,j}=['

','','', '

        else
            CurCell{i,j}=['

','',num2str(matrix(i,j)), '

        end
    end
end
set(handles.uitable1,'Data',CurCell);

%答案按钮

function Answer_Callback(hObject, eventdata, handles)
% hObject    handle to Answer (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global matrix AnsMat CurMat CurCell;
solve(matrix,1);
CurMat = AnsMat;
% CurCell = num2cell(AnsMat);
Error = AnsMat-matrix;
[row,col] = find(Error);
for i = 1 : length(row)
    CurCell{row(i),col(i)}=['

','',num2str(CurMat(row(i),col(i))), '

end
set(handles.uitable1,'Data',CurCell);
 

%更正按钮

function Modify_Callback(hObject, eventdata, handles)
% hObject    handle to Modify (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global matrix AnsMat CurMat CurCell;
    solve(matrix,1);
% data2 = get(handles.uitable1,'Data');
if (CurMat==matrix)
    hs = msgbox('请先输入您的答案,再进行更正!')
    ht = findobj(hs, 'Type', 'text');
    set(ht, 'FontSize', 14, 'Unit', 'normal');
    set(hs, 'Position', [500 300 300 100]);
%     set(hs,'resize','on')
elseif (CurMat==AnsMat)
    hs = msgbox('恭喜您全部答对了!')
     ht = findobj(hs, 'Type', 'text');
    set(ht, 'FontSize', 14, 'Unit', 'normal');
    set(hs, 'Position', [500 300 300 100]);
else
    hs = msgbox('红色为答错部分,下次继续努力!')
    ht = findobj(hs, 'Type', 'text');
    set(ht, 'FontSize', 14, 'Unit', 'normal');
    set(hs, 'Position', [500 300 300 100]);
    
    Error = AnsMat-CurMat;
    [row,col] = find(Error);
for i = 1 : length(row)
    CurCell{row(i),col(i)}=['

','',num2str(AnsMat(row(i),col(i))), '

end
set(handles.uitable1,'Data',CurCell);

   
end

 

%uitable的cellselection函数

function uitable1_CellSelectionCallback(hObject, eventdata, handles)
global matrix CurMat CurCell;
if ~isempty(eventdata.Indices)
    row = eventdata.Indices(1);
    row_t = fix((row-1)/3)+1;
    col = eventdata.Indices(2);
    col_t = fix((col-1)/3)+1;
    if matrix(row,col) ~= 0
        hs = msgbox('无法修改');
        ht = findobj(hs, 'Type', 'text');
        set(ht, 'FontSize', 14, 'Unit', 'normal');
        set(hs, 'Position', [500 300 300 100]);
    else 
        d = str2double(inputdlg('请输入您认为正确的数字:'));
        x = reshape(CurMat((row_t*3-2):(row_t*3),(col_t*3-2):(col_t*3)),1,9);
        D = [CurMat(row,:); CurMat(:,col)'; x];
        m = find(D==d);
        
        if (d>=0) && (d<=9) && (rem(d,1)==0)
            if ~isempty(m)
                hs = msgbox('无效数字,请注意游戏规则!');
                ht = findobj(hs, 'Type', 'text');
                set(ht, 'FontSize', 14, 'Unit', 'normal');
                set(hs, 'Position', [500 300 300 100]);
                return;
            else
                CurMat(row,col) = d;
                CurCell{row,col}=['

','',num2str(CurMat(row,col)), '

                set(handles.uitable1,'Data',CurCell);
            end
        else
            hs = msgbox('无效数字,请输入0-9的整数');
            ht = findobj(hs, 'Type', 'text');
            set(ht, 'FontSize', 14, 'Unit', 'normal');
            set(hs, 'Position', [500 300 300 100]);
            return;
        end
    end    
end

%uitable的CreateFcn函数
function uitable1_CreateFcn(hObject, eventdata, handles)
global matrix CurMat CurCell;
 matrix = zeros(9);
 CurMat = matrix;
 CurCell = num2cell(CurMat);
for i = 1:9
    for j = 1:9
        if matrix(i,j)==0
            CurCell{i,j}=['

','','', '

        else
            CurCell{i,j}=['

','',num2str(matrix(i,j)), '

        end
    end
 end
 set(hObject,'Data',CurCell);
 set(hObject,'ColumnWidth',{55,55,55,55,55,55,55,55,55})
 guidata(hObject,handles)
 

%解数独

function solve (matrix,id)
    
    if id > 81
        global AnsMat;
        AnsMat = matrix;
    else
        row = floor((id-1)/9) + 1;
        col = mod((id-1),9) + 1;
        if matrix(row,col) ~= 0
             solve(matrix,id + 1)
        else
            for num = 1 : 9
                if check_cell(matrix,row,col,num) == true
                   matrix(row,col) = num;
                   solve(matrix,id + 1)
                end
            end
        end
    end
end

 

function result = check_row(matrix,i,j,num)
    result = true;
    for col = 1:9
        if matrix(i,col) == num
            result = false;
            break;
        end
    end
end

function result = check_col(matrix,i,j,num)
    result = true;
    for row = 1:9
        if matrix(row,j) == num
            result = false;
            break;
        end
    end
end

function result = check_cell(matrix,i,j,num)
    if check_row(matrix,i,j,num) == false
        result = false;
    elseif check_col(matrix,i,j,num) == false
        result = false;
    elseif check_3_by_3(matrix,i,j,num) == false
        result = false;
    else
        result = true;
    end
end

 

function result = check_3_by_3(matrix,i,j,num)
    result = true;
    
    block_row = floor((i-1)/3)+1;
    block_col = floor((j-1)/3)+1;
    
    block_row_to = block_row * 3;
    block_row_from = block_row_to - 2;
    block_col_to = block_col * 3;
    block_col_from = block_col_to - 2;
    
    for row = block_row_from : block_row_to
        for col = block_col_from : block_col_to
            if matrix(row,col) == num
            result = false;
            break;
        end
    end
end

你可能感兴趣的:(学习资料整合)