java简单版扫雷实现

java简单版扫雷实现

实现原理

扫雷实现第一步就是生成棋盘,第二步就要生成每个棋子,定义棋子属性是否是地雷,第三部就是给不是地雷的棋子赋值(附近多少地雷)。

直接贴代码

/**
 * 扫雷棋盘
 */
public class DemoTest {
    public static void main(String[] args) {
        int[][] arrs = generate();
        print(arrs);
    }

    private static void print(int[][] arrs) {
        if (arrs == null || arrs.length == 0) {
            return;
        }
        for (int i = 0; i < arrs.length; i++) {
            for (int j = 0; j < arrs[i].length; j++) {
                if (arrs[i][j] == -2) {
                    System.out.print("P ");
                }else {
                    System.out.print(arrs[i][j] + " ");
                }
            }
            //换行打印
            System.out.println();
        }
    }

    private static int[][] generate() {
        Scanner in =new Scanner(System.in);
        //设置棋盘的大小
        System.out.println("请输入棋盘大小:");
        int a = in.nextInt();
        if (a <= 0) {
            System.out.println("棋盘大小必须大于0");
            return null;
        }
        //设置地雷的个数
        System.out.println("请设置地雷的个数:");
        int b = in.nextInt();
        if (b < 0) {
            System.out.println("地雷个数必须大于0");
            return null;
        }else if (b > a*a) {
            System.out.println("地雷个数必须小于" + a*a);
            return null;
        }
        int[][] arrs = new int[a][a];
        //地雷标记位-2,其它标记为-1
        Random r = new Random();
        for (int i = 0; i < arrs.length; i++) {
            for (int j = 0; j < arrs[i].length; j++) {
                if (b > 0) {
                    boolean flag = r.nextBoolean();
                    if (flag) {
                        arrs[i][j] = -2;
                        b --;
                    }else {
                        arrs[i][j] = -1;
                    }
                }else {
                    arrs[i][j] = -1;
                }
            }
        }

        for (int i = 0; i < arrs.length; i++) {
            for (int j = 0; j < arrs[i].length; j++) {
                if (i == 0) {
                    if (j == 0) {
                        if (arrs[i][j] == -1) {
                            int count = 0;
                            if (arrs[i+1][j] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j+1] == -2) {
                                count ++;
                            }
                            if (arrs[i][j+1] == -2) {
                                count ++;
                            }
                            arrs[i][j] = count;
                        }
                    }else if(j == arrs[i].length -1){
                        if (arrs[i][j] == -1) {
                            int count = 0;
                            if (arrs[i][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j] == -2) {
                                count ++;
                            }
                            arrs[i][j] = count;
                        }
                    }else{
                        if (arrs[i][j] == -1) {
                            int count = 0;
                            if (arrs[i][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j+1] == -2) {
                                count ++;
                            }
                            if (arrs[i][j+1] == -2) {
                                count ++;
                            }
                            arrs[i][j] = count;
                        }
                    }
                }else if (i == arrs.length -1){
                    if (j == 0) {
                        if (arrs[i][j] == -1) {
                            int count = 0;
                            if (arrs[i-1][j] == -2) {
                                count ++;
                            }
                            if (arrs[i-1][j+1] == -2) {
                                count ++;
                            }
                            if (arrs[i][j+1] == -2) {
                                count ++;
                            }
                            arrs[i][j] = count;
                        }
                    }else if(j == arrs[i].length -1){
                        if (arrs[i][j] == -1) {
                            int count = 0;
                            if (arrs[i][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i-1][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i-1][j] == -2) {
                                count ++;
                            }
                            arrs[i][j] = count;
                        }
                    }else{
                        if (arrs[i][j] == -1) {
                            int count = 0;
                            if (arrs[i][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i-1][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i-1][j] == -2) {
                                count ++;
                            }
                            if (arrs[i-1][j+1] == -2) {
                                count ++;
                            }
                            if (arrs[i][j+1] == -2) {
                                count ++;
                            }
                            arrs[i][j] = count;
                        }
                    }
                }else {
                    if (j == 0) {
                        if (arrs[i][j] == -1) {
                            int count = 0;
                            if (arrs[i-1][j] == -2) {
                                count ++;
                            }
                            if (arrs[i-1][j+1] == -2) {
                                count ++;
                            }
                            if (arrs[i][j+1] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j+1] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j] == -2) {
                                count ++;
                            }
                            arrs[i][j] = count;
                        }
                    }else if(j == arrs[i].length -1){
                        if (arrs[i][j] == -1) {
                            int count = 0;
                            if (arrs[i-1][j] == -2) {
                                count ++;
                            }
                            if (arrs[i-1][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j] == -2) {
                                count ++;
                            }
                            arrs[i][j] = count;
                        }
                    }else{
                        if (arrs[i][j] == -1) {
                            int count = 0;
                            if (arrs[i][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i-1][j-1] == -2) {
                                count ++;
                            }
                            if (arrs[i-1][j] == -2) {
                                count ++;
                            }
                            if (arrs[i-1][j+1] == -2) {
                                count ++;
                            }
                            if (arrs[i][j+1] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j+1] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j] == -2) {
                                count ++;
                            }
                            if (arrs[i+1][j-1] == -2) {
                                count ++;
                            }
                            arrs[i][j] = count;
                        }
                    }
                }
            }
        }
        return arrs;
    }
}

成果展示

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值