📄 puzzle8.java
字号:
import java.awt.*;
import java.awt.event.*;
/*import SearchTree;
/*import awb.*;*/
import javax.swing.*;
/*<applet code=Puzzle8.class width=200 height=200>
</applet>
*/
public class Puzzle8 extends JApplet implements ActionListener
{
int[] searchdata;
int[] puzzle;
Button bSearch, bReset, bSample, bTrace;
IntField[] puzzlefd;
IntField timefd, lookupsfd, stepsfd;
SearchTree search;
boolean trace = true;
boolean verPuzzle(int[] inpuzzle) // verify if puzzle is filled correctly
{
int[] inpuzzlecpy = new int[9]; // use copy
for(int i=0; i<9; i++)
{
inpuzzlecpy[i] = inpuzzle[i];
}
java.util.Arrays.sort(inpuzzlecpy); // sort array
if(inpuzzlecpy[8]>8 || inpuzzlecpy[0]<0) // check for invalid values
return false;
for(int i=0; i<8; i++)
{
if(inpuzzlecpy[i] == inpuzzlecpy[i+1]) // check for double values
return false;
}
return true;
}
public void init ()
{
searchdata = new int[3];
search = new SearchTree();
puzzle = new int[12];
puzzlefd = new IntField[9];
timefd = new IntField(5);
stepsfd = new IntField(5);
lookupsfd = new IntField(5);
timefd.setLabel("Time to solve:");
stepsfd.setLabel("Steps to solve:");
lookupsfd.setLabel("Moves tested:");
Container contentPane = getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel r = new JPanel();
r= new JPanel();
JPanel b = new JPanel();
b= new JPanel();
r.setMaximumSize(new Dimension(160, 960));
b.setBorder(BorderFactory.createEmptyBorder(55, 0, 0, 0));
for(int i=0; i<9; i++)
{
puzzlefd[i] = new IntField(2);
puzzlefd[i].setInt(i);
r.add(puzzlefd[i]);
}
bSearch = new Button ("Search");
bSearch.addActionListener(this);
bReset = new Button ("Reset");
bReset.addActionListener(this);
bSample = new Button ("Sample Board");
bSample.addActionListener(this);
bTrace = new Button ("Trace ON");
bTrace.addActionListener(this);
contentPane.add(r);
b.add(bSearch);
b.add(bReset);
b.add(bSample);
b.add(bTrace);
b.add(timefd);
b.add(stepsfd);
b.add(lookupsfd);
contentPane.add(b);
}
public void actionPerformed(ActionEvent event)
{
Object cause = event.getSource();
if (cause == bSearch) //do bfsearch
{
for(int i=0; i<9; i++)
{puzzle[i]=puzzlefd[i].getInt();}
if(verPuzzle(puzzle))
{
searchdata = search.bfs(puzzle,800);
timefd.setInt(searchdata[0]);
lookupsfd.setInt(searchdata[1]);
stepsfd.setInt(searchdata[2]);
}
else
JOptionPane.showMessageDialog(this, "No valid 8-puzzle!",
"Error",JOptionPane.ERROR_MESSAGE);
for(int i=0; i<9; i++)
{puzzlefd[i].setInt(puzzle[i]);}
}
if (cause == bReset) //reset input
{
for(int i=0; i<9; i++)
{
puzzlefd[i].setInt(i);
}
}
if (cause == bSample) //set sample puzzle input
{
puzzlefd[0].setInt(0);
puzzlefd[1].setInt(3);
puzzlefd[2].setInt(4);
puzzlefd[3].setInt(1);
puzzlefd[4].setInt(8);
puzzlefd[5].setInt(2);
puzzlefd[6].setInt(6);
puzzlefd[7].setInt(7);
puzzlefd[8].setInt(5);
}
if (cause == bTrace) //switch tracing mode
{
trace = !trace;
search.setTrace(trace);
if(trace)
bTrace.setLabel("Trace ON");
else
bTrace.setLabel("Trace OFF");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -