Java实现汉诺塔游戏.doc_第1页
Java实现汉诺塔游戏.doc_第2页
Java实现汉诺塔游戏.doc_第3页
Java实现汉诺塔游戏.doc_第4页
Java实现汉诺塔游戏.doc_第5页
免费预览已结束,剩余14页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/盘类 Disk.javapackage towers;import java.awt.*;public class Disk extends Button /* * 当前Disk对象编号 */int number;/* * 当前盘上是否有盘 */boolean hasDiskUpstairs = false;/* * 初始化一个Disk对象 * param number * param tower */public Disk(int number, Tower tower) this.number = number;setBackground(Color.BLUE);addMouseMotionListener(tower);addMouseListener(tower);/* * 获取是否当前盘的上方有盘属性 * return */public boolean getHasDiskUpstairs() return hasDiskUpstairs;/* * 设置当前盘上是否还有盘属性 * param b */public void setHasDiskUpstairs(boolean b) hasDiskUpstairs = b;/* * 获取当前盘的编号 * return */public int getNumber() return number;/游戏配置类 GameConfig.javapackage towers;public class GameConfig /* * 盘子总数 */private int totalDisk;/* * 最大盘的宽度 */private int diskWidth;/* * 最大盘的高度 */private int diskHeight;/* * 游戏主窗口高度 */private int mainFrameHeight;/* * 游戏主窗口宽度 */private int mainFrameWidth;/* * 游戏面板宽度 */private int gamePanelWidth;/* * 游戏面板高度 */private int gamePanelHeight;/* * return the diskHeight */public int getDiskHeight() return diskHeight;/* * return the diskWidth */public int getDiskWidth() return diskWidth;/* * return the gamePanelHeight */public int getGamePanelHeight() return gamePanelHeight;/* * return the gamePanelWidth */public int getGamePanelWidth() return gamePanelWidth;/* * return the mainFrameHeight */public int getMainFrameHeight() return mainFrameHeight;/* * return the mainFrameWidth */public int getMainFrameWidth() return mainFrameWidth;/* * return the totalDisk */public int getTotalDisk() return totalDisk;/* * param diskHeight the diskHeight to set */public void setDiskHeight(int diskHeight) this.diskHeight = diskHeight;/* * param diskWidth the diskWidth to set */public void setDiskWidth(int diskWidth) this.diskWidth = diskWidth;/* * param gamePanelHeight the gamePanelHeight to set */public void setGamePanelHeight() this.gamePanelHeight = this.getTotalDisk() * this.getDiskHeight() + 200;/* * param gamePanelWidth the gamePanelWidth to set */public void setGamePanelWidth() this.gamePanelWidth = 8 * this.getDiskWidth();/* * param mainFrameHeight the mainFrameHeight to set */public void setMainFrameHeight() this.mainFrameHeight = this.getGamePanelHeight() + 50;/* * param mainFrameWidth the mainFrameWidth to set */public void setMainFrameWidth() this.mainFrameWidth = this.getGamePanelWidth();/* * param totalDisk the totalDisk to set */public void setTotalDisk(int totalDisk) this.totalDisk = totalDisk;/塔类 Tower.javapackage towers;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Tower extends JPanel implements MouseListener,MouseMotionListener /* * 3个塔的塔点数组 */TowerPoint point;/* * 记录鼠标位置 */int x, y;/* * 控制盘子移动的变量 */boolean move;/* * 盘子组成的数组 */Disk diskArray;/* * 记录盘子的初始坐标 */int startX, startY;/* * 记录盘子的初始塔点 */int startI;/* * 盘的总数 */int totalDisk;/* * 最大号盘子的宽和高 */int width, height;/* * 各个塔名 */char towerName = A, B, C ;/* * 游戏配置 */GameConfig config;/* * 初始化一个SolveTowers对象 * * param totalDisk * param w * param h * param name * param text */public Tower(GameConfig config, char towerName) this.config = config;this.towerName = towerName;this.totalDisk = config.getTotalDisk();this.width = config.getDiskWidth();this.height = config.getDiskHeight();setLayout(null);setBackground(Color.cyan);addMouseListener(this);/创建盘组diskArray = new Diskthis.totalDisk;point = new TowerPoint3 * this.totalDisk;int diskHeight = 20;/ 给出A塔中塔点坐标for (int i = 0; i this.totalDisk; i+) pointi = new TowerPoint(2 * width, 100 + diskHeight, false);diskHeight = diskHeight + height;diskHeight = 20;/ 给出B塔中塔点坐标for (int i = this.totalDisk; i 2 * this.totalDisk; i+) pointi = new TowerPoint(4 * width, 100 + diskHeight, false);diskHeight = diskHeight + height;diskHeight = 20;/ 给出C塔中塔点坐标for (int i = 2 * this.totalDisk; i = 0; i-) / 创建盘子数组diskArrayi = new Disk(i, this);diskArrayi.setSize(tempWidth, height); / 各个盘子的大小tempWidth = tempWidth - sub;for (int i = 0; i = 1)diskArrayi.setHasDiskUpstairs(true);/* * 绘制游戏界面 */public void paintComponent(Graphics g) / 将3个塔中的塔点分别连成直线super.paintComponent(g);/ 画出A塔g.drawLine(point0.getX(), point0.getY(), pointthis.totalDisk - 1.getX(),pointthis.totalDisk - 1.getY();/ 画出B塔g.drawLine(pointthis.totalDisk.getX(), pointthis.totalDisk.getY(), point2 * this.totalDisk - 1.getX(), point2 * this.totalDisk - 1.getY();/ 画出C塔g.drawLine(point2 * this.totalDisk.getX(), point2 * this.totalDisk.getY(),point3 * this.totalDisk - 1.getX(), point3 * this.totalDisk - 1.getY();/ 画出基准线,即由A塔最低端塔点到C塔最低端塔点的线段g.drawLine(pointthis.totalDisk - 1.getX() - width, pointthis.totalDisk - 1.getY(),point3 * this.totalDisk - 1.getX() + width, point3 * this.totalDisk - 1.getY();int leftx = pointthis.totalDisk - 1.getX() - width;int lefty = pointthis.totalDisk - 1.getY();int w = (point3 * this.totalDisk - 1.getX() + width)- (pointthis.totalDisk - 1.getX() - width);int h = height / 2;g.setColor(Color.orange);g.fillRect(leftx, lefty, w, h);/ 画出各个塔点g.setColor(Color.BLACK);int size = 1;for (int i = 0; i 3 * this.totalDisk; i+) g.fillOval(pointi.getX() - size / 2, pointi.getY() - size / 2,size, size);/ 画出各个塔名g.drawString( + towerName0 + 塔, pointthis.totalDisk - 1.getX(),pointthis.totalDisk - 1.getY() + 30);g.drawString( + towerName1 + 塔, point2 * this.totalDisk - 1.getX(),pointthis.totalDisk - 1.getY() + 30);g.drawString( + towerName2 + 塔, point3 * this.totalDisk - 1.getX(),pointthis.totalDisk - 1.getY() + 30);/ 画出游戏任务提示,即起点、终点g.drawString(将全部盘子从 + towerName0 + 塔搬运到 + towerName1 + 塔或+ towerName2 + 塔, pointthis.totalDisk - 1.getX(), pointthis.totalDisk - 1.getY() + 80);/* * 实现MouseListener中的抽象方法 */public void mousePressed(MouseEvent e) Disk disk = null;Rectangle rect = null;if (e.getSource() = this) move = false;if (move = false) if (e.getSource() instanceof Disk) disk = (Disk) e.getSource();startX = disk.getBounds().x;startY = disk.getBounds().y;rect = disk.getBounds();for (int i = 0; i 3 * this.totalDisk; i+) int x = pointi.getX();int y = pointi.getY();if (rect.contains(x, y) startI = i;break;this.repaint();/* * 实现MouseListener中的抽象方法 */public void mouseMoved(MouseEvent e) /* * 实现MouseListener中的抽象方法 * 在盘子上拖动鼠标,将导致盘子上发生鼠标拖动事件 */public void mouseDragged(MouseEvent e) Disk disk = null;if (e.getSource() instanceof Disk) / 返回盘对象disk = (Disk) e.getSource();move = true;/ 将鼠标拖动事件转移到容器,导致容器上发生鼠标拖动事件e = SwingUtilities.convertMouseEvent(disk, e, this);if (e.getSource() = this) if (move & disk != null) x = e.getX();y = e.getY();if (disk.getHasDiskUpstairs() = false) / 拖动盘移动disk.setLocation(x - disk.getWidth() / 2, y- disk.getHeight() / 2);this.repaint();/* * 实现MouseListener中的抽象方法 */public void mouseReleased(MouseEvent e) Disk disk = null;move = false;Rectangle rect = null;if (e.getSource() instanceof Disk) / 返回盘子对象disk = (Disk) e.getSource();/ 初始化当前所移动的图形rect = disk.getBounds();/ 将鼠标释放事件转移到容器,导致容器上发生鼠标释放事件。e = SwingUtilities.convertMouseEvent(disk, e, this);if (e.getSource() = this) boolean containTowerPoint = false;int x = 0, y = 0;int endI = 0;if (disk != null) / 检查盘子是否被移动到了一个塔点上for (int i = 0; i = 1) pointendI.addDisk(disk, this);/起点不是某个塔底if (startI != totalDisk - 1& startI != 2 * totalDisk - 1& startI != 3 * totalDisk - 1) pointstartI + 1.getDisk().setHasDiskUpstairs(false);pointstartI.setHasDisk(false);tempDisk.setHasDiskUpstairs(true); else pointstartI.setHasDisk(false);tempDisk.setHasDiskUpstairs(true); else / 如果下面的盘子小,将当前盘子放回原处disk.setLocation(startX, startY); else / 如果下面的塔点没有盘子,将当前盘子放回原处disk.setLocation(startX, startY);/ 如果在非塔点位置上松开拖动的盘子,将当前盘子放回原处if (disk != null & !containTowerPoint) disk.setLocation(startX, startY);this.repaint();/* * 实现MouseListener中的抽象方法 */public void mouseEntered(MouseEvent e) /* * 实现MouseListener中的抽象方法 */public void mouseExited(MouseEvent e) /* * 实现MouseListener中的抽象方法 */public void mouseClicked(MouseEvent e) /塔点类 TowerPoint.javapackage towers;public class TowerPoint /* * 当前塔点坐标 */int x, y;/* * 当前塔点上是否有盘 */boolean hasDisk;/* * 当前塔点上盘对象 */Disk disk = null;/* * 当前塔点所在塔对象 */Tower tower = null;/* * 初始化一个TowerPoint对象 * param x * param y * param hasDisk */public TowerPoint(int x, int y, boolean hasDisk) this.x = x;this.y = y;this.hasDisk = hasDisk;/* * 判断当前塔上是否有盘 * return */public boolean getHasDisk() return hasDisk;/* * 设置当前塔上是否有盘 * param hasDisk */public void setHasDisk(boolean hasDisk) this.hasDisk = hasDisk;/* * 返回当前点的X坐标值 * return */public int getX() return x;/* * 返回当前点的Y坐标值 * return */public int getY() return y;/* * 在当前点上添加一个盘 * param 盘子 * param tower */public void addDisk(Disk disk, Tower tower) this.tower = tower;tower.setLayout(null);this.disk = disk;tower.add(this.disk);int w = disk.getBounds().width;int h = disk.getBounds().height;this.disk.setBounds(x - w / 2, y - h / 2, w, h);this.hasDisk = true;tower.validate();/* * 获取当前点上的盘对象 * return */public Disk getDisk() return this.disk;/游戏主类 Hanoi.javapackage towers;import java.awt.*;import java.awt.event.*;public class Hanoi extends Frame implements ActionListener public Tower tower = null;GameConfig defaultConfig = null;GameConfig userConfig = null;public Button renew = null;public Label label = null;public Choice totalNumberChoice = null;public char towerName = A, B, C ;public Hanoi() defaultConfig = new GameConfig();defaultConfig.setTotalDisk(20);defaultConfig.setDiskWidth(120);defaultConfig.setDiskHeight(20);defaultConfig.setGamePanelWidth();defaultConfig.setGamePanelHeight();defaultConfig.setMainFrameWidth();defaultConfig.setMainFrameHeight();init();public void init() this.setTitle(SolveTowers Game);this.setBounds(100, 50, defaultConfig.getMainFrameWidth(), defaultConfig.getMainFrameHeight();this.setLayout(null);tower = new Tower(defaultConfig, towerName);tower.setBounds(0, 0, defaultConfig.getGamePanelWidth(), defaultConfig.getGamePanelHeight();add(tower);renew = new Button(重新开始);renew.setBounds(0, defaultConfig.getGamePanelHeight(), 100, 50);renew.addActionListener(this);add(renew);label = new Label(选择盘数:);label.setBounds(170, defaultConfig.getGamePanelHeight(), 60, 50);this.add(label);totalNumberChoice = new Choice();for(int i=1; i=this.defaultConfig.getTotalDisk(); i+)totalNumberChoice.add(i + );totalNumberChoice.setBounds(250, defaultConfig.getGamePanelHeig

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论