import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Hnt extends Applet implements MouseListener,ActionListener,ItemListener
{
public int x=0,y=0;
public int count=0;
public int csize=3;//,size=20; //设置盘子的个数
public int tower[][]=new int[20][20]; //纪录每个柱子中盘子存放的内容
public int From; //用于纪录是哪个盘子需要转移!
public int top[]={19,-1,-1}; //纪录柱子顶端的位置,并进行初始化。
public int itop=0,ltop=0; //纪录是两次操作柱子的编号。
public int begin=0; //记录原柱子的编号
public int set; //盘子的基本宽度
public int iima;
public int currz;
public String Music[]={new String("up.wav"),new String("down.wav"),new String("lost.wav"),new String("win.wav")};
public String ima[]={new String("upA.gif"),new String("downA.gif"),new String("error.png"),new String("laugh.png")};
public String Message=" "; //记录信息
public Rectangle pan[]=new Rectangle[200]; //根据盘子的个数来完成盘子的设置。
public AudioClip music[]=new AudioClip[4]; //音乐设置
public Image image[]=new Image[4];
public Image bg;
public boolean run=false,yanshi=false,free=true,mode=true; //标志是移盘还是放盘。
public int bgnumber;
public TextField text;
public Label lab1,lab2;
public Button but,but2;
public Checkbox one,two;
public Choice cho;
public Color zi=new Color(153,50,205);
public myDialog md;
public Que q=new Que();
public int time=10;
public void init()
{
CheckboxGroup cg=new CheckboxGroup();
one=new Checkbox("自由模式",true,cg);
two=new Checkbox("正规模式",false,cg);
md=new myDialog("完成");
text=new TextField("3",20);
but=new Button("切换");
but2=new Button("演示");
cho=new Choice();
lab1=new Label("游戏模式:");
add(lab1);
add(two);
add(one);
lab1=new Label(" 演示间隔:");
add(lab1);
cho.add("1 秒");
cho.add("0.2秒");
cho.add("0.5秒");
cho.add("2 秒");
cho.add("3 秒");
add(cho);
lab1=new Label("输入盘子的个数:");
add(lab1);
add(text);
add(but);
add(but2);
two.addItemListener(this);
one.addItemListener(this);
cho.addItemListener(this);
but.addActionListener(this);
but2.addActionListener(this);
for(int j=0;j<4;j++)
{
music[j]=getAudioClip(getDocumentBase(),Music[j]);
image[j]=getImage(getDocumentBase(),ima[j]);
}
iima=0;
addMouseListener(this); //添加鼠标监听
bgnumber=(int)(Math.random()*16);
bg=getImage(getDocumentBase(),bgnumber+".png");
set=120/(22);
for(int i=0,j=19;i<20;i++) //初始化柱子A中的信息
{
tower[0][i]=j--;
pan[i]=new Rectangle((int)(80-(i/2.0+0.5)*set),215-(20-i)*set,(i+1)*set,set);//将盘子进行初始化
}
initPan();
}
public void initPan()
{
top[0]=csize-1;top[1]=-1;top[2]=-1;
set=120/(csize+2);
for(int i=0,j=csize-1;i
tower[0][i]=j--;
pan[i].setBounds((int)(80-(i/2.0+0.5)*set),245-(csize-i)*set,(i+1)*set,set);//将盘子进行初始化
}
repaint();
}
public void paint(Graphics g)
{
g.drawImage(bg,0,65,420,215,this);
String str=new String("状态: "+Message);
if(run) currz=ltop;
else currz=itop;
g.drawImage(image[iima],70+130*currz,85,20,20,this);
g.setColor(Color.pink);
g.drawString(str,10,265);
str="移动次数: "+count;
g.drawString(str,340,265);
g.drawLine(20,245,140,245); //画柱子
g.drawLine(150,245,270,245);
g.drawLine(280,245,400,245);
g.drawLine(80,125,80,245);
g.drawLine(210,125,210,245);
g.drawLine(340,125,340,245);
for(int i=0;i
g.setColor(Color.pink);
g.fillRect(pan[i].x,pan[i].y,pan[i].width,pan[i].height);
g.setColor(zi);
g.drawRect(pan[i].x,pan[i].y,pan[i].width,pan[i].height);
}
if(yanshi)
Yanshi();
}
public void mouseClicked(MouseEvent e)
{
if(!yanshi)
{
if(Message=="/tWin")
{
initPan();
Message="";
}
run=!run;
x=e.getX(); //获取当前鼠标点击的位置,从而判断是哪个柱子应该接受响应
y=e.getY();
if(x>=20 && x<=140 && y>=105 && y<=245) //知道是哪个柱子
itop=0;
else if(x>=150 && x<=270 && y>=105 && y<=245)
itop=1;
else if(x>=280 && x<=400 && y>=105 && y<=245)
itop=2;
else
{
run=!run;
return;
}
if(run)
{
if(top[itop]==-1)
{
run=false;
From=-1;
}
else
{
From=tower[itop][top[itop]];
ltop=itop;
music[0].play(); //向上移动声音播放
iima=0;
}
}
if(!run && From!=-1) //落盘子操作
{
if(top[itop]!=-1 && From>tower[itop][top[itop]])
{
Message="Wrong";
music[2].play(); //摆放错误
iima=2;
repaint();
return;
}
Message=" ";
top[ltop]--;
pan[From].setLocation(pan[From].x+(itop-ltop)*130,245-(top[itop]+2)*set); //直接将盘移动
top[itop]++;
tower[itop][top[itop]]=From;
iima=1;
music[1].play();
count++;
check();
}
repaint();
}
}
public void check()
{
if(top[0]==csize-1 || top[1]==csize-1 || top[2]==csize-1)
{
if(begin!=itop)
{
begin=itop;
Message="/tWin";
music[3].play();
iima=3;
bgnumber=(int)(Math.random()*16);
bg=getImage(getDocumentBase(),bgnumber+".png");
md.SetString(csize,count);
count=0;
md.setVisible(true);
}
}
else if(!mode && top[2]==csize-1)
{
begin=itop;
Message="/tWin";
music[3].play();
iima=3;
bgnumber=(int)(Math.random()*16);
bg=getImage(getDocumentBase(),bgnumber+".png");
md.SetString(csize,count);
count=0;
md.setVisible(true);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==but)
{
count=0;
if(yanshi)
{
yanshi=false;
iima=1;
q.clear();
}
try
{
int i=Integer.valueOf(text.getText()).intValue();
if(i>2 && i<21)
{
csize=i;
initPan();
}
else
text.setText("盘子的个数只限制在3~20之间");
}
catch(NumberFormatException e1)
{
text.setText("请输入0~9的数字");
}
}
else if(e.getSource()==but2)
{
if(csize<11)
{
iima=0;
yanshi=true;
hanoi(csize,0,1,2);
initPan();
}
else
text.setText("演示只限在10个盘子以内!");
}
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseClosing(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void movie(int get,int out)
{
q.push(get,out);
}
public void hanoi(int n,int a,int b,int c)
{
if(n==1) movie(a,c);
else
{
hanoi(n-1,a,c,b);
movie(a,c);
hanoi(n-1,b,a,c);
}
}
public void Yanshi()
{
try{
Thread.sleep((int)(time*100));
}catch(Exception e){}
if(q.size()==0)
{
yanshi=false;
text.setText("演示完成 移动次数:"+count);
iima=3;
count=0;
q.clear();
begin=2;
}
else
{
ltop=q.popFirst();
itop=q.popSecond();
try{
From=tower[ltop][top[ltop]];
}
catch(Exception e){}
top[ltop]--;
pan[From].setLocation(pan[From].x+(itop-ltop)*130,245-(top[itop]+2)*set); //直接将盘移动
top[itop]++;
tower[itop][top[itop]]=From;
count++;
}
repaint();
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==one)
mode=true;
else if(e.getSource()==two)
mode=false;
else switch(cho.getSelectedIndex())
{
case 0:
time=10;
break;
case 1:
time=2;
break;
case 2:
time=5;
break;
case 3:
time=20;
break;
case 4:
time=40;
}
}
}
class myDialog extends Frame implements ActionListener
{
boolean stop=true;
Label lab;
TextField tes;
Button continu;
GridLayout gl;
myDialog(String title)
{
super(title);
lab=new Label(" 恭喜!你已经成功的完成!");
tes=new TextField("",30);
tes.setEnabled(false);
continu=new Button("继续");
setSize(220,120);
gl=new GridLayout(3,1);
setLayout(gl);
add(lab);
add(tes);
add(continu);
continu.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==continu)
setVisible(false);
}
public void SetString(int i,int j)
{
int count=(int)Math.pow(2,i)-1;
String message=new String();
if(j==count)
message="完美";
else if(j<=count*2)
message="漂亮";
else
message="努力";
String str=new String("盘子个数:"+i+" 移动次数: "+j+" 状况:"+message);
tes.setText(str);
}
}
class Que
{
int First[]=new int[1024];
int Second[]=new int[1024];
int Top=0,End=0;
public void push(int First,int Second)
{
this.First[End]=First;
this.Second[End++]=Second;
}
public int popFirst()
{
return First[Top];
}
public int popSecond()
{
return Second[Top++];
}
public void clear()
{
Top=0;
End=0;
}
public int size()
{
return End-Top;
}
}