matlab猜数字游戏代码,重庆高考作文Matlab编写的猜数字游戏触的部首

计算器使用方法-广告合同2021年1月1日发(作者:杨思仲)

Matlab编写的猜数字游戏

一个猜数字的游戏,先随机生成一个1到100之间的整

数,由游戏者来猜此数,当输入的数过大,会提

示数字大了,当输入的数过小,

会提示数字小了,当猜中此数,游戏结束。

好玩而已,呵呵,欢迎共同学习,email:slqinyi@

function

caishuzi(arg)

if nargin < 1

arg =

'newgame';

end

if

strcmp(arg,'newgame')

clf

%

set(gcf,'resize','off'); %

禁止调整窗口的大小

set(gcf,'name','猜数字

Version 1.0') % 修改标题栏

set(gcf,'numbertitle','off') %

去掉标题栏中的 figure 1

num = randint(1,1,100);

% 随机产生个一小于100的正整数

h_casu =

uicontrol(gcf,'style','edit',... <

br>'unit','normalized','positi

on',[0.8,0.75,0.08,0.1],...

'Backgroun

dColor',[1,1,1],'ForegroundColor',[0 0

1],...

'fontsize',12,'visible'

,'off','string',num2str

(num),&

#39;tag','t_casu');

h_suru =

uicontrol(gcf,'style','edit',... <

br>'unit','normalized','positi

on',[0.8,0.75,0.08,0.1],...

'Backgroun

dColor',[1,1,1],'ForegroundColor',[0 0

1],...

'fontsize',12,'tag',

9;t_suru');

h_jisu =

uicontrol(gcf,'style','edit',... <

br>'unit','normalized','positi

on',[0.75,0.75,0.08,0.1],...

'Backgrou

ndColor',[1,1,1],'ForegroundColor',[0

0 1],...

'fontsize',12,'visible

9;,'off','string',num2str

(0),&

#39;tag','t_jisu');

wz_string

= '请输入一个整数(1~100):';

h_wenz =

uicontrol(gcf,'style','text',... <

br>'unit','normalized','positi

on',[0.12,0.75,0.65,0.1],...

'Backgrou

ndColor',[1,1,1],'ForegroundColor',[0

0 1],...

'fontsize',24,'string'

;,wz_string,'tag','t_wenz

39;);

cs_string = '您已输入的次数为 0 次';

h_cisu =

uicontrol(gcf,'style','text',... <

br>'unit','normalized','positi

on',[0.12,0.6,0.65,0.1],...

'Backgroun

dColor',[1,1,1],'ForegroundColor',[0 0

1],...

'fontsize',24,'string',

cs_string,'tag','t_cisu'

;);

h_newg = uicontrol(gcf,'style',

39;pushbutton',...

'unit','nor

malized','position',[0.34,0.15,0.17,0.

1],...

'BackgroundColor',[1,1,1],'

ForegroundColor',[0 0 1],...

'fontsize',24,'string','新

','callback','caishuzi('&

#39;newgame'')',&

#39;tag','

;t_newg');

h_tiji = uicontrol(gcf,

9;style','pushbutton',...

'uni

t','normalized','position',[0.

54,0.15,0.17,0.1],...

'BackgroundColor'

;,[1,1,1],'ForegroundColor',[0 0 1],...

'fontsize',24,'string','提

','callback','caishuzi('&

#39;submit'')',

9;tag','

t_tiji');

h_tuci = uicontrol(gcf,'

;style','pushbutton',...

'unit

','normalized','position',[0.7

4,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,'s

tring',num2str(jis))

cs_string =

['您已输入的次数为' num2str(jis) '次'];

set(h_cisu,'string',cs_string);

h_jieg =

uicontrol(gcf,'style','text',... <

br>'unit','normalized','positi

on',[0.12,0.45,0.65,0.1],...

'Backgrou

ndColor',[1,1,1],'ForegroundColor',[0

0 1],...

'fontsize',24,'visible

9;,'off','tag','t_jie

g'

;);

if ges==num

jg_string =

'恭喜您!您答对了!';

set(h_jieg,'string

9;,jg_string,'visible','on');

set(h_tiji,'enable','off')

elseif ges

jg_string =

'您输入的数有点小哦!';

set(h_jieg,'string

39;,jg_string,'visible','on');

else

jg_string = '您输入的数有点大哦!';

set(h_jieg,'string',jg_string,'visibl

e','on');

end

end

补水保湿护肤品-窗外歌词

樱花白里透红-婚礼致辞

香港旅游景点大全-相爱却不能在一起

exol-固执

焦仲卿和刘兰芝-锄禾ppt

木耳菜的做法-二月二龙抬头祝福语

凉拌变蛋-三年级语文下册教学计划

无味-七十周年大阅兵观后感

function varargout = caishuzi(varargin) %CAISHUZI M-file for caishuzi.fig % CAISHUZI, by itself, creates a new CAISHUZI or raises the existing % singleton*. % % H = CAISHUZI returns the handle to a new CAISHUZI or the handle to % the existing singleton*. % % CAISHUZI('Property','Value',...) creates a new CAISHUZI using the % given property value pairs. Unrecognized properties are passed via % varargin to caishuzi_OpeningFcn. This calling syntax produces a % warning when there is an existing singleton*. % % CAISHUZI('CALLBACK') and CAISHUZI('CALLBACK',hObject,...) call the % local function named CALLBACK in CAISHUZI.M with the given input % arguments. % % *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 caishuzi % Last Modified by GUIDE v2.5 08-Mar-2008 22:02:40 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @caishuzi_OpeningFcn, ... 'gui_OutputFcn', @caishuzi_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 caishuzi is made visible. function caishuzi_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 unrecognized PropertyName/PropertyValue pairs from the % command line (see VARARGIN) % Choose default command line output for caishuzi handles.output = hObject; handles.source = rand4; %生成一个随机的四位数 handles.pc = 0; %用来保存按‘ok’键的次数,但不成功 % Update handles structure guidata(hObject, handles); % UIWAIT makes caishuzi wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = caishuzi_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; function edit_input_Callback(hObject, eventdata, handles) % hObject handle to edit_input (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit_input as text % str2double(get(hObject,'String')) returns contents of edit_input as a double handles.instr = str2double(get(hObject,'String')); %从编辑框取输入值 guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function edit_input_CreateFcn(hObject, eventdata, handles) % hObject handle to edit_input (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in pushbutton_clo. function pushbutton_clo_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_clo (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) delete(handles.figure1) %关闭界面 % --- Executes on selection change in popupmenu. function popupmenu_Callback(hObject, eventdata, handles) % hObject handle to popupmenu (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: contents = get(hObject,'String') returns popupmenu contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu handles.bushu = get(hObject,'value'); %获得指定步数限制,还未完成该功能 % --- Executes during object creation, after setting all properties. function popupmenu_CreateFcn(hObject, eventdata, handles) % hObject handle to popupmenu (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in pushbutton_ok. function pushbutton_ok_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_ok (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) A = handles.source; %A为生成的随机4位数 instr = handles.instr; %输入的4位数 handles.pc = handles.pc + 1; %按键次数 b = 0; %将输入的4位数拆开,组成矩阵B for i=1:4 B(5-i) = mod(instr,10); for j=1:4 if isequal(B(5-i),A(j)) %判断B与A是否有相同元素,并将其个数存入b b=b+1; end end instr = fix(instr/10); end %判断输入是否合法 f=0; for i=1:4 C(1:4)=B(i); if sum(C==B)>=2 set(handles.text_rez, 'string','请重新输入4位不重复数!'); f=1; break end end if f==0 %输入合法 a=sum(A==B); %A与B的元素及其位置均相同的元素个数 b = b - a; %A与B的元素相同但位置不相同的元素个数 set(handles.text_rez, 'string',['第',num2str(handles.pc),'步: ',num2str(B),' ',... num2str(a),'A',num2str(b),'B']); %显示步骤及结果 end % --- Executes on button press in pushbutton_reset. function pushbutton_reset_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_reset (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.source = rand4; %重新生成一个随机4位数 guidata(hObject, handles); % --- Executes on button press in pushbutton_ans. function pushbutton_ans_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_ans (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.text_rez, 'string',['正确答案: ',num2str(handles.source)]); %显示正确答案 %定义生成随机4位数的函数 function Y=rand4() M=randperm(10); M(find(M==10))=0; Y=M(1:4);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值