用java实现扫雷游戏

java实现扫雷游戏
一、基本思路:
创建一个JButton对象数组(标识符为buttons),设置MouseListener获得鼠标事件;创建back(整形)二维数组,与buttons中的每一个对象相对应,并且用于后台运算。
二、关键代码:
1.获取一组随机生成的坐标,用于设置地雷。方法名:getBombs(),代码:

private void getBombs() {
		for (int i = 0; i < bombNum; i++) {	//bombNum指地雷数目
			int row = (int) (Math.random() * this.row);
			int col = (int) (Math.random() * this.col);
			if (back[row][col] == 9) {
				i--;
			} else {
				back[row][col] = 9;
			}
		}
	}

2.获取back数组。方法名:setBack(),代码:

private void setBack() {
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (back[i][j] == 0) {
					int count = 0;
					if (back[i][j] == 9)
						continue;
					if (i > 0 && j > 0 && back[i - 1][j - 1] == 9)
						count++;
					if (i > 0 && back[i - 1][j] == 9)
						count++;
					if (i > 0 && j < col - 1 && back[i - 1][j + 1] == 9)
						count++;
					if (j > 0 && back[i][j - 1] == 9)
						count++;
					if (j < col - 1 && back[i][j + 1] == 9)
						count++;
					if (i < col - 1 && j > 0 && back[i + 1][j - 1] == 9)
						count++;
					if (i < col - 1 && back[i + 1][j] == 9)
						count++;
					if (i < col - 1 && j < col - 1 && back[i + 1][j + 1] == 9)
						count++;
					back[i][j] = count;
				}
			}
		}
	}

3.打开按钮对应的back值。方法openCell(),代码:

private void openCell(int i, int j) {
		if (!buttons[i][j].isEnabled()) {
			return;
		}
		buttons[i][j].setEnabled(false);
		if (back[i][j] == 0) {
			if (i > 0 && j > 0 && back[i - 1][j - 1] != 9)
				openCell(i - 1, j - 1);
			if (i > 0 && back[i - 1][j] != 9)
				openCell(i - 1, j);
			if (i > 0 && j < col - 1 && back[i - 1][j + 1] != 9)
				openCell(i - 1, j + 1);
			if (j > 0 && back[i][j - 1] != 9)
				openCell(i, j - 1);
			if (j < col - 1 && back[i][j + 1] != 9)
				openCell(i, j + 1);
			if (i < col - 1 && j > 0 && back[i + 1][j - 1] != 9)
				openCell(i + 1, j - 1);
			if (i < col - 1 && back[i + 1][j] != 9)
				openCell(i + 1, j);
			if (i < col - 1 && j < col - 1 && back[i + 1][j + 1] != 9)
				openCell(i + 1, j + 1);
		} else {
			setBackImage(i, j);	//设置图片
		}
	}

4.检查是否胜利。方法:checkWin(),代码:

private void checkWin() {
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				if (buttons[i][j].isEnabled() && back[i][j] != 9)
					return;
			}
		}
		map.updateUI();
		setWinMap();	//设置成功后面板显示
		JOptionPane.showMessageDialog(jf, "Yeah,你赢了!");
	}

三、容易出问题的地方
1.设置窗体时,使用setVisible(true)方法时注意将该方法放在最后:

private void initialize(){
	jf.setSize(...);
	//其余代码
	jf.setVisible(true);	//最后一行
}

否则容易出现窗体内的组件不显示的问题。
2.当点开“重新开始”或者其他的等级时,注意使用updateUI()方法解决窗体重画延时的问题(如图):
重画延时问题
3.使用JButton中的setDisabledIcon()方法时图片不显示问题,解决方法:该JButton对象之前需要有一个图片(使用setIcon()设置),然后使用setDisabledIcon()方法时就可以解决这个问题了。
四、源代码
https://download.csdn.net/download/alazyperson/11390895

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单Java实现扫雷游戏的代码,供参考: ``` import java.util.*; public class Minesweeper { private int[][] board; private boolean[][] revealed; private boolean[][] flagged; private int numMines; private int numRows; private int numCols; private int numRevealed; public Minesweeper(int numRows, int numCols, int numMines) { this.numRows = numRows; this.numCols = numCols; this.numMines = numMines; this.numRevealed = 0; this.board = new int[numRows][numCols]; this.revealed = new boolean[numRows][numCols]; this.flagged = new boolean[numRows][numCols]; initializeBoard(); } private void initializeBoard() { Random rand = new Random(); int minesPlaced = 0; while (minesPlaced < numMines) { int row = rand.nextInt(numRows); int col = rand.nextInt(numCols); if (board[row][col] == 0) { board[row][col] = -1; minesPlaced++; } } for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { if (board[row][col] == -1) { updateAdjacentCells(row, col); } } } } private void updateAdjacentCells(int row, int col) { for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < numRows && j >= 0 && j < numCols && board[i][j] != -1) { board[i][j]++; } } } } public void printBoard() { System.out.println("\n"); for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { if (revealed[row][col]) { if (board[row][col] == -1) { System.out.print("* "); } else { System.out.print(board[row][col] + " "); } } else if (flagged[row][col]) { System.out.print("F "); } else { System.out.print("- "); } } System.out.println(); } } public boolean isGameWon() { return numRevealed == numRows * numCols - numMines; } public boolean isGameLost() { for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { if (revealed[row][col] && board[row][col] == -1) { return true; } } } return false; } public boolean isCellRevealed(int row, int col) { return revealed[row][col]; } public boolean isCellFlagged(int row, int col) { return flagged[row][col]; } public boolean isCellMine(int row, int col) { return board[row][col] == -1; } public void revealCell(int row, int col) { if (!revealed[row][col] && !flagged[row][col]) { revealed[row][col] = true; numRevealed++; if (board[row][col] == 0) { revealAdjacentCells(row, col); } } } private void revealAdjacentCells(int row, int col) { for (int i = row - 1; i <= row + 1; i++) { for (int j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < numRows && j >= 0 && j < numCols && !revealed[i][j]) { revealCell(i, j); } } } } public void flagCell(int row, int col) { if (!revealed[row][col]) { flagged[row][col] = !flagged[row][col]; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter number of rows: "); int numRows = sc.nextInt(); System.out.print("Enter number of columns: "); int numCols = sc.nextInt(); System.out.print("Enter number of mines: "); int numMines = sc.nextInt(); Minesweeper game = new Minesweeper(numRows, numCols, numMines); while (!game.isGameWon() && !game.isGameLost()) { game.printBoard(); System.out.print("Enter row to reveal: "); int row = sc.nextInt(); System.out.print("Enter column to reveal: "); int col = sc.nextInt(); if (game.isCellMine(row, col)) { System.out.println("Game over!"); game.revealCell(row, col); } else { game.revealCell(row, col); if (game.isGameWon()) { System.out.println("You win!"); } } System.out.print("Do you want to flag a cell? (Y/N) "); String response = sc.next(); if (response.equalsIgnoreCase("Y")) { System.out.print("Enter row to flag: "); row = sc.nextInt(); System.out.print("Enter column to flag: "); col = sc.nextInt(); game.flagCell(row, col); } } game.printBoard(); } } ``` 这个实现中,我们使用了一个二维数组来表示扫雷游戏的棋盘,其中包含了每个格子周围的地雷数量。我们还使用了两个布尔类型的二维数组来表示每个格子是否已经被揭示或标记。在程序的主循环中,玩家可以选择一个格子来揭示它,或者标记一个格子为地雷。如果揭示的格子是地雷,游戏将结束。否则,游戏将继续进行,直到玩家揭示所有非地雷格子或揭示了一个地雷。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值