程序Hannoi塔_第1页
程序Hannoi塔_第2页
程序Hannoi塔_第3页
程序Hannoi塔_第4页
程序Hannoi塔_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、import java.io.*;import java.awt.*;public class Disk extends Button int number; boolean 上方有盘=false; public Disk(int numble,HannoiTower con) this.number=number; setBackground(Color.blue); addMouseMotionListener(con); addMouseListener(con); public boolean get上方有盘() return 上方有盘; public void set上方有盘(boo

2、lean b) 上方有盘=b; public int getNumber() return number; import javax.swing.*;import java.awt.*;import java.awt.event.*;public class HannoiTower extends JPanel implements MouseListener,MouseMotionListenerTowerPoint point;int x,y;boolean move=false;Disk 盘子;int startX,startY;int startI;int 盘子数目=0;int wid

3、th,height;char towerName='A','B','C'TextArea 信息条=null;public HannoiTower(int number,int w,int h,charname,TextArea text)towerName=name;盘子数目=number;width=w;height=h;信息条=text;setLayout(null);addMouseListener(this);盘子=new Disk盘子数目;point=new TowerPoint3*盘子数目;int space=20;for(int i

4、=0;i<盘子数目;i+)pointi=new TowerPoint(40+width,100+space,false);space=space+height;space=20;for(int i=盘子数目;i<2*盘子数目;i+)pointi=new TowerPoint(160+width,100+space,false);space=space+height;space=20;for(int i=2*盘子数目;i<3*盘子数目;i+)pointi=new TowerPoint(280+width,100+space,false);space=space+height;i

5、nt tempWidth=width;int sub=(int)(tempWidth*0.2);for(int i=盘子数目-1;i>=0;i-)盘子i=new Disk(i,this);盘子i.setSize(tempWidth,height);tempWidth=tempWidth-sub;for(int i=0;i<盘子数目;i+)pointi.放置盘子(盘子i,this);if(i>=1)盘子i.set上方有盘(true);public void paintComponent(Graphics g)super.paintComponent(g);g.drawLine(

6、point0.getX(),point0.getY(),point盘子数目-1.getX(),point盘子数目-1.getY();g.drawLine(point盘子数目.getX(),point盘子数目.getY(),point2*盘子数目-1.getX(),point2*盘子数目-1.getY();g.drawLine(point2*盘子数目.getX(),point2*盘子数目.getY(),point3*盘子数目-1.getX(),point3*盘子数目-1.getY();g.drawLine(point盘子数目-1.getX()-width,point盘子数目-1.getY(),p

7、oint3*盘子数目-1.getX()+width,point3*盘子数目-1.getY();int leftx=point盘子数目-1.getX()-width;int lefty=point盘子数目-1.getY();int w=(point3*盘子数目-1.getX()+width)-(point盘子数目-1.getX()-width);int h=height/2;g.setColor(Color.orange);g.fillRect(leftx,lefty,w,h);g.setColor(Color.red);int size=4;for(int i=0;i<3*盘子数目;i+

8、)g.fillOval(pointi.getX()-size/2,pointi.getY()-size/2,size,size);g.drawString(" "+towerName0+"塔",point盘子数目-1.getX(),point盘子数目-1.getY()+30);g.drawString(" "+towerName1+"塔",point2*盘子数目-1.getX(),point盘子数目-1.getY()+30);g.drawString(" "+towerName2+"塔

9、",point3*盘子数目-1.getX(),point盘子数目-1.getY()+30);g.drawString("将全部盘子从"+towerName0+"塔搬运到"+towerName1+"塔或"+towerName2+"塔",point盘子数目-1.getX(),point盘子数目-1.getY()+80);public void mousePressed(MouseEvent e)Disk 盘子=null;Rectangle rect=null;if(e.getSource()=this)mov

10、e=false;if(move=false)if(e.getSource() instanceof Disk)盘子=(Disk)e.getSource();startX=盘子.getBounds().x;startY=盘子.getBounds().y;rect=盘子.getBounds();for(int i=0;i<3*盘子数目;i+)int x=pointi.getX();int y=pointi.getY();if(rect.contains(x,y)startI=i;break;public void mouseMoved(MouseEvent e)public void mou

11、seDragged(MouseEvent e)Disk disk=null;if(e.getSource() instanceof Disk)disk=(Disk)e.getSource();move=true;e=SwingUtilities.convertMouseEvent(disk,e,this);if(e.getSource()=this)if(move&&disk!=null)x=e.getX();y=e.getY();if(disk.get上方有盘()=false)disk.setLocation(x-disk.getWidth()/2,y-disk.getHei

12、ght()/2);public void mouseReleased(MouseEvent e)Disk disk=null;move=false;Rectangle rect=null;if(e.getSource() instanceof Disk)disk=(Disk)e.getSource();rect=disk.getBounds();e=SwingUtilities.convertMouseEvent(disk,e,this);if(e.getSource()=this)boolean containTowerPoint=false;int x=0,y=0;int endI=0;i

13、f(disk!=null)for(int i=0;i<3*盘子数目;i+)x=pointi.getX();y=pointi.getY();if(rect.contains(x,y)containTowerPoint=true;endI=i;break;if(disk!=null&&containTowerPoint)if(pointendI.是否有盘子()=true)disk.setLocation(startX,startY);elseif(endI=盘子数目-1|endI=2*盘子数目-1|endI=3*盘子数目-1)pointendI.放置盘子(disk,this)

14、;if(startI!=盘子数目-1&&startI!=2*盘子数目-1&&startI!=3*盘子数目-1)(pointstartI+1.获取盘子().set上方有盘(false);pointstartI.set有盘子(false);elsepointstartI.set有盘子(false);elseif(pointendI+1.是否有盘子()=true)Disk tempDisk=pointendI+1.获取盘子();if(tempDisk.getNumber()-disk.getNumber()>=1)pointendI.放置盘子(disk,this

15、);if(startI!=盘子数目-1&&startI!=2*盘子数目-1&&startI!=3*盘子数目-1)(pointstartI+1.获取盘子().set上方有盘(false);pointstartI.set有盘子(false);tempDisk.set上方有盘(true);elsepointstartI.set有盘子(false);tempDisk.set上方有盘(true);elsedisk.setLocation(startX,startY);elsedisk.setLocation(startX,startY);if(disk!=null&

16、;&!containTowerPoint)disk.setLocation(startX,startY);public void mouseEntered(MouseEvent e)public void mouseExited(MouseEvent e)public void mouseClicked(MouseEvent e)public void 自动演示搬运盘子(int 盘子数,char one,char two,char three)if(盘子数=1)信息条.append(" "+one+"到:"+three+"塔n"

17、;);Disk disk=在塔中获取最上面的盘子(one);int startI=在塔中获取最上面盘子的位置(one);int endI=在塔中获取最上面盘子的上方位置(three);if(disk!=null)pointendI.放置盘子(disk,this);pointstartI.set有盘子(false);tryThread.sleep(1000);catch(Exception ee)else自动演示搬运盘子(盘子数-1,one,three,two);信息条.append(" "+one+"到:"+three+"塔n");D

18、isk disk=在塔中获取最上面的盘子(one);int startI=在塔中获取最上面盘子的位置(one);int endI=在塔中获取最上面盘子的上方位置(three);if(disk!=null)pointendI.放置盘子(disk,this);pointstartI.set有盘子(false);tryThread.sleep(1000);catch(Exception ee)自动演示搬运盘子(盘子数-1,two,one,three);public Disk 在塔中获取最上面的盘子(char 塔名)Disk disk=null;if(塔名=towerName0)for(int i=0

19、;i<盘子数目;i+)if(pointi.是否有盘子()=true)disk=pointi.获取盘子();break;if(塔名=towerName1)for(int i=盘子数目;i<2*盘子数目;i+)if(pointi.是否有盘子()=true)disk=pointi.获取盘子();break;if(塔名=towerName2)for(int i=2*盘子数目;i<3*盘子数目;i+)if(pointi.是否有盘子()=true)disk=pointi.获取盘子();break;return disk;public int 在塔中获取最上面盘子的上方位置(char 塔名

20、)int position=0;if(塔名=towerName0)int i=0;for(i=0;i<盘子数目;i+)if(pointi.是否有盘子()=true)position=Math.max(i-1,0);break;if(i=盘子数目)position=盘子数目-1;if(塔名=towerName1)int i=0;for(i=盘子数目;i<2*盘子数目;i+)if(pointi.是否有盘子()=true)position=Math.max(i-1,0);break;if(i=2*盘子数目)position=2*盘子数目-1;if(塔名=towerName2)int i=

21、0;for(i=2*盘子数目;i<3*盘子数目;i+)if(pointi.是否有盘子()=true)position=Math.max(i-1,0);break;if(i=3*盘子数目)position=3*盘子数目-1;return position;public int 在塔中获取最上面盘子的位置(char 塔名)int position=0;if(塔名=towerName0)int i=0;for(i=0;i<盘子数目;i+)if(pointi.是否有盘子()=true)position=i;break;if(i=盘子数目)position=盘子数目-1;if(塔名=towe

22、rName1)int i=0;for(i=盘子数目;i<2*盘子数目;i+)if(pointi.是否有盘子()=true)position=i;break;if(i=2*盘子数目)position=2*盘子数目-1;if(塔名=towerName2)int i=0;for(i=2*盘子数目;i<3*盘子数目;i+)if(pointi.是否有盘子()=true)position=i;break;if(i=3*盘子数目)position=3*盘子数目-1;return position;import javax.swing.*;import java.awt.*;import java

23、.awt.event.*;public class Tower extends Frame implements ActionListener,RunnableHannoiTower tower=null;Button renew,auto=null;char towerName='A','B','c'int 盘子数目,盘宽,盘高;Thread thread;TextArea 信息条=null;public Tower()thread=new Thread(this);盘子数目=5;盘宽=80;盘高=18;信息条=new TextArea(12,

24、12);信息条.setText(null);tower=new HannoiTower(盘子数目,盘宽,盘高,towerName,信息条);renew=new Button("重新开始");auto=new Button("自动演示搬盘子");renew.addActionListener(this);auto.addActionListener(this);add(tower,BorderLayout.CENTER);add(renew,BorderLayout.SOUTH);add(auto,BorderLayout.NORTH);add(信息条,B

25、orderLayout.EAST);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););setVisible(true);setBounds(60,20,670,540);validate();public void actionPerformed(ActionEvent e)if(e.getSource()=renew)if(!(thread.isAlive()this.remove(tower);信息条.setText(null); tower=new HannoiTower(盘子数目,盘宽,盘高,towerName,信息条); add(tower,BorderLayout.CENTER); validate(); elseif(e.getSource()=auto)if(!

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论