void ChoseWayCell(ref List <mazeGTCell> nb) { if (choseWayType == CHOSEWAYTYPE.Rand) { int randp = UnityEngine.Random.Range(0, nb.Count); mazeGTCell rc = nb[randp]; pathRec.Push(rc); } else { //總機率. int total = 0; foreach (mazeGTCell mc in nb) { total += mc.tempRandValue; } int randp = UnityEngine.Random.Range(0, total); //算出落在那. int range = 0; foreach (mazeGTCell mc in nb) { range += mc.tempRandValue; if (randp < range) { pathRec.Push(mc); break; } } } }
//週沒有路徑.建立一個 LOOP 的迷宮. bool IsNoPathNear(mazeGTCell cell, mazeGTCell parentcell) { //neberhood. List <mazeGTCell> nb = new List <mazeGTCell> (); //L. if (cell.x > xkeep) { mazeGTCell nc = cells[cell.x - 1, cell.y]; if (nc.celltype == mazeGTCell.CELLTYPE.path && parentcell != nc) { return(false); } } //R. if (cell.x < x - 1 - xkeep) { mazeGTCell nc = cells[cell.x + 1, cell.y]; if (nc.celltype == mazeGTCell.CELLTYPE.path && parentcell != nc) { return(false); } } //B. if (cell.y > ykeep) { mazeGTCell nc = cells[cell.x, cell.y - 1]; if (nc.celltype == mazeGTCell.CELLTYPE.path && parentcell != nc) { return(false); } } //U. if (cell.y < y - 1 - ykeep) { mazeGTCell nc = cells[cell.x, cell.y + 1]; if (nc.celltype == mazeGTCell.CELLTYPE.path && parentcell != nc) { return(false); } } return(true); }
public void GenMaze(int sizex, int sizey) { x = sizex; y = sizey; if (cells != null) { System.Array.Clear(cells, 0, cells.Length); } statckMax = -1; farCell = null; cells = new mazeGTCell[x, y]; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) { mazeGTCell newmc = new mazeGTCell(); newmc.x = j; newmc.y = i; newmc.celltype = mazeGTCell.CELLTYPE.wall; cells[j, i] = newmc; } } xkeep = 0; ykeep = 0; if (wallAround) { xkeep = 1; ykeep = 1; } int startx = UnityEngine.Random.Range(0 + xkeep, x - xkeep); int starty = UnityEngine.Random.Range(0 + ykeep, y - ykeep); //step1. mazeGTCell startcell = cells[startx, starty]; startcell.isStart = true; pathRec.Push(startcell); //step2. while (pathRec.Count > 0) { mazeGTCell next = (mazeGTCell)pathRec.Peek(); next.celltype = mazeGTCell.CELLTYPE.path; //記下最遠. if (pathRec.Count > statckMax) { statckMax = pathRec.Count; farCell = next; } //step3. if (!FindTheWay(next)) { //step4. pathRec.Pop(); } } if (farCell != null) { farCell.isEnd = true; } }
bool FindTheWay(mazeGTCell cell) { //neberhood. List <mazeGTCell> nb = new List <mazeGTCell> (); //L. if (cell.x > xkeep) { mazeGTCell nc = cells[cell.x - 1, cell.y]; if (nc.celltype == mazeGTCell.CELLTYPE.wall && IsNoPathNear(nc, cell)) { nc.tempNeberType = mazeGTCell.NEBERTYPE.L; nc.tempRandValue = choseRandL; nb.Add(nc); } } //R. if (cell.x < x - 1 - xkeep) { mazeGTCell nc = cells[cell.x + 1, cell.y]; if (nc.celltype == mazeGTCell.CELLTYPE.wall && IsNoPathNear(nc, cell)) { nc.tempNeberType = mazeGTCell.NEBERTYPE.R; nc.tempRandValue = choseRandR; nb.Add(nc); } } //B. if (cell.y > ykeep) { mazeGTCell nc = cells[cell.x, cell.y - 1]; if (nc.celltype == mazeGTCell.CELLTYPE.wall && IsNoPathNear(nc, cell)) { nc.tempNeberType = mazeGTCell.NEBERTYPE.B; nc.tempRandValue = choseRandB; nb.Add(nc); } } //U. if (cell.y < y - 1 - ykeep) { mazeGTCell nc = cells[cell.x, cell.y + 1]; if (nc.celltype == mazeGTCell.CELLTYPE.wall && IsNoPathNear(nc, cell)) { nc.tempNeberType = mazeGTCell.NEBERTYPE.U; nc.tempRandValue = choseRandU; nb.Add(nc); } } if (nb.Count > 0) { ChoseWayCell(ref nb); } else { return(false); } return(true); }