1. 程式人生 > >簡單版掃雷(有頁面)-java

簡單版掃雷(有頁面)-java

首先設計一個二維陣列,用於存放雷和周圍八個格子雷的個數,再定義兩個變數,用於存放地雷個數和陣列大小。

使用隨機資料生成雷,並給雷四周都新增1。加入JFrame ,實現點選按鈕判斷是否是雷,如果是雷提示失敗,遊戲結束


import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;


public class Test03 extends JFrame implements ActionListener {


private JPanel contentPane;
JButton jButton[][];
JPanel panel;


static int numOfMine = 5;// 定義10個地雷
static int mapSize = 5;// 定義9乘9的方格


public Test03() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);


UIManager.put("Button.font", new Font("Dialog", Font.BOLD, 18));


panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel label = new JLabel("");
label.setBounds(71, 43, 53, 14);
panel.add(label);


anniu();
saolei();
}


public void anniu() {
jButton = new JButton[mapSize][mapSize];
for (int i = 0; i < mapSize; i++) {
for (int j = 0; j < mapSize; j++) {
jButton[i][j] = new JButton("");
// jButton[i][j].setBackground(Color.gray);
jButton[i][j].addActionListener(this);
jButton[i][j].setBounds(50 * j + 9, 50 * i + 5, 50, 50);
panel.add(jButton[i][j]);
}
}
}


public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test03 test02 = new Test03();
test02.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});


}


public void saolei() {


int[][] map = new int[mapSize][mapSize];// 所有的地雷
Random random = new Random();
int[] around = { -1, 0, 1 };// 所有地雷格子周圍的座標偏移量


for (int i = 0; i < mapSize; i++) {// 生成格子
for (int j = 0; j < mapSize; j++) {
System.out.print("[]");
}
System.out.println();
}


for (int i = 0; i < numOfMine; i++) {// 隨機生成十個地雷,大於等於100表示地雷
int x, y;
do {
x = random.nextInt(mapSize);
y = random.nextInt(mapSize);
} while (map[x][y] >= 100);
map[x][y] = 100;


for (int dy : around) {
for (int dx : around) {
if (dx == 0 && dy == 0) {// 代表地雷自身
continue;
}
if ((x + dx) >= 0 && (x + dx) < mapSize && (y + dy) >= 0
&& (y + dy) < mapSize) {// 防止越界
map[x + dx][y + dy]++;
}
}
}
}
System.out.println("------------------------");
for (int i = 0; i < mapSize; i++) {// 生成格子
for (int j = 0; j < mapSize; j++) {
if (map[i][j] >= 100) {// 地雷
System.out.print("[@]");
} else if (map[i][j] == 0) {// 四周無地雷
System.out.print("[ ]");
} else {// 四周地雷數
System.out.print("[" + map[i][j] + "]");
}
}
System.out.println();
}


for (int i = 0; i < mapSize; i++) {// 生成格子
for (int j = 0; j < mapSize; j++) {
if (map[i][j] >= 100) {
jButton[i][j].setActionCommand("☠");//
} else
jButton[i][j].setActionCommand(String.valueOf(map[i][j]));//
}
System.out.println();
}


}


int i = mapSize * mapSize;


@Override
public void actionPerformed(ActionEvent e) {
System.out.println("=====" + e.getActionCommand());
JButton j = (JButton) e.getSource();
j.setText(e.getActionCommand());
i--;
if (i == numOfMine) {
JOptionPane.showMessageDialog(Test03.this, "SUCCESS Game Over",
"SUCCESS", JOptionPane.INFORMATION_MESSAGE);
}
if (e.getActionCommand().equals("☠")) {
JOptionPane.showMessageDialog(Test03.this, "Failed Game Over",
"Failed", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}


}
}