博客
关于我
matlab 汉诺塔
阅读量:220 次
发布时间:2019-02-28

本文共 4557 字,大约阅读时间需要 15 分钟。

matlab 版的汉诺塔

游戏效果如下在这里插入图片描述
只做了一层到五层的
可以复制下来玩玩试试~

function hano(level)if nargin < 1   level = 5;endhold on,axis equalaxis(0.5+[0,60,0,30])set(gca,'xtick',[],'ytick',[],'xcolor','w','ycolor','w')set(gca,'color','k')ground=[60.5,1.5;60.5,0.5;0.5,0.5;0.5,1.5];column_1=[10.5,1.5;10.5,20;9.5,20;9.5,1.5];column_2=[30.5,1.5;30.5,20;29.5,20;29.5,1.5];column_3=[50.5,1.5;50.5,20;49.5,20;49.5,1.5];step=0;arrow=[1];A=[];B=[];C=[];A=level:-1:1;block_1=@(l,h)[10+2*l,3*h-1;10+2*l,3*h+1;10-2*l,3*h+1;10-2*l,3*h-1];block_2=@(l,h)[30+2*l,3*h-1;30+2*l,3*h+1;30-2*l,3*h+1;30-2*l,3*h-1];block_3=@(l,h)[50+2*l,3*h-1;50+2*l,3*h+1;50-2*l,3*h+1;50-2*l,3*h-1];block__=@(l,p)[p+2*l,25-1;p+2*l,25+1;p-2*l,25+1;p-2*l,25-1];set(gcf, 'KeyPressFcn', @key);
function draw(~,~)        delete(findobj(gcf,'type','text'))        delete(findobj(gcf,'type','patch'))        text(5,-2,'step=')        text(12,-2,num2str(step))        text(5,-6,'level=')        text(12,-6,num2str(level))        text(20,-2,'use the key(\leftarrow \rightarrow) to move the claw')        text(20,-6,'use the key(\uparrow \downarrow) to pick or land the block')        fill(ground(:,1),ground(:,2),'w','EdgeColor','none')        fill(column_1(:,1),column_1(:,2),'w','EdgeColor','none')        fill(column_2(:,1),column_2(:,2),'w','EdgeColor','none')        fill(column_3(:,1),column_3(:,2),'w','EdgeColor','none')        text(20*arrow(1)-10-1,30,'\downarrow','Color','w','Fontsize',25)        if length(arrow)==2            p=20*arrow(1)-10;            block=block__(arrow(2),p);            fill(block(:,1),block(:,2),'w','EdgeColor','none')          end        for i=1:length(A)            block=block_1(A(i),i);            fill(block(:,1),block(:,2),'w','EdgeColor','none')           end        for i=1:length(B)            block=block_2(B(i),i);            fill(block(:,1),block(:,2),'w','EdgeColor','none')           end        for i=1:length(C)            block=block_3(C(i),i);            fill(block(:,1),block(:,2),'w','EdgeColor','none')           end        if length(C)==level            buttonName1=questdlg('Congratulations! You win','you win','close','restart','next level','close');            if isempty(buttonName1)                buttonName1='end';            end            if strcmp(buttonName1,'restart')                hano(level)            end            if strcmp(buttonName1,'close')                close;            end            if strcmp(buttonName1,'next level')                if level<5                    hano(level+1)                end                if level==5                    msgbox('it is already the highest level')                end            end        end    end
function key(~,event)   switch event.Key       case 'leftarrow'           if arrow(1)-1~=0               arrow(1)=arrow(1)-1;step=step+1;           end       case 'rightarrow'           if arrow(1)-3~=0               arrow(1)=arrow(1)+1;step=step+1;           end       case 'uparrow'           if length(arrow)==1               if (arrow(1)==1)&&(~isempty(A))                   arrow=[arrow(1),A(end)];                   A(end)=[];               end               if (arrow(1)==2)&&(~isempty(B))                   arrow=[arrow(1),B(end)];                   B(end)=[];               end               if (arrow(1)==3)&&(~isempty(C))                   arrow=[arrow(1),C(end)];                   C(end)=[];               end           end       case 'downarrow'           if length(arrow)==2               if (arrow(1)==1)                   if isempty(A)                       A=[A,arrow(2)];                       arrow(2)=[];                   else                       if A(end)>arrow(2)                           A=[A,arrow(2)];                           arrow(2)=[];                       end                   end               end               if (arrow(1)==2)                   if isempty(B)                       B=[B,arrow(2)];                       arrow(2)=[];                   else                       if B(end)>arrow(2)                           B=[B,arrow(2)];                           arrow(2)=[];                       end                   end                          end               if (arrow(1)==3)                   if isempty(C)                       C=[C,arrow(2)];                       arrow(2)=[];                   else                       if C(end)>arrow(2)                           C=[C,arrow(2)];                           arrow(2)=[];                       end                   end               end           end   end   draw()   enddraw()end

把这三段都复制下来放在同一个m文件里即可 。

转载地址:http://fjls.baihongyu.com/

你可能感兴趣的文章
mysql 常见问题
查看>>
MYSQL 幻读(Phantom Problem)不可重复读
查看>>
mysql 往字段后面加字符串
查看>>
mysql 快照读 幻读_innodb当前读 与 快照读 and rr级别是否真正避免了幻读
查看>>
MySQL 快速创建千万级测试数据
查看>>
mysql 快速自增假数据, 新增假数据,mysql自增假数据
查看>>
MySql 手动执行主从备份
查看>>
Mysql 批量修改四种方式效率对比(一)
查看>>
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>