java扫雷项⽬总结建议,扫雷⼩游戏难点总结
扫雷⼩游戏难点总结:
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 OriginPoi
nt;//左下⾓原点 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; _ansform.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 = _ansform.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)
; } } _ansform.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(); _abled = true; _ = _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[_t
mpColume - 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[] res
ult = 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; 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(); _abled = true; int _BoxMatrixNum = BoxMatrix[_column, _row]; string _str =
_BoxMatrixNum + ""; _ = _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; _ansform.FindChild("SlicedSprite").GetComponent().color = ; } else if (gameState == GameState.ChaCha) { gameState = GameState.MyStep;
_ansform.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(!_abled) {
_abled = true; string _str = BoxMatrix[num1, num2] + ""; _ = _str; TotleBlock--; ShowText(TotleBlock, TotleLandmine); } } void PaiKongDiGui(int _y, int _x) { UILabel _uiLabel = mQizi[_y, _x].transform.FindChild("Label").GetComponent(); if (!_abled) { _abled = true; string _str = BoxMatrix[_y, _x] + ""; _ = _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); } } //右下
入门的java游戏小程序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); } } } }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论