//左移填满空行 public int[] DoMoveToLeft() { int[] ret = new int[11]; int c; int i; StarsBoard fullBoard = new StarsBoard(); //二值化完整棋盘,用于判定是否为空行 StarsBoard[] tmpBoard = new StarsBoard[5]; //新的棋盘数组 for (c = 0; c <= 4; c++) { fullBoard = fullBoard.Or(_starsBoard[c]); tmpBoard[c] = new StarsBoard(); } //遇到非空行就复制到新的之中 int curidx = 0; for (i = 0; i < 10; i++) { if (!fullBoard.IsEmptyColumn(i)) { for (c = 0; c <= 4; c++) { tmpBoard[c].SetColumn(curidx, System.Convert.ToInt32(_starsBoard[c].GetColumn(i))); } ret[curidx] = i; //记录当前位置存储的是原来的第几列。 curidx++; } } //记录有效个数 ret[10] = curidx; //替换现有引用 _starsBoard = tmpBoard; return(ret); }
//进行或运算 public StarsBoard Or(StarsBoard value) { for (int i = 0; i <= 9; i++) { this.m_Array[i] = System.Convert.ToInt32(this.m_Array[i] | value.m_Array[i]); } return(this); }
/**打乱*/ public void Upset() { //获取星星颜色,位置id,准备打乱 List <int> cors = new List <int>(); List <int> posIds = new List <int>(); Star star; int i, j; for (i = 0; i < 10; i++) { for (j = 0; j < _stars[i].Count; j++) { star = _stars[i][j].GetComponent <Star>(); cors.Add(star.color); posIds.Add(star.posId); star.Dispose(); } _stars[i].Clear(); for (j = 0; j < 10; j++) { _stars[i].Add(null); } } int[] corArr = cors.ToArray(); int[] posIdArr = posIds.ToArray(); RandomArr.Randomize(corArr, corArr.Length); //重建 StarsBoard[] starsBoards = new StarsBoard[] { new StarsBoard(), new StarsBoard(), new StarsBoard(), new StarsBoard(), new StarsBoard() }; for (i = 0; i < 5; i++) { starsBoards[i].SetAll(false); } int cor; int posId; for (i = 0; i < corArr.Length; i++) { cor = corArr[i]; posId = posIdArr[i]; starsBoards[cor].SetTrue(posId); PushStar(cor, posId); } _starsPosition = new StarsPosition(starsBoards); //移除空的元素 i = _stars.Length; while (--i >= 0) { j = _stars[i].Count; while (--j >= 0) { if (!_stars[i][j]) { _stars[i].RemoveAt(j); } } } }
//按坐标获取可消除块数 public int GetBlackByPoint(int p, ref int[] retval) { int tmpcnt = 0; StarsBoard mTable = new StarsBoard(); StarsBoard fullBoard = new StarsBoard(); //二值化完整棋盘,用于判定是否为空行 for (int i = 0; i < 5; i++) { fullBoard = fullBoard.Or(_starsBoard[i]); } for (int i = 0; i < 5; i++) { if (_starsBoard[i].GetValue(p)) { _starsBoard[i].FillPath(mTable, fullBoard, p, ref retval, ref tmpcnt); retval[100] = (int)(i); //颜色 retval[101] = (int)(tmpcnt); //长度 return(tmpcnt); } } return(0); }
//获取合理招法 public int NextGenerateMove(ref int[][] retval) { int cnt = 0; int tmpcnt = 0; StarsBoard mTable = new StarsBoard(); StarsBoard fullBoard = new StarsBoard(); //二值化完整棋盘,用于判定是否为空行 for (int c = 0; c <= 4; c++) { fullBoard = fullBoard.Or(_starsBoard[c]); } int p; for (int c = 0; c <= 4; c++) //遍历颜色 { mTable = new StarsBoard(); for (int y = 0; y <= 9; y++) //遍历坐标 { for (int x = 0; x <= 9; x++) { p = (int)((x << 4) | y); int[] tmpps = new int[102]; if (mTable.GetValue(p) == false) { _starsBoard[c].FillPath(mTable, fullBoard, p, ref tmpps, ref tmpcnt); if (tmpcnt > 1) { tmpps[100] = (int)(c); //颜色 tmpps[101] = (int)(tmpcnt); //长度 retval[cnt] = tmpps; //记录结果 cnt++; } } } } } return(cnt); }
public void NewStarsPosition(int[] cnts) { //清理 _selectTotal = 0; int i; int j; for (i = 0; i < 10; i++) { for (j = 0; j < _stars[i].Count; j++) { _stars[i][j].GetComponent <Star>().Dispose(); } _stars[i].Clear(); for (j = 0; j < 10; j++) { _stars[i].Add(null); } } //新建 int id; StarsBoard[] starsBoards = new StarsBoard[] { new StarsBoard(), new StarsBoard(), new StarsBoard(), new StarsBoard(), new StarsBoard() }; for (i = 0; i < 5; i++) { starsBoards[i].SetAll(false); for (j = 0; j < cnts[i]; j++) { id = RandomArr.GetNextRandomElement(); starsBoards[i].SetTrue(id); PushStar(i, id, true); } //Debug.Log( starsBoards[i].toString()); } _starsPosition = new StarsPosition(starsBoards); }
//这个函数与上面的函数基本相同,但只统计连续个数不返回坐标。 public int SumStar(StarsBoard mTable, StarsBoard FullBoard, int p) { Stack <int> mArray = new Stack <int>(); //栈——将处理点表 int mP = p; //正在处理点 int mAP; //临时变量——可能被入栈的点 int cnt = System.Convert.ToInt32(0); int x; int i; mArray.Push(mP); //入栈 do { if (mTable.GetValue(mP) == false) //若未处理过 { x = mP >> 4; if ((this.m_Array[x] & (1 << (mP & 0xF))) != 0) //若有子 { //临近点入栈 if ((mP & 0xF) > 0) //上 { mAP = (int)(mP - 1); if (!mTable.GetValue(mAP)) { mArray.Push(mAP); } } if ((mP & 0xF) < 10) //下 { mAP = (int)(mP + 1); if (!mTable.GetValue(mAP)) { mArray.Push(mAP); } } if (x > 0) //左 { mAP = (int)(mP - 0x10); if (!mTable.GetValue(mAP)) { for (i = x - 1; i >= 0; i--) //向右查找第一个非空行取坐标。 { if (FullBoard.m_Array[i] != 0) { mAP = (int)((i << 4) | (mP & 0xF)); break; } } mArray.Push(mAP); } } if (x < 9) //右 { mAP = (int)(mP + 0x10); if (!mTable.GetValue(mAP)) { for (i = x + 1; i <= 9; i++) { if (FullBoard.m_Array[i] != 0) { mAP = (int)((i << 4) | (mP & 0xF)); break; } } mArray.Push(mAP); } } cnt++; //记录个数 } mTable.SetTrue(mP); //修改为已处理 } if (mArray.Count == 0) //还有点就出栈,没有就结束 { break; } else { mP = mArray.Pop(); } } while (true); return(cnt); }
public StarsBoard(StarsBoard pos) { Array.Copy(pos.m_Array, m_Array, 10); }
//获取连通性。局面,已处理表,点坐标,连通点集,连通点集包含的点数。 public void FillPath(StarsBoard mTable, StarsBoard FullBoard, int p, ref int[] retval, ref int retcnt) { Stack <int> mArray = new Stack <int>(); //栈——将处理点表 int mP = p; //正在处理点 int mAP; //临时变量——可能被入栈的点 int cnt = System.Convert.ToInt32(0); int x; int i; mArray.Push(mP); //入栈 do { if (mTable.GetValue(mP) == false) //若未处理过 { x = mP >> 4; if ((this.m_Array[x] & (1 << (mP & 0xF))) != 0) //若有子 { //临近点入栈 if ((mP & 0xF) > 0) //上 { mAP = (int)(mP - 1); if (!mTable.GetValue(mAP)) { mArray.Push(mAP); } } if ((mP & 0xF) < 10) //下 { mAP = (int)(mP + 1); if (!mTable.GetValue(mAP)) { mArray.Push(mAP); } } if (x > 0) //左 { mAP = (int)(mP - 0x10); if (!mTable.GetValue(mAP)) { for (i = x - 1; i >= 0; i--) { if (FullBoard.m_Array[i] != 0) { mAP = (int)((i << 4) | (mP & 0xF)); //向左查找第一个非空行取坐标。 break; } } mArray.Push(mAP); } } if (x < 9) //右 { mAP = (int)(mP + 0x10); if (!mTable.GetValue(mAP)) { for (i = x + 1; i <= 9; i++) //向右查找第一个非空行取坐标。 { if (FullBoard.m_Array[i] != 0) { mAP = (int)((i << 4) | (mP & 0xF)); break; } } mArray.Push(mAP); } } //记录本点 retval[cnt] = mP; cnt++; } mTable.SetTrue(mP); //修改为已处理 } if (mArray.Count == 0) //还有点就出栈,没有就结束 { break; } else { mP = mArray.Pop(); } } while (true); //按Y进行排序,以便恢复局面时使用。 Array.Sort(retval, 0, cnt, pc); //返回有效个数 retcnt = cnt; }