JAVA扫雷程序,模拟windows早期扫雷软件,用swing实现。
https://www.jb51.net/article/250486.htm
大概这样:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class saolei implements ActionListener {
JFrame frame=new JFrame("扫雷小游戏");
JButton reset=new JButton("重来");
Container container=new Container();
//游戏数据结构
final int row=20;
final int col=20;
final int leiCount=30;
JButton [][] buttons=new JButton[row][col];
int [][] counts=new int[row][col];
final int LEICODE=10;
// 构造函数
public saolei(){
//1、设置窗口
frame.setSize(1000, 800);
frame.setResizable(true);
//是否可改变窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//2、添加重来按钮
addResetButton();
//添加按钮
addButtons();
//埋雷
addLei();
//添加雷的计算
calcNeiboLei();
frame.setVisible(true);
}
public void addResetButton(){
reset.setBackground(Color.cyan);
reset.setOpaque(true);
reset.addActionListener(this);
frame.add(reset,BorderLayout.NORTH);
}
public void addLei(){
Random rand=new Random();
int randRow,randCol;
for(int i=0;i<leiCount;i++){
randRow=rand.nextInt(row);
randCol=rand.nextInt(col);
if(counts[randRow][randCol]== LEICODE){
i--;
}else{
counts[randRow][randCol]=LEICODE;
//buttons[randRow][randCol].setText("*");
}
}
}
public void addButtons(){
frame.add(container,BorderLayout.CENTER);
container.setLayout(new GridLayout(row,col));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
JButton button=new JButton();
button.setBackground(Color.orange);
button.setOpaque(true);
button.addActionListener(this);
buttons[i][j]=button;
container.add(button);
}
}
}
public void calcNeiboLei(){
int count;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
count=0;
if(counts[i][j]==LEICODE) continue;
if(i>0 && j>0 && counts[i-1][j-1]==LEICODE) count++;
if(i>0&&counts[i-1][j]==LEICODE) count++;
if(i>0 && j<19 && counts[i-1][j+1]==LEICODE) count++;
if(j>0 && counts[i][j-1]==LEICODE) count++;
if(j<19 && counts[i][j+1]==LEICODE) count++;
if(i<19&&j>0&&counts[i+1][j-1]==LEICODE) count++;
if(i<19&&counts[i+1][j]==LEICODE) count++;
if(i<19&&j<19&&counts[i+1][j+1]==LEICODE) count++;
counts[i][j]=count;
//buttons[i][j].setText(counts[i][j]+"");
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton button=(JButton)e.getSource();
if(button.equals(reset)){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
buttons[i][j].setBackground(Color.orange);
counts[i][j]=0;
}
}
addLei();
calcNeiboLei();
}else{
int count=0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(button.equals(buttons[i][j])){
count=counts[i][j];
if(count==LEICODE){
LoseGame();
}else{
openCell(i,j);
checkWin();
} return;
}
}
}
}
}
void checkWin(){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(buttons[i][j].isEnabled()==true && counts[i][j]!=LEICODE) return;
}
}
JOptionPane.showMessageDialog(frame, "握草牛啤,你竟然赢了!");
}
void openCell(int i,int j){
if(buttons[i][j].isEnabled()==false) return;
buttons[i][j].setEnabled(false);
if(counts[i][j]==0){
if(i>0 && j>0 && counts[i-1][j-1]!=LEICODE) openCell(i-1, j-1);
if(i>0&&counts[i-1][j]!=LEICODE) openCell(i-1, j);
if(i>0 && j<19 && counts[i-1][j+1]!=LEICODE) openCell(i-1, j+1);
if(j>0 && counts[i][j-1]!=LEICODE) openCell(i, j-1);
if(j<19 && counts[i][j+1]!=LEICODE) openCell(i, j+1);
if(i<19&&j>0&&counts[i+1][j-1]!=LEICODE) openCell(i+1, j-1);
if(i<19&&counts[i+1][j]!=LEICODE) openCell(i+1, j);
if(i<19&&j<19&&counts[i+1][j+1]!=LEICODE) openCell(i+1, j+1);
buttons[i][j].setText(counts[i][j]+"");
}else{
buttons[i][j].setText(counts[i][j]+"");
}
}
void LoseGame(){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
int count=counts[i][j];
if(count==LEICODE){
buttons[i][j].setText("×");
buttons[i][j].setBackground(Color.red);
buttons[i][j].setEnabled(false);
}else{
buttons[i][j].setText(count+"");
buttons[i][j].setEnabled(false);
}
}
}
}
public static void main(String[] args) {
saolei lei=new saolei();
}
}
很高兴为您介绍如何使用Java和Swing编写扫雷程序。
首先,在您的Java IDE中创建一个新的Java项目。然后,在项目中创建一个名为MineSweeper
的新类。
import javax.swing.JFrame;
public class MineSweeper extends JFrame {
public static void main(String[] args) {
new MineSweeper();
}
public MineSweeper() {
setTitle("Mine Sweeper");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
在这个示例代码中,我们创建了一个继承JFrame的MineSweeper类,并将它设置为程序的入口点。在构造函数中,我们设置了窗口的标题、大小、关闭操作和可见性。
下一步是创建菜单和工具栏。我们需要使用JMenuBar和JToolBar类来完成这个任务。在MineSweeper类的构造函数中添加以下代码:
import javax.swing.*;
public class MineSweeper extends JFrame {
public static void main(String[] args) {
new MineSweeper();
}
public MineSweeper() {
// 设置标题和大小
setTitle("Mine Sweeper");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加菜单栏
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
// 添加菜单
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
// 添加工具栏
JToolBar toolBar = new JToolBar();
add(toolBar, BorderLayout.NORTH);
// 设置窗口可见
setVisible(true);
}
}
在这个示例代码中,我们创建一个JMenuBar并将其设置为窗口的菜单栏。然后,我们向菜单栏添加了一个名为"File"的菜单。接下来,我们创建了一个名为toolBar的JToolBar对象,并将其添加到窗口的NORTH位置。
现在我们需要创建一个游戏板来实现扫雷游戏。我们可以使用JPanel类来创建游戏面板。在MineSweeper类的构造函数中添加以下代码:
import javax.swing.*;
public class MineSweeper extends JFrame {
public static void main(String[] args) {
new MineSweeper();
}
public MineSweeper() {
// 设置标题和大小
setTitle("Mine Sweeper");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加菜单栏
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
// 添加菜单
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
// 添加工具栏
JToolBar toolBar = new JToolBar();
add(toolBar, BorderLayout.NORTH);
// 创建游戏面板
JPanel gamePanel = new JPanel();
add(gamePanel);
// 设置窗口可见
setVisible(true);
}
}
在这个示例代码中,我们创建了一个名为gamePanel的JPanel对象,并将其添加到MineSweeper类的中央。
完成上述步骤后,我们现在需要在gamePanel面板上创建一些格子。每个格子都可以使用JButton类实现。在MineSweeper类的构造函数中添加以下代码:
```java
import javax.swing.*;
public class MineSweeper extends JFrame {
public static void main(String[] args) {
new MineSweeper();
}
public MineSweeper() {
// 设置标题和大小
setTitle("Mine Sweeper");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加菜单栏
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
// 添加菜单
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
// 添加工
参考gpt:
下面是一个简单的扫雷游戏的示例,这个示例使用了一个 JButton 的二维数组来表示扫雷游戏的格子。通过点击格子按钮,可以进行游戏操作。地雷用 -1 表示,空格子的数字表示周围地雷的数量。
你可以根据自己的需要调整游戏的尺寸和地雷数量,然后运行这个程序,就可以在 Swing 窗口中玩扫雷游戏了。使用 Java Swing 实现:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Minesweeper extends JFrame {
private JButton[][] grid;
private int[][] board;
private int size;
private int mines;
public Minesweeper(int size, int mines) {
this.size = size;
this.mines = mines;
grid = new JButton[size][size];
board = new int[size][size];
initializeBoard();
createGUI();
generateMines();
countAdjacentMines();
}
private void initializeBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = 0;
}
}
}
private void createGUI() {
setTitle("Minesweeper");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(new GridLayout(size, size));
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
int row = (int) btn.getClientProperty("row");
int col = (int) btn.getClientProperty("col");
int value = board[row][col];
if (value == -1) {
// Clicked on a mine
btn.setText("*");
gameOver();
} else {
// Clicked on an empty cell
btn.setText(Integer.toString(value));
btn.setEnabled(false);
}
}
};
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
JButton btn = new JButton();
btn.putClientProperty("row", i);
btn.putClientProperty("col", j);
btn.addActionListener(listener);
grid[i][j] = btn;
add(btn);
}
}
pack();
setVisible(true);
}
private void generateMines() {
int count = 0;
while (count < mines) {
int row = (int) (Math.random() * size);
int col = (int) (Math.random() * size);
if (board[row][col] != -1) {
board[row][col] = -1;
count++;
}
}
}
private void countAdjacentMines() {
int[] dx = {-1, 0, 1, -1, 1, -1, 0, 1};
int[] dy = {-1, -1, -1, 0, 0, 1, 1, 1};
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == -1) {
continue;
}
int count = 0;
for (int k = 0; k < 8; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if (isValid(nx, ny) && board[nx][ny] == -1) {
count++;
}
}
board[i][j] = count;
}
}
}
private boolean isValid(int x, int y) {
return x >= 0 && x < size && y >= 0 && y < size;
}
private void gameOver() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == -1) {
grid[i][j].setText("*");
grid[i][j].setEnabled(false);
}
}
}
JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
int size = 10; // Adjust the grid size as per your preference
int mines = 15; // Adjust the number of mines as per your preference
new Minesweeper(size, mines);
});
}
}
满足:https://zhuanlan.zhihu.com/p/551843783
为了实现用Java和Swing创建一个模拟Windows早期扫雷软件的扫雷程序,您可以按照以下步骤进行操作:
JFrame
作为主窗口,并添加合适的组件来显示扫雷游戏的方格布局和状态。JButton
作为方格,并为每个方格添加点击事件监听器。以上是一个简单的扫雷游戏的实现步骤,具体的代码实现细节需要根据您的需求和设计来进行调整和扩展。