相信以前都玩过扫雷吧,比较闲,自己写了个玩,性能算法什么的都没考虑。。
下载地址:http://download.csdn.net/detail/u010697537/8728967
以下就是我2个小时的成果,很简单的demo
大致思路:
1.定好10*10的方格,设定20个雷,并随机雷的位置
2.遍历所有的方格位置,计算方格上显示的数字或者雷或者0
3.点击方格,判断当前方格位置的内容,长按标记是不是雷
难点:1.计算各个方格中该显示的数字(比较懒,直接用try catch来防止2维数组下标越界)
2.点击空白(边上没有雷的方格),边上的一块区域内的方格全部显示内容
这些都在代码里面有,不多说了,直接上代码:
复制代码
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
318
319
320
321
322
323
324
325
326<pre name="code" class="java">package com.example.winmine; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Random; import java.util.Set; import android.R.integer; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.TextView; public class MainActivity extends Activity { private GridView gridview; private List<Integer> list; private List<Integer> numFoundList; private List<Integer> mineFoundList; private Context context; private int itemWidth; private int sum; private static final int TYPE_MINE = -1; private static final int TYPE_NULL = 0; private static final int LINE = 10; private static final int SUM_MINE = 20; boolean[][] isNullArr; // private List<Point> nullPosList; /** * 点击某个格子,边上一起也显示的集合 * */ private Set<Integer> nullPosSet; private List<Integer> nullPosList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); itemWidth = getResources().getDisplayMetrics().widthPixels/10; gridview = (GridView) findViewById(R.id.gridview); context = this; sum = LINE*LINE; getList(); gridview.setAdapter(new MyAdapter()); gridview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub if(list.get(arg2)==TYPE_MINE){ new AlertDialog.Builder(context).setMessage("你是sb!!") .setPositiveButton("确定", null).show(); }else { if(hasNull(arg2)){ nullPosSet = new HashSet<Integer>(); nullPosList = new ArrayList<Integer>(); getNullList(arg2); for(int i:nullPosSet){ ((TextView)arg0.getChildAt(i)).setTextColor(getResources().getColor(android.R.color.black)); arg0.getChildAt(i).setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light)); arg0.getChildAt(i).setOnClickListener(null); numFoundList.add(i); } }else{ ((TextView)arg1).setTextColor(getResources().getColor(android.R.color.black)); arg1.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light)); arg1.setOnClickListener(null); numFoundList.add(arg2); } Log.e("@@@", "gameList.size()=="+numFoundList.size()); if(numFoundList.size()>=(LINE*LINE-SUM_MINE)){ new AlertDialog.Builder(context).setMessage("success!!") .setPositiveButton("确定", null).show(); } } } }); gridview.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub if(arg1.getTag()==null){ arg1.setTag(list.get(arg2)); ((TextView)arg1).setText("*"); ((TextView)arg1).setTextColor(getResources().getColor(android.R.color.black)); arg1.setBackgroundColor(getResources().getColor(android.R.color.holo_red_dark)); }else{ ((TextView)arg1).setText(arg1.getTag().toString()); ((TextView)arg1).setTextColor(getResources().getColor(android.R.color.white)); arg1.setBackgroundColor(getResources().getColor(android.R.color.white)); arg1.setTag(null); } return true; } }); } public void getList() { list = new ArrayList<Integer>(); numFoundList = new ArrayList<Integer>(); Set<Integer> sets = new HashSet<Integer>(); Random random = new Random(); while (sets.size() < SUM_MINE) { sets.add(random.nextInt(sum)); } for(int i=0;i<sum;i++){ if(sets.contains(i)){ list.add(TYPE_MINE); }else{ list.add(TYPE_NULL); } } int[][] array = new int[LINE][LINE]; isNullArr = new boolean[LINE][LINE]; for(int i=0;i<LINE;i++){ for(int j=0;j<LINE;j++){ array[i][j] = list.get(i*LINE+j); } } for(int i=0;i<LINE;i++){ for(int j=0;j<LINE;j++){ int num = list.get(i*LINE+j); if(num!=TYPE_MINE){ try{ num+=array[i-1][j-1]; }catch(Exception e){} try{ num+=array[i-1][j]; }catch(Exception e){} try{ num+=array[i-1][j+1]; }catch(Exception e){} try{ num+=array[i][j-1]; }catch(Exception e){} try{ num+=array[i][j+1]; }catch(Exception e){} try{ num+=array[i+1][j-1]; }catch(Exception e){} try{ num+=array[i+1][j]; }catch(Exception e){} try{ num+=array[i+1][j+1]; }catch(Exception e){} list.set(i*LINE+j, -num); if(num==TYPE_NULL){ isNullArr[i][j]=true; } } } } } private boolean hasNull(int index){ try{ if(isNullArr[index/LINE-1][index%LINE-1])return true; }catch(Exception e){} try{ if(isNullArr[index/LINE-1][index%LINE])return true; }catch(Exception e){} try{ if(isNullArr[index/LINE-1][index%LINE+1])return true; }catch(Exception e){} try{ if(isNullArr[index/LINE][index%LINE-1])return true; }catch(Exception e){} try{ if(isNullArr[index/LINE][index%LINE])return true; }catch(Exception e){} try{ if(isNullArr[index/LINE][index%LINE+1])return true; }catch(Exception e){} try{ if(isNullArr[index/LINE+1][index%LINE-1])return true; }catch(Exception e){} try{ if(isNullArr[index/LINE+1][index%LINE])return true; }catch(Exception e){} try{ if(isNullArr[index/LINE+1][index%LINE+1])return true; }catch(Exception e){} return false; } private void getNullList(List<Integer> posList){ for(int p:posList){ getNullList(p); } } private void getNullList(int index){ if(nullPosList.contains(index))return; nullPosList.add(index); List<Integer> nullPos = new ArrayList<Integer>(); try{ if(isNullArr[index/LINE-1][index%LINE-1]){ if(!nullPosList.contains(index-LINE-1)) nullPos.add(index-LINE-1); } if(list.get(index-LINE-1)!=TYPE_MINE) nullPosSet.add(index-LINE-1); }catch(Exception e){} try{ if(isNullArr[index/LINE-1][index%LINE]){ if(!nullPosList.contains(index-LINE)) nullPos.add(index-LINE); } if(list.get(index-LINE)!=TYPE_MINE) nullPosSet.add(index-LINE); }catch(Exception e){} try{ if(isNullArr[index/LINE-1][index%LINE+1]){ if(!nullPosList.contains(index-LINE+1)) nullPos.add(index-LINE+1); } if(list.get(index-LINE+1)!=TYPE_MINE) nullPosSet.add(index-LINE+1); }catch(Exception e){} try{ if(isNullArr[index/LINE][index%LINE-1]){ if(!nullPosList.contains(index-1)) nullPos.add(index-1); } if(list.get(index-1)!=TYPE_MINE) nullPosSet.add(index-1); }catch(Exception e){} try{ if(isNullArr[index/LINE][index%LINE+1]){ if(!nullPosList.contains(index+1)) nullPos.add(index+1); } if(list.get(index+1)!=TYPE_MINE) nullPosSet.add(index+1); }catch(Exception e){} try{ if(isNullArr[index/LINE+1][index%LINE-1]){ if(!nullPosList.contains(index+LINE-1)) nullPos.add(index+LINE-1); } if(list.get(index+LINE-1)!=TYPE_MINE) nullPosSet.add(index+LINE-1); }catch(Exception e){} try{ if(isNullArr[index/LINE+1][index%LINE]){ if(!nullPosList.contains(index+LINE)) nullPos.add(index+LINE); } if(list.get(index+LINE)!=TYPE_MINE) nullPosSet.add(index+LINE); }catch(Exception e){} try{ if(isNullArr[index/LINE+1][index%LINE+1]){ if(!nullPosList.contains(index+LINE+1)) nullPos.add(index+LINE+1); } if(list.get(index+LINE+1)!=TYPE_MINE) nullPosSet.add(index+LINE+1); }catch(Exception e){} getNullList(nullPos); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub arg1 = LayoutInflater.from(context).inflate(R.layout.item, null); AbsListView.LayoutParams params = new AbsListView.LayoutParams(itemWidth-1, itemWidth); arg1.setLayoutParams(params); if(list.get(arg0)==TYPE_MINE){ ((TextView)arg1).setText("*"); }else if(list.get(arg0)==TYPE_NULL){ ((TextView)arg1).setText(""); }else{ ((TextView)arg1).setText(""+list.get(arg0)); } return arg1; } } }
<pre name="code" class="html"><TextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="@android:color/white" android:background="@android:color/white" android:gravity="center"/>
复制代码
1
temWidth);arg1.setLayoutParams(params);if(list.get(arg0)==TYPE_MINE){((TextView)arg1).setText("*");}else if(list.get(arg0)==TYPE_NULL){((TextView)arg1).setText("");}else{((TextView)arg1).setText(""+list.get(arg0));}return arg1;}}}
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" tools:context=".MainActivity" > <GridView android:id="@+id/gridview" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numColumns="10" android:verticalSpacing="1.0px" android:horizontalSpacing="1.0px" /> </RelativeLayout>
复制代码
1
最后
以上就是内向黑米最近收集整理的关于android开发-写个扫雷玩玩的全部内容,更多相关android开发-写个扫雷玩玩内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复