用matlab算24点小游戏,24点游戏的Matlab程序

本文介绍了一个使用MATLAB编写的24点游戏程序,该程序允许用户手动输入四个1到13之间的数字,或者随机生成这四个数字。通过调用特定的计算函数来寻找能够使这四个数字通过加减乘除运算得到24的表达式。

function GUI_games24

S.fh = figure('units','pixels',...

'position',[500 500 800 200],...

'menubar','none',...

'name','24点游戏',...

'numbertitle','off',...

'resize','off');

S.ti = uicontrol('style','text',...

'units','pix',...

'position',[300 150 180 30],...

'string','24点的计算程序','fontsize',15);

S.ra= uicontrol('style','pushbutton',...

'units','pix',...

'position',[10 100 180 30],...

'string','随机生成',...

'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','求解计算',...

'callback',{@pb_call});

S.re = uicontrol('style','text',...

'units','pix',...

'position',[200 20 350 30],...

'string','结果显示','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','请按reset按钮,输入1-13之间的整数');

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)

% 此程序解决经典的24点游戏

% disspp(x)

% example:x=[3 3 8 8]

% disspp(x)

% 算法在网上有的,也可以编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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值