资源简介
基于java的八数码DFS搜索算法以及界面代码,eclipse项目文件,欢迎交流!!!
代码片段和文件信息
package 人工稚嫩;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class EightNumframe extends frame implements ActionListenerKeyListener
{
Button restart = new Button(“随机打乱“);//在下拉菜单中设置选项
Button nextPath = new Button(“提示“);
Button printPath = new Button(“开始“);
Button exit = new Button(“退出程序“);
Button path = new Button(“步数“);
Button[] button;
Panel panelpanel1;
int rowcol;
private static int positioncellNum;
final int dr[] = { 0-1 0 1};
final int dc[] = {-1 0 1 0};
public EightNumframe(int rowint col) {
try {
UIManager.setLookAndFeel(“com.sun.java.swing.plaf.windows.WindowsLookAndFeel“);
} catch (Exception e) {
e.printStackTrace();
}
this.row = row;
this.col = col;
cellNum = row*col;
restart.addActionListener(this);
exit.addActionListener(this);
nextPath.addActionListener(this);
printPath.addActionListener(this);
path.addActionListener(this);
panel1 = new Panel(new GridLayout(13)) ;
panel1.add(restart);
panel1.add(nextPath);
panel1.add(path);
panel1.add(printPath);
panel1.add(exit);
panel = new Panel(new GridLayout(rowcol)) ;
button = new Button[cellNum];
for(int i = 0; i < cellNum; i++) {
if(i == cellNum - 1) {
button[i] = new Button(“ “);
}else {
button[i] = new Button(String.valueOf(i + 1));
}
button[i].setFont(new Font(“Courier“ 1 20));
button[i].addActionListener(this);
button[i].addKeyListener(this);
panel.add(button[i]);
}
position = cellNum - 1;
this.add(BorderLayout.NORTHpanel1);
this.add(BorderLayout.CENTERpanel);
this.settitle(“八数码“);
this.setVisible(true);
this.setSize(300300);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width/2;
int screenHeight = screenSize.height/2;
int height = this.getHeight();
int width = this.getWidth();
this.setLocation(screenWidth-width/2 screenHeight-height/2);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
void start() {
int a[] = new int[9];
do {
int k = 0;
Random random=new Random();
Set set=new HashSet();
while(set.size() < cellNum-1) {
int n=random.nextInt(cellNum-1)+1;
if(!set.contains(n)) {
set.add(n);
a[k++] = n;
}
}
a[k] = 0;
}while(!EightNumPath.isok(a));
for(int i = 0; i < 9; i++)
button[i].setLabel(String.valueOf(a[i]));
button[cellNum-1].setLabel(“ “);
position = cellNum - 1;
}
boolean win() {
for(int i = 0; i < cellNum - 1; i++) {
if(button[i].getLabel().equals(“ “)) {
return false;
}else if(Integer.valueOf(button[i].getLabel()) != i+1) {
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-04-14 18:37 八数码问题DFS搜索及界面\
文件 301 2017-04-11 21:12 八数码问题DFS搜索及界面\.classpath
文件 388 2017-04-11 21:12 八数码问题DFS搜索及界面\.project
目录 0 2018-04-14 18:37 八数码问题DFS搜索及界面\.settings\
文件 598 2017-04-11 21:12 八数码问题DFS搜索及界面\.settings\org.eclipse.jdt.core.prefs
目录 0 2018-04-14 18:37 八数码问题DFS搜索及界面\bin\
目录 0 2018-04-14 18:37 八数码问题DFS搜索及界面\bin\人工稚嫩\
文件 717 2017-04-13 08:57 八数码问题DFS搜索及界面\bin\人工稚嫩\EightNumfr
文件 7612 2017-04-13 08:57 八数码问题DFS搜索及界面\bin\人工稚嫩\EightNumfr
文件 2998 2017-04-11 21:13 八数码问题DFS搜索及界面\bin\人工稚嫩\EightNumPath.class
目录 0 2018-04-14 18:37 八数码问题DFS搜索及界面\src\
目录 0 2018-04-14 18:37 八数码问题DFS搜索及界面\src\人工稚嫩\
文件 6567 2017-04-13 08:57 八数码问题DFS搜索及界面\src\人工稚嫩\EightNumfr
文件 2518 2017-04-11 21:13 八数码问题DFS搜索及界面\src\人工稚嫩\EightNumPath.java
- 上一篇:决策树Java代码实现
- 下一篇:java 图书管理系统源码
评论
共有 条评论