2048游戏matlab代码

2048游戏matlab代码


2024年6月13日发(作者:)

说明:本代码为2048嬉戏matlab代码,程序未进行嬉戏结束判定,节目如下。

function g2048(action)

global totalscore flag score_board

if nargin<1

figure_h=figure;

set(figure_h,'Units','points')

set(figure_h,'UserData',figure_h);

totalscore=0;

flag=0;

score_board=zeros(1,16);

action='initialize';

end

switch action

case 'initialize';

figure_h=get(gcf,'UserData');

set(figure_h,...

'Color',[0.4 0.4 0.4],...

'Menubar','none',...

'Name','2048',...

'NumberTitle','off',...

'Position',[200 200 320 355],...

'Resize','off');

axis('off')

game_score=uicontrol(figure_h,...

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

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

'HorizontalAlignment','center',...

'FontSize',12,...

'Units','points',...

'Position',[235 305 65 30],...

'String','Score',...

'Style','edit',...

'Tag','game_score');

new_game_h=uicontrol(figure_h,...

'Callback','g2048 restart',...

'FontSize',12, ...

'Units','points',...

'Position',[35 30 65 30],...

'String','New Game',...

'Style','pushbutton');

% close

close_h=uicontrol(figure_h,...

'Callback','close(gcf)',...

'Fontsize',12, ...

'Units','points',...

'Position',[225 30 65 30],...

'String','Close',...

'Style','pushbutton');

% right

move_right=uicontrol(figure_h,...

'Callback','g2048 right',...

'Fontsize',12, ...

'Units','points',...

'Position',[255 185 60 30],...

'String','Right',...

'Style','pushbutton');

% left

move_left=uicontrol(figure_h,...

'Callback','g2048 left',...

'Fontsize',12, ...

'Units','points',...

'Position',[5 185 60 30],...

'String','Left',...

'Style','pushbutton');

% up

move_up=uicontrol(figure_h,...

'Callback','g2048 up',...

'Fontsize',12, ...

'Units','points',...

'Position',[130 300 60 30],...

'String','Up',...

'Style','pushbutton');

% down

move_down=uicontrol(figure_h,...

'Callback','g2048 down',...

'Fontsize',12, ...

'Units','points',...

'Position',[130 80 60 30],...

'String','Down',...

'Style','pushbutton');

% setup the game board

irows=1;

for counter=1:16

jcols=rem(counter,4);

if jcols==0

jcols=4;

end

position=[40*jcols+40 85+40*irows 40 40];

index=(irows-1)*4+jcols;

if jcols==4

irows=irows+1;

end

s(index)=uicontrol(figure_h,...

'FontSize',18,...

'FontWeight','bold',...

'Units','points',...

'Position',position,...

'Style','pushbutton',...

'Tag',num2str(index));

end

set(figure_h,'userdata',board);

g2048('restart')

case 'restart'

totalscore=0;

score_board=zeros(1,16);

g2048('addnum') ;

g2048('addnum') ;

g2048('show')

case'show'

num_0=find(score_board==0);

board=get(gcf,'UserData');

set(s,{'string'},num2cell(score_board)')

set(s,...

'BackgroundColor',[0.701961 0.701961 0.701961],...

'Enable','on',...

'Visible','on')

set(s(num_0),...

'BackgroundColor','black',...

'Enable','off',...

'String',' ');

score_handle=findobj(gcf,'Tag','game_score');

set(score_handle,...

'String',num2str(totalscore),...

'Tag','game_score');

case 'down'

C=score_board;

for i=1:4

A=[score_board(i) score_board(i+4) score_board(i+8) score_board(i+12)];

[B score]=move(A);

score_board(i)=B(1); score_board(i+4)=B(2);score_board(i+8)=B(3);

score_board(i+12)=B(4);

totalscore=totalscore+score;

end

if C==score_board

else

g2048('show');

g2048('addnum') ;

pause(0.2);

g2048('show');

end

case 'up'

C=score_board;

for i=13:16

A=[score_board(i) score_board(i-4) score_board(i-8) score_board(i-12)];

[B score]=move(A);

score_board(i)=B(1); score_board(i-4)=B(2);score_board(i-8)=B(3);

score_board(i-12)=B(4);

totalscore=totalscore+score;

end

if C==score_board

else

g2048('show');

g2048('addnum') ;

pause(0.2);

g2048('show');

end

case 'right'

C=score_board;

for i=4:4:16

A=[score_board(i) score_board(i-1) score_board(i-2) score_board(i-3)];

[B score]=move(A);

score_board(i)=B(1); score_board(i-1)=B(2);score_board(i-2)=B(3);

score_board(i-3)=B(4);

totalscore=totalscore+score;

end

if C==score_board

else

g2048('show');

g2048('addnum') ;

pause(0.2);

g2048('show');

end

case 'left'

C=score_board;

for i=1:4:13

A=[score_board(i) score_board(i+1) score_board(i+2) score_board(i+3)];

[B score]=move(A);

score_board(i)=B(1); score_board(i+1)=B(2);score_board(i+2)=B(3);

score_board(i+3)=B(4);

totalscore=totalscore+score;

end

if C==score_board

else

g2048('show');

g2048('addnum') ;

pause(0.2);

g2048('show');

end

case'addnum'

num_0=find(score_board==0);

l=length(num_0);

if l>0

score_board(num_0(ceil(l*rand)))=2+2*(rand<0.1);

end

end

end

function Y=addnum(X)

num_0=find(X==0);

l=length(num_0);

X(num_0(ceil(l*rand)))=2+2*(rand<0.1);

Y=X;

end

function [B score]=move(A)

score=0;

for k=1:2

for i=1:3

if A(i)==0

for j=i:3

A(j)=A(j+1);

end

A(4)=0;

end

end

end

if A(1)==A(2)

if A(3)==A(4)

A(1)=A(1)+A(2);A(2)=A(3)+A(4);A(3)=0;A(4)=0;score=A(1)+A(2);

else A(1)=A(1)+A(2);A(2)=A(3);A(3)=A(4);A(4)=0;score=A(1);

end

else if A(2)==A(3)

A(1)=A(1);A(2)=A(2)+A(3);A(3)=A(4);A(4)=0;score=A(2);

else if A(3)==A(4)

A(1)=A(1);A(2)=A(2);A(3)=A(3)+A(4);A(4)=0;score=A(3);

else score=0;

end

end

end

B=A;

end


发布者:admin,转转请注明出处:http://www.yc00.com/web/1718240333a2747616.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信