我是靠谱客的博主 内向黑米,最近开发中收集的这篇文章主要介绍android开发-写个扫雷玩玩,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

相信以前都玩过扫雷吧,比较闲,自己写了个玩,性能算法什么的都没考虑。。

下载地址:http://download.csdn.net/detail/u010697537/8728967

以下就是我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>

 

 


最后

以上就是内向黑米为你收集整理的android开发-写个扫雷玩玩的全部内容,希望文章能够帮你解决android开发-写个扫雷玩玩所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部