📄 puzzle8.java
字号:
if (square[2][0] == 1) {
md += 2;
}
else if (square[2][0] == 2) {
md += 3;
}
else if (square[2][0] == 3) {
md += 4;
}
else if (square[2][0] == 4) {
md += 3;
}
else if (square[2][0] == 5) {
md += 2;
}
else if (square[2][0] == 6) {
md += 1;
}
else if (square[2][0] == 7) {
md += 0;
}
else if (square[2][0] == 8) {
md += 1;
}
if (square[2][1] == 1) {
md += 3;
}
else if (square[2][1] == 2) {
md += 2;
}
else if (square[2][1] == 3) {
md += 3;
}
else if (square[2][1] == 4) {
md += 2;
}
else if (square[2][1] == 5) {
md += 1;
}
else if (square[2][1] == 6) {
md += 0;
}
else if (square[2][1] == 7) {
md += 1;
}
else if (square[2][1] == 8) {
md += 2;
}
if (square[2][2] == 1) {
md += 4;
}
else if (square[2][2] == 2) {
md += 3;
}
else if (square[2][2] == 3) {
md += 2;
}
else if (square[2][2] == 4) {
md += 1;
}
else if (square[2][2] == 5) {
md += 0;
}
else if (square[2][2] == 6) {
md += 1;
}
else if (square[2][2] == 7) {
md += 2;
}
else if (square[2][2] == 8) {
md += 3;
}
return md;
}
int expand(int[][] square, int[][][] tempsquare) {
int b = 0;
int i, j, k, col = -1, row = -1;
for (i = 0; i < 4; i++) {
for (j = 0; j < 3; j++) {
for (k = 0; k < 3; k++) {
tempsquare[i][j][k] = square[j][k];
}
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (square[i][j] == 0) {
row = i;
col = j;
break;
}
}
}
if (row == 0 && col == 0) {
tempsquare[0][0][0] = tempsquare[0][0][1];
tempsquare[0][0][1] = 0;
tempsquare[1][0][0] = tempsquare[1][1][0];
tempsquare[1][1][0] = 0;
b = 2;
}
else if (row == 0 && col == 1) {
tempsquare[0][0][1] = tempsquare[0][0][0];
tempsquare[0][0][0] = 0;
tempsquare[1][0][1] = tempsquare[1][1][1];
tempsquare[1][1][1] = 0;
tempsquare[2][0][1] = tempsquare[2][0][2];
tempsquare[2][0][2] = 0;
b = 3;
}
else if (row == 0 && col == 2) {
tempsquare[0][0][2] = tempsquare[0][0][1];
tempsquare[0][0][1] = 0;
tempsquare[1][0][2] = tempsquare[1][1][2];
tempsquare[1][1][2] = 0;
b = 2;
}
else if (row == 1 && col == 0) {
tempsquare[0][1][0] = tempsquare[0][0][0];
tempsquare[0][0][0] = 0;
tempsquare[1][1][0] = tempsquare[1][1][1];
tempsquare[1][1][1] = 0;
tempsquare[2][1][0] = tempsquare[2][2][0];
tempsquare[2][2][0] = 0;
b = 3;
}
else if (row == 1 && col == 1) {
tempsquare[0][1][1] = tempsquare[0][1][0];
tempsquare[0][1][0] = 0;
tempsquare[1][1][1] = tempsquare[1][0][1];
tempsquare[1][0][1] = 0;
tempsquare[2][1][1] = tempsquare[2][1][2];
tempsquare[2][1][2] = 0;
tempsquare[3][1][1] = tempsquare[3][2][1];
tempsquare[3][2][1] = 0;
b = 4;
}
else if (row == 1 && col == 2) {
tempsquare[0][1][2] = tempsquare[0][0][2];
tempsquare[0][0][2] = 0;
tempsquare[1][1][2] = tempsquare[1][1][1];
tempsquare[1][1][1] = 0;
tempsquare[2][1][2] = tempsquare[2][2][2];
tempsquare[2][2][2] = 0;
b = 3;
}
else if (row == 2 && col == 0) {
tempsquare[0][2][0] = tempsquare[0][1][0];
tempsquare[0][1][0] = 0;
tempsquare[1][2][0] = tempsquare[1][2][1];
tempsquare[1][2][1] = 0;
b = 2;
}
else if (row == 2 && col == 1) {
tempsquare[0][2][1] = tempsquare[0][2][0];
tempsquare[0][2][0] = 0;
tempsquare[1][2][1] = tempsquare[1][1][1];
tempsquare[1][1][1] = 0;
tempsquare[2][2][1] = tempsquare[2][2][2];
tempsquare[2][2][2] = 0;
b = 3;
}
else if (row == 2 && col == 2) {
tempsquare[0][2][2] = tempsquare[0][2][1];
tempsquare[0][2][1] = 0;
tempsquare[1][2][2] = tempsquare[1][1][2];
tempsquare[1][1][2] = 0;
b = 2;
}
return b;
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
this.setTitle("puzzle game");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});
jLabel1.setBorder(BorderFactory.createRaisedBevelBorder());
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setBounds(new Rectangle(6, 3, 127, 104));
jLabel6.setBorder(BorderFactory.createRaisedBevelBorder());
jLabel6.setHorizontalAlignment(SwingConstants.CENTER);
jLabel9.setBorder(BorderFactory.createRaisedBevelBorder());
jLabel9.setHorizontalAlignment(SwingConstants.CENTER);
jLabel8.setBorder(BorderFactory.createRaisedBevelBorder());
jLabel8.setHorizontalAlignment(SwingConstants.CENTER);
jLabel7.setBorder(BorderFactory.createRaisedBevelBorder());
jLabel7.setHorizontalAlignment(SwingConstants.CENTER);
jLabel4.setBorder(BorderFactory.createRaisedBevelBorder());
jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
jLabel5.setBorder(BorderFactory.createRaisedBevelBorder());
jLabel5.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setBorder(BorderFactory.createRaisedBevelBorder());
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setBorder(BorderFactory.createRaisedBevelBorder());
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
begin.setBounds(new Rectangle(138, 338, 120, 53));
begin.setText("next");
begin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
position++;
if (position < moves) {
jLabel1.setText(Integer.toString(solutions[position][0]));
jLabel2.setText(Integer.toString(solutions[position][1]));
jLabel3.setText(Integer.toString(solutions[position][2]));
jLabel4.setText(Integer.toString(solutions[position][3]));
jLabel5.setText(Integer.toString(solutions[position][4]));
jLabel6.setText(Integer.toString(solutions[position][5]));
jLabel7.setText(Integer.toString(solutions[position][6]));
jLabel8.setText(Integer.toString(solutions[position][7]));
jLabel9.setText(Integer.toString(solutions[position][8]));
}
}
});
jLabel9.setBounds(new Rectangle(263, 204, 129, 96));
jLabel8.setBounds(new Rectangle(133, 204, 129, 96));
jLabel7.setBounds(new Rectangle(7, 204, 126, 96));
jLabel6.setBounds(new Rectangle(263, 107, 129, 96));
jLabel5.setBounds(new Rectangle(134, 107, 128, 97));
jLabel4.setBounds(new Rectangle(6, 109, 128, 94));
jLabel3.setBounds(new Rectangle(264, 0, 128, 106));
this.getContentPane().add(jLabel2);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jLabel4);
this.getContentPane().add(jLabel1);
this.getContentPane().add(jLabel5);
this.getContentPane().add(jLabel8);
this.getContentPane().add(jLabel6);
this.getContentPane().add(jLabel7);
this.getContentPane().add(jLabel9);
this.getContentPane().add(begin);
jLabel2.setBounds(new Rectangle(134, 1, 130, 106));
}
private void initialboard() {
int[] placed = new int[9];
int i = 0, j = 0, k;
for (i = 0; i < 9; i++) {
placed[i] = 0;
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
board[i][j] = 0;
}
}
Random random = new Random(new Date().getTime());
for (k = 0; k < 9; k++) {
boolean found = false;
int digit;
do {
digit = random.nextInt(9);
found = placed[digit] == 0;
if (found) {
placed[digit] = 1;
}
}
while (!found);
do {
i = random.nextInt(3);
j = random.nextInt(3);
found = board[i][j] == 0;
if (found) {
board[i][j] = digit;
}
}
while (!found);
}
}
long gettime(){
return time;
}
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JLabel jLabel8 = new JLabel();
JLabel jLabel9 = new JLabel();
JButton begin = new JButton();
TitledBorder titledBorder1 = new TitledBorder("puzzle game");
int maxMoves = 100;
int g = 0, position = 0;
int moves = 0;
long time = 0;
int[][] solutions = new int[maxMoves + 1][9];
int[][] board = new int[3][3];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -