用JAVA写一个不用任何图片的扫雷小游戏minesweeper game

本文介绍了一个基于Java实现的扫雷游戏,包括游戏的界面布局、不同难度设置、雷区生成、雷数计算以及游戏逻辑。通过点击和右键操作,玩家可以开启和标记地雷,达到不同难度级别的挑战。游戏还提供了重新开始的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

游戏开始了


前言

先来看一看游戏运行的画面:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述


运行环境:

使用软件:eclipse2021-12版
JDK版本:JDK15.0.1

一、代码部分

1.项目结构以及下载方式

如图:
在这里插入图片描述

下载方式:
链接:https://pan.baidu.com/s/16_YBQvncL0h0TL91IgwD8A
提取码:xhkr

2.代码主体部分

MineSweeper.java:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MineSweeper implements MouseListener, ActionListener {
	JPanel p = new JPanel();
	JFrame frame = new JFrame("扫雷");
	@SuppressWarnings("rawtypes")
	JComboBox combobox = new JComboBox();
	JButton reset = new JButton("重新开始");
	Container container = new Container();

	// 游戏数据结构
	MineSweeperConstant constant = new MineSweeperConstant();
	JButton[][] buttons = new JButton[constant.row][constant.col];// 定义按钮
	int[][] counts = new int[constant.row][constant.col];// 定义整型数组保存按钮下方雷数

	// 创建构造方法
	public MineSweeper() {
		// 显示窗口
		frame.setSize(600, 700);// 600*700
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new BorderLayout());

		// 添加重来、选择难度按钮
		addtopButton();

		// 添加雷区按钮
		addButtons();

		// 埋雷
		addLei();

		// 添加雷的计数
		calcNeiboLei();

		frame.setVisible(true);
	}

	void addtopButton() {
		p.removeAll();
		p.add(reset);
		reset.setBackground(Color.green);
		reset.setOpaque(true);
		reset.addActionListener(this);
		// combobox.addItem("选择难度");
		combobox.addItem("新手难度");
		combobox.addItem("初级难度");
		combobox.addItem("中级难度");
		combobox.addItem("高级难度");
		combobox.addItem("大师难度");
		combobox.setBackground(Color.GREEN);
		combobox.setOpaque(true);
		combobox.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(final ItemEvent e) {
				final String item = e.getItem().toString();
				if (item == "新手难度") {
					constant.leiCount = 20;
					ResetGame();
				} else if (item == "初级难度") {
					constant.leiCount = 43;
					ResetGame();
				} else if (item == "中级难度") {
					constant.leiCount = 63;
					ResetGame();
				} else if (item == "高级难度") {
					constant.leiCount = 99;
					ResetGame();
				} else if (item == "大师难度") {
					constant.leiCount = 119;
					ResetGame();
				}

			}

		});
		p.add(combobox);
		frame.add(p, BorderLayout.NORTH);
		// p.add(new Label("总雷数:"+constant.leiCount,Label.CENTER));
		// p.add(new Label("总雷数:"+constant.leiCount,Label.RIGHT));

	}

	/*
	 * void addnanduButton() { nandu.setBackground(Color.green);
	 * nandu.setOpaque(true); nandu.addActionListener(this);
	 * frame.add(nandu,BorderLayout.WEST); }
	 * 
	 * void addResetButton() { reset.setBackground(Color.green);
	 * reset.setOpaque(true); reset.addActionListener(this);
	 * //reset.addMouseListener(this); frame.add(reset,BorderLayout.NORTH); }
	 */

	void addLei() {
		final Random rand = new Random();
		int randRow, randCol;
		for (int i = 0; i < constant.leiCount; i++) {
			randRow = rand.nextInt(constant.row);
			randCol = rand.nextInt(constant.col);
			if (counts[randRow][randCol] == constant.LEICODE) {
				i--;
			} else {
				counts[randRow][randCol] = constant.LEICODE;
				// buttons[randRow][randCol].setText("X");
			}
		}
	}

	void addButtons() {
		frame.add(container, BorderLayout.CENTER);
		container.setLayout(new GridLayout(constant.row, constant.col));
		for (int i = 0; i < constant.row; i++) {
			for (int j = 0; j < constant.col; j++) {
				final JButton button = new JButton();
				button.setBackground(Color.white);
				button.setOpaque(true);
				button.addActionListener(this);
				button.addMouseListener((MouseListener) this);
				buttons[i][j] = button;
				container.add(button);
			}
		}
	}

	void calcNeiboLei() {
		int count;
		for (int i = 0; i < constant.row; i++) {
			for (int j = 0; j < constant.col; j++) {
				count = 0;
				if (counts[i][j] == constant.LEICODE)
					continue;
				if (i > 0 && j > 0 && counts[i - 1][j - 1] == constant.LEICODE)
					count++;
				if (i > 0 && counts[i - 1][j] == constant.LEICODE)
					count++;
				if (i > 0 && j < 19 && counts[i - 1][j + 1] == constant.LEICODE)
					count++;
				if (j > 0 && counts[i][j - 1] == constant.LEICODE)
					count++;
				if (j < 19 && counts[i][j + 1] == constant.LEICODE)
					count++;
				if (i < 19 && j > 0 && counts[i + 1][j - 1] == constant.LEICODE)
					count++;
				if (i < 19 && counts[i + 1][j] == constant.LEICODE)
					count++;
				if (i < 19 && j < 19 && counts[i + 1][j + 1] == constant.LEICODE)
					count++;
				counts[i][j] = count;
				buttons[i][j].setMargin(new Insets(0, 0, 0, 0));// 让按钮随按钮上的图案变化
				// buttons[i][j].setText(counts[i][j] + "");
			}
		}
	}

	@Override
	public void actionPerformed(final ActionEvent e) {

		final JButton button = (JButton) e.getSource();
		if (button.equals(reset)) {
			ResetGame();// 重新开始游戏
		} else {
			int count = 0;
			for (int i = 0; i < constant.row; i++) {
				for (int j = 0; j < constant.col; j++) {
					if (button.equals(buttons[i][j])) {
						count = counts[i][j];
						if (count == constant.LEICODE) {
							loseGame();
						} else {
							openCell(i, j);
							checkWin();
						}
						return;
					}
				}
			}
		}
	}

	public void mouseClicked(final MouseEvent e) {
		final JButton button = (JButton) e.getSource();
		if (e.getButton() == MouseEvent.BUTTON3) {// 判断鼠标右击动作
			for (int i = 0; i < constant.row; i++) {
				for (int j = 0; j < constant.col; j++) {
					if (button.equals(buttons[i][j])) {
						if ((buttons[i][j].isEnabled() == true)) {
							// buttons[i][j].setEnabled(false);
							buttons[i][j].setMargin(new Insets(0, 0, 0, 0));// 让按钮随按钮上的图案变化
							buttons[i][j].setText("?");
							return;
						}
					}
				}
			}
		}
	}

	void ResetGame() {
		for (int i = 0; i < constant.row; i++) {
			for (int j = 0; j < constant.col; j++) {
				buttons[i][j].setText("");
				buttons[i][j].setEnabled(true);
				buttons[i][j].setBackground(Color.white);
				counts[i][j] = 0;
			}
		}
		addLei();
		calcNeiboLei();
	}

	void checkWin() {
		for (int i = 0; i < constant.row; i++) {
			for (int j = 0; j < constant.col; j++) {
				if (buttons[i][j].isEnabled() == true && counts[i][j] != constant.LEICODE)
					return;
			}
		}
		JOptionPane.showMessageDialog(frame, "Yeah,你赢了!");
	}

	// 使用递归方法打开格子
	void openCell(final int i, final int j) {
		if (buttons[i][j].isEnabled() == false)
			return;
		buttons[i][j].setBackground(Color.yellow);
		buttons[i][j].setOpaque(true);
		buttons[i][j].setEnabled(false);
		if (counts[i][j] == 0) {
			if (i > 0 && j > 0 && counts[i - 1][j - 1] != constant.LEICODE)
				openCell(i - 1, j - 1);
			if (i > 0 && j < 19 && counts[i - 1][j] != constant.LEICODE)
				openCell(i - 1, j);
			if (i > 0 && j < 19 && counts[i - 1][j + 1] != constant.LEICODE)
				openCell(i - 1, j + 1);
			if (j > 0 && counts[i][j - 1] != constant.LEICODE)
				openCell(i, j - 1);
			if (j < 19 && counts[i][j + 1] != constant.LEICODE)
				openCell(i, j + 1);
			if (i < 19 && j > 0 && counts[i + 1][j - 1] != constant.LEICODE)
				openCell(i + 1, j - 1);
			if (i < 19 && counts[i + 1][j] != constant.LEICODE)
				openCell(i + 1, j);
			if (i < 19 && j < 19 && counts[i + 1][j + 1] != constant.LEICODE)
				openCell(i + 1, j + 1);
		} else {
			buttons[i][j].setMargin(new Insets(0, 0, 0, 0));
			buttons[i][j].setText(counts[i][j] + "");
		}
	}

	void loseGame() {
		for (int i = 0; i < constant.row; i++) {
			for (int j = 0; j < constant.col; j++) {
				final int count = counts[i][j];
				if (count == constant.LEICODE) {
					buttons[i][j].setMargin(new Insets(0, 0, 0, 0));
					buttons[i][j].setText("雷");
					buttons[i][j].setBackground(Color.red);
					buttons[i][j].setEnabled(false);
				} else {
					buttons[i][j].setMargin(new Insets(0, 0, 0, 0));
					buttons[i][j].setText(count + "");
					buttons[i][j].setEnabled(false);

				}
			}
		}
		JOptionPane.showMessageDialog(frame, "error,你输了!");
	}

	public static void main(final String[] args) {
		new MineSweeper();
	}

	@Override
	public void mousePressed(final MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(final MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(final MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(final MouseEvent e) {
		// TODO Auto-generated method stub

	}
}

3.代码常量部分

MineSweeperConstant.java:

public class MineSweeperConstant {
	final int row = 20;// 行数30
	final int col = 20;// 列数30
	final int LEICODE = 10;// 定义雷下方的数字
	protected int temp = 20;
	protected int leiCount = temp;// 雷数30
}

总结

不同的难度对应不同的地雷数量。
项目简单,容易上手,代码也容易理解。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值