• 大小: 154KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-15
  • 语言: Java
  • 标签: java  汉诺塔  小游戏  

资源简介

用java实现的汉诺塔游戏,含源代码和可执行exe文件,供初学者参考学习

资源截图

代码片段和文件信息

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Hanoi extends Jframe {
  DrawPanel panel = new DrawPanel(3);
  ButtonPanel panel2 = new ButtonPanel();
  KeyBoardListener keyboardlistener = new KeyBoardListener(panel);
  public Hanoi() {
    this.setFocusable(true);
    panel.setBounds(2222);
panel.setBorder(BorderFactory.createLineBorder(Color.black));
    ButtonPanel.start.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    panel.refresh(3);
  }
});
ButtonPanel.quit.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    int res;
res = JOptionPane.showConfirmDialog(null“确定退出?““退出“JOptionPane.YES_NO_OPTION);
if(res == JOptionPane.YES_OPTION) {
      System.exit(0);
}
  }
});
ButtonPanel.help.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    JOptionPane.showMessageDialog(null“将所有盘子移动到最后一个柱子上,通过键盘上下左右键操作。另外请注意每一关都是有步数限制的“);
  }
});
    add(panelBorderLayout.CENTER);
add(panel2BorderLayout.EAST);
this.addKeyListener(keyboardlistener);
ButtonPanel.start.addKeyListener(keyboardlistener);
ButtonPanel.help.addKeyListener(keyboardlistener);
ButtonPanel.quit.addKeyListener(keyboardlistener);
  }
   
  public static void main(String[] args) {
    Hanoi frame = new Hanoi();
frame.settitle(“汉诺塔“);
frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
frame.setSize(500309);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
  }
}

//键盘监听器
class KeyBoardListener extends KeyAdapter {
  DrawPanel panel;
  KeyBoardListener(DrawPanel panel) {
    this.panel = panel;
  }
  
  public void keyPressed(KeyEvent e) {
int n = e.getKeyCode();
    if(n == 38) {
  panel.getPlate();
} else if(n == 40) {
  panel.setPlate();
} else if(n == 39) {
  panel.movePlateRight();
} else if(n == 37) {
  panel.movePlateLeft();
}
  }
  public void keyTyped(KeyEvent e) {
        
  }
  public void keyReleased(KeyEvent e) {
        
  }
}


//图形界面模块
class DrawPanel extends JPanel {
  ArrayList plates = new ArrayList();//盘子
  static Pillar[] pillars = new Pillar[3];//柱子
  Hand hand = new Hand(1);//抓取工具
  int chosen;//被抓起的盘子id
  int n;//盘子个数
  boolean init;//初始化判断
  int step;//步数
  
  static {
    for(int i=0;i<3;i++) {
  pillars[i] = new Pillar(i+1);
}
  }
  
  //构造方法
  DrawPanel(int a) {
init = true;
this.refresh(a);
  }
  //刷新
  public void refresh(int a) {
    n = a;
cleanUp();
Plate[] temp = new Plate[a];
int i;
for(i=1;i   temp[i-1] = new Plate(i);
  temp[i-1].pillarid = 1;
  temp[i-1].position = a-i+1;
  pillars[0].count++;
  pillars[0].plateid[temp[i-1].position] = temp[i-1].id;
  plates.add(temp[i-1]);
}
if(!init) {
  repaint();
} else {
  init = false;
}
  }
  
  //清空

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     449536  2014-03-02 14:20  Hanoi\Hanoi.exe

     文件       6801  2014-03-02 11:46  Hanoi\Hanoi.java

     目录          0  2014-03-02 21:21  Hanoi

----------- ---------  ---------- -----  ----

               456337                    3


评论

共有 条评论