public LabCell delete(int key) { neigbours.Remove(key); int neighbourSize = neigbours.Count; if (neighbourSize == 1) { int lastKey = new List <int> (neigbours.Keys)[0]; LabCell lastneigh = neigbours[lastKey]; neigbours.Clear(); return(lastneigh.delete((lastKey + 2) % 4)); } else if (neighbourSize == 0) { print("end " + x + " " + y + " " + key); return(deadCell); } else { return(this); } }
void Wilson(bool[,] map, int size) { int s = size / 2 + 1; LabCell[,] miniLab = new LabCell[s + 2, s + 2]; for (int i = 1; i <= s; i++) { for (int j = 1; j <= s; j++) { miniLab[i, j] = new LabCell(); } } Vector2[] directions = { new Vector2(0, 0), new Vector2(-1, 0), new Vector2(0, 1), new Vector2(1, 0), new Vector2(0, -1) }; #region TheAlgorithm int Aux, x, y; short dir; #region ChoosingTheFirstCell Aux = ChooseCell(s); x = (int)Mathf.Ceil((float)Aux / s); y = Aux % s; if (y == 0) { y = s; } miniLab[x, y].MakeOpen(); int visited = s * s - 1; #endregion while (visited > 0) { short[,] road = new short[s + 1, s + 1]; #region ChoosingARandomEmptyCell List <Vector2> CloseCells = new List <Vector2>(); for (int i = 1; i <= s; i++) { for (int j = 1; j <= s; j++) { if (!miniLab[i, j].isOpen()) { CloseCells.Add(new Vector2(i, j)); } } } //print(CloseCells.Count); int random = UnityEngine.Random.Range(0, CloseCells.Count - 1); //Mathf.Clamp(random, 0, CloseCells.Count); //print("Random number: " + random); Vector2 V = CloseCells[random]; x = (int)V.x; y = (int)V.y; int initX = x; int initY = y; #endregion #region MakeARoad int debu = 0; while (!miniLab[x, y].isOpen()) { float rand; do { rand = UnityEngine.Random.Range(1, 5); dir = (short)rand; //print(dir); } while (!Inside(x + (int)directions[dir].x, y + (int)directions[dir].y, s)); x += (int)directions[dir].x; y += (int)directions[dir].y; if (road[x, y] == 0) { road[x, y] = dir; } if (miniLab[x, y].isOpen()) { // print("am ajuns:" + visited); } debu++; if (debu >= 1000) { print("FF " + visited); } } #endregion #region BackTrack debu = 0; while (x != initX || y != initY) { int d = road[x, y]; if (!miniLab[x, y].isOpen()) { visited--; } miniLab[x, y].DestroyWall(Opposite(d)); x += (int)directions[Opposite(d)].x; y += (int)directions[Opposite(d)].y; if (!miniLab[x, y].isOpen()) { visited--; } miniLab[x, y].DestroyWall(d); } #endregion } #endregion #region MakingTheMap for (int i = 1; i <= s; i++) { for (int j = 1; j <= s; j++) { int auxX, auxY; auxX = i * 2 - 1; auxY = j * 2 - 1; map[auxX, auxY] = true; for (int d = 1; d <= 4; d++) { int newX, newY; newX = auxX + (int)directions[d].x; newY = auxY + (int)directions[d].y; if (Inside(newX, newY, size)) { if (!miniLab[i, j].walls[d]) { map[newX, newY] = true; } } } } } #endregion }
public LabEvent[] RetrieveEvents(LabCell cell) { return(_labFile.events.events.Where(x => x.cellId == cell.id).ToArray()); }
public Tuple(int i, LabCell lc) { _1 = i; this._2 = lc; }
/*int[] createStartFinish () { * int xSize = lab.GetLength (0); * int ySize = lab.GetLength (0); * * * return {0,0}; * }*/ int[,,] createLabRandom(int sizeX, int sizeY) { int[,,] lab = new int[sizeX, sizeY, 4]; LabCell[,] labCells = new LabCell[sizeX, sizeY]; for (int i = 0; i < sizeX; i++) { for (int j = 0; j < sizeY; j++) { labCells [i, j] = new LabCell(i, j); } } WallValue[,] wallsId = new WallValue[sizeX + 1, sizeY + 1]; for (int i = 0; i < sizeX + 1; i++) { for (int j = 0; j < sizeY + 1; j++) { wallsId [i, j] = null; } } WallValue firstValue = new WallValue(); for (int i = 0; i < sizeX; i++) { for (int j = 0; j < sizeY; j++) { LabCell curCell = labCells [i, j]; if (i != 0) { curCell.neigbours.Add(3, labCells [i - 1, j]); } else { wallsId [i, j] = firstValue; lab [i, j, 3] = 1; } if (i != sizeX - 1) { curCell.neigbours.Add(1, labCells [i + 1, j]); } else { wallsId [i + 1, j] = firstValue; lab [i, j, 1] = 1; } if (j != 0) { curCell.neigbours.Add(2, labCells [i, j - 1]); } else { wallsId [i, j] = firstValue; lab [i, j, 2] = 1; } if (j != sizeY - 1) { curCell.neigbours.Add(0, labCells [i, j + 1]); } else { wallsId [i, j + 1] = firstValue; lab [i, j, 0] = 1; } } } int count = 0; Queue <LabCell> listRandomWalker = new Queue <LabCell>(); listRandomWalker.Enqueue(labCells [Random.Range(0, sizeX), Random.Range(0, sizeY)]); listRandomWalker.Enqueue(labCells [Random.Range(0, sizeX), Random.Range(0, sizeY)]); listRandomWalker.Enqueue(labCells [Random.Range(0, sizeX), Random.Range(0, sizeY)]); while (listRandomWalker.Count != 0) { LabCell randomWalker = listRandomWalker.Dequeue(); //createLabPhysique (lab,count); count++; if (!randomWalker.equals(deadCell) && randomWalker.neigbours.Count != 0) { List <int> neigboursKeys; while (((int)Random.Range(0, 2)) != 1) { neigboursKeys = new List <int> (randomWalker.neigbours.Keys); int rand = (int)Random.Range(0, neigboursKeys.Count - 1); int moveKey = neigboursKeys [rand]; randomWalker = randomWalker.neigbours [moveKey]; } neigboursKeys = new List <int> (randomWalker.neigbours.Keys); int toDeletePos = Random.Range(0, neigboursKeys.Count); int toDeleteKey = neigboursKeys [toDeletePos]; LabCell neighbour = randomWalker.neigbours [toDeleteKey]; int oppositeKey = (toDeleteKey + 2) % 4; listRandomWalker.Enqueue(neighbour.delete(oppositeKey)); listRandomWalker.Enqueue(randomWalker.delete(toDeleteKey)); int rwx = randomWalker.x; int rwy = randomWalker.y; int idy = 0, idx = 0; WallValue [] wallValue = new WallValue [2]; int [,] wallPos = { { rwx, rwy }, { rwx + 1, rwy + 1 } }; if (toDeleteKey == 1 || toDeleteKey == 3) { idx = rwx + (-(toDeleteKey - 2) + 1) / 2; wallValue [0] = wallsId [idx, rwy]; wallValue [1] = wallsId [idx, rwy + 1]; wallPos [0, 0] = idx; wallPos [1, 0] = idx; } else { idy = rwy + (-(toDeleteKey - 1) + 1) / 2; wallValue [0] = wallsId [rwx, idy]; wallValue [1] = wallsId [rwx + 1, idy]; wallPos [0, 1] = idy; wallPos [1, 1] = idy; } string s = ""; if (wallValue [0] == null && wallValue [1] == null) { lab [rwx, rwy, toDeleteKey] = 1; lab [neighbour.x, neighbour.y, oppositeKey] = 1; WallValue newValue = new WallValue(); wallsId [wallPos [0, 0], wallPos [0, 1]] = newValue; wallsId [wallPos [1, 0], wallPos [1, 1]] = newValue; } else if (wallValue [0] == null) { lab [rwx, rwy, toDeleteKey] = 1; lab [neighbour.x, neighbour.y, oppositeKey] = 1; wallsId [wallPos [0, 0], wallPos [0, 1]] = wallsId [wallPos [1, 0], wallPos [1, 1]]; } else if (wallValue [1] == null) { lab [rwx, rwy, toDeleteKey] = 1; lab [neighbour.x, neighbour.y, oppositeKey] = 1; wallsId [wallPos [1, 0], wallPos [1, 1]] = wallsId [wallPos [0, 0], wallPos [0, 1]]; } else if (!wallValue [0].Equals(wallValue [1])) { lab [rwx, rwy, toDeleteKey] = 1; lab [neighbour.x, neighbour.y, oppositeKey] = 1; wallsId [wallPos [1, 0], wallPos [1, 1]].setValue( wallsId [wallPos [0, 0], wallPos [0, 1]]); } else { } } } return(lab); }
public bool equals(LabCell other) { return(x == other.x && y == other.y); }