怎样是保持一个汉诺塔(可自由移动,自动移动)在300——400行之间。
我们几个人倒是弄了很久都不能。不知所措,望能人用JBuilder 9做出来一个。感激不尽!也可得到回报。
我找了一个,代码如下:
/*
*汉诺塔
*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;
}