zoukankan      html  css  js  c++  java
  • Java模拟实现扫雷功能

     1 //棋子
     2 public class Chess {
     3     private boolean isBoomb=false;
     4     private int id;//下标
     5 
     6     //点击方法
     7     public int click(IChessBoard cb) {
     8         if (isBoomb) {
     9             System.out.println("触雷");
    10             return -1;
    11         }
    12 
    13         //获取自己的所有邻居,把自己的ID传过去,然后进行判断每个邻居是否是炸弹
    14         ArrayList<Chess> neig = cb.getNei(id);  //cb.getNei();回调函数
    15         int cnt = 0;
    16         for (Chess c : neig) {
    17             if (c.isBoomb) {
    18                 cnt++;//如果是雷
    19             }
    20         }
    21         //
    22         return cnt;
    23     }
    24 
    25 
    26     //构造方法
    27     public Chess(int id) {
    28         this.id = id;
    29     }
    30 
    31     public boolean isBoomb() {
    32         return isBoomb;
    33     }
    34 
    35     public void setBoomb(boolean boomb) {
    36         isBoomb = boomb;
    37     }
    38 
    39     public int getId() {
    40         return id;
    41     }
    42 
    43     public void setId(int id) {
    44         this.id = id;
    45     }
    46 }
      1 /**
      2  * //得到一个参数,把此参数的所有邻居放到数组里
      3  *
      4  * @author liuwenlong
      5  * @create 2020-07-21 13:57:14
      6  */
      7 //棋盘类
      8 @SuppressWarnings("all")
      9 public class ChessBoard implements IChessBoard {
     10     private ArrayList<Chess> board = new ArrayList<>();//构造一个随机数的地雷画板
     11     private int maxx;
     12     private int miny;
     13     private int boomNum;
     14 
     15     public ChessBoard(int maxx, int miny, int boomNum) {
     16         this.maxx = maxx;
     17         this.miny = miny;
     18         this.boomNum = boomNum;
     19         initBoard();//初始化要调用一下
     20     }
     21 
     22     //-------------------------------------
     23     //获取一个棋子
     24     public Chess getChess(int x, int y) {
     25         return getChess(y * maxx + x);
     26     }
     27 
     28     public Chess getChess(int id) {
     29         return board.get(id);
     30     }
     31     //-----------------------------------
     32 
     33     public int getBoomNum() {
     34         return boomNum;
     35     }
     36 
     37     public void setBoomNum(int boomNum) {
     38         this.boomNum = boomNum;
     39     }
     40 
     41     public int getMaxx() {
     42         return maxx;
     43     }
     44 
     45     public void setMaxx(int maxx) {
     46         this.maxx = maxx;
     47     }
     48 
     49     public int getMiny() {
     50         return miny;
     51     }
     52 
     53     public void setMiny(int miny) {
     54         this.miny = miny;
     55     }
     56 
     57     public void initBoard() {
     58         //构造棋子
     59         for (int i = 0; i < (maxx * miny); i++) {
     60             board.add(new Chess(i));  //在棋子构造方法内使用static自增
     61         }
     62 
     63         //生成一些雷
     64         //生成15个不重复的随机数,ArrayList<Integer> mineIds = 。。
     65 //        mineIds = ChessBoard.getRandom(mineIds, 0, maxx * miny);
     66 //        for (int i = 0; i < boomNum; i++) {  //使用foreach
     67 //            board.[i].setBoomb(true);
     68 //            //board.get(i).setBoomb(true);
     69 //        }
     70         ArrayList<Integer> mineIds = getRandom(0, 99, 15);
     71 //        for (int i = 0; i < boomNum; i++) {
     72 //            board.get(i).setBoomb(true);
     73 //        }
     74         for (int i : mineIds) {
     75             board.get(i).setBoomb(true);
     76         }
     77     }
     78 
     79     // int id = y*maxx+x;下标
     80     public void showBoard() {
     81         for (int i = 0; i < maxx; i++) {
     82             String line = "";
     83             for (int j = 0; j < maxx; j++) {
     84                 int id = i * maxx + j;
     85                 Chess c = board.get(id);
     86                 if (c.isBoomb()) {
     87                     line += 1 + " ";
     88                 } else {
     89                     line += 0 + " ";
     90                 }
     91             }
     92             System.out.println(line);
     93         }
     94     }
     95 
     96 
     97     /**
     98      * 随机生成 N--M,N个不重复随机数  使用ArrayList
     99      *
    100      * @param startRange 起始数字
    101      * @param endRange   终止数字
    102      * @param count      个数
    103      */
    104     public static ArrayList<Integer> getRandom(int startRange, int endRange, int count) {
    105         ArrayList<Integer> arr = new ArrayList<>();
    106         for (int i = 0; i < count; i++) {
    107             arr.add(((int) (Math.random() * (endRange - startRange + 1) + startRange)));
    108             for (int j = 0; j < i; j++) {
    109                 if (arr.get(i) == arr.get(j)) {
    110                     arr.remove(i);
    111                     i--;
    112                     break;
    113                 }
    114             }
    115         }
    116         return arr;
    117     }
    118 
    119 
    120     //给一个下标,算出该id周围所有邻居
    121     @Override
    122     public ArrayList<Chess> getNei(int id) {
    123 
    124         //根据下标转换坐标
    125         //整除 id/maxx--y坐标
    126         //取余 id%max  --x 坐标
    127         int x0 = id % maxx;
    128         int y0 = id / maxx;
    129 
    130         //邻居列表
    131         ArrayList<Chess> nei = new ArrayList<>();
    132         for (int ydlt = -1; ydlt < 2; ydlt++) {
    133             int y = y0 + ydlt;
    134             if (y < 0 || y >= miny) {  //miny
    135                 continue;
    136             }
    137             for (int xdlt = -1; xdlt < 2; xdlt++) {
    138                 int x = x0 + xdlt;
    139                 if (x < 0 || x >= maxx || (xdlt == 0 && ydlt == 0)) {
    140                     continue;
    141                 }
    142 
    143                 //这个棋子是邻居,根据坐标换成下标
    144                 int cid = y * maxx + x;
    145                 nei.add(board.get(cid));
    146             }
    147         }
    148         return nei;
    149     }
    150 
    151 
    152     public ArrayList<Chess> getNei(int x, int y) {
    153 
    154         return getNei(y * maxx + x); //坐标转下标
    155     }
    156 }
    1 /**
    2  * @author liuwenlong
    3  * @create 2020-07-21 14:01:38
    4  */
    5 @SuppressWarnings("all")
    6 public interface IChessBoard {
    7     //告诉我哪一个棋子
    8     public ArrayList<Chess> getNei(int id);
    9 }
     1 /**
     2  * @author liuwenlong
     3  * @create 2020-07-21 14:28:09
     4  */
     5 @SuppressWarnings("all")
     6 public class Test {
     7     public static void main(String[] args) {
     8         ChessBoard cb = new ChessBoard(10, 10, 15);
     9         cb.showBoard();
    10         System.out.println("点击的'X'坐标为:");
    11         int x = new Scanner(System.in).nextInt();
    12         System.out.println("点击的'Y'坐标为:");
    13         int y = new Scanner(System.in).nextInt();
    14         int number = cb.getChess(x, y).click(cb);
    15         if (number>=0){
    16             System.out.println("共有"+number+"个雷");
    17         }
    18     }
    19 }

  • 相关阅读:
    乐视电视修改五大图标
    Google Home Mini配置失败解决办法
    使用FreeHttp任意篡改http报文 (FreeHttp使用及实现说明)
    IDEA和VS快捷键对比
    C# 中的字符串内插
    在线运行.NET代码
    在Windows服务器安装ss服务端用于逃脱公司行为管理
    .NET Core的代码安全分析工具
    用DotNetDetour HOOK .net类库
    C#代码获取另一程序的错误提示,并关闭窗口。
  • 原文地址:https://www.cnblogs.com/lwl80/p/13356896.html
Copyright © 2011-2022 走看看