扫雷Plus(加入等级控制)

本文档介绍了如何在原有扫雷游戏中加入等级控制,通过Game类实现不同难度级别的游戏设置,并配合Menu和Level方法提供用户界面选择。作者通过实例展示了如何通过代码调整雷区大小和数量,以及如何设计用户交互流程。

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

声明:这几篇博客我都用扫雷为例用来巩固练习我学习的不同知识,这并不是写扫雷的最优方式,只是为了满足自己练习而写。

我们之前的一篇博客,写了扫雷,完成了扫雷的基本原理,扫雷,插旗都实现了,今天我们主要加入了等级控制,首先这是我上篇博客的链接:上篇扫雷链接

Game类

class Game {
    //     定义12行12列,实际扫雷的区域只有10*10
    static int ROW = 12;

    static int COL = 12;
    //    定义雷的个数
    static int NUM = 20;
    //    0为无炸弹,1为有炸弹
    public static byte BOOM = '1';

    public Game(int ROW, int COL, int NUM) {
        this.ROW = ROW;
        this.COL = COL;
        this.NUM = NUM;
    }

    public static void setMines(int[][] mine_board, int row, int col) {
        int i = 0;
        while (i < NUM) {
            Random random = new Random();//默认随机种子是系统时间
            int x = random.nextInt(ROW - 2) + 1;
            int y = random.nextInt(COL - 2) + 1;
            if (mine_board[x][y] == BOOM) {
                continue;
            }
            mine_board[x][y] = BOOM;
            i++;
        }
    }

    public static void showBoard(int[][] board, int row, int col) {
        System.out.print(" ");
        for (int i = 1; i <= row - 2; i++) {
            System.out.printf("%4d", i);
        }
        System.out.println();
        System.out.print(" ");
        for (int k = 1; k <= col - 2; k++) {
            System.out.print("----");
        }
        System.out.println();
        for (int i = 1; i <= ROW - 2; i++) {
            System.out.printf("%2d|", i);
            for (int j = 1; j <= COL - 2; j++) {
                System.out.printf(" %c |", board[i][j]);
            }
            System.out.println();
            for (int k = 1; k <= COL - 2; k++) {
                System.out.print("----");
            }
            System.out.println();
        }
    }

    public static void flag(int[][] board, int a, int b) {
        if (b == 0) {
            System.out.print("请输入插旗坐标<x y> ");
            Scanner sc = new Scanner(System.in);
            int x = sc.nextInt();
            int y = sc.nextInt();
            board[x][y] = '$';
        } else {
            while (true) {
                System.out.print("请输入取消插旗坐标<x y> ");
                Scanner sc = new Scanner(System.in);
                int i = sc.nextInt();
                int j = sc.nextInt();
                if (board[i][j] == '$') {
                    board[i][j] = '*';
                    break;
                } else {
                    System.out.println("输入坐标有误!");
                    continue;

                }
            }
        }
    }

    public static int countMines(int[][] board, int x, int y) {
        return board[x - 1][y - 1] + board[x - 1][y]
                + board[x - 1][y + 1] + board[x][y - 1]
                + board[x][y + 1] + board[x + 1][y - 1]
                + board[x + 1][y] + board[x + 1][y + 1] - 8 * '0';
    }

    public static void game() {
        int[][] show_board = new int[ROW][COL];
        int[][] mine_board = new int[ROW][COL];
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                show_board[i][j] = '*';
                mine_board[i][j] = '0';
            }
        }
        setMines(mine_board, ROW, COL);
        int tot = (ROW - 2) * (COL - 2) - NUM;
        int clear = 0;
        while (true) {
            showBoard(show_board, ROW, COL);
            System.out.print("请输扫雷入坐标<x y> ");
            Scanner sc = new Scanner(System.in);
            int x = sc.nextInt();
            int y = sc.nextInt();
            if ((x == 0 && y == 0) || (x == 0 && y == 1)) {
                flag(show_board, x, y);
                continue;
            }
            if (!(x >= 1 && x <= ROW - 2 && y >= 1 && y <= COL - 2)) {
                System.out.println("扫雷的位置有问题!");
                continue;
            }
            if (show_board[x][y] != '*') {
                System.out.println("扫雷的位置上已经排除!");
                continue;
            }
            if (mine_board[x][y] == '1') {
                System.out.println("不好意思,你被炸死了!");
                break;
            } else {
                int count = countMines(mine_board, x, y);
                show_board[x][y] = count + '0';
                clear++;
            }
            if (clear >= tot) {
                System.out.println("恭喜你,游戏胜利!");
                break;
            }
        }

    }
}

首先我们将上个博客代码中所有的方法打包成为一个新的Game类。

Menu方法和Level方法

 public static void menu(){
        System.out.println("####################");
        System.out.println("#####  1.Play  #####");
        System.out.println("#####  2.Exit  #####");
        System.out.println("####################");
        System.out.print("Please Select> ");
    }
    public static void Level(){
        System.out.println("####################");
        System.out.println("#####  level1  #####");
        System.out.println("#####  level2  #####");
        System.out.println("#####  level3  #####");
        System.out.println("####################");
        System.out.print("Please Select> ");
    }

这两个方法定义在minesweepplus类中,也就是包含main方法的类,主要用来打印我们需要选择的界面。

Main方法

 public static void main(String[] args) {
        boolean quit=true;
        while(quit){
            menu();
            Scanner sc=new Scanner(System.in);
            int select=sc.nextInt();
            switch(select){
                case 1:
                    Level();
                    Scanner scn=new Scanner(System.in);
                    int level=sc.nextInt();
                    switch(level){
                        case 1:
                            Game game1=new Game(7,7,5);
                            game1.game();
                            break;
                        case 2:
                            Game game2=new Game(9,9,10);
                            game2.game();
                            break;
                        case 3:
                            Game game3=new Game(12,12,20);
                            game3.game();
                            break;
                        default:
                            System.out.println("输入有误!");
                            break;
                    }
                    break;
                case 2:
                    quit=false;
                    break;
                default:
                    System.out.println("输入有误,请重新输入!");
                    break;
            }
        }
    }

main方法中我们首先进入死循环,接着调用menu方法打印菜单进行选择。

case 1:
                    Level();
                    Scanner scn=new Scanner(System.in);
                    int level=sc.nextInt();
                    switch(level){
                        case 1:
                            Game game1=new Game(7,7,5);
                            game1.game();
                            break;
                        case 2:
                            Game game2=new Game(9,9,10);
                            game2.game();
                            break;
                        case 3:
                            Game game3=new Game(12,12,20);
                            game3.game();
                            break;
                        default:
                            System.out.println("输入有误!");
                            break;
                    }
                    break;

选一,调用level方法打印,根据提示进行选择,然后我们只需要根据不同选择new 一个Game对象然后用构造方法给定不同的值就完成了等级的控制。

总代码

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


class Game {
    //     定义12行12列,实际扫雷的区域只有10*10
    static int ROW = 12;

    static int COL = 12;
    //    定义雷的个数
    static int NUM = 20;
    //    0为无炸弹,1为有炸弹
    public static byte BOOM = '1';

    public Game(int ROW, int COL, int NUM) {
        this.ROW = ROW;
        this.COL = COL;
        this.NUM = NUM;
    }

    public static void setMines(int[][] mine_board, int row, int col) {
        int i = 0;
        while (i < NUM) {
            Random random = new Random();//默认随机种子是系统时间
            int x = random.nextInt(ROW - 2) + 1;
            int y = random.nextInt(COL - 2) + 1;
            if (mine_board[x][y] == BOOM) {
                continue;
            }
            mine_board[x][y] = BOOM;
            i++;
        }
    }

    public static void showBoard(int[][] board, int row, int col) {
        System.out.print(" ");
        for (int i = 1; i <= row - 2; i++) {
            System.out.printf("%4d", i);
        }
        System.out.println();
        System.out.print(" ");
        for (int k = 1; k <= col - 2; k++) {
            System.out.print("----");
        }
        System.out.println();
        for (int i = 1; i <= ROW - 2; i++) {
            System.out.printf("%2d|", i);
            for (int j = 1; j <= COL - 2; j++) {
                System.out.printf(" %c |", board[i][j]);
            }
            System.out.println();
            for (int k = 1; k <= COL - 2; k++) {
                System.out.print("----");
            }
            System.out.println();
        }
    }

    public static void flag(int[][] board, int a, int b) {
        if (b == 0) {
            System.out.print("请输入插旗坐标<x y> ");
            Scanner sc = new Scanner(System.in);
            int x = sc.nextInt();
            int y = sc.nextInt();
            board[x][y] = '$';
        } else {
            while (true) {
                System.out.print("请输入取消插旗坐标<x y> ");
                Scanner sc = new Scanner(System.in);
                int i = sc.nextInt();
                int j = sc.nextInt();
                if (board[i][j] == '$') {
                    board[i][j] = '*';
                    break;
                } else {
                    System.out.println("输入坐标有误!");
                    continue;

                }
            }
        }
    }

    public static int countMines(int[][] board, int x, int y) {
        return board[x - 1][y - 1] + board[x - 1][y]
                + board[x - 1][y + 1] + board[x][y - 1]
                + board[x][y + 1] + board[x + 1][y - 1]
                + board[x + 1][y] + board[x + 1][y + 1] - 8 * '0';
    }

    public static void game() {
        int[][] show_board = new int[ROW][COL];
        int[][] mine_board = new int[ROW][COL];
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                show_board[i][j] = '*';
                mine_board[i][j] = '0';
            }
        }
        setMines(mine_board, ROW, COL);
        int tot = (ROW - 2) * (COL - 2) - NUM;
        int clear = 0;
        while (true) {
            showBoard(show_board, ROW, COL);
            System.out.print("请输扫雷入坐标<x y> ");
            Scanner sc = new Scanner(System.in);
            int x = sc.nextInt();
            int y = sc.nextInt();
            if ((x == 0 && y == 0) || (x == 0 && y == 1)) {
                flag(show_board, x, y);
                continue;
            }
            if (!(x >= 1 && x <= ROW - 2 && y >= 1 && y <= COL - 2)) {
                System.out.println("扫雷的位置有问题!");
                continue;
            }
            if (show_board[x][y] != '*') {
                System.out.println("扫雷的位置上已经排除!");
                continue;
            }
            if (mine_board[x][y] == '1') {
                System.out.println("不好意思,你被炸死了!");
                break;
            } else {
                int count = countMines(mine_board, x, y);
                show_board[x][y] = count + '0';
                clear++;
            }
            if (clear >= tot) {
                System.out.println("恭喜你,游戏胜利!");
                break;
            }
        }

    }
}

public class MineSweepPlus {
    public static void menu(){
        System.out.println("####################");
        System.out.println("#####  1.Play  #####");
        System.out.println("#####  2.Exit  #####");
        System.out.println("####################");
        System.out.print("Please Select> ");
    }
    public static void Level(){
        System.out.println("####################");
        System.out.println("#####  level1  #####");
        System.out.println("#####  level2  #####");
        System.out.println("#####  level3  #####");
        System.out.println("####################");
        System.out.print("Please Select> ");
    }
    public static void main(String[] args) {
        boolean quit=true;
        while(quit){
            menu();
            Scanner sc=new Scanner(System.in);
            int select=sc.nextInt();
            switch(select){
                case 1:
                    Level();
                    Scanner scn=new Scanner(System.in);
                    int level=sc.nextInt();
                    switch(level){
                        case 1:
                            Game game1=new Game(7,7,5);
                            game1.game();
                            break;
                        case 2:
                            Game game2=new Game(9,9,10);
                            game2.game();
                            break;
                        case 3:
                            Game game3=new Game(12,12,20);
                            game3.game();
                            break;
                        default:
                            System.out.println("输入有误!");
                            break;
                    }
                    break;
                case 2:
                    quit=false;
                    break;
                default:
                    System.out.println("输入有误,请重新输入!");
                    break;
            }
        }
    }
}

运行结果

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿拉蕾wjh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值