在doc控制界面的扫雷程序

介绍

  用Java实现在doc控制界面的扫雷程序。

项目结构

在这里插入图片描述
  只需建3个类,界面显示类MineClearanceFrame、布置地雷类SetMine和探测地雷类Detect

源代码

界面显示类MineClearanceFrame

package frame;

import detect.Detect;
import setMine.SetMine;
import java.util.Date;

public class MindClearanceFrame {
    private int rows,cols,mineCount;                        // 行数、列数,地雷数
    private static final int minCount = 10;                 // 最小地雷数
    private static final int subtractMaxCount = 10;         // 与行数和列数乘积的插值,即最大地雷数与行数*列数的差值
    private static final char displayNotDetect = '*';       // 未被探测的区域显示的符号
    private static final String displayMine = "雷";          // 地雷显示的符号
    SetMine setMine ;
    Detect detect;

    public MindClearanceFrame(int row,int col,int count){
        rows=row>=10?row:10;
        cols=col>=10?col:10;
        if(count<minCount)
            mineCount=minCount;
        else if(count>row*col-subtractMaxCount)
            mineCount=row*col-subtractMaxCount;
        else
            mineCount=count;

        setMine = new SetMine(rows,cols,mineCount);
        detect = new Detect(rows,cols);
        detect.setHasMine(setMine.getHasMine());
        display();
    }

    // 显示当前扫雷情况
    public void display(){
        System.out.println("扫雷1.0     日期:"+new Date());

        for(int i=1;i<rows+1;i++){
            for(int j=1;j<cols+1;j++){
                if(detect.getDetected()[i][j]){
                    if(setMine.getHasMine()[i][j])
                        System.out.print(displayMine+"  ");
                    else
                        System.out.print(setMine.getMineCount()[i][j]+"  ");
                }
                else {
                    System.out.print(displayNotDetect +"  ");
                }
            }
            System.out.print('\n');
        }
    }

    // 查看坐标(x,y)单元,判断游戏是否结束。
    public boolean click(int x,int y){
        int result=detect.detect(x+1,y+1);
        switch (result){
            case -2:System.out.println("该坐标不合法!");
                break;
            case -1:System.out.println("该坐标已探测!");
                break;
            case 1:display();
                System.out.println("你踩雷了!");

                return true;
            case 0:display();
                break;
        }

        return detect.isGameOver();
    }
}

布置地雷类SetMine

package setMine;

public class SetMine {
    private int count;                      //地雷的数量
    private boolean[][] hasMine;            //是否有雷
    private int[][] mineCount;              //地雷周围的数字

    public SetMine(int rows,int cols,int count){
        hasMine = new boolean[rows+2][cols+2];
        mineCount = new int[rows+2][cols+2];
        this.count=count;

        // 初始化
        for(int i=0;i<rows+2;i++)
            for(int j=0;j<cols+2;j++){
                hasMine[i][j]=false;
                mineCount[i][j]=0;
            }

        setMine(rows,cols);         // 随机生成地雷
        setMineCount(rows,cols);    // 计算地雷周围的数字
    }

    // 随机生成地雷
    public void setMine(int rows, int cols){
        int r,c;

        for(int i=0;i<count;i++){
            r=(int)(Math.random()*rows)+1;
            c=(int)(Math.random()*cols)+1;
            if(hasMine[r][c]){
                i--;
            }
            else {
                hasMine[r][c]=true;
            }
        }
    }

    // 计算地雷周围的数字
    public void setMineCount(int rows,int cols){
        for(int i=1;i<rows+1;i++)
            for(int j=1;j<cols+1;j++){
                if(hasMine[i][j]){    //有地雷
                    for(int x=i-1;x<=i+1;x++)
                        for(int y=j-1;y<=j+1;y++)
                            mineCount[x][y]++;
                }
            }
    }

    public boolean[][] getHasMine(){
        return hasMine;
    }

    public int[][] getMineCount() {
        return mineCount;
    }
}

探测地雷类Detect

package detect;

public class Detect {
    private boolean[][] detected;       //是否被探测
    private boolean[][] hasMine;        //是否有雷

    public Detect(int rows,int clos){
        detected=new boolean[rows+2][clos+2];       //初始化是否被探测的判断矩阵,长度加2是为了后面不需要考虑边界情况。
        for (int i=0;i<rows+2;i++)
            for (int j=0;j<clos+2;j++)
                detected[i][j]=false;
    }

    public void setHasMine(boolean[][] hasMine){
        this.hasMine=hasMine;
    }

    public boolean[][] getDetected() {
        return detected;
    }

    //进行单元格探测
    public int detect(int x, int y){
        if(1<=x&&x<=detected.length-1&&1<=y&&y<=detected[0].length){        //判断坐标是否合法
            if(!detected[x][y]){        //未被探测
                detected[x][y]=true;
                if(hasMine[x][y])       //探测到地雷
                    return 1;
                else
                    return 0;
            }
            else {                      //已经被探测过了
                return -1;
            }
        }
        else {                          //坐标不合法
            return -2;
        }
    }

    // 判断所有的单元格是否被探测完
    public boolean isGameOver(){
        for(int i=1;i<detected.length-1;i++)
            for(int j=1;j<detected[0].length-1;j++)
                if(!detected[i][j])
                    return false;

        System.out.println("你胜利了!");

        return true;
    }
}

如何使用

  使用时就是首先声明一个MindClearanceFrame类对象,设置好扫雷的大小即行数和列数、设置雷的个数。然后调用类对象的click(x,y)方法,参数x为横坐标从0开始,参数y为列坐标从0开始。返回参数为bool类型,返回true表示游戏结束,返回false表示游戏可以继续进行。

测试类代码!!!

package integrationTest;

import frame.MindClearanceFrame;
import org.junit.Test;

public class IntegrationTest {
    @Test
    public void test(){
        int row=10;
        int col=10;
        int count=10;
        MindClearanceFrame mindClearanceFrame = new MindClearanceFrame(row,col,count);

        for (int x=0;x<row;x++)
            for (int y=0;y<col;y++)
                if(mindClearanceFrame.click(x,y))
                    return;
    }
}

运行结果

扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  1  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  1  1  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  1  1  2  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  1  1  2  1  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  1  1  2  1  1  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  1  1  2  1  1  0  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  1  1  2  1  1  0  1  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  1  1  2  1  1  0  1  1  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  1  1  2  1  1  0  1  1  
0  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  1  1  2  1  1  0  1  1  
0  0  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  1  1  2  1  1  0  1  1  
0  0  1  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
扫雷1.0     日期:Wed Apr 13 14:50:11 CST 2022
0  0  1  1  2  1  1  0  1  1  
0  0  1*  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  
你踩雷了!

Process finished with exit code 0
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值