package auto; /**
* IDA*求解15puzzle问题
* IDA*整合了IDDFS和A*算法。其中IDDFS控制了求解过程中的内存开销,A*算法意味着"启发式"搜索。
* IDA*可以理解成迭代深度的A*算法,其中这里的"深度"指的是问题的解的"假定损耗"。
* 使"假定损耗"不断迭代增加,检验是否能在"假定损耗"的前提找到可行解,如果不行的话,就继续迭代。
* 这里和A*算法不同的是没有开放列表,由于采用了IDDFS的策略,IDA*是深度优先搜索的,故此没有开放列表。
* @author wly
* @date 2013-12-20
*
*/
public class IDAStarAlgorithm { //分别代表左、上、右、下四个移动方向的操作数
private int[] up = {-1,0};
private int[] down = {1,0};
private int[] left = {0,-1};
private int[] right = {0,1}; /**注意,这里UP和DOWN,LEFT和RIGHT必须是两两相对的,因为后面代码中使用
* ((dPrev != dCurr) && (dPrev%2 == dCurr%2))
* 来判断前后两个移动方向是否相反
*/
private final int UP = 0;
private final int DOWN = 2;
private final int LEFT = 1;
private final int RIGHT = 3; private int SIZE; //各个目标点坐标
private int[][] targetPoints; //用于记录移动步骤,存储0,1,2,3,对应上,下,左,右
private static int[] moves = new int[100000]; private static long ans = 0;; //当前迭代的"设想代价" //目标状态
private static int[][] tState = {
{1 ,2 ,3 ,4 ,5} ,
{6 ,7 ,8 ,9,10} ,
{11,12,13,14,15} ,
{16,17,18,19,20},
{21,22,23,24,0}
}; private static int[][] sState = {
{1,2,3,4,5},
{6,7,8,9,0},
{10,11,12,13,14},
{15,16,17,18,19},
{20,21,22,23,24}
}; //初始状态
// private static int[][] sState = {
// {12,1 ,10,2 } ,
// {7 ,11,4 ,14} ,
// {5 ,0 ,9 ,15} ,
// {8 ,13,6 ,3}
// }; private static int blank_row,blank_column; public IDAStarAlgorithm(int[][] state) {
SIZE = state.length;
targetPoints = new int[SIZE * SIZE][2]; this.sState = state;
//得到空格坐标
for(int i=0;i<state.length;i++) {
for(int j=0;j<state[i].length;j++) {
if(state[i][j] == 0) {
blank_row = i;
blank_column = j;
break;
}
}
} //得到目标点坐标数组
for(int i=0;i<state.length;i++) {
for(int j=0;j<state.length;j++) {
targetPoints[tState[i][j]][0] = i; //行信息 targetPoints[tState[i][j]][1] = j; //列信息
}
}
for(int i=0;i<moves.length;i++) {
moves[i] = -1;
}
} /**
* 讨论问题的可解性
* @param state 状态
*/
private boolean canSolve(int[][] state) {
if(state.length % 2 == 1) { //问题宽度为奇数
return (getInversions(state) % 2 == 0);
} else { //问题宽度为偶数
if((state.length - blank_row) % 2 == 1) { //从底往上数,空格位于奇数行
return (getInversions(state) % 2 == 0);
} else { //从底往上数,空位位于偶数行
return (getInversions(state) % 2 == 1);
}
}
} public static void main(String[] args) { IDAStarAlgorithm idaAlgorithm = new IDAStarAlgorithm(sState);
if(idaAlgorithm.canSolve(sState)) {
System.out.println("--问题可解,开始求解--");
//以曼哈顿距离为初始最小代价数
int j = idaAlgorithm.getHeuristic(sState);
System.out.println("初始manhattan距离:" + j);
int i = -1;//置空默认移动方向 long time = System.currentTimeMillis();
//迭代加深"最小代价数"
for(ans=j;;ans++) {
if(idaAlgorithm.solve(sState
,blank_row,blank_column,0,i,j)) {
break;
}
}
System.out.println("求解用时:"+(System.currentTimeMillis() - time)); idaAlgorithm.printMatrix(sState);
int[][] matrix = idaAlgorithm.move(sState,moves[0]);
for(int k=1;k<ans;k++) {
matrix = idaAlgorithm.move(matrix, moves[k]);
} } else {
System.out.println("--抱歉!输入的问题无可行解--");
}
} public int[] getSolvePath(int[][] data) {
for(int i=0;i<moves.length;i++) {
moves[i] = -1;
}
if(canSolve(data)) {
System.out.println("--问题可解,开始求解--");
//以曼哈顿距离为初始最小代价数
int j = getHeuristic(data);
System.out.println("初始manhattan距离:" + j);
int i = -1;//置空默认移动方向 long time = System.currentTimeMillis();
//迭代加深"最小代价数"
for(ans=j;;ans++) {
if(solve(data
,blank_row,blank_column,0,i,j)) {
break;
}
}
System.out.println("求解用时:"+(System.currentTimeMillis() - time)); // printMatrix(data);
// int[][] matrix = move(data,moves[0]);
// for(int k=1;k<ans;k++) {
// matrix = move(matrix, moves[k]);
// }
return moves;
} else {
System.out.println("--抱歉!输入的问题无可行解--");
return null;
}
} public int[][] move(int[][]state,int direction) {
int row = 0;
int column = 0;
for(int i=0;i<state.length;i++) {
for(int j=0;j<state.length;j++) {
if(state[i][j] == 0) {
row = i;
column = j;
}
}
}
switch(direction) {
case UP:
state[row][column] = state[row-1][column];
state[row-1][column] = 0;
break;
case DOWN:
state[row][column] = state[row+1][column];
state[row+1][column] = 0;
break;
case LEFT:
state[row][column] = state[row][column-1];
state[row][column-1] = 0;
break;
case RIGHT:
state[row][column] = state[row][column+1];
state[row][column+1] = 0;
break;
}
printMatrix(state);
return state;
} public void printMatrix(int[][] matrix) {
System.out.println("------------");
for(int i=0;i<matrix.length;i++) {
for(int j=0;j<matrix.length;j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
} /**
* 求解方法
* @param state 当前状态
* @param blank_row 空位的行坐标
* @param blank_column 空格的列坐标
* @param dep 当前深度
* @param d 上一次移动的方向
* @param h 当前状态估价函数
* @return
*/
public boolean solve(int[][] state,int blank_row,int blank_column,
int dep,long d,long h) { long h1; //和目标矩阵比较,看是否相同,如果相同则表示问题已解
boolean isSolved = true;
for(int i=0;i<SIZE;i++) {
for(int j=0;j<SIZE;j++) {
if(state[i][j] != tState[i][j]) {
isSolved = false;
}
}
}
if(isSolved) {
return true;
} if(dep == ans) {
return false;
} //用于表示"空格"移动后的坐标位置
int blank_row1 = blank_row;
int blank_column1 = blank_column;
int[][] state2 = new int[SIZE][SIZE]; for(int direction=0;direction<4;direction++) {
for(int i=0;i<state.length;i++) {
for(int j=0;j<state.length;j++) {
state2[i][j] = state[i][j];
}
} //本地移动方向和上次移动方向刚好相反,跳过这种情况的讨论
if(direction != d && (d%2 == direction%2)) {
continue;
} if(direction == UP) {
blank_row1 = blank_row + up[0];
blank_column1 = blank_column + up[1];
} else if(direction == DOWN) {
blank_row1 = blank_row + down[0];
blank_column1 = blank_column + down[1];
} else if(direction == LEFT) {
blank_row1 = blank_row + left[0];
blank_column1 = blank_column + left[1];
} else {
blank_row1 = blank_row + right[0];
blank_column1 = blank_column + right[1];
} //边界检查
if(blank_column1 < 0 || blank_column1 == SIZE
|| blank_row1 < 0 || blank_row1 == SIZE) {
continue ;
} //交换空格位置和当前移动位置对应的单元格
state2[blank_row][blank_column] = state2[blank_row1][blank_column1];
state2[blank_row1][blank_column1] = 0; //查看当前空格是否正在靠近目标点
if(direction == DOWN && blank_row1
> targetPoints[state[blank_row1][blank_column1]][0]) {
h1 = h - 1;
} else if(direction == UP && blank_row1
< targetPoints[state[blank_row1][blank_column1]][0]){
h1 = h - 1;
} else if(direction == RIGHT && blank_column1
> targetPoints[state[blank_row1][blank_column1]][1]) {
h1 = h - 1;
} else if(direction == LEFT && blank_column1
< targetPoints[state[blank_row1][blank_column1]][1]) {
h1 = h - 1;
} else {
//这种情况发生在任意可能的移动方向都会使得估价函数值变大
h1 = h + 1;
} if(h1+dep+1>ans) { //剪枝
continue;
} moves[dep] = direction; //迭代深度求解
if(solve(state2, blank_row1, blank_column1, dep+1, direction, h1)) {
return true;
}
}
return false;
} /**
* 得到估价函数值
*/
public int getHeuristic(int[][] state) {
int heuristic = 0;
for(int i=0;i<state.length;i++) {
for(int j=0;j<state[i].length;j++) {
if(state[i][j] != 0) {
heuristic = heuristic +
Math.abs(targetPoints[state[i][j]][0] - i)
+ Math.abs(targetPoints[state[i][j]][1] - j); }
}
}
return heuristic;
} /**
* 计算问题的"倒置变量和"
* @param state
*/
private int getInversions(int[][] state) {
int inversion = 0;
int temp = 0;
for(int i=0;i<state.length;i++) {
for(int j=0;j<state[i].length;j++) {
int index = i* state.length + j + 1;
while(index < (state.length * state.length)) {
if(state[index/state.length][index%state.length] != 0
&& state[index/state.length]
[index%state.length] < state[i][j]) {
temp ++;
}
index ++;
}
inversion = temp + inversion;
temp = 0;
}
}
return inversion;
} }
package auto; import java.awt.BorderLayout;
import java.awt.EventQueue; import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder; import java.awt.GridLayout; import javax.swing.JLabel; import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Color; import javax.swing.JButton; import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Stack; public class playGame extends JFrame { private JPanel contentPane;
/* private static JLabel p1;
private static JLabel p2;
private static JLabel p3;
private static JLabel p4;
private static JLabel p5;
private static JLabel p6;
private static JLabel p7;
private static JLabel p8;
private static JLabel p9;
*/
private final int UP = 0;
private final int DOWN = 2;
private final int LEFT = 1;
private final int RIGHT = 3;
static JLabel[] p=new JLabel[10];
static JLabel[][] pa;
private int start=0;
static int now;
private static String[] na;
private static String[] ni;
static String[] nn;
static HashSet<String> s;
//static String ans="123456789";
static int rear;
static int[] a;
static final int size=3;
static int [][] state;
static int [][] tstate;
static int[] moves=new int[100010];
static int ans;
public static int blank_row,blank_column;
public static playGame frame ;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new playGame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
* Create the frame.
*/
public playGame() {
setForeground(Color.CYAN);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0, 0, 450, 300);
setSize(600,530);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane); p[1] = new JLabel("\u56FE\u7247");
p[1].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(canswap(1)){
swap(p[1],p[now]);
swap(1,now);
now=1; if(iscorrect()){
p[now].setVisible(true);
JOptionPane.showMessageDialog(null, "恭喜拼图成功");
}
}
} });
GridBagConstraints gbc_p1 = new GridBagConstraints();
gbc_p1.gridwidth = 2;
gbc_p1.gridheight = 2;
gbc_p1.insets = new Insets(0, 0, 5, 5);
gbc_p1.gridx = 1;
gbc_p1.gridy = 1;
contentPane.add(p[1], gbc_p1); p[2] = new JLabel("\u56FE\u7247");
p[2].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(canswap(2)){
swap(p[2],p[now]);
swap(2,now);
now=2; if(iscorrect()){
p[now].setVisible(true);
JOptionPane.showMessageDialog(null, "恭喜拼图成功");
}
}
}
});
GridBagConstraints gbc_p2 = new GridBagConstraints();
gbc_p2.gridheight = 2;
gbc_p2.anchor = GridBagConstraints.NORTHWEST;
gbc_p2.gridwidth = 2;
gbc_p2.insets = new Insets(0, 0, 5, 5);
gbc_p2.gridx = 3;
gbc_p2.gridy = 1;
contentPane.add(p[2], gbc_p2); p[3] = new JLabel("\u56FE\u7247");
p[3].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(canswap(3)){
swap(p[3],p[now]);
swap(3,now);
now=3; if(iscorrect()){
p[now].setVisible(true);
JOptionPane.showMessageDialog(null, "恭喜拼图成功");
}
}
}
});
GridBagConstraints gbc_p3 = new GridBagConstraints();
gbc_p3.gridheight = 2;
gbc_p3.gridwidth = 2;
gbc_p3.insets = new Insets(0, 0, 5, 5);
gbc_p3.gridx = 5;
gbc_p3.gridy = 1;
contentPane.add(p[3], gbc_p3); JButton last = new JButton("\u4E0A\u4E00\u5F20");
last.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(start<0)
start=start+3;
init(start);
}
});
GridBagConstraints gbc_last = new GridBagConstraints();
gbc_last.insets = new Insets(0, 0, 5, 0);
gbc_last.gridx = 11;
gbc_last.gridy = 1;
contentPane.add(last, gbc_last); p[4] = new JLabel("\u56FE\u7247");
p[4].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(canswap(4)){
swap(p[4],p[now]);
swap(4,now);
now=4; if(iscorrect()){
p[now].setVisible(true);
JOptionPane.showMessageDialog(null, "恭喜拼图成功");
}
}
}
});
GridBagConstraints gbc_p4 = new GridBagConstraints();
gbc_p4.gridwidth = 2;
gbc_p4.gridheight = 2;
gbc_p4.insets = new Insets(0, 0, 5, 5);
gbc_p4.gridx = 1;
gbc_p4.gridy = 3;
contentPane.add(p[4], gbc_p4); p[5] = new JLabel("\u56FE\u7247");
p[5].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(canswap(5)){
swap(p[5],p[now]);
swap(5,now);
now=5; if(iscorrect()){
p[now].setVisible(true);
JOptionPane.showMessageDialog(null, "恭喜拼图成功");
}
}
}
});
GridBagConstraints gbc_p5 = new GridBagConstraints();
gbc_p5.gridwidth = 2;
gbc_p5.gridheight = 2;
gbc_p5.insets = new Insets(0, 0, 5, 5);
gbc_p5.gridx = 3;
gbc_p5.gridy = 3;
contentPane.add(p[5], gbc_p5); p[6] = new JLabel("\u56FE\u7247");
p[6].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(canswap(6)){
swap(p[6],p[now]);
swap(6,now);
now=6; if(iscorrect()){
p[now].setVisible(true);
JOptionPane.showMessageDialog(null, "恭喜拼图成功");
}
}
}
});
GridBagConstraints gbc_p6 = new GridBagConstraints();
gbc_p6.gridwidth = 2;
gbc_p6.gridheight = 2;
gbc_p6.insets = new Insets(0, 0, 5, 5);
gbc_p6.gridx = 5;
gbc_p6.gridy = 3;
contentPane.add(p[6], gbc_p6); JButton next = new JButton("\u4E0B\u4E00\u5F20");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start++;
if(start>=3)
start-=3;
init(start);
}
});
GridBagConstraints gbc_next = new GridBagConstraints();
gbc_next.insets = new Insets(0, 0, 5, 0);
gbc_next.gridx = 11;
gbc_next.gridy = 3;
contentPane.add(next, gbc_next); p[7] = new JLabel("\u56FE\u7247");
p[7].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(canswap(7)){
swap(p[7],p[now]);
swap(7,now);
now=7;
if(iscorrect()){
p[now].setVisible(true);
JOptionPane.showMessageDialog(null, "恭喜拼图成功");
}
}
}
});
GridBagConstraints gbc_p7 = new GridBagConstraints();
gbc_p7.gridwidth = 2;
gbc_p7.gridheight = 2;
gbc_p7.insets = new Insets(0, 0, 5, 5);
gbc_p7.gridx = 1;
gbc_p7.gridy = 5;
contentPane.add(p[7], gbc_p7); p[8] = new JLabel("\u56FE\u7247");
p[8].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(canswap(8)){
swap(p[8],p[now]);
swap(8,now);
now=8;
if(iscorrect()){
p[now].setVisible(true);
JOptionPane.showMessageDialog(null, "恭喜拼图成功");
}
}
}
});
GridBagConstraints gbc_p8 = new GridBagConstraints();
gbc_p8.gridwidth = 2;
gbc_p8.gridheight = 2;
gbc_p8.insets = new Insets(0, 0, 5, 5);
gbc_p8.gridx = 3;
gbc_p8.gridy = 5;
contentPane.add(p[8], gbc_p8); p[9] = new JLabel("\u56FE\u7247");
p[9].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(canswap(9)){
swap(p[9],p[now]);
swap(9,now);
now=9; if(iscorrect()){
p[now].setVisible(true);
JOptionPane.showMessageDialog(null, "恭喜拼图成功");
}
}
}
});
GridBagConstraints gbc_p9 = new GridBagConstraints();
gbc_p9.gridwidth = 2;
gbc_p9.gridheight = 2;
gbc_p9.insets = new Insets(0, 0, 5, 5);
gbc_p9.gridx = 5;
gbc_p9.gridy = 5;
contentPane.add(p[9], gbc_p9); JButton auto = new JButton("\u81EA\u52A8\u6F14\u793A");
auto.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pa=new JLabel[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
pa[i][j]=p[i*3+j+1];
}
}
gettstate();
AutoRunner au=new AutoRunner(tstate);
if(au.canSolve(tstate)){
System.out.println("--问题可解,开始求解--");
//以曼哈顿距离为初始最小代价数
int j = au.getHeuristic(tstate);
System.out.println("初始manhattan距离:" + j);
int i = -1;//置空默认移动方向 long time = System.currentTimeMillis();
//迭代加深"最小代价数"
for(au.ans=j;;au.ans++) {
if(au.solve(tstate
,blank_row,blank_column,0,i,j)) {
break;
}
}
moves=au.getmoves(); JOptionPane.showMessageDialog(null,"求解用时:"+(System.currentTimeMillis() - time)/1000.0); //JOptionPane.showMessageDialog(null, "第"+(0)+"步!");
for(int k=0;k<au.ans;k++){
tstate=move(tstate,moves[k]);
for(i=0;i<tstate.length;i++){
for(j=0;j<tstate[i].length;j++){
if(tstate[i][j]==0){
pa[i][j].setVisible(false);
}
else{
pa[i][j].setVisible(true);
}
}
} JOptionPane.showMessageDialog(null, "第"+(k+1)+"步!");
try{
Thread thread = Thread.currentThread();
thread.sleep(1000);//暂停1.5秒后程序继续执行
}catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} /* try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
*/
print(tstate);
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
pa[i][j].setVisible(true);
} }
}else { JOptionPane.showMessageDialog(null,"--抱歉!输入的问题无可行解--");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
p[i*3+j+1]=pa[i][j];
}
}
init(start);
} for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
p[i*3+j+1]=pa[i][j];
}
}
}
/*int[][] matrix = au.move(tstate,moves[0]);
for(int k=1;k<au.ans;k++) {
matrix = au.move(matrix, moves[k]);
} } else {
System.out.println("--抱歉!输入的问题无可行解--");
}
*/
});
GridBagConstraints gbc_auto = new GridBagConstraints();
gbc_auto.insets = new Insets(0, 0, 5, 0);
gbc_auto.gridx = 11;
gbc_auto.gridy = 5;
contentPane.add(auto, gbc_auto); JButton button = new JButton("\u672C\u5F20");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
init(start);
}
});
GridBagConstraints gbc_button = new GridBagConstraints();
gbc_button.gridx = 11;
gbc_button.gridy = 7;
contentPane.add(button, gbc_button);
playGame.init(start);
} /* public static void show1(int[][] tstate2) {
// TODO Auto-generated method stub
for(int i=0;i<tstate2.length;i++){
for(int j=0;j<tstate2[i].length;j++){
if(state[i][j]==0){
pa[i][j].setVisible(false);
}
else{
pa[i][j].setVisible(true);
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/
public static void print(int[][] matrix){
System.out.println("------------");
for(int i=0;i<matrix.length;i++) {
for(int j=0;j<matrix.length;j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
} public int[][] move(int[][]state1,int direction) {
int row = 0;
int column = 0;
for(int i=0;i<state1.length;i++) {
for(int j=0;j<state1.length;j++) {
if(state1[i][j] == 0) {
row = i;
column = j;
}
}
}
switch(direction) {
case UP:
swap(pa[row-1][column],pa[row][column]); state1[row][column] = state1[row-1][column];
state1[row-1][column] = 0;
break;
case DOWN:
swap(pa[row+1][column],pa[row][column]); state1[row][column] = state1[row+1][column];
state1[row+1][column] = 0;
break;
case LEFT:
swap(pa[row][column-1],pa[row][column]); state1[row][column] = state1[row][column-1];
state1[row][column-1] = 0;
break;
case RIGHT:
swap(pa[row][column+1],pa[row][column]); state1[row][column] = state1[row][column+1];
state1[row][column+1] = 0;
break;
}
return state1;
} public static void gettstate(){
tstate=new int[size][size];
// int t=Integer.parseInt(""+ni[9].charAt(8));
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
char a1=ni[i*3+j+1].charAt(8);
String s1=""+a1;
if(a1=='9'){
tstate[i][j]=0;
blank_row=i;
blank_column=j;
}
else{
tstate[i][j]=Integer.parseInt(s1);
}
}
} }
private static void init(int s) {
// TODO Auto-generated method stub
char a='a';
a+=s;
na=new String[15];
ni=new String[15];
for(int i=1;i<10;i++){
na[i]="images/"+a+i+".jpg";
//System.out.println(na[i]);
}
int m[]=new int[10]; //随机产生10个不同的数(1,9);
for(int i=1;i<10;i++){
int n=(int) (Math.random()*1000%9+1);
while(canputin(m,i,n)==false){
n=(int) (Math.random()*1000%9+1);
}
m[i]=n;
System.out.println(n);
ni[n]=na[i]; //插入方式i是位置,na[n]是图片
//correct 方法是第一个位置为图片1.。。
}
for(int i=1;i<10;i++){
System.out.println(ni[i]);
}
state=new int[size][size];
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
char a1=na[i*3+j+1].charAt(8);
String s1=""+a1;
state[i][j]=Integer.parseInt(s1);
}
}
state[size-1][size-1]=0;
for(int i=1;i<10;i++)
{
p[i].setIcon(new ImageIcon(ni[i]));
p[i].setText(""); } for(int i=1;i<9;i++){
p[i].setVisible(true);
}
p[9].setVisible(false);
now=9; } private static boolean canputin(int[] m, int s, int n) {
// TODO Auto-generated method stub
for(int i=1;i<s;i++){
if(m[i]==n)
return false;
}
return true;
} //swap中b为setVisible(false); public static void swap(int m,int n){
String ss=ni[m];
ni[m]=ni[n];
ni[n]=ss;
}
private static boolean iscorrect(){
for(int i=1;i<10;i++){
if(!na[i].equals(ni[i]))
return false;
}
return true;
} public static boolean canswap(int i){
if(i+1==now||i-1==now||i+3==now||i-3==now)
return true;
return false;
} public static void swap(JLabel a,JLabel b){
JLabel c=new JLabel();
c.setIcon(a.getIcon());
a.setIcon(b.getIcon());
b.setIcon(c.getIcon());
a.setVisible(false);
b.setVisible(true);
} private static boolean correct(String[] nt) {
// TODO Auto-generated method stub
for(int i=1;i<10;i++){
if(!na[i].equals(nt[i])){
return false;
}
}
return true;
} private static boolean canplay() {
// TODO Auto-generated method stub
for(int i=1;i<10;i++){
if(!ni[i].equals(na[i]))
return false;
}
return true;
}
}
只想说目前封装的实在是太烂了!