Unity实现简易扫雷

为君一笑 提交于 2019-12-03 03:15:12

扫雷的核心思想就是递归,点击一个格子,四周八个格子都会进行检测雷的操作,直到不能检测,用的是广度搜索算法。因为不想写太长的博客,技术也有限,所以这是个简易的扫雷,可扩展性也不好,大家看看思想就好,有兴趣可以用一些架构的思想做一个高大上的扫雷,做完可以联系我交流一下。以下是代码,有注释,不解释。

注:地图的生成可以参考我上一篇博客,写的是TXT格式的存储和读取,存的就是地图信息。


using
UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;
using UnityEngine.UI;
/// <summary>
/// 格子类
/// </summary>
public class Grid
{
    public bool isLei;//是否是雷
    public bool isClick;//是否点击了
    public byte Count;//周围有几个雷
}

public class Map : MonoBehaviour, IPointerClickHandler
{
    [SerializeField](意义是在Inspect面板显示出来,但是再其他脚本无法调用)
    private Transform gridContainer;
    [SerializeField]
    private GameObject gridPrefab;


    public const int RowCount = 10;
    public const int ColCount = 10;

    private Grid[,] grids = new Grid[RowCount, ColCount];
    private GameObject[,] tiles = new GameObject[RowCount, ColCount];
    private Vector2[] dir = new Vector2[] { Vector2.up, Vector2 .down, Vector2.left, Vector2.right,
        new Vector2 (-1, 1), new Vector2(1, 1), new Vector2 (-1, -1), new Vector2(1, -1) };

    public void StartGame()
    {
        for (int i = 0; i < RowCount; i++)
        {
            for (int j = 0; j < ColCount; j++)
            {
                grids[i, j] = new Grid ();
                //这个是随机雷的数量和位置
                grids[i, j].isLei = UnityEngine. Random.Range(1, 11) > 2 ? false : true;
                GameObject grid = Instantiate(gridPrefab);
                grid.transform.SetParent(gridContainer);
                grid.name = i.ToString() + "," + j.ToString();
                tiles[i, j] = grid;
            }
        }
    }
    /// <summary>
    /// 格子点击
    /// </summary>
    /// <param name="x"> x坐标</param>
    /// <param name="y"> y坐标</param>
    public void GridClick(int x,int y)
    {
       
        if (!grids[x, y].isClick)
        {
            if (grids[x, y].isLei)
            {
                Debug.Log("点到了雷" );
                return;
            }
            //循环遍历周围8个方向,是否有雷,算出Count;
            for (int i = 0; i < dir.Length; i++)
            {
                int temp_x = (int )(x + dir[i].x);
                int temp_y = (int )(y + dir[i].y);
                //判断是否越界
                if (temp_x >= 0 && temp_x < RowCount && temp_y >= 0 && temp_y < ColCount)
                {
                    if (grids[temp_x, temp_y].isLei)
                    {
                        grids[x, y].Count++;
                    }
                }
            }         
            //赋值
            grids[x, y].isClick = true;
            tiles[x, y].transform.GetChild(0).gameObject.SetActive( true);
            tiles[x, y].GetComponent< Image>().color = Color .grey;
            if (grids[x, y].Count > 0)
            {
                tiles[x, y].GetComponentInChildren< Text>().text = grids[x, y].Count.ToString();
            }
            else
            {
                DiGui(x, y);
            }

        }
    }

    public void DiGui(int x,int y)
    {
        //循环遍历周围8个方向,是否有雷,算出Count;
        for (int i = 0; i < dir.Length; i++)
        {
            int temp_x = (int )(x + dir[i].x);
            int temp_y = (int )(y + dir[i].y);
            //判断是否越界
            if (temp_x >= 0 && temp_x < RowCount && temp_y >= 0 && temp_y < ColCount)
            {
                GridClick(temp_x, temp_y);
            }
        }
    }
    public void Start()
    {
        StartGame();
    }

    public void OnPointerClick(PointerEventData eventData)
    {

        GameObject enter = eventData.pointerEnter;
        if (enter.name.Contains("," ))
        {
            int x = int .Parse(enter.name.Split(',')[0]);
            int y = int .Parse(enter.name.Split(',')[1]);
            GridClick(x, y);
        }
    }

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!