| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1487 人关注过本帖
标题:汉诺塔问题
只看楼主 加入收藏
我为你狂H
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-4-7
收藏
 问题点数:0 回复次数:2 
汉诺塔问题

怎样是保持一个汉诺塔(可自由移动,自动移动)在300——400行之间。

我们几个人倒是弄了很久都不能。不知所措,望能人用JBuilder 9做出来一个。感激不尽!也可得到回报。

搜索更多相关主题的帖子: 汉诺塔 能人 JBuilder 感激不尽 不知所措 
2006-04-26 17:07
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
收藏
得分:0 
我在j2se贴了一个 去看看吧

when i want to ask anyone,i will ask myself first.
2006-07-17 20:38
yangxingfu
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-1-3
收藏
得分:0 

我找了一个,代码如下:

/*
*汉诺塔
*by aiyuheng
*06年7月16日14:24
*
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//hanoi 的主类
public class Hanoi extends JFrame
{
HanoiPanel p;
public Hanoi(String s)
{
super(s);
p=new HanoiPanel();
Container container=getContentPane();
container.add(p);
//菜单
JMenuBar menubar=new JMenuBar();
setJMenuBar(menubar);
JMenu game=new JMenu("game");
JMenuItem information=new JMenuItem("游戏说明");
information.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,
"用空格键选中,用左右箭头移动",
"information",
JOptionPane.INFORMATION_MESSAGE);
}
});
JMenuItem exit=new JMenuItem("退出");
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
game.add(information);
game.add(exit);
game.addSeparator();
menubar.add(game);
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
///main 方法
public static void main(String [] args)
{
Hanoi hanoi=new Hanoi("hanoi");
hanoi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hanoi.setVisible(true);
hanoi.setResizable(true);
}
public static final int DEFAULT_WIDTH=407;
public static final int DEFAULT_HEIGHT=320;
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//具体操作在这个JPanel类中
class HanoiPanel extends JPanel
{
private int num,step,y,g=3;
private int member;
private HanoiBox[] box;
private boolean canMove,win;
public HanoiPanel()
{
start(g);
KeyHandler listener=new KeyHandler();
addKeyListener(listener);
setFocusable(true);
}
///初始化
public void start(int m)
{
num=member=m;
box=new HanoiBox[num];
for(int i=0;i<num;i++)
{
box[i]=new HanoiBox(5+i*10,230-i*15,130-20*i,Tower.A);
}
}
//paintComponent 方法画出塔和方块
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
String counter="";
g2.drawString("您走的步数:"+counter.valueOf(step)+"步",10,10);
g2.setColor(Color.black);
g2.fill3DRect(65,100,10,200,true);
g2.fill3DRect(195,100,10,200,true);
g2.fill3DRect(325,100,10,200,true);
g2.fill3DRect(0,250,400,20,true);
g2.setColor(Color.red);
for(int i=0;i<box.length;i++)
g2.fill3DRect(box[i].getX(),box[i].getY(),box[i].getWidth(),10,true);
g2.setColor(Color.blue);
g2.fill3DRect(box[num-1].getX(),box[num-1].getY(),box[num-1].getWidth(),10,true);

}
//判断是否过关
public boolean win()
{
win=true;
for(int i=0;i<box.length;i++)
{
if(box[i].getTower()!=Tower.C)
win=false;
}
return win;
}
//上下移动的方法
public int moveY(Tower t)
{
int min=230;
int k=0;
y=box[num-1].getY();
for(int i=0;i<box.length;i++)
{
if(box[i].getTower()==t)
{
++k;
if(box[i].getY()<=min)
min=box[i].getY();
}
else if(k==0)
min=245;
}
return y=min-15;
}
//判断能否向左右移动
public boolean canMove(Tower t)
{
int width=box[num-1].getWidth();
for(int i=0;i<box.length;i++)
{
if(box[i].getTower()==t)
{
if(box[i].getWidth()<width)
canMove=false;
else
canMove=true;
}
}
return canMove;
}
//内部类实现键盘事件和具体操作
private class KeyHandler extends KeyAdapter
{
public void keyPressed(KeyEvent key)
{
int x=num-1;
switch(key.getKeyCode())
{
case KeyEvent.VK_LEFT:
{
if(box[x].getTower()==Tower.A&&canMove(Tower.A))
{
if(canMove(Tower.C))
{
step++;
box[x].setX(260);
box[x].setY(moveY(Tower.C));
box[x].setTower(Tower.C);
repaint();
}
}
else if(box[x].getTower()==Tower.B&&canMove(Tower.B))
{
if(canMove(Tower.A))
{
step++;
box[x].setX(-130);
box[x].setY(moveY(Tower.A));
box[x].setTower(Tower.A);
repaint();
}
}
else if(box[x].getTower()==Tower.C&&canMove(Tower.C))
{
if(canMove(Tower.B))
{
step++;
box[x].setX(-130);
box[x].setY(moveY(Tower.B));
box[x].setTower(Tower.B);
repaint();
}
}
break;
}
case KeyEvent.VK_RIGHT:
{
if(box[x].getTower()==Tower.C&&canMove(Tower.C))
{
if(canMove(Tower.A))
{
step++;
box[x].setX(-260);
box[x].setY(moveY(Tower.A));
box[x].setTower(Tower.A);
repaint();
}
}
else if(box[x].getTower()==Tower.B&&canMove(Tower.B))
{
if(canMove(Tower.C))
{
step++;
box[x].setX(130);
box[x].setY(moveY(Tower.C));
box[x].setTower(Tower.C);
repaint();
}
}
else if(box[x].getTower()==Tower.A&&canMove(Tower.A))
{
if(canMove(Tower.B))
{
step++;
box[x].setX(130);
box[x].setY(moveY(Tower.B));
box[x].setTower(Tower.B);
repaint();
}
}
break;
}
case KeyEvent.VK_SPACE:
{
if(num>1)
num-=1;
else
num=member;
repaint();
}
if(win())
{
step=0;
for(int i=0;i<box.length;i++)
{
box[i]=null;
}
if(g>=6)
g=3;
start(++g);
JOptionPane.showMessageDialog(null,
"你过关了!",
"win",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}


//方块类 提供一些操作的方法
import java.awt.event.*;
import java.awt.*;
class HanoiBox
{
private int xBox,yBox,width;
private Tower tower;
public HanoiBox(int x,int y,int z,Tower t)
{
xBox=x;
yBox=y;
width=z;
tower=t;
}
public int getX()
{
return xBox;
}
public int getY()
{
return yBox;
}
public int getWidth()
{
return width;
}
public Tower getTower()
{
return tower;
}
public void setTower(Tower t)
{
tower=t;
}
public void setX(int x)
{
xBox+=x;
}
public void setY(int y)
{
yBox=y;
}
}
enum Tower
{
A,B,C;
}


AABBox游戏开发网址导航
2007-01-04 14:30
快速回复:汉诺塔问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017468 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved