Matlab GUI 数独游戏

利用Matlab GUI 实现数独游戏,由于还是matlab小白,目前只能实现最简单的功能。

第一步:在命令行输入guide,利用拖动控件的方式设计GUI界面。用到控件有81个可编辑文本、3个按钮、6个单选按钮(因为我只做了6关,所以是6个单选按钮,根据需要游戏关数也可以自己增加)和1个按钮组。
第二步:编写显示数独题目的barrier函数、显示结果的result函数、以及清空文本的clearout函数。
第三步:编写单选按钮组、开始按钮、完成按钮及退出按钮的回调函数。

数独游戏界面如下图:
在这里插入图片描述
第一关数独游戏题目如下图:
在这里插入图片描述
第一关数独游戏结果如下图:
填写数字后,点击完成按钮,数字为蓝色表示填写正确,数字为红色表示填写错误。
在这里插入图片描述
由于上传不了文件(需要所有程序的可以私信,免费发送),每关的程序都是固定模板,以第六关为例(可编辑文本edit正常应该是1…81,因为我设计界面时删除了一部分,所以程序里出现的是edit1、14、15…93):
显示数独题目的barrier函数

function barrier6 ( hObject, eventdata, handles )
% 第六关题目
clearout(hObject, eventdata, handles );
set(handles.edit1,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit14,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit15,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit16,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit17,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit18,'string','6','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit19,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit20,'string','4','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit21,'string','3','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit22,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit23,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit24,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit25,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit26,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit27,'string','7','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit28,'string','1','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit29,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit30,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit31,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit32,'string','2','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit33,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit34,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit35,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit36,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit37,'string','9','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit38,'string','8','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit39,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit40,'string','4','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit41,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit42,'string','2','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit43,'string','7','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit44,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit45,'string','1','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit46,'string','3','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit47,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit48,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit49,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit50,'string','1','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit51,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit52,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit53,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit54,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit55,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit56,'string','7','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit57,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit58,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit59,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit60,'string','6','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit61,'string','4','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit62,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit63,'string','9','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit64,'string','2','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit65,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit66,'string','5','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit67,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit68,'string','9','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit69,'string','8','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit70,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit71,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit72,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit73,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit74,'string','3','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit75,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit76,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit77,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit78,'string','5','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit79,'string','3','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit80,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit81,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit82,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit83,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit84,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit85,'string','7','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit86,'string','3','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit87,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit88,'string','1','FontSize',20,'FontWeight','bold','ForegroundColor','k');
set(handles.edit89,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit90,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit91,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit92,'string','','FontSize',14,'FontWeight','normal');
set(handles.edit93,'string','','FontSize',14,'FontWeight','normal');
end

显示结果的result函数

function result6 (hObject, eventdata, handles )
% 第六关结果 
a=[9 8 7 2 1 6 5 4 3 5 4 3 9 8 7 1 6 2 6 2 1 5 4 3 9 8 7 4 5 2 7 6 1 3 9 8 3 1 9 8 2 5 6 7 4 8 7 6 4 3 9 2 1 5 2 9 8 6 5 4 7 3 1 1 6 5 3 7 8 4 2 9 7 3 4 1 9 2 8 5 6];
a0=[0 0 0 0 0 6 0 4 3 0 0 0 0 0 7 1 0 0 0 2 0 0 0 0 9 8 0 4 0 2 7 0 1 3 0 0 0 1 0 0 0 0 0 7 0 0 0 6 4 0 9 2 0 5 0 9 8 0 0 0 0 3 0 0 0 5 3 0 0 0 0 0 7 3 0 1 0 0 0 0 0];
b=a-a0; %a0为题目,a为答案
n=1;
for m=1:81
    e=find(b>0); %要填的数字的位置
if b(m)>0
    c(n)=b(m); %要填的数字
    n=n+1;
end
end
d1=size(c,2); %要填的数字个数
d=81-d1; %题目的数字个数

i=0; %累计正确的数字
j=0; %累计填写的数字
guess1 = str2num(get(handles.edit1,'String'));
if guess1 == a(1)
    i=i+1;
elseif guess1 > 0
    j=j+1;
end
guess2 = str2num(get(handles.edit14,'String'));
if guess2 == a(2)
    i=i+1;
elseif guess2 > 0
    j=j+1;
end
guess3 = str2num(get(handles.edit15,'String'));
if guess3 == a(3)
    i=i+1;
elseif guess3 > 0
    j=j+1;
end
guess4 = str2num(get(handles.edit16,'String'));
if guess4 == a(4)
    i=i+1;
elseif guess4 > 0
    j=j+1;
end
guess5 = str2num(get(handles.edit17,'String'));
if guess5 == a(5)
    i=i+1;
elseif guess5 > 0
    j=j+1;
end
guess6 = str2num(get(handles.edit18,'String'));
if guess6 == a(6)
    i=i+1;
elseif guess6 > 0
    j=j+1;
end
guess7 = str2num(get(handles.edit19,'String'));
if guess7 == a(7)
    i=i+1;
elseif guess7 > 0
    j=j+1;
end
guess8 = str2num(get(handles.edit20,'String'));
if guess8 == a(8)
    i=i+1;
elseif guess8 > 0
    j=j+1;
end        
guess9 = str2num(get(handles.edit21,'String'));
if guess9 == a(9)
    i=i+1;
elseif guess9 > 0
    j=j+1;
end
guess10 = str2num(get(handles.edit22,'String'));
if guess10 == a(10)
    i=i+1;
elseif guess10 > 0
    j=j+1;
end
guess11 = str2num(get(handles.edit23,'String'));
if guess11 == a(11)
    i=i+1;
elseif guess11 > 0
    j=j+1;
end        
guess12 = str2num(get(handles.edit24,'String'));
if guess12 == a(12)
    i=i+1;
elseif guess12 > 0
    j=j+1;
end 
guess13 = str2num(get(handles.edit25,'String'));
if guess13 == a(13)
    i=i+1;
elseif guess13 > 0
    j=j+1;
end
guess14 = str2num(get(handles.edit26,'String'));
if guess14 == a(14)
    i=i+1;
elseif guess14 > 0
    j=j+1;
end
guess15 = str2num(get(handles.edit27,'String'));
if guess15 == a(15)
    i=i+1;
elseif guess15 > 0
    j=j+1;
end
guess16 = str2num(get(handles.edit28,'String'));
if guess16 == a(16)
    i=i+1;
elseif guess16 > 0
    j=j+1;
end        
guess17 = str2num(get(handles.edit29,'String'));
if guess17 == a(17)
    i=i+1;
elseif guess17 > 0
    j=j+1;
end        
guess18 = str2num(get(handles.edit30,'String'));
if guess18 == a(18)
    i=i+1;
elseif guess18 > 0
    j=j+1;
end        
guess19 = str2num(get(handles.edit31,'String'));
if guess19 == a(19)
    i=i+1;
elseif guess19 > 0
    j=j+1;
end
guess20 = str2num(get(handles.edit32,'String'));
if guess20 == a(20)
    i=i+1;
elseif guess20 > 0
    j=j+1;
end
guess21 = str2num(get(handles.edit33,'String'));
if guess21 == a(21)
    i=i+1;
elseif guess21 > 0
    j=j+1;
end
guess22 = str2num(get(handles.edit34,'String'));
if guess22 == a(22)
    i=i+1;
elseif guess22 > 0
    j=j+1;
end        
guess23 = str2num(get(handles.edit35,'String'));
if guess23 == a(23)
    i=i+1;
elseif guess23 > 0
    j=j+1;
end        
guess24 = str2num(get(handles.edit36,'String'));
if guess24 == a(24)
    i=i+1;
elseif guess24 > 0
    j=j+1;
end        
guess25 = str2num(get(handles.edit37,'String'));
if guess25 == a(25)
    i=i+1;
elseif guess25 > 0
    j=j+1;
end 
guess26 = str2num(get(handles.edit38,'String'));
if guess26 == a(26)
    i=i+1;
elseif guess26 > 0
    j=j+1;
end
guess27 = str2num(get(handles.edit39,'String'));
if guess27 == a(27)
    i=i+1;
elseif guess27 > 0
    j=j+1;
end 
guess28 = str2num(get(handles.edit40,'String'));
if guess28 == a(28)
    i=i+1;
elseif guess28 > 0
    j=j+1;
end
guess29 = str2num(get(handles.edit41,'String'));
if guess29 == a(29)
    i=i+1;
elseif guess29 > 0
    j=j+1;
end        
guess30 = str2num(get(handles.edit42,'String'));
if guess30 == a(30)
    i=i+1;
elseif guess30 > 0
    j=j+1;
end
guess31 = str2num(get(handles.edit43,'String'));
if guess31 == a(31)
    i=i+1;
elseif guess31 > 0
    j=j+1;
end  
guess32 = str2num(get(handles.edit44,'String'));
if guess32 == a(32)
    i=i+1;
elseif guess32 > 0
    j=j+1;
end
guess33 = str2num(get(handles.edit45,'String'));
if guess33 == a(33)
    i=i+1;
elseif guess33 > 0
    j=j+1;
end        
guess34 = str2num(get(handles.edit46,'String'));
if guess34 == a(34)
    i=i+1;
elseif guess34 > 0
    j=j+1;
end        
guess35 = str2num(get(handles.edit47,'String'));
if guess35 == a(35)
    i=i+1;
elseif guess35 > 0
    j=j+1;
end 
guess36 = str2num(get(handles.edit48,'String'));
if guess36 == a(36)
    i=i+1;
elseif guess36 > 0
    j=j+1;
end
guess37 = str2num(get(handles.edit49,'String'));
if guess37 == a(37)
    i=i+1;
elseif guess37 > 0
    j=j+1;
end
guess38 = str2num(get(handles.edit50,'String'));
if guess38 == a(38)
    i=i+1;
elseif guess38 > 0
    j=j+1;
end        
guess39 = str2num(get(handles.edit51,'String'));
if guess39 == a(39)
    i=i+1;
elseif guess39 > 0
    j=j+1;
end 
guess40 = str2num(get(handles.edit52,'String'));
if guess40 == a(40)
    i=i+1;
elseif guess40 > 0
    j=j+1;
end
guess41 = str2num(get(handles.edit53,'String'));
if guess41 == a(41)
    i=i+1;
elseif guess41 > 0
    j=j+1;
end    
guess42 = str2num(get(handles.edit54,'String'));
if guess42 == a(42)
    i=i+1;
elseif guess42 > 0
    j=j+1;
end
guess43 = str2num(get(handles.edit55,'String'));
if guess43 == a(43)
    i=i+1;
elseif guess43 > 0
    j=j+1;
end        
guess44 = str2num(get(handles.edit56,'String'));
if guess44 == a(44)
    i=i+1;
elseif guess44 > 0
    j=j+1;
end    
guess45 = str2num(get(handles.edit57,'String'));
if guess45 == a(45)
    i=i+1;
elseif guess45 > 0
    j=j+1;
end
guess46 = str2num(get(handles.edit58,'String'));
if guess46 == a(46)
    i=i+1;
elseif guess46 > 0
    j=j+1;
end
guess47 = str2num(get(handles.edit59,'String'));
if guess47 == a(47)
    i=i+1;
elseif guess47 > 0
    j=j+1;
end        
guess48 = str2num(get(handles.edit60,'String'));
if guess48 == a(48)
    i=i+1;
elseif guess48 > 0
    j=j+1;
end
guess49 = str2num(get(handles.edit61,'String'));
if guess49 == a(49)
    i=i+1;
elseif guess49 > 0
    j=j+1;
end 
guess50 = str2num(get(handles.edit62,'String'));
if guess50 == a(50)
    i=i+1;
elseif guess50 > 0
    j=j+1;
end
guess51 = str2num(get(handles.edit63,'String'));
if guess51 == a(51)
    i=i+1;
elseif guess51 > 0
    j=j+1;
end        
guess52 = str2num(get(handles.edit64,'String'));
if guess52 == a(52)
    i=i+1;
elseif guess52 > 0
    j=j+1;
end        
guess53 = str2num(get(handles.edit65,'String'));
if guess53 == a(53)
    i=i+1;
elseif guess53 > 0
    j=j+1;
end   
guess54 = str2num(get(handles.edit66,'String'));
if guess54 == a(54)
    i=i+1;
elseif guess54 > 0
    j=j+1;
end
guess55 = str2num(get(handles.edit67,'String'));
if guess55 == a(55)
    i=i+1;
elseif guess55 > 0
    j=j+1;
end 
guess56 = str2num(get(handles.edit68,'String'));
if guess56 == a(56)
    i=i+1;
elseif guess56 > 0
    j=j+1;
end
guess57 = str2num(get(handles.edit69,'String'));
if guess57 == a(57)
    i=i+1;
elseif guess57 > 0
    j=j+1;
end        
guess58 = str2num(get(handles.edit70,'String'));
if guess58 == a(58)
    i=i+1;
elseif guess58 > 0
    j=j+1;
end        
guess59 = str2num(get(handles.edit71,'String'));
if guess59 == a(59)
    i=i+1;
elseif guess59 > 0
    j=j+1;
end        
guess60 = str2num(get(handles.edit72,'String'));
if guess60 == a(60)
    i=i+1;
elseif guess60 > 0
    j=j+1;
end   
guess61 = str2num(get(handles.edit73,'String'));
if guess61 == a(61)
    i=i+1;
elseif guess61 > 0
    j=j+1;
end
guess62 = str2num(get(handles.edit74,'String'));
if guess62 == a(62)
    i=i+1;
elseif guess62 > 0
    j=j+1;
end
guess63 = str2num(get(handles.edit75,'String'));
if guess63 == a(63)
    i=i+1;
elseif guess63 > 0
    j=j+1;
end        
guess64 =str2num(get(handles.edit76,'String'));
if guess64 == a(64)
    i=i+1;
elseif guess64 > 0
    j=j+1;
end        
guess65 = str2num(get(handles.edit77,'String'));
if guess65 == a(65)
    i=i+1;
elseif guess65 > 0
    j=j+1;
end        
guess66 = str2num(get(handles.edit78,'String'));
if guess66 == a(66)
    i=i+1;
elseif guess66 > 0
    j=j+1;
end
guess67 = str2num(get(handles.edit79,'String'));
if guess67 == a(67)
    i=i+1;
elseif guess67 > 0
    j=j+1;
end
guess68 = str2num(get(handles.edit80,'String'));
if guess68 == a(68)
    i=i+1;
elseif guess68 > 0
    j=j+1;
end
guess69 = str2num(get(handles.edit81,'String'));
if guess69 == a(69)
    i=i+1;
elseif guess69 > 0
    j=j+1;
end
guess70 = str2num(get(handles.edit82,'String'));
if guess70 == a(70)
    i=i+1;
elseif guess70 > 0
    j=j+1;
end        
guess71 = str2num(get(handles.edit83,'String'));
if guess71 == a(71)
    i=i+1;
elseif guess71 > 0
    j=j+1;
end  
guess72 =str2num(get(handles.edit84,'String'));
if guess72 == a(72)
    i=i+1;
elseif guess72 > 0
    j=j+1;
end
guess73 =str2num(get(handles.edit85,'String'));
if guess73 == a(73)
    i=i+1;
elseif guess73 > 0
    j=j+1;
end        
guess74 =str2num(get(handles.edit86,'String'));
if guess74 == a(74)
    i=i+1;
elseif guess74 > 0
    j=j+1;
end        
guess75 = str2num(get(handles.edit87,'String'));
if guess75 == a(75)
    i=i+1;
elseif guess75 > 0
    j=j+1;
end        
guess76 = str2num(get(handles.edit88,'String'));
if guess76 == a(76)
    i=i+1;
elseif guess76 > 0
    j=j+1;
end    
guess77 = str2num(get(handles.edit89,'String'));
if guess77 == a(77)
    i=i+1;
elseif guess77 > 0
    j=j+1;
end
guess78 =str2num(get(handles.edit90,'String'));
if guess78 == a(78)
    i=i+1;
elseif guess78 > 0
    j=j+1;
end
guess79 = str2num(get(handles.edit91,'String'));
if guess79 == a(79)
    i=i+1;
elseif guess79 > 0
    j=j+1;
end
guess80 = str2num(get(handles.edit92,'String'));
if guess80 == a(80)
    i=i+1;
elseif guess80 > 0
    j=j+1;
end
guess81 = str2num(get(handles.edit93,'String'));
if guess81 == a(81)
    i=i+1;
elseif guess81 > 0
    j=j+1;
end

if i==d && j==0
    h = msgbox('请填写数字!');
    uiwait(h);
elseif i<d  
    h = msgbox('请不要改题目!');
    uiwait(h);
    clearout(hObject, eventdata, handles );
elseif i==81  %填写的数字全部正确
    h = msgbox('顺利过关!');
    uiwait(h);
    clearout(hObject, eventdata, handles );
elseif j>=0   %=0时表示填写的数字正确,>0时表示填写的数字有错误,并将错误的数字改正确
    if guess1 ~= c(1)
        set(handles.edit1,'string','9','ForegroundColor','r');
    end
    if guess2 ~= c(2)
        set(handles.edit14,'string','8','ForegroundColor','r');
    end
    if guess3 ~= c(3)
        set(handles.edit15,'string','7','ForegroundColor','r');
    end
    if guess4 ~= c(4)
        set(handles.edit16,'string','2','ForegroundColor','r');
    end
    if guess5 ~= c(5)
        set(handles.edit17,'string','1','ForegroundColor','r');
    end
    if guess7 ~= c(6)
        set(handles.edit19,'string','5','ForegroundColor','r');
    end
    if guess10 ~= c(7)
        set(handles.edit22,'string','5','ForegroundColor','r');
    end
    if guess11 ~= c(8)
        set(handles.edit23,'string','4','ForegroundColor','r');
    end
    if guess12 ~= c(9)
        set(handles.edit24,'string','3','ForegroundColor','r');
    end
    if guess13 ~= c(10)
        set(handles.edit25,'string','9','ForegroundColor','r');
    end
    if guess14 ~= c(11)
        set(handles.edit26,'string','8','ForegroundColor','r');
    end
    if guess17 ~= c(12)
        set(handles.edit29,'string','6','ForegroundColor','r');
    end
    if guess18 ~= c(13)
        set(handles.edit30,'string','2','ForegroundColor','r');
    end
    if guess19 ~= c(14)
        set(handles.edit31,'string','6','ForegroundColor','r');
    end
    if guess21 ~= c(15)
        set(handles.edit33,'string','1','ForegroundColor','r');
    end
    if guess22 ~= c(16)
        set(handles.edit34,'string','5','ForegroundColor','r');
    end
    if guess23 ~= c(17)
        set(handles.edit35,'string','4','ForegroundColor','r');
    end
    if guess24 ~= c(18)
        set(handles.edit36,'string','3','ForegroundColor','r');
    end
    if guess27 ~= c(19)
        set(handles.edit39,'string','7','ForegroundColor','r');
    end
    if guess29 ~= c(20)
        set(handles.edit41,'string','5','ForegroundColor','r');
    end
    if guess32 ~= c(21)
        set(handles.edit44,'string','6','ForegroundColor','r');
    end
    if guess35 ~= c(22)
        set(handles.edit47,'string','9','ForegroundColor','r');
    end
    if guess36 ~= c(23)
        set(handles.edit48,'string','8','ForegroundColor','r');
    end
    if guess37 ~= c(24)
        set(handles.edit49,'string','3','ForegroundColor','r');
    end
    if guess39 ~= c(25)
        set(handles.edit51,'string','9','ForegroundColor','r');
    end
    if guess40 ~= c(26)
        set(handles.edit52,'string','8','ForegroundColor','r');
    end
    if guess41 ~= c(27)
        set(handles.edit53,'string','2','ForegroundColor','r');
    end
    if guess42 ~= c(28)
        set(handles.edit54,'string','5','ForegroundColor','r');
    end
    if guess43 ~= c(29)
        set(handles.edit55,'string','6','ForegroundColor','r');
    end
    if guess45 ~= c(30)
        set(handles.edit57,'string','4','ForegroundColor','r');
    end
    if guess46 ~= c(31)
        set(handles.edit58,'string','8','ForegroundColor','r');
    end
    if guess47 ~= c(32)
        set(handles.edit59,'string','7','ForegroundColor','r');
    end
    if guess50 ~= c(33)
        set(handles.edit62,'string','3','ForegroundColor','r');
    end
    if guess53 ~= c(34)
        set(handles.edit65,'string','1','ForegroundColor','r');
    end
    if guess55 ~= c(35)
        set(handles.edit67,'string','2','ForegroundColor','r');
    end
    if guess58 ~= c(36)
        set(handles.edit70,'string','6','ForegroundColor','r');
    end
    if guess59 ~= c(37)
        set(handles.edit71,'string','5','ForegroundColor','r');
    end
    if guess60 ~= c(38)
        set(handles.edit72,'string','4','ForegroundColor','r');
    end
    if guess61 ~= c(39)
        set(handles.edit73,'string','7','ForegroundColor','r');
    end
    if guess63 ~= c(40)
        set(handles.edit75,'string','1','ForegroundColor','r');
    end
    if guess64 ~= c(41)
        set(handles.edit76,'string','1','ForegroundColor','r');
    end
    if guess65 ~= c(42)
        set(handles.edit77,'string','6','ForegroundColor','r');
    end  
    if guess68 ~= c(43)
        set(handles.edit80,'string','7','ForegroundColor','r');
    end 
    if guess69 ~= c(44)
        set(handles.edit81,'string','8','ForegroundColor','r');
    end
    if guess70 ~= c(45)
        set(handles.edit82,'string','4','ForegroundColor','r');
    end    
    if guess71 ~= c(46)
        set(handles.edit83,'string','2','ForegroundColor','r'); 
    end
    if guess72 ~= c(47)
        set(handles.edit84,'string','9','ForegroundColor','r');
    end
    if guess75 ~= c(48)
        set(handles.edit87,'string','4','ForegroundColor','r');
    end
    if guess77 ~= c(49)
        set(handles.edit89,'string','9','ForegroundColor','r');
    end
    if guess78 ~= c(50)
        set(handles.edit90,'string','2','ForegroundColor','r');
    end
    if guess79 ~= c(51)
        set(handles.edit91,'string','8','ForegroundColor','r');
    end
    if guess80 ~= c(52)
        set(handles.edit92,'string','5','ForegroundColor','r');
    end
    if guess81 ~= c(53)
        set(handles.edit93,'string','6','ForegroundColor','r');
    end    

    h = msgbox('请继续努力!');
    uiwait(h);
    clearout(hObject, eventdata, handles );
end

end

清空可编辑文本的clearout函数

function clearout(hObject, eventdata, handles )
% 清空
% 'BacbgroundColor','w'表示可输入文本框的背景颜色改为白色
% 'FontWeight','normal'表示字体正常
set(handles.edit1,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit14,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit15,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit16,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit17,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit18,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit19,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit20,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit21,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit22,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit23,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit24,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit25,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit26,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit27,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit28,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit29,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit30,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit31,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit32,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit33,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit34,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit35,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit36,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit37,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit38,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit39,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit40,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit41,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit42,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit43,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit44,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit45,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit46,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit47,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit48,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit49,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit50,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit51,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit52,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit53,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit54,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit55,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit56,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit57,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit58,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit59,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit60,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit61,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit62,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit63,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit64,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit65,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit66,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit67,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit68,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit69,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit70,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit71,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit72,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit73,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit74,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit75,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit76,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit77,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit78,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit79,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit80,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit81,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit82,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit83,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit84,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit85,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit86,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit87,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit88,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit89,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit90,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit91,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit92,'string','','ForegroundColor','b','FontWeight','normal');
set(handles.edit93,'string','','ForegroundColor','b','FontWeight','normal');
end

单选按钮组回调函数下面的程序:

global how
str=get(hObject,'tag');
switch str
    case 'radiobutton1'
        how=1;
    case 'radiobutton2'
        how=2;
    case 'radiobutton3'
        how=3;
    case 'radiobutton4'
        how=4;      
    case 'radiobutton5'
        how=5;       
    case 'radiobutton6'
        how=6;            
end

开始按钮回调函数下面的程序:

global how
if how == 1
    barrier1( hObject, eventdata, handles )
elseif how == 2
    barrier2( hObject, eventdata, handles )  
elseif how == 3
    barrier3( hObject, eventdata, handles )      
elseif how == 4
    barrier4( hObject, eventdata, handles )       
elseif how == 5
    barrier5( hObject, eventdata, handles )
elseif how == 6
    barrier6( hObject, eventdata, handles )    
end

完成按钮回调函数下面的程序:

global how
if how == 1
    result1(hObject, eventdata, handles )
elseif how == 2
    result2(hObject, eventdata, handles )   
elseif how == 3
    result3(hObject, eventdata, handles )       
elseif how == 4
    result4(hObject, eventdata, handles )       
elseif how == 5
    result5(hObject, eventdata, handles )
elseif how == 6
    result6(hObject, eventdata, handles )     
end

退出按钮回调函数下面的程序:

close(gcf);%退出界面

参考文献:
编写一个简单的GUI
单选按钮和按钮组

  • 20
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 52
    评论
以下是一个简单的 MATLAB 数独游戏设计实现。 1. 界面设计 ```matlab function sudoku_gui % 创建主窗口 f = figure('Name', '数独游戏', 'NumberTitle', 'off', 'MenuBar', 'none', ... 'ToolBar', 'none', 'Position', [200, 200, 450, 500]); % 创建数独面板 sudoku_panel = uipanel('Title', '数独', 'Position', [0.05, 0.3, 0.9, 0.6], 'Parent', f); % 创建按钮面板 button_panel = uipanel('Title', '操作', 'Position', [0.05, 0.05, 0.9, 0.2], 'Parent', f); % 创建数独矩阵 sudoku = zeros(9, 9); % 创建数独单元格 cells = create_cells(sudoku_panel); % 创建按钮 uicontrol('Style', 'pushbutton', 'String', '生成数独', 'Position', [20, 20, 100, 30], ... 'Callback', @(src, event) generate_sudoku(cells), 'Parent', button_panel); uicontrol('Style', 'pushbutton', 'String', '清空数独', 'Position', [140, 20, 100, 30], ... 'Callback', @(src, event) clear_sudoku(cells), 'Parent', button_panel); uicontrol('Style', 'pushbutton', 'String', '求解数独', 'Position', [260, 20, 100, 30], ... 'Callback', @(src, event) solve_sudoku(cells), 'Parent', button_panel); end % 创建数独单元格 function cells = create_cells(parent) cells = zeros(9, 9); for i = 1:9 for j = 1:9 cells(i, j) = uicontrol('Style', 'edit', 'String', '', 'Position', ... [50 * (j - 1) + 10, 50 * (9 - i) + 10, 40, 40], ... 'HorizontalAlignment', 'center', 'FontSize', 16, 'Enable', 'off', ... 'BackgroundColor', 'white', 'Parent', parent); end end end ``` 2. 数独生成 ```matlab % 生成数独 function generate_sudoku(cells) % 清空数独单元格 clear_sudoku(cells); % 随机选择一些格子填上初始数字 nums = randperm(9, 6); for k = 1:length(nums) n = nums(k); i = randi([1, 9]); j = randi([1, 9]); while ~isempty(get(cells(i, j), 'String')) i = randi([1, 9]); j = randi([1, 9]); end set(cells(i, j), 'String', num2str(n), 'Enable', 'off'); end % 求解数独 sudoku = read_sudoku(cells); sudoku = solveSudoku(sudoku); % 将求解结果填入数独单元格 fill_sudoku(cells, sudoku); end % 读取数独单元格中的数独矩阵 function sudoku = read_sudoku(cells) sudoku = zeros(9, 9); for i = 1:9 for j = 1:9 str = get(cells(i, j), 'String'); if ~isempty(str) sudoku(i, j) = str2double(str); end end end end % 将数独矩阵填入数独单元格 function fill_sudoku(cells, sudoku) for i = 1:9 for j = 1:9 if sudoku(i, j) > 0 set(cells(i, j), 'String', num2str(sudoku(i, j)), 'Enable', 'off'); end end end end ``` 3. 清空数独 ```matlab % 清空数独 function clear_sudoku(cells) for i = 1:9 for j = 1:9 set(cells(i, j), 'String', '', 'Enable', 'on'); end end end ``` 4. 求解数独 ```matlab % 求解数独 function solve_sudoku(cells) % 读取数独单元格中的数独矩阵 sudoku = read_sudoku(cells); % 求解数独 sudoku = solveSudoku(sudoku); % 将求解结果填入数独单元格 fill_sudoku(cells, sudoku); end ``` 最后,运行 `sudoku_gui` 函数即可打开数独游戏界面。
评论 52
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值