用java写一个扫雷小游戏
时间: 2023-02-25 21:23:07 浏览: 61
这是一个关于如何用 Java 来编写扫雷小游戏的示例:
首先,我们需要创建一个类来表示游戏的棋盘。这个类可以有一个二维数组来存储每个格子的状态,例如,是否有地雷、是否已经被扫过,以及它周围有多少地雷。
然后我们可以创建一个方法来处理玩家的输入,例如点击的位置和是否要标记地雷。如果玩家点击了一个地雷,我们可以调用另一个方法来处理游戏结束。如果玩家点击了一个没有地雷的位置,我们可以调用另一个方法来更新棋盘的状态。
最后,我们可以创建一个主函数来运行游戏,这个函数会循环处理玩家的输入,直到游戏结束。
这是一个简单的示例,但它应该能帮助你开始编写你自己的扫雷游戏。
相关问题
用Java写一个扫雷小游戏
好,下面是一个简单的 Java 扫雷小游戏的实现:
import java.util.*;
public class Minesweeper {
private int[][] board;
private boolean[][] revealed;
private int rows;
private int cols;
private int numMines;
public Minesweeper(int rows, int cols, int numMines) {
this.board = new int[rows][cols];
this.revealed = new boolean[rows][cols];
this.rows = rows;
this.cols = cols;
this.numMines = numMines;
// initialize board with mines
Random rand = new Random();
int count = 0;
while (count < numMines) {
int randRow = rand.nextInt(rows);
int randCol = rand.nextInt(cols);
if (board[randRow][randCol] != -1) {
board[randRow][randCol] = -1;
count++;
}
}
// fill in numbers for non-mine cells
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (board[i][j] != -1) {
int countMines = 0;
if (i > 0 && j > 0 && board[i-1][j-1] == -1) countMines++;
if (i > 0 && board[i-1][j] == -1) countMines++;
if (i > 0 && j < cols-1 && board[i-1][j+1] == -1) countMines++;
if (j > 0 && board[i][j-1] == -1) countMines++;
if (j < cols-1 && board[i][j+1] == -1) countMines++;
if (i < rows-1 && j > 0 && board[i+1][j-1] == -1) countMines++;
if (i < rows-1 && board[i+1][j] == -1) countMines++;
if (i < rows-1 && j < cols-1 && board[i+1][j+1] == -1) countMines++;
board[i][j] = countMines;
}
}
}
}
public void printBoard() {
System.out.print(" ");
for (int j = 0; j < cols; j++) {
System.out.print(j + " ");
}
System.out.println();
for (int i = 0; i < rows; i++) {
System.out.print(i + " ");
for (int j = 0; j < cols; j++) {
if (revealed[i][j]) {
if (board[i][j] == -1) {
System.out.print("* ");
} else {
System.out.print(board[i][j] + " ");
}
} else {
System.out.print(". ");
}
}
System.out.println();
}
}
public boolean click(int row, int col) {
if (board[row][col] == -1) {
System.out.println("Game over!");
return true;
}
revealed[row][col] = true;
if (board[row][col] == 0) {
if (row > 0 && col > 0 && !revealed[row-1][col-1]) click(row-1, col-1);
if (row > 0 && !revealed[row-1][col]) click(row-1, col);
if (row > 0 && col < cols-1 && !revealed[row-1][col+1]) click(row-1, col+1);
if (col > 0 && !revealed[row][col-1]) click(row, col-1);
if (col < cols-1 && !revealed[row][col+1]) click(row, col+1);
if (row < rows-1 && col > 0 && !revealed[row+1][col-1]) click(row+1, col-1);
if (row < rows-1 && !revealed[row+1][col]) click(row+1, col);
if (row < rows-1 && col < cols-1 && !revealed[row+1][col+1]) click(row+1, col+1);
}
return false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter number of columns: ");
int cols = scanner.nextInt();
System.out.print("Enter number of mines: ");
int numMines = scanner.nextInt();
Minesweeper game = new Minesweeper(rows, cols, numMines);
game.printBoard();
boolean gameOver = false;
while (!gameOver) {
System.out.print("Enter row to click: ");
int row = scanner.nextInt();
System.out.print("Enter column to click: ");
int col = scanner.nextInt();
gameOver = game.click(row, col);
game.printBoard();
}
scanner.close();
}
}
这个扫雷小游戏用的是命令行界面,玩家需要输入行号和列号来选择要点击的方块。游戏会在命令行界面中显示,并且会在游戏结束时显示“Game over!”。
基于JAVA写一个扫雷小游戏
好的,下面是一个简单的基于Java的扫雷小游戏示例代码,供您参考。
import java.util.Scanner;
import java.util.Random;
public class Minesweeper {
private int[][] board; // 存储格子状态和雷的位置
private boolean[][] visible; // 存储格子是否被翻开
private int n; // 游戏区域大小
private int mines; // 雷的数量
private int remaining; // 剩余未翻开的格子数
// 初始化游戏
public Minesweeper(int n, int mines) {
this.n = n;
this.mines = mines;
remaining = n * n - mines;
board = new int[n][n];
visible = new boolean[n][n];
// 初始化雷的位置
Random rand = new Random();
for (int i = 0; i < mines; i++) {
int x = rand.nextInt(n);
int y = rand.nextInt(n);
if (board[x][y] != -1) {
board[x][y] = -1;
} else {
i--;
}
}
// 计算每个方格周围雷的数量
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] != -1) {
int count = 0;
for (int di = -1; di <= 1; di++) {
for (int dj = -1; dj <= 1; dj++) {
int ni = i + di;
int nj = j + dj;
if (ni >= 0 && ni < n && nj >= 0 && nj < n && board[ni][nj] == -1) {
count++;
}
}
}
board[i][j] = count;
}
}
}
}
// 根据坐标翻开格子
public boolean reveal(int x, int y) {
if (board[x][y] == -1) {
return false;
}
visible[x][y] = true;
remaining--;
if (board[x][y] == 0) {
for (int di = -1; di <= 1; di++) {
for (int dj = -1; dj <= 1; dj++) {
int ni = x + di;
int nj = y + dj;
if (ni >= 0 && ni < n && nj >= 0 && nj < n && !visible[ni][nj]) {
reveal(ni, nj);
}
}
}
}
return true;
}
// 打印游戏状态
public void print() {
System.out.print(" ");
for (int j = 0; j < n; j++) {
System.out.print(j + " ");
}
System.out.println();
for (int i = 0; i < n; i++) {
System.out.print(i + " ");
for (int j = 0; j < n; j++) {
if (visible[i][j]) {
if (board[i][j] == -1) {
System.out.print("* ");
} else {
System.out.print(board[i][j] + " ");
}
} else {
System.out.print(". ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入游戏区域大小: ");
int n = scanner.nextInt();
System.out.print("请输入雷的数量: ");
int mines = scanner.nextInt();
Minesweeper game = new Minesweeper(n, mines);
game.print();
while (game.remaining > 0) {
System.out.print("请输入要翻开的格子坐标(x y): ");
int x = scanner.nextInt();
int y = scanner.nextInt();
if (!game.reveal(x, y)) {
System.out.println("你输了!");
return;
}
game.print();
}
System.out.println("你赢了!");
}
}
这个示例代码实现了一个基于控制台的扫雷小游戏,玩家可以输入坐标来翻开格子,并且会打印出当前游戏状态。还有很多可以改进的地方,比如添加计时、排行榜等功能。
相关推荐







