##MainActivity 类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318public class MainActivity extends Activity implements OnClickListener, OnLongClickListener { // 最外层布局 LinearLayout textviews; LinearLayout buttons; int[][] map = new int[10][10]; // 用来隐藏所有Button List<Button> buttonList = new ArrayList<Button>(); // -1 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textviews = (LinearLayout) findViewById(R.id.textviews); buttons = (LinearLayout) findViewById(R.id.buttons); initData(); initView(); } Set<Integer> random地雷; private void initData() { // 10个地雷 显示* 数组中是-1 // 90个 雷的边上是数字,其他是空白 0 1-8 // 100个数字 从里面随机取走10个 // 初始化 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { map[i][j] = 0; } } // 抽取100个数 99 random地雷 = getRandom(); // 丢入map for (Integer integer : random地雷) { int hang = integer / 10;// 98 int lie = integer % 10; // 所有的地雷用-1代替 map[hang][lie] = -1; } // 为所有的空白地点去设置数值 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (map[i][j] == -1) continue; // 继续下次循环 int sum = 0; // 左上角 if (i != 0 && j != 0) {// 防止下标越界 if (map[i - 1][j - 1] == -1) sum++; } // 上面 if (j != 0) { if (map[i][j - 1] == -1) sum++; } // 右上角 if (j != 0 && i != 9) { if (map[i + 1][j - 1] == -1) sum++; } // 左边 if (i != 0) { if (map[i - 1][j] == -1) sum++; } // 右边 if (i != 9) { if (map[i + 1][j] == -1) sum++; } // 左下角 if (j != 9 && i != 0) { if (map[i - 1][j + 1] == -1) sum++; } if (j != 9) { if (map[i][j + 1] == -1) sum++; } if (j != 9 && i != 9) { if (map[i + 1][j + 1] == -1) sum++; } map[i][j] = sum; } } // 打印看看 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { System.out.print(map[i][j] + " "); } System.out.println(); } } private Set<Integer> getRandom() { // 没有重复的 Set<Integer> set = new HashSet<Integer>(); while (set.size() != 10) { int random = (int) (Math.random() * 100); set.add(random); } return set; } // 创建视图 private void initView() { int width = getResources().getDisplayMetrics().widthPixels / 10; LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, width); for (int i = 0; i < 10; i++) { // 每一条的布局 LinearLayout tvs = new LinearLayout(this); tvs.setOrientation(LinearLayout.HORIZONTAL); LinearLayout btns = new LinearLayout(this); btns.setOrientation(LinearLayout.HORIZONTAL); // 添加内层的100个按钮和文本 for (int j = 0; j < 10; j++) { // 底层的TextView TextView tv = new TextView(this); tv.setBackgroundResource(R.drawable.textview_bg); tv.setLayoutParams(params); tv.setGravity(Gravity.CENTER); if (map[i][j] == -1) tv.setText("*"); else if (map[i][j] != 0) tv.setText(map[i][j] + ""); tvs.addView(tv); // 底层的Button Button btn = new Button(this); btn.setBackgroundResource(R.drawable.button); btn.setLayoutParams(params); btn.setTag(i * 10 + j); btn.setOnClickListener(this); btn.setOnLongClickListener(this); buttonList.add(btn); btns.addView(btn); } textviews.addView(tvs); buttons.addView(btns); } } @Override public void onClick(View v) { int id = (Integer) v.getTag(); int hang = id / 10; int lie = id % 10; // 隐藏按钮,显示底下的数据 v.setVisibility(View.INVISIBLE); isOver(hang, lie); // 判断点击的是否是一个数字 if (map[hang][lie] == 0) { // 开始递归 显示周围所有的空白(hang, lie); } if (isWin()) { Toast.makeText(this, "游戏胜利", Toast.LENGTH_SHORT).show(); } } // 显示周围所有的button public void 显示周围所有的空白(int i, int j) { // 检测周围的元素,如果为0 继续调用自身,并且显示 // 不是,就显示button // 从左上角开始 // 左上角 // 先显示自己 buttonList.get(i * 10 + j).setVisibility(View.INVISIBLE); if (i != 0 && j != 0) {// 防止下标越界 if (map[i - 1][j - 1] == 0) { if (判断是否需要递归(i - 1, j - 1)) 显示周围所有的空白(i - 1, j - 1); } else { 隐藏button(i - 1, j - 1); } } // 上面 if (j != 0) { if (map[i][j - 1] == 0) { if (判断是否需要递归(i, j - 1)) 显示周围所有的空白(i, j - 1); } else { 隐藏button(i, j - 1); } } // 右上角 if (j != 0 && i != 9) { if (map[i + 1][j - 1] == 0) { if (判断是否需要递归(i + 1, j - 1)) 显示周围所有的空白(i + 1, j - 1); } else { 隐藏button(i + 1, j - 1); } } // 左边 if (i != 0) { if (map[i - 1][j] == 0) { if (判断是否需要递归(i - 1, j)) 显示周围所有的空白(i - 1, j); } else { 隐藏button(i - 1, j); } } // 右边 if (i != 9) { if (map[i + 1][j] == 0) { if (判断是否需要递归(i + 1, j)) 显示周围所有的空白(i + 1, j); } else { 隐藏button(i + 1, j); } } // 左下角 if (j != 9 && i != 0) { if (map[i - 1][j + 1] == 0) { if (判断是否需要递归(i - 1, j + 1)) 显示周围所有的空白(i - 1, j + 1); } else { 隐藏button(i - 1, j + 1); } } if (j != 9) { if (map[i][j + 1] == 0) { if (判断是否需要递归(i, j + 1)) 显示周围所有的空白(i, j + 1); } else { 隐藏button(i, j + 1); } } if (j != 9 && i != 9) { if (map[i + 1][j + 1] == 0) { if (判断是否需要递归(i + 1, j + 1)) 显示周围所有的空白(i + 1, j + 1); } else { 隐藏button(i + 1, j + 1); } } } private void 隐藏button(int i, int j) { int 位置 = i * 10 + j; buttonList.get(位置).setVisibility(View.INVISIBLE); } boolean 判断是否需要递归(int hang, int lie) { // 判断是否是现实的 int 位置 = hang * 10 + lie; if (buttonList.get(位置).getVisibility() == View.INVISIBLE) return false; else return true; } private boolean isOver(int hang, int lie) { // OVER if (map[hang][lie] == -1) { Toast.makeText(this, "GameOver", Toast.LENGTH_SHORT).show(); for (int i = 0; i < buttonList.size(); i++) { buttonList.get(i).setVisibility(View.INVISIBLE); } return true; } return false; } // 最多10个旗子 List<Integer> 旗子 = new ArrayList<Integer>(); @Override public boolean onLongClick(View v) { // 插旗子 // 1. 有了旗子 就取消 // 2. 没有就插旗 Button btn = (Button) v; int tag = (Integer) v.getTag(); if (旗子.contains(tag)) { // 如果使用drawableTop 对应的java代码的方法 // setCompoundDrawablesWithIntrinsicBounds btn.setText(""); // int ArrayList.remove(int);//下标 旗子.remove((Integer) tag); } else { // 没有插旗就需要插旗,判断数量是否超过了上限 if (旗子.size() != 10) { 旗子.add(tag); btn.setText("∉ " + 旗子.size()); btn.setTextColor(Color.RED); } else { Toast.makeText(this, "没有旗子了", Toast.LENGTH_SHORT).show(); } } // 消耗事件, return true; } // 是否胜利 public boolean isWin() { int sum = 0; for (int i = 0; i < buttonList.size(); i++) { if (buttonList.get(i).getVisibility() == View.INVISIBLE) sum++; } if (sum == 90) return true; return false; } }
##xml
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <FrameLayout android:id="@+id/framelayout" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/textviews" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" /> <LinearLayout android:id="@+id/buttons" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> </FrameLayout> </LinearLayout>
自定义的背景:
button.xml
复制代码
1
2
3
4
5
6
7
8<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <stroke android:width="1dp" android:color="@color/colorAccent" /> <solid android:color="@android:color/darker_gray" /> </shape>
textview_bg.xml
复制代码
1
2
3
4
5<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:color="@color/colorAccent" android:width="1dp"/> </shape>
项目下载
最后
以上就是任性睫毛膏最近收集整理的关于android小游戏 扫雷的全部内容,更多相关android小游戏内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复