Matlab编写的猜数字游戏.doc
Matlab编写的猜数字游戏 一个猜数字的游戏,先随机生成一个1到100之间的整数,由游戏者来猜此数,当输入的数过大,会提示数字大了,当输入的数过小,会提示数字小了,当猜中此数,游戏结束。好玩而已,呵呵,欢迎共同学习,email:slqinyifunction caishuzi(arg)if nargin < 1arg = 'newgame'endif strcmp(arg,'newgame')clf% set(gcf,'resize','off'); % 禁止调整窗口的大小set(gcf,'name','猜数字 Version 1.0') % 修改标题栏set(gcf,'numbertitle','off') % 去掉标题栏中的 figure 1num = randint(1,1,100); % 随机产生个一小于100的正整数h_casu = uicontrol(gcf,'style','edit',.'unit','normalized','position',0.8,0.75,0.08,0.1,.'BackgroundColor',1,1,1,'ForegroundColor',0 0 1,.'fontsize',12,'visible','off','string',num2str(num),'tag','t_casu'); h_suru = uicontrol(gcf,'style','edit',.'unit','normalized','position',0.8,0.75,0.08,0.1,.'BackgroundColor',1,1,1,'ForegroundColor',0 0 1,.'fontsize',12,'tag','t_suru');h_jisu = uicontrol(gcf,'style','edit',.'unit','normalized','position',0.75,0.75,0.08,0.1,.'BackgroundColor',1,1,1,'ForegroundColor',0 0 1,.'fontsize',12,'visible','off','string',num2str(0),'tag','t_jisu');wz_string = '请输入一个整数(1100):'h_wenz = uicontrol(gcf,'style','text',.'unit','normalized','position',0.12,0.75,0.65,0.1,.'BackgroundColor',1,1,1,'ForegroundColor',0 0 1,.'fontsize',24,'string',wz_string,'tag','t_wenz');cs_string = '您已输入的次数为 0 次'h_cisu = uicontrol(gcf,'style','text',.'unit','normalized','position',0.12,0.6,0.65,0.1,.'BackgroundColor',1,1,1,'ForegroundColor',0 0 1,.'fontsize',24,'string',cs_string,'tag','t_cisu');h_newg = uicontrol(gcf,'style','pushbutton',.'unit','normalized','position',0.34,0.15,0.17,0.1,.'BackgroundColor',1,1,1,'ForegroundColor',0 0 1,.'fontsize',24,'string','新 局','callback','caishuzi(''newgame'')','tag','t_newg');h_tiji = uicontrol(gcf,'style','pushbutton',.'unit','normalized','position',0.54,0.15,0.17,0.1,.'BackgroundColor',1,1,1,'ForegroundColor',0 0 1,.'fontsize',24,'string','提 交','callback','caishuzi(''submit'')','tag','t_tiji');h_tuci = uicontrol(gcf,'style','pushbutton',.'unit','normalized','position',0.74,0.15,0.17,0.1,.'BackgroundColor',1,1,1,'ForegroundColor',0 0 1,.'fontsize',24,'string','退 出','callback','close','tag','t_tucu');elseif strcmp(arg,'sub mit')h_casu = findobj(gcf,'tag','t_casu');h_suru = findobj(gcf,'tag','t_suru');h_jisu = findobj(gcf,'tag','t_jisu');h_cisu = findobj(gcf,'tag','t_cisu');h_tiji = findobj(gcf,'tag','t_tiji');num = str2num(get(h_casu,'string');ges = str2num(get(h_suru,'string');jis = str2num(get(h_jisu,'string');jis = jis+1;set(h_jisu,'string',num2str(jis)cs_string = '您已输入的次数为' num2str(jis) '次'set(h_cisu,'string',cs_string);h_jieg = uicontrol(gcf,'style','text',.'unit','normalized','position',0.12,0.45,0.65,0.1,.'BackgroundColor',1,1,1,'ForegroundColor',0 0 1,.'fontsize',24,'visible','off','tag','t_jieg');if ges=numjg_string = '恭喜您!您答对了!'set(h_jieg,'string',jg_string,'visible','on');set(h_tiji,'enable','off')elseif ges<numjg_string = '您输入的数有点小哦!'set(h_jieg,'string',jg_string,'visible','on');elsejg_string = '您输入的数有点大哦!'set(h_jieg,'string',jg_string,'visible','on');endend5 的 数);游&#, & # , & ;#, (; !点数入 ) # #; #, , & & (;&小有输 & ; #;#,#& ;# () & #; ;#, (; !对您;#& = )# # ; ;# # # & ,& ;. # # ;.,0 0.;& #; ;& . & & ;& ) ; ;& _;&次#)( #为入已 #= ) # & + =);# ;& )# ;#,_ ( );# &, ( ) ;& #; & );& & ;# ) & , & = ) & _ , ;#&( = ) # _ ,; # ( ; & ; , );# , & #; & , # & & #, . 0; ;& , ;.,. 0 ,. ;& ,& ;. ;&& & ( );# ;, &#&#; & &# & # ;#;# & #,;# . , & . ,. ,., # & #,& .,;& , # ) & _ & ;#)#; ;& ( ; & & & , # ;#,;# ., ; & , ;.,. 0 0.0 ;# & ; ;& . && &, )# _ # & & _,; # #& ;., # #, # ., 0 0; ;& # & & . & & & ;# ( =; 次 次入; ); , &#, _ ;# , & . 0, # , . 0 . 0 & &,# # & . & & # ; :0 数一输#= ) & # , # & ; ;#, .,0;# &, ;. . 0.0 # # ;# # ., , # );& _ , ;# ; & ., 0; & ,;& ;. 0,0;# , & , . # & & ( ) ;& ;& ;# # &#; ;#,;# . 0,#& &, & ; . 0 .0,# # & , # . # & ;# =整整0一生 0, ( 栏掉 )# && # 题改修) 0. 字;#, ; & 大的调禁 ) ;&# # ( ; & ; # . 同欢呵,束戏,中了数提过入当字提大数输此者戏整间0个一先,字一