資源簡介
基于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? 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()?????????????????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? 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 圖書管理系統源碼
評論
共有 條評論