java代码拼图小游戏,图片被分割为33或者44、5*5的小格,然后打乱顺序,但其中有一个是空白位置(例如下图中的最后一行有空白),点击空白位置周围的图片,将图片移动到空白处。直至最后拼成标准答案,提示游戏成功。
时间: 2023-06-26 17:06:02 浏览: 159
以下是一个简单的Java代码,实现了拼图小游戏:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PuzzleGame extends JFrame implements ActionListener {
private JPanel panel;
private JButton[] buttons;
private int[] numbers;
public PuzzleGame() {
setTitle("Puzzle Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 2, 2));
add(panel, BorderLayout.CENTER);
buttons = new JButton[16];
numbers = new int[16];
for (int i = 0; i < 15; i++) {
buttons[i] = new JButton(String.valueOf(i + 1));
numbers[i] = i + 1;
buttons[i].addActionListener(this);
panel.add(buttons[i]);
}
buttons[15] = new JButton("");
numbers[15] = 0;
buttons[15].setEnabled(false);
panel.add(buttons[15]);
shuffle();
setVisible(true);
}
private void shuffle() {
for (int i = 0; i < 1000; i++) {
int index = (int) (Math.random() * 15);
int number = numbers[index];
if (index > 3 && numbers[index - 4] == 0) {
buttons[index - 4].setText(String.valueOf(number));
buttons[index].setText("");
numbers[index - 4] = number;
numbers[index] = 0;
} else if (index < 12 && numbers[index + 4] == 0) {
buttons[index + 4].setText(String.valueOf(number));
buttons[index].setText("");
numbers[index + 4] = number;
numbers[index] = 0;
} else if (index % 4 != 0 && numbers[index - 1] == 0) {
buttons[index - 1].setText(String.valueOf(number));
buttons[index].setText("");
numbers[index - 1] = number;
numbers[index] = 0;
} else if (index % 4 != 3 && numbers[index + 1] == 0) {
buttons[index + 1].setText(String.valueOf(number));
buttons[index].setText("");
numbers[index + 1] = number;
numbers[index] = 0;
}
}
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
int index = -1;
for (int i = 0; i < 16; i++) {
if (buttons[i] == button) {
index = i;
break;
}
}
if (index > 3 && numbers[index - 4] == 0) {
buttons[index - 4].setText(button.getText());
button.setText("");
numbers[index - 4] = numbers[index];
numbers[index] = 0;
} else if (index < 12 && numbers[index + 4] == 0) {
buttons[index + 4].setText(button.getText());
button.setText("");
numbers[index + 4] = numbers[index];
numbers[index] = 0;
} else if (index % 4 != 0 && numbers[index - 1] == 0) {
buttons[index - 1].setText(button.getText());
button.setText("");
numbers[index - 1] = numbers[index];
numbers[index] = 0;
} else if (index % 4 != 3 && numbers[index + 1] == 0) {
buttons[index + 1].setText(button.getText());
button.setText("");
numbers[index + 1] = numbers[index];
numbers[index] = 0;
}
checkWin();
}
private void checkWin() {
for (int i = 0; i < 15; i++) {
if (numbers[i] != i + 1) {
return;
}
}
JOptionPane.showMessageDialog(this, "You win!");
}
public static void main(String[] args) {
new PuzzleGame();
}
}
```
这个小游戏使用了Java的Swing库来实现界面和按钮,通过数组来记录每个按钮所代表的数字,然后通过随机移动按钮来打乱顺序。在点击按钮时,会检查是否可以移动到空白位置,如果可以则执行移动,并检查是否已经拼成标准答案。如果是,则显示游戏成功的提示框。
阅读全文