用matlab算24点小游戏,24点游戏的Matlab程序
function GUI_games24
S.fh = figure('units','pixels',...
'position',[500 500 800 200],...
'menubar','none',...
'name','24點(diǎn)游戲',...
'numbertitle','off',...
'resize','off');
S.ti = uicontrol('style','text',...
'units','pix',...
'position',[300 150 180 30],...
'string','24點(diǎn)的計(jì)算程序','fontsize',15);
S.ra= uicontrol('style','pushbutton',...
'units','pix',...
'position',[10 100 180 30],...
'string','隨機(jī)生成',...
'callback',{@ra_call});
S.ed1 = uicontrol('style','edit',...
'units','pix',...
'position',[10 60 180 30],...
'string',' ');
S.ed2 = uicontrol('style','edit',...
'units','pix',...
'position',[200 60 180 30],...
'string',' ');
S.ed3 = uicontrol('style','edit',...
'units','pix',...
'position',[400 60 180 30],...
'string',' ');
S.ed4 = uicontrol('style','edit',...
'units','pix',...
'position',[600 60 180 30],...
'string',' ');
S.pb = uicontrol('style','pushbutton',...
'units','pix',...
'position',[10 20 180 30],...
'string','求解計(jì)算',...
'callback',{@pb_call});
S.re = uicontrol('style','text',...
'units','pix',...
'position',[200 20 350 30],...
'string','結(jié)果顯示','fontsize',15);
S.reset = uicontrol('style','pushbutton',...
'units','pix',...
'position',[600 20 180 30],...
'string','reset',...
'callback',{@re_call});
function pb_call(varargin)
% Callback for the pushbutton.
R1 = str2double(get(S.ed1,'string'));
R2 = str2double(get(S.ed2,'string'));
R3 = str2double(get(S.ed3,'string'));
R4 = str2double(get(S.ed4,'string'));
x=[R1,R2,R3,R4];
if any(x>13)
set(S.re,'string','請(qǐng)按reset按鈕,輸入1-13之間的整數(shù)');
return
end
expression=disspp(x);
if length(expression{1})<4
set(S.re,'string','fail!!');
else
cc=cell(1,length(expression{1}));
for i=1:length(expression{1})
if expression{1}(i)>30
cc{i}=char(expression{1}(i));
else
cc{i}=num2str(expression{1}(i));
end
end
set(S.re,'string',cell2mat(cc));
end
end
function re_call(varargin)
% Callback for the pushbutton.
set(S.ed1,'string','');
set(S.ed2,'string','');
set(S.ed3,'string','');
set(S.ed4,'string','');
set(S.re,'string','');
end
function ra_call(varargin)
% Callback for the pushbutton.
rap=randint(1,4,[1,13]);
set(S.ed1,'string',int2str(rap(1)));
set(S.ed2,'string',int2str(rap(2)));
set(S.ed3,'string',int2str(rap(3)));
set(S.ed4,'string',int2str(rap(4)));
end
end
function expression=disspp(x)
% 此程序解決經(jīng)典的24點(diǎn)游戲
% disspp(x)
% example:x=[3 3 8 8]
% disspp(x)
% 算法在網(wǎng)上有的,也可以編c++,java,asp,jap,html&etc,有興趣者可以試試
% copyright:\流水
% 編寫于5月27日晨
if nargin==0
x=[6 6 6 6];
end
global number expression
number=x;
for i=1:4
expression{i}=number(i);
end
number=x;
bol=search(4);
if bol==1
disp('Sucess.')
else
disp('fail');
end
function bool=search(n)
global number expression
PRECISION=1e-6;
% COUNT_OF_NUMBER=4;
NUMBER_TO_BE_CAL=24;
if n==1
if abs(number(1)-NUMBER_TO_BE_CAL)
for i=1:length(expression{1})
if expression{1}(i)>30
a{i}=char(expression{1}(i));
else
a{i}=num2str(expression{1}(i));
end
end
a=cell2mat(a);
disp(a);
bool=1;
return
else
bool=0;
return
end
end
for i=1:n
for j=i+1:n
a=number(i);
b=number(j);
number(j)=number(n);
expa=expression{i};
expb=expression{j};
expression{j}=expression{n};
expression{i}=[40,expa,43,expb,41];
global expression
number(i)=a+b;
if search(n-1)
global expression
bool=1;
return
end
expression{i}=[40,expa,45,expb,41];
number(i)=a-b;
if search(n-1)
global expression
bool=1;
return
end
expression{i}=[40,expb,45,expa,41];
number(i)=b-a;
if search(n-1)
global expression
bool=1;
return
end
expression{i}=[40,expa,42,expb,41];
number(i)=a*b;
if search(n-1)
global expression
bool=1;
return
end
if b~=0
expression{i}=[40,expa,47,expb,41];
number(i)=a/b;
if search(n-1)
global expression
bool=1;
return
end
end
if a~=0
expression{i}=[40,expb,47,expa,41];
number(i)=b/a;
if search(n-1)
global expression
bool=1;
return
end
end
number(i)=a;
number(j)=b;
expression{i}=expa;
expression{j}=expb;
end
end
bool=0;
總結(jié)
以上是生活随笔為你收集整理的用matlab算24点小游戏,24点游戏的Matlab程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 传音 Spark 20 系列手机渲染图和
- 下一篇: matlab求滤波器的冲激响应,在TMS