扫雷小游戏难点总结:
1 布雷的生成算法
2 难易度调节,离散空间数据靠近程度
3 空白区域的递归算法(或队列的搜索)
4 输赢的判断
u3d上, ui为NGUI
代码 部分 ,写得比较乱 也懒得整理了
using UnityEngine; using System.Collections; using System; public class SaoLei2 : MonoBehaviour { public GameObject gamePanel; public GameObject ShowLable; public GameObject _Qizi; public GameObject _Chacha; public GameObject ScoreLabel; public int BoxWidth = 10; public int BoxHeight = 10; public int TwohengWidth = 50; private int[,] BoxMatrix; private Vector3 OriginPoint;//左下角原点 public GameObject[,] mQizi; private int WinnerPlayer = 0;//1圈赢 -1叉赢 private int TotleBlock;//现在下的步骤0,0 private int TotleLandmine;//现在下的步骤0,0 private string scoreStr; enum GameState { MyStep = 0, ChaCha = 1, GameOver, } GameState gameState = GameState.MyStep; public int landmine = 10; void Start() { TotleBlock = BoxHeight * BoxWidth ; //剩余的格子 TotleLandmine = landmine;//剩余的旗子 scoreStr = "left : " + TotleBlock + " landmine : " + TotleLandmine; _Chacha.transform.localPosition = new Vector3(420,192,0);//new Vector3(Screen.width/2 +50 ,Screen.height/2 ,0); //左上角为原点 OriginPoint = new Vector3(-(BoxWidth - 1) * 0.5f * TwohengWidth, -(BoxHeight - 1) * 0.5f * TwohengWidth, 0); //儿子 BoxMatrix = new int[BoxHeight,BoxWidth];//0 表示空,1表示圈,-1表示叉 mQizi = new GameObject[BoxHeight,BoxWidth]; for (int i = 0; i < BoxHeight; i++) { for (int j = 0; j < BoxWidth; j++) { BoxMatrix[i, j] = 0; mQizi[i, j] = Instantiate(_Qizi) as GameObject; mQizi[i, j].name = "qizi_" + i + "_" + j; mQizi[i, j].transform.parent = _Qizi.transform.parent; mQizi[i, j].transform.localPosition = new Vector3(OriginPoint.x + (j) * TwohengWidth, OriginPoint.y + (i) * TwohengWidth, 0); mQizi[i, j].transform.localScale = new Vector3(1, 1, 1); } } _Qizi.transform.localPosition = new Vector3(-1000, 0, 0); //构建一个雷区 //取得10个不重复的随机数 //landmine = 10; //将2维展开成1维, int[] ranArr = ChooseSet(BoxWidth * BoxHeight ,landmine); //然后再合并为2维的索引 int[] ranArr_Row = new int[landmine]; for(int i=0;i(); _uiLabel.enabled = true; _uiLabel.text = _str; } } } //初始化地雷 void initlandmine(int[] _Column,int[] _Row) { //重头戏,对BoxMatrix[i, j]进行初始化操作 //默认的全是0,一个个雷的操作 for (int i = 0; i < landmine; i++) { int _tmpColume = _Column[i]; int _tmpRow = _Row[i]; BoxMatrix[_tmpColume , _tmpRow ] = -1; //遍历上下左右和斜角方向的点,+1操作 //左 if (_tmpRow > 0) { //左中 if (BoxMatrix[_tmpColume, _tmpRow-1] != -1) { BoxMatrix[_tmpColume, _tmpRow - 1]++; } //左上 if (_tmpColume < BoxHeight-1) { if (BoxMatrix[_tmpColume+1 , _tmpRow - 1] != -1) { BoxMatrix[_tmpColume+1, _tmpRow - 1]++; } } //左下 if (_tmpColume > 0) { if (BoxMatrix[_tmpColume - 1, _tmpRow - 1] != -1) { BoxMatrix[_tmpColume - 1, _tmpRow - 1]++; } } } //右 if (_tmpRow < BoxWidth-1) { //右中 if (BoxMatrix[_tmpColume, _tmpRow + 1] != -1) { BoxMatrix[_tmpColume, _tmpRow + 1]++; } //右上 if (_tmpColume < BoxHeight - 1) { if (BoxMatrix[_tmpColume + 1, _tmpRow + 1] != -1) { BoxMatrix[_tmpColume + 1, _tmpRow + 1]++; } } //右下 if (_tmpColume > 0) { if (BoxMatrix[_tmpColume - 1, _tmpRow + 1] != -1) { BoxMatrix[_tmpColume - 1, _tmpRow + 1]++; } } } //上 if (_tmpColume < BoxHeight - 1) { if (BoxMatrix[_tmpColume +1, _tmpRow] != -1) { BoxMatrix[_tmpColume +1, _tmpRow]++; } } //下 if (_tmpColume > 0) { if (BoxMatrix[_tmpColume - 1, _tmpRow] != -1) { BoxMatrix[_tmpColume - 1, _tmpRow]++; } } } } //不重复的选取 int[] ChooseSet(int ArrTotle, int _maxNum) { //生成所有数组 int[] sortArr = new int[ArrTotle]; for (int i = 0; i < ArrTotle; i++) { sortArr[i] = i; } //用于保存返回结果 int[] result = new int[_maxNum]; //打乱这个sort数组,然后取出前n个就是随机选取的了 for (int i = 0; i < sortArr.Length; i++) { int temp = sortArr[i]; int randomIndex = UnityEngine.Random.Range(0, sortArr.Length); sortArr[i] = sortArr[randomIndex]; sortArr[randomIndex] = temp; } for (int i = 0; i < _maxNum; i++) { result[i] = sortArr[i]; Debug.Log(result[i]); } return result; } //扫雷动作 void ButtonPress(GameObject o) { if (gameState == GameState.GameOver) return; string[] _strArr = (o.name).Split(new char[] { '_' }); int _row = Convert.ToInt32(_strArr[2]); int _column = Convert.ToInt32(_strArr[1]); Debug.Log(_column + "," + _row); bool _ispress = mQizi[_column, _row].transform.FindChild("Label").GetComponent().enabled; if (!_ispress) { if (!mQizi[_column, _row].transform.FindChild("chacha").GetComponent().enabled) { if (gameState == GameState.MyStep) { UILabel _uiLabel = mQizi[_column, _row].transform.FindChild("Label").GetComponent(); _uiLabel.enabled = true; int _BoxMatrixNum = BoxMatrix[_column, _row]; string _str = _BoxMatrixNum + ""; _uiLabel.text = _str; //一不小心触雷就死了 //如果是0 扩展周围的雷 TotleBlock--; ShowText(TotleBlock, TotleLandmine); if (_BoxMatrixNum == -1) { gameState = GameState.GameOver; GameResult(0); } else if (_BoxMatrixNum == 0) { PaiKong(_column,_row); } } else if (gameState == GameState.ChaCha) { if (TotleLandmine > 0) { //画为叉叉 TotleBlock--; TotleLandmine--; ShowText(TotleBlock, TotleLandmine); mQizi[_column, _row].transform.FindChild("chacha").GetComponent().enabled = true; } } } } } //放旗子动作 void ButtonChaCha(GameObject o) { if (gameState == GameState.MyStep) { gameState = GameState.ChaCha; _Chacha.transform.FindChild("SlicedSprite").GetComponent().color = Color.green; } else if (gameState == GameState.ChaCha) { gameState = GameState.MyStep; _Chacha.transform.FindChild("SlicedSprite").GetComponent().color = Color.white; } } //主函数判断是不是胜利 void Update() { if (gameState != GameState.GameOver) { if (TotleBlock == 0) { GameResult(1); } } } //胜利失败显示 void GameResult(int result) { if (result == 0) { Debug.Log("gameover"); ShowLable.GetComponent().enabled = true; ShowLable.GetComponent().text = "gameover"; } else { Debug.Log("you are winner"); ShowLable.GetComponent().enabled = true; ShowLable.GetComponent().text = "you are winner"; } } void ShowText(int num1,int num2) { scoreStr = "left : " + num1 + " landmine : " + num2; ScoreLabel.GetComponent().text = scoreStr; } void ShowDiLei(int num1, int num2) { UILabel _uiLabel = mQizi[num1, num2].transform.FindChild("Label").GetComponent(); if(!_uiLabel.enabled) { _uiLabel.enabled = true; string _str = BoxMatrix[num1, num2] + ""; _uiLabel.text = _str; TotleBlock--; ShowText(TotleBlock, TotleLandmine); } } void PaiKongDiGui(int _y, int _x) { UILabel _uiLabel = mQizi[_y, _x].transform.FindChild("Label").GetComponent(); if (!_uiLabel.enabled) { _uiLabel.enabled = true; string _str = BoxMatrix[_y, _x] + ""; _uiLabel.text = _str; TotleBlock--; ShowText(TotleBlock, TotleLandmine); if (BoxMatrix[_y, _x] == 0) { //递归 PaiKong(_y, _x); } } } void PaiKong(int _y, int _x) { //左 if (_x > 0) { //左中 if (BoxMatrix[_y, _x - 1] != -1) { PaiKongDiGui(_y, _x - 1); } //左上 if (_y < BoxHeight - 1) { if (BoxMatrix[_y + 1, _x - 1] != -1) { PaiKongDiGui(_y + 1, _x - 1); } } //左下 if (_y > 0) { if (BoxMatrix[_y - 1, _x - 1] != -1) { PaiKongDiGui(_y - 1, _x - 1); } } } //右 if (_x < BoxWidth - 1) { //右中 if (BoxMatrix[_y, _x + 1] != -1) { PaiKongDiGui(_y, _x + 1); } //右上 if (_y < BoxHeight - 1) { if (BoxMatrix[_y + 1, _x + 1] != -1) { PaiKongDiGui(_y + 1, _x + 1); } } //右下 if (_y > 0) { if (BoxMatrix[_y - 1, _x + 1] != -1) { PaiKongDiGui(_y - 1, _x + 1); } } } //上 if (_y < BoxHeight - 1) { if (BoxMatrix[_y + 1, _x] != -1) { PaiKongDiGui(_y + 1, _x); } } //下 if (_y > 0) { if (BoxMatrix[_y - 1, _x] != -1) { PaiKongDiGui(_y - 1, _x); } } } }