1. 程式人生 > 實用技巧 >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  * @authorliuwenlong
  5  * @create2020-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  * @authorliuwenlong
3  * @create2020-07-21 14:01:38
4  */
5 @SuppressWarnings("all")
6 public interface IChessBoard {
7     //告訴我哪一個棋子
8     public ArrayList<Chess> getNei(int id);
9 }
 1 /**
 2  * @authorliuwenlong
 3  * @create2020-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 }