android開發-寫個掃雷玩玩
阿新 • • 發佈:2019-01-07
相信以前都玩過掃雷吧,比較閒,自己寫了個玩,效能演算法什麼的都沒考慮。。
以下就是我2個小時的成果,很簡單的demo
大致思路:
1.定好10*10的方格,設定20個雷,並隨機雷的位置
2.遍歷所有的方格位置,計算方格上顯示的數字或者雷或者0
3.點選方格,判斷當前方格位置的內容,長按標記是不是雷
難點:1.計算各個方格中該顯示的數字(比較懶,直接用try catch來防止2維陣列下標越界)
2.點選空白(邊上沒有雷的方格),邊上的一塊區域內的方格全部顯示內容
這些都在程式碼裡面有,不多說了,直接上程式碼:
<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"/>
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;}}}
<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>