MyFirstgame 拼图
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;
}
}
只想说目前封装的实在是太烂了!
MyFirstgame 拼图的更多相关文章
- 拼图小游戏之计算后样式与CSS动画的冲突
先说结论: 前几天写了几个非常简单的移动端小游戏,其中一个拼图游戏让我郁闷了一段时间.因为要获取每张图片的位置,用`<style>`标签写的样式,直接获取计算后样式再用来交换位置,结果就悲 ...
- JavaScript版拼图小游戏
慕课网上准备开个新的jQuery教程,花了3天空闲时间写了一个Javascript版的拼图小游戏,作为新教程配套的分析案例 拼图游戏网上有不少的实现案例了,但是此源码是我自己的实现,所以不做太多的比较 ...
- 利用Vue.js实现拼图游戏
之前写过一篇<基于Vue.js的表格分页组件>的文章,主要介绍了Vue组件的编写方法,有兴趣的可以访问这里进行阅读:http://www.cnblogs.com/luozhihao/p/5 ...
- WPF学习系列 游戏-选张图片做成9宫格拼图
今天要学习一个拼图项目. 目标是传入一张图片,然后将它分成9份,去掉一份,鼠标点击进行拼图. 源文件结构很简单 第一步.新建项目 这一步没什么好说的,新建一个项目就跟源文件结构一样了 第二步.页面布局 ...
- 原生js完成拼图小游戏
废话不说,看代码,图片可以自己找,我这直接引用了百度的了 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml ...
- ACM: NBUT 1105 多连块拼图 - 水题 - 模拟
NBUT 1105 多连块拼图 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format: Practice Appoint ...
- JavaScript拼图游戏
今天是2016年最后一天上班了.最近几天都比较休闲,有时间空闲下来写写文档之类的. 2016过得真是快.感觉没做什么就过去了.想到之前想坚持每个月写一写博客都没坚持到.希望2017年可以吧. 无聊之余 ...
- 关于有偿提供拼图响应式后台的通知---------pintuer ui的官方通知(www.pintuer.com)
拼图响应式前端框架版响应式后台正式发布. 考虑到目前拼图的状况,我们不打算免费开放下载,但也不会收各位朋友1分钱,该版后台将有偿提供给各位给予拼图贡献的朋友. 废话不多说,一切皆以有图有真相,下面上图 ...
- 2016国内最值得期待的响应式前端框架pintuer(拼图)--http://www.pintuer.com
近期,需要将项目从pc端的应用扩展到移动端. 当然移动框架的第一选择必然是bootstrap,但是bootstrap作为移动端明显过于死板,而且作为国外的产品,对于国内的应用明显水土不服.框架里总有那 ...
随机推荐
- 基于JSON的级联列表实现
html代码: <select id="provice" onChange="setCity()"> <option value=" ...
- 怎样用SQL语句查询一个数据库中的所有表?
怎样用SQL语句查询一个数据库中的所有表? --读取库中的所有表名 select name from sysobjects where xtype='u'--读取指定表的所有列名select nam ...
- iOS 自动化打包
理想的情况:不打开工程的情况下,直接双击就能打包出一个IPA文件,这样就可以让测试直接使用 itools 进行安装. 分如下两种情况: 1)不依赖cocoapod 管理项目的自动化. 如果没有一个简 ...
- putty连接报NetWork error:connection refused
首先通过物理终端进入到linux上,手工检查ssh发现没运行 /etc/init.d/sshd status 使用rpm -V 命令可检查到ssh的软件包正常 /rpm -V openssh-serv ...
- Python学习路程day19
Python之路,Day19 - Django 进阶 本节内容 自定义template tags 中间件 CRSF 权限管理 分页 Django分页 https://docs.djangoproj ...
- 2016 2 - 23 arc中的所有权修饰符(_strong修饰符与_weak修饰符)
一 _strong修饰符 1._strong修饰符是id类型和对象类型默认的所有权修饰符.如下: id obj = [[NSObject alloc] init];//在没用明确变量所有权修饰符时,会 ...
- 关于selenium2(webdriver)自动化测试过程中标签页面或者窗口切换的处理解决方案
1. 通过页面或者window 的name切换: switch_to_frame(name) switch_to_window(name) 那么问题来了,出现2个或者以上窗口时候,新打开的windo ...
- 【C编译器】MinGw安装与使用(调试问题待续)
不想装vs2005之类的,想要一个轻量级的C语言编译器,希望将焦点放在如何写好代码上: 本人信奉:代码质量是靠设计和检视保证的,不是靠调试: 1.安装MinGW http://www.mingw.or ...
- NSOperation基本概念
NSOperation的作用 配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤 先将 ...
- PHP易混淆函数的区分方法及意义
1.echo和print的区别 PHP中echo和print的功能基本相同(输出),但是两者之间还是有细微差别的.echo输出后没有返回值,但print有返回值,当其执行失败时返回flase.因此 ...