我为你狂H 发表于 2006-4-26 17:07

汉诺塔问题

<P>怎样是保持一个汉诺塔(可自由移动,自动移动)在300——400行之间。<br><br>我们几个人倒是弄了很久都不能。不知所措,望能人用JBuilder 9做出来一个。感激不尽!也可得到回报。</P>

aiyuheng 发表于 2006-7-17 20:38

我在j2se贴了一个 去看看吧

yangxingfu 发表于 2007-1-4 14:30

<P>我找了一个,代码如下:<BR><BR>/*<BR> *汉诺塔 <BR> *by aiyuheng<BR> *06年7月16日14:24<BR> *<BR>*/<BR>import java.awt.*;<BR>import javax.swing.*;<BR>import java.awt.event.*;<BR>//hanoi 的主类<BR>public class Hanoi extends JFrame<BR>{<BR> HanoiPanel p;<BR> public Hanoi(String s)<BR> {<BR>  super(s);<BR>  p=new HanoiPanel();<BR>  Container container=getContentPane();<BR>  container.add(p);<BR>  //菜单<BR>  JMenuBar menubar=new JMenuBar();<BR>  setJMenuBar(menubar);<BR>  JMenu game=new JMenu("game");<BR>  JMenuItem information=new JMenuItem("游戏说明");<BR>  information.addActionListener(new ActionListener()<BR>        {<BR>         public void actionPerformed(ActionEvent e)<BR>         {<BR>          JOptionPane.showMessageDialog(null, <BR>          "用空格键选中,用左右箭头移动",<BR>           "information", <BR>           JOptionPane.INFORMATION_MESSAGE);<BR>         }<BR>        });<BR>  JMenuItem exit=new JMenuItem("退出");<BR>  exit.addActionListener(new ActionListener()<BR>       {<BR>        public void actionPerformed(ActionEvent e)<BR>        {<BR>         System.exit(0);<BR>        }<BR>       });<BR>  game.add(information);<BR>  game.add(exit); <BR>  game.addSeparator();          <BR>  menubar.add(game);<BR>  setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);<BR> }<BR> ///main 方法<BR> public static void main(String [] args)<BR> {<BR>  Hanoi hanoi=new Hanoi("hanoi");<BR>  hanoi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<BR>  hanoi.setVisible(true);<BR>  hanoi.setResizable(true);<BR> }<BR> public static final int DEFAULT_WIDTH=407;<BR> public static final int DEFAULT_HEIGHT=320;<BR>}</P>
<P>import javax.swing.*;<BR>import java.awt.*;<BR>import java.awt.event.*;<BR>//具体操作在这个JPanel类中<BR>class HanoiPanel extends JPanel<BR>{<BR> private int num,step,y,g=3;<BR> private int member;<BR> private HanoiBox[] box;<BR> private boolean canMove,win;<BR> public HanoiPanel()<BR> {<BR>  start(g);<BR>  KeyHandler listener=new KeyHandler();<BR>  addKeyListener(listener);<BR>  setFocusable(true);<BR> }<BR> ///初始化<BR> public void start(int m)<BR> {<BR>  num=member=m;<BR>  box=new HanoiBox[num];<BR>  for(int i=0;i&lt;num;i++)<BR>  { <BR>   box[i]=new HanoiBox(5+i*10,230-i*15,130-20*i,Tower.A);<BR>  }<BR> }<BR> //paintComponent 方法画出塔和方块<BR> public void paintComponent(Graphics g)<BR> {<BR>  super.paintComponent(g);<BR>  Graphics2D g2=(Graphics2D)g;<BR>  String counter="";<BR>  g2.drawString("您走的步数:"+counter.valueOf(step)+"步",10,10);<BR>  g2.setColor(Color.black);<BR>  g2.fill3DRect(65,100,10,200,true);<BR>  g2.fill3DRect(195,100,10,200,true);<BR>  g2.fill3DRect(325,100,10,200,true);<BR>  g2.fill3DRect(0,250,400,20,true);<BR>  g2.setColor(Color.red);<BR>  for(int i=0;i&lt;box.length;i++)<BR>   g2.fill3DRect(box[i].getX(),box[i].getY(),box[i].getWidth(),10,true);<BR>  g2.setColor(Color.blue);<BR>  g2.fill3DRect(box[num-1].getX(),box[num-1].getY(),box[num-1].getWidth(),10,true);<BR>  <BR> }<BR> //判断是否过关<BR> public boolean win()<BR> {<BR>  win=true; <BR>  for(int i=0;i&lt;box.length;i++)<BR>  {<BR>   if(box[i].getTower()!=Tower.C)<BR>    win=false;<BR>  }<BR>  return win;<BR> }<BR> //上下移动的方法<BR> public int moveY(Tower t)<BR> {<BR>  int min=230;<BR>  int k=0;<BR>  y=box[num-1].getY();<BR>  for(int i=0;i&lt;box.length;i++)<BR>  {<BR>   if(box[i].getTower()==t)<BR>   {<BR>    ++k;<BR>    if(box[i].getY()&lt;=min)<BR>     min=box[i].getY(); <BR>   }<BR>   else if(k==0)<BR>     min=245;<BR>  }<BR>  return y=min-15;<BR> }<BR> //判断能否向左右移动<BR> public boolean canMove(Tower t)<BR> {<BR>  int width=box[num-1].getWidth();<BR>  for(int i=0;i&lt;box.length;i++)<BR>  {<BR>   if(box[i].getTower()==t)<BR>   {<BR>    if(box[i].getWidth()&lt;width)<BR>     canMove=false;<BR>    else<BR>     canMove=true; <BR>   }<BR>  }<BR>  return canMove;<BR> }<BR> //内部类实现键盘事件和具体操作<BR> private class KeyHandler extends KeyAdapter<BR> {<BR>  public void keyPressed(KeyEvent key)<BR>  {<BR>   int x=num-1;<BR>   switch(key.getKeyCode())<BR>   {<BR>    case KeyEvent.VK_LEFT:<BR>    {<BR>     if(box[x].getTower()==Tower.A&amp;&amp;canMove(Tower.A))<BR>     {<BR>      if(canMove(Tower.C))  <BR>      {<BR>       step++;<BR>       box[x].setX(260);<BR>       box[x].setY(moveY(Tower.C));<BR>       box[x].setTower(Tower.C);<BR>       repaint();<BR>      }<BR>     }<BR>     else if(box[x].getTower()==Tower.B&amp;&amp;canMove(Tower.B))<BR>     {<BR>      if(canMove(Tower.A))<BR>      {<BR>       step++;<BR>       box[x].setX(-130);<BR>       box[x].setY(moveY(Tower.A));<BR>       box[x].setTower(Tower.A);<BR>       repaint();<BR>      } <BR>     }<BR>     else if(box[x].getTower()==Tower.C&amp;&amp;canMove(Tower.C))<BR>     { <BR>      if(canMove(Tower.B))<BR>      { <BR>          step++;<BR>       box[x].setX(-130);<BR>       box[x].setY(moveY(Tower.B));<BR>       box[x].setTower(Tower.B);<BR>       repaint();<BR>      }<BR>     }<BR>     break;<BR>    }<BR>    case KeyEvent.VK_RIGHT:<BR>    {<BR>     if(box[x].getTower()==Tower.C&amp;&amp;canMove(Tower.C))<BR>     {<BR>      if(canMove(Tower.A))<BR>      {<BR>       step++;  <BR>       box[x].setX(-260);<BR>       box[x].setY(moveY(Tower.A));<BR>       box[x].setTower(Tower.A);<BR>       repaint();<BR>      }<BR>     }<BR>     else if(box[x].getTower()==Tower.B&amp;&amp;canMove(Tower.B))<BR>     {<BR>      if(canMove(Tower.C))<BR>      {<BR>       step++;<BR>       box[x].setX(130);<BR>       box[x].setY(moveY(Tower.C));<BR>       box[x].setTower(Tower.C);<BR>       repaint();<BR>      }<BR>     }<BR>     else if(box[x].getTower()==Tower.A&amp;&amp;canMove(Tower.A))<BR>     { <BR>      if(canMove(Tower.B))<BR>      {<BR>       step++;<BR>       box[x].setX(130);<BR>       box[x].setY(moveY(Tower.B));<BR>       box[x].setTower(Tower.B);<BR>       repaint();<BR>      }<BR>     }<BR>     break;<BR>    }<BR>    case KeyEvent.VK_SPACE:<BR>    {<BR>     if(num&gt;1)<BR>      num-=1;<BR>     else<BR>      num=member; <BR>     repaint(); <BR>    }<BR>    if(win())<BR>    {<BR>     step=0;<BR>     for(int i=0;i&lt;box.length;i++)<BR>     {<BR>      box[i]=null;<BR>     }<BR>     if(g&gt;=6)<BR>      g=3;<BR>     start(++g);<BR>     JOptionPane.showMessageDialog(null, <BR>      "你过关了!",<BR>      "win", <BR>      JOptionPane.INFORMATION_MESSAGE);<BR>    }<BR>   }<BR>  }<BR> }<BR>}</P>
<P><BR>//方块类 提供一些操作的方法<BR>import java.awt.event.*;<BR>import java.awt.*;<BR>class HanoiBox <BR>{<BR> private int xBox,yBox,width;<BR> private Tower tower;<BR> public HanoiBox(int x,int y,int z,Tower t)<BR> {<BR>  xBox=x;<BR>  yBox=y;<BR>  width=z;<BR>  tower=t;<BR> }<BR> public int getX()<BR> {<BR>  return xBox;<BR> }<BR> public int getY()<BR> {<BR>  return yBox;<BR> }<BR> public int getWidth()<BR> {<BR>  return width;<BR> }<BR> public Tower getTower()<BR> {<BR>  return tower;<BR> }<BR> public void setTower(Tower t)<BR> {<BR>  tower=t;<BR> }<BR> public void setX(int x)<BR> {<BR>  xBox+=x;<BR> }<BR> public void setY(int y)<BR> {<BR>  yBox=y;<BR> }<BR>}<BR>enum Tower<BR>{<BR> A,B,C;<BR>}<BR></P>

页: [1]

编程论坛