java扫雷

一个使用Java编写的扫雷程序:
Minesweeper.java

import java.util.Random;
import java.util.Scanner;

public class Minesweeper {
    // 定义X和Y轴的大小
    private static final int X = 10; 
    private static final int Y = 10;
    //雷数
    private static final int B = 10;

    private char[][] showMines = new char[X][Y];
    private char[][] mine = new char[X][Y];
    private char[][] mineNumber = new char[X][Y];
    private char[][] safe = new char[X][Y];

    private Random random = new Random();

    public void set() {
        for (int x = 0; x < X; x++) {
            for (int y = 0; y < Y; y++) {
                showMines[x][y] = 'g';
                mine[x][y] = '0';
                mineNumber[x][y] = '0';
                safe[x][y] = '1';
            }
        }

        for (int a = 0; a < B; a++) {
            int x, y;
            do {
                x = random.nextInt(X);
                y = random.nextInt(Y);
            // 确保不重复放置雷
            } while (mine[x][y] == '1'); 
            mine[x][y] = '1';
            safe[x][y] = '0';
        }
    }

    public void show() {
        System.out.print("    ");
        for (int a = 1; a <= X; a++) {
            System.out.printf("%-4d", a);
        }
        System.out.println();
        for (int a = Y - 1; a >= 0; a--) {
            System.out.printf("%-4d", a + 1);
            for (int b = 0; b < X; b++) {
                System.out.printf("   %c", showMines[b][a]);
            }
            System.out.println();
        }
    }

    public void number() {
        for (int x = 0; x < X; x++) {
            for (int y = 0; y < Y; y++) {
                int count = 0;
                for (int dx = -1; dx <= 1; dx++) {
                    for (int dy = -1; dy <= 1; dy++) {
                        if (dx == 0 && dy == 0) continue;
                        int nx = x + dx;
                        int ny = y + dy;
                        if (nx >= 0 && nx < X && ny >= 0 && ny < Y && mine[nx][ny] == '1') {
                            count++;
                        }
                    }
                }
                mineNumber[x][y] = (char) ('0' + count);
            }
        }
    }

    public void reveal(int x, int y) {
        if (x < 0 || x >= X || y < 0 || y >= Y || mineNumber[x][y] != '0' || showMines[x][y] != 'g') {
            return;
        }
        showMines[x][y] = mineNumber[x][y];
        safe[x][y] = '0';
        for (int dx = -1; dx <= 1; dx++) {
            for (int dy = -1; dy <= 1; dy++) {
                reveal(x + dx, y + dy);
            }
        }
    }

    public int play() {
        Scanner scanner = new Scanner(System.in);
        set();
        number();
        show();

        while (true) {
            int safeCount = 0;
            for (int a = 0; a < X; a++) {
                for (int b = 0; b < Y; b++) {
                    safeCount += safe[a][b] - '0';
                }
            }
            if (safeCount == 0) {
                System.out.println("You win!");
                for (int a = 0; a < X; a++) {
                    for (int b = 0; b < Y; b++) {
                        showMines[a][b] = mine[a][b];
                    }
                }
                show();
                break;
            }

            System.out.print("Enter coordinates (x y): ");
            int x = scanner.nextInt() - 1;
            int y = scanner.nextInt() - 1;

            if (x < 0 || x >= X || y < 0 || y >= Y) {
                System.out.println("Invalid coordinates!");
                continue;
            }

            if (mine[x][y] == '1') {
                System.out.println("Game Over!");
                for (int a = 0; a < X; a++) {
                    for (int b = 0; b < Y; b++) {
                        showMines[a][b] = mine[a][b];
                    }
                }
                show();
                break;
            } else {
                showMines[x][y] = mineNumber[x][y];
                safe[x][y] = '0';
                reveal(x, y);
                show();
            }
        }

        System.out.print("Do you want to continue? (1/0): ");
        int a = scanner.nextInt();
        if (a == 1) {
            play();
        }
        scanner.close();
        return 0;
    }

    public static void main(String[] args) {
        Minesweeper game = new Minesweeper();
        game.play();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值