protected virtual void MaybeExtendPath(LittleBox newLittleBox) { List <LittleBox> newLittleBoxList; LittleBox currentPathEnd = pathDick [activePathColor][pathDick [activePathColor].Count - 1]; if (ExperimentPanel.activeTouchyBall.outOfBounds) { newLittleBoxList = OutOfBoundsPath(newLittleBox, currentPathEnd); } else { newLittleBoxList = InBoundsPath(newLittleBox, currentPathEnd); } if (IsValidPath(newLittleBoxList)) { for (int i = 1; i < newLittleBoxList.Count; i++) { pathDick[activePathColor].ExtendPath(newLittleBoxList[i]); } } // else if (newLittleBox.isEndPoint && newLittleBox.pathColor == activePathColor && newLittleBox != pathDick[activePathColor].activatedEndPoint) // { // pathDick[activePathColor].SelectEndPoint(newLittleBox); // } }
public override void DoAction(LittleBox newLittleBox) { if (settingPaths) { if (!pathDick.ContainsKey(newPathColor)) { pathEndDick.Add(newPathColor, new PathList()); FlowPuzzle.shortenPathsDick.Add(newPathColor, new List <FlowBox>()); pathDick.Add(newPathColor, new PathList()); pathDick[newPathColor].pathColor = newPathColor; } if (pathEndDick[newPathColor].Count < 2) { newLittleBox.isEndPoint = true; newLittleBox.Occupy(newPathColor, ExperimentPanel.puzzleSprites[0], 0); newLittleBox.images[0].color = Color.clear; pathEndDick[newPathColor].Add(newLittleBox); } else { print("Pick a new path color"); } } else { base.DoAction(newLittleBox); } }
private List <LittleBox> OutOfBoundsPath(LittleBox destinationLB, LittleBox originLB) { List <LittleBox> newLittleBoxList = new List <LittleBox> (); List <LittleBox> wayPointLBsList = new List <LittleBox> { originLB }; Dictionary <int, string[]> quadsDick = PathTouchyBall.GetQuadrantsDick(); for (int i = 1; i <= quadsDick.Count; i++) { string cornerName = quadsDick[i][0] + quadsDick[i][1]; if (cornersDick[cornerName] != originLB && cornersDick[cornerName] != destinationLB) { wayPointLBsList.Add(cornersDick[cornerName]); } } wayPointLBsList.Add(destinationLB); for (int i = 0; i < wayPointLBsList.Count - 1; i++) { int axis = 1; if (wayPointLBsList[i + 1].position[1] == wayPointLBsList[i].position[1]) { axis = 0; } int change = (wayPointLBsList[i + 1].position[axis] - wayPointLBsList[i].position[axis]) / Mathf.Abs(wayPointLBsList[i + 1].position[axis] - wayPointLBsList[i].position[axis]); for (int j = 0; Mathf.Abs(j) < Mathf.Abs(wayPointLBsList[i + 1].position[axis] - wayPointLBsList[i].position[axis]); j += change) { int x = wayPointLBsList[i].position[0] + (1 - axis) * j; int y = wayPointLBsList[i].position[1] + axis * j; newLittleBoxList.Add(littleBoxMatrix[x][y]); } } newLittleBoxList.Add(destinationLB); return(newLittleBoxList); }
protected override void NewActivePath(LittleBox newLittleBox) { base.NewActivePath(newLittleBox); foreach (FlowBox flowBox in pathDick[activePathColor]) { flowBox.SetBackGround(false); } }
public override void DoAction(LittleBox littleBox) { base.DoAction(littleBox); Press(littleBox); if (bugsCount == 0) { expPanel.EndPuzzle(true); } }
public override void Disable() { expPanel.mainTexts [0].text = ""; forkDeadEndDick.Clear(); finishEndPoint = null; connectedCells.Clear(); cellList.Clear(); base.Disable(); }
private void CreateBug(LittleBox littleBox) { bugsCount++; resolvedBugsList.Remove(littleBox); littleBox.occupied = true; // littleBox.images[0].color = Color.yellow; littleBox.images[1].enabled = true; littleBox.images [1].sprite = ExperimentPanel.puzzleSprites[5]; }
public virtual void StopTouching() { activePath = false; activatedEndPoint = null; if (completedPath) { CheckIfPuzzleIsComplete(); completedPath = false; } }
private bool OtherNeighborInSamePath(LittleBox subtractedLB, LittleBox randomNeighborLB) { foreach (LittleBox otherNeighbor in subtractedLB.neighbors) { if (otherNeighbor != randomNeighborLB && otherNeighbor.pathColor == randomNeighborLB.pathColor) { return(true); } } return(false); }
public override void TouchBox(LittleBox newLittleBox) { if (newLittleBox.pathColor == PathPuzzle.activePathColor) { ExperimentPanel.tbImage.image.color = new Color(PathPuzzle.activePathColor.r, PathPuzzle.activePathColor.g, PathPuzzle.activePathColor.b, 0.3f); } else if (newLittleBox.isEndPoint) { ExperimentPanel.tbImage.image.color = inactiveColor; } }
protected override void CompletePath(LittleBox otherEndPoint) { isComplete = true; int xNewDiff = otherEndPoint.position[0] - this[Count - 1].position[0]; int yNewDiff = otherEndPoint.position[1] - this[Count - 1].position[1]; ChangeLastPathEndSprite(xNewDiff, yNewDiff); otherEndPoint.pathPosition = Count; Add(otherEndPoint); ExperimentPanel.activePuzzle.CheckIfPuzzleIsComplete(); }
public override void DoAction(LittleBox littleBox) { if (activePath) { MaybeAdjustPath(littleBox); } else if (littleBox.occupied) { NewActivePath(littleBox); } }
protected virtual void NewActivePath(LittleBox newLittleBox) { activePath = true; activePathColor = newLittleBox.pathColor; if (newLittleBox.isEndPoint) { activatedEndPoint = newLittleBox; pathDick[activePathColor].SelectEndPoint(newLittleBox); } else { pathDick[activePathColor].MaybeShortenPath(newLittleBox); } }
protected void MaybeAdjustPath(LittleBox newLittleBox) { if (newLittleBox == activatedEndPoint) { pathDick[activePathColor].SelectEndPoint(newLittleBox); } else if (newLittleBox.pathColor == activePathColor && !newLittleBox.isEndPoint) { pathDick[activePathColor].MaybeShortenPath(newLittleBox); } else if (!pathDick[activePathColor].isComplete) { MaybeExtendPath(newLittleBox); } }
public override void SelectEndPoint(LittleBox newActivatedEndPoint) { isComplete = false; activatedEndPoint = newActivatedEndPoint; activatedEndPoint.pathPosition = 0; foreach (LittleBox littleBox in this) { if (littleBox != activatedEndPoint && littleBox.pathColor == pathColor) { littleBox.Unoccupy(); } } Clear(); Add(activatedEndPoint); }
private void AddLBtoCellList(LittleBox neighborCell, LittleBox currentCell) { neighborCell.images [0].color = Color.green; cellList.Add(neighborCell); pathDick[pathColor].ConnectNeighbors(true, currentCell, neighborCell); connectedCells [currentCell].Add(neighborCell); if (!connectedCells.ContainsKey(neighborCell)) { connectedCells.Add(neighborCell, new List <LittleBox>()); } connectedCells [neighborCell].Add(currentCell); neighborCell.pathPosition = currentCell.pathPosition + 1; neighborCell.occupied = true; // neighborCell.debugText.text = neighborCell.pathPosition.ToString (); }
private void ResolveBug(LittleBox littleBox) { bugsCount--; littleBox.occupied = false; if (!isGenerating) { littleBox.images[1].sprite = ExperimentPanel.puzzleSprites[6]; littleBox.timer = 0f; resolvedBugsList.Add(littleBox); } else { littleBox.images[1].enabled = false; // littleBox.images[0].color = Color.gray; } }
private LittleBox ConnectFurthestDeadEndToFork(LittleBox cell, int startingPosition, int direction) { cell.images[0].color = Color.blue; cell.isTraversed = true; foreach (LittleBox neighbor in connectedCells[cell]) { if (connectedCells[neighbor].Count > 2 && Mathf.Abs(startingPosition - neighbor.pathPosition) > Mathf.Abs(forkDeadEndDick[neighbor].pathPosition - neighbor.pathPosition)) { return(neighbor); } else if (connectedCells[neighbor].Count <= 2 && neighbor.pathPosition - cell.pathPosition == direction) { return(ConnectFurthestDeadEndToFork(neighbor, startingPosition, direction)); } } return(null); }
private void Awake() { if (GetComponentInParent <PuzzleScene>()) { GameManager.inPuzzleScene = true; } puzzleSprites = pathSprites; mainTexts = GetComponentsInChildren <Text> (); foreach (ExperimentPuzzle expPuzzle in expPuzzles) { expPuzzle.Initiate(); } firstPuzzleWall.gameObject.SetActive(false); RectTransform thisRectTransform = GetComponent <RectTransform> (); littleBoxArray = new LittleBox[(int)(Mathf.Pow(maxMatrixSize, 2))]; littleBoxArray [0] = firstLittleBox; firstLittleBox.Initiate(); for (int i = 1; i < littleBoxArray.Length; i++) { LittleBox newLittleBox = (LittleBox)Instantiate(firstLittleBox); newLittleBox.Initiate(); newLittleBox.rectTransform.SetParent(thisRectTransform); newLittleBox.rectTransform.localScale = Vector3.one; littleBoxArray[i] = newLittleBox; newLittleBox.Reset(); } firstLittleBox.Reset(); extraPuzzleWallArray = new PuzzleWall[maxMatrixSize * 2]; extraPuzzleWallArray [0] = firstPuzzleWall; firstPuzzleWall.Initiate(); for (int i = 1; i < extraPuzzleWallArray.Length; i++) { PuzzleWall newPuzzleWall = (PuzzleWall)Instantiate(firstPuzzleWall); newPuzzleWall.Initiate(); newPuzzleWall.rectTransform.SetParent(thisRectTransform); newPuzzleWall.rectTransform.localScale = Vector3.one; extraPuzzleWallArray[i] = newPuzzleWall; newPuzzleWall.gameObject.SetActive(false); } firstPuzzleWall.gameObject.SetActive(false); tbImage = GetComponentInChildren <TouchyBallImage> (); tbImage.Initiate(); tbImage.image.rectTransform.sizeDelta = new Vector2(Screen.width * 0.3f, Screen.width * 0.3f); }
private LittleBox FindConnectedForks(LittleBox neighbor, LittleBox lastCell) { neighbor.isTraversed = true; if (connectedCells[neighbor].Count > 2) { return(neighbor); } else { neighbor.images[0].color = Color.red; foreach (LittleBox newNeighbor in connectedCells[neighbor]) { if (newNeighbor != lastCell) { return(FindConnectedForks(newNeighbor, neighbor)); } } } return(null); }
private void Press(LittleBox littleBox) { foreach (LittleBox neighbor in littleBox.neighbors) { if (!neighbor.occupied) { CreateBug(neighbor); } else { ResolveBug(neighbor); } } if (!littleBox.occupied) { CreateBug(littleBox); } else { ResolveBug(littleBox); } }
public override void ExtendPath(LittleBox newLittleBox) { if (!isComplete) { if (newLittleBox == MazePuzzle.finishEndPoint) { CompletePath(newLittleBox); } else { int xNewDiff = newLittleBox.position [0] - this[Count - 1].position [0]; int yNewDiff = newLittleBox.position [1] - this[Count - 1].position [1]; if (Count > 1) { ChangeLastPathEndSprite(xNewDiff, yNewDiff); } int newRotation = -90 * xNewDiff + 90 * (Mathf.Abs(yNewDiff) - 1 * yNewDiff); newLittleBox.Occupy(pathColor, ExperimentPanel.puzzleSprites[3], newRotation); newLittleBox.pathPosition = Count; Add(newLittleBox); } } }
private List <LittleBox> InBoundsPath(LittleBox destinationLB, LittleBox originLB) { List <LittleBox> newLittleBoxList = new List <LittleBox> (); float slope = (float)(destinationLB.position[1] - originLB.position[1]) / (float)(destinationLB.position[0] - originLB.position[0]); int xDirection = 0; if (destinationLB.position[0] != originLB.position[0]) { xDirection = (destinationLB.position[0] - originLB.position[0]) / Mathf.Abs(destinationLB.position[0] - originLB.position[0]); } int yDirection = 0; if (destinationLB.position[1] != originLB.position[1]) { yDirection = (destinationLB.position[1] - originLB.position[1]) / Mathf.Abs(destinationLB.position[1] - originLB.position[1]); } int Y = 0; for (int x = 0; Mathf.Abs(x) <= Mathf.Abs(destinationLB.position[0] - originLB.position[0]) && (newLittleBoxList.Count == 0 || newLittleBoxList[newLittleBoxList.Count - 1] != destinationLB); x += xDirection) { newLittleBoxList.Add(littleBoxMatrix[originLB.position[0] + x][originLB.position[1] + Y]); if (newLittleBoxList[newLittleBoxList.Count - 1] == destinationLB) { break; } if ((Mathf.Abs(x) + 1) * Mathf.Abs(slope) >= Mathf.Abs(Y)) { for (int y = Y; Mathf.Abs(y) + 1 < (Mathf.Abs(x) + 1) * Mathf.Abs(slope) && (newLittleBoxList.Count == 0 || newLittleBoxList[newLittleBoxList.Count - 1] != destinationLB); y += yDirection) { newLittleBoxList.Add(littleBoxMatrix[originLB.position[0] + x][originLB.position[1] + y + yDirection]); Y += yDirection; } } } return(newLittleBoxList); }
public void DoAction(LittleBox lb) { expPuzzles [activePuzzleIndex].DoAction(lb); }
public virtual void TouchBox(LittleBox newLittleBox) { }
private IEnumerator MoveTouchyBall() { Vector3 lastMousePosition = Vector3.zero; LittleBox lastLittleBox = null; Camera activeCamera; if (!GameManager.inPuzzleScene) { activeCamera = NonOverlayCamera.thisCamera; } else { activeCamera = Camera.main; } while (puzzleActive) { // Mouse if (Input.GetMouseButton(0) && (Input.mousePosition != lastMousePosition || !activeTouchyBall.gameObject.activeSelf)) { lastMousePosition = Input.mousePosition; if (!activeTouchyBall.gameObject.activeSelf) { activeTouchyBall.gameObject.SetActive(true); } RaycastHit hit; if (Physics.Raycast(activeCamera.ScreenPointToRay(Input.mousePosition), out hit, 100f /*, LayerMask.NameToLayer("UI")*/)) { LittleBox newLittleBox = hit.transform.gameObject.GetComponent <LittleBox>(); if (newLittleBox != null && (newLittleBox != lastLittleBox || Input.GetMouseButtonDown(0))) { DoAction(newLittleBox); activeTouchyBall.TouchBox(newLittleBox); lastLittleBox = newLittleBox; } if (activeTouchyBall.outOfBounds) { activeTouchyBall.BackInBounds(); } } else { activeTouchyBall.OutOfBounds(); } activeTouchyBall.Move(); } else if (!Input.GetMouseButton(0) && activeTouchyBall.gameObject.activeSelf) { activeTouchyBall.gameObject.SetActive(false); } // Finger Touch // if (Input.touchCount > 0 && Input.GetTouch(0).phase != TouchPhase.Stationary) // { // // snaps to middle of closest littlebox // if (Input.GetTouch(0).phase == TouchPhase.Moved) // { // itb.Move(Input.GetTouch(0).position); // } // else if (Input.GetTouch(0).phase == TouchPhase.Began) // { // itb.gameObject.SetActive(true); // itb.Move(Input.GetTouch(0).position); // } // else itb.gameObject.SetActive(false); // } yield return(null); } }
protected override IEnumerator PuzzleGenerator() { // Initialize if (pathCount < matrixSize) { // Starts with straight paths and then wraps the appropriate number of paths around shorten paths // to fill the entire matrix for (int i = 0; i < pathCount; i++) { for (int j = 0; j < matrixSize; j++) { littleBoxMatrix [i] [j].images [1].enabled = true; littleBoxMatrix [i] [j].images [1].color = pathColorList [i]; littleBoxMatrix [i] [j].pathColor = pathColorList [i]; pathDick [pathColorList [i]].Add(littleBoxMatrix [i] [j]); } } // Shorten Paths for (int i = 0; i < matrixSize - pathCount; i++) { for (int j = 0; j < matrixSize - pathCount - i; j++) { littleBoxMatrix[pathCount - 1 - i][matrixSize - 1 - j].images[1].enabled = false; pathDick[pathColorList[pathCount - 1 - i]].Remove(littleBoxMatrix[pathCount - 1 - i][matrixSize - 1 - j]); } } // Extend appropraite paths for (int i = 0; i < pathCount; i++) { LittleBox endLittleBox = pathDick[pathColorList[i]][pathDick[pathColorList[i]].Count - 1]; // if the box to the right of the endLittleBox is empty if (!littleBoxMatrix[endLittleBox.position[0] + 1][endLittleBox.position[1]].images[1].enabled) { int x = endLittleBox.position[0] + 1; while (x < matrixSize && !littleBoxMatrix[x][endLittleBox.position[1]].images[1].enabled) { littleBoxMatrix[x][endLittleBox.position[1]].images[1].enabled = true; littleBoxMatrix[x][endLittleBox.position[1]].pathColor = endLittleBox.pathColor; pathDick[endLittleBox.pathColor].Add(littleBoxMatrix[x][endLittleBox.position[1]]); x++; } int y = endLittleBox.position[1] - 1; while (y >= 0) { littleBoxMatrix[x - 1][y].images[1].enabled = true; littleBoxMatrix[x - 1][y].pathColor = endLittleBox.pathColor; pathDick[endLittleBox.pathColor].Add(littleBoxMatrix[x - 1][y]); y--; } } } } else { // Starts from a corner and then snakes across the matrix int pathLength = matrixSize * matrixSize / pathCount; int pathColorIndex = -1; int totalCount = -1; for (int i = 0; i < matrixSize; i++) { for (int j = 0; j < matrixSize; j++) { totalCount++; int y = j; if (i % 2 == 1) { y = matrixSize - 1 - j; } // if new path if (totalCount % pathLength == 0) { pathColorIndex++; if (pathColorIndex > pathCount - 1) { pathColorIndex = pathCount - 1; pathDick [pathColorList [pathColorIndex]] [pathDick [pathColorList [pathColorIndex]].Count - 1].images [1].enabled = false; } } littleBoxMatrix [i] [y].pathColor = pathColorList [pathColorIndex]; pathDick [pathColorList [pathColorIndex]].Add(littleBoxMatrix [i] [y]); } } } // Adjust Sprites for Generation foreach (Color pathColor in pathDick.Keys) { for (int i = 0; i < pathDick[pathColor].Count; i++) { pathDick [pathColor] [i].images [0].color = Color.clear; pathDick [pathColor] [i].images [1].enabled = false; pathDick [pathColor] [i].images [1].sprite = ExperimentPanel.puzzleSprites [0]; if (i == 0 || i == pathDick [pathColor].Count - 1) { pathDick [pathColor] [i].images [1].enabled = true; pathDick [pathColor] [i].images [1].color = pathDick [pathColor] [i].pathColor; } } } // Rotate - !!Doesn't actually rotate // 0, 0 = up then right (0); 0, 1 = right then down (90); 1, 0 = down then left (180); 1,1 = left then up (270) // axis2 then axis1 int firstRandomIndex = Random.Range(0, 2); int secondRandomIndex = Random.Range(0, 2); if (firstRandomIndex != 0 || secondRandomIndex != 0) { List <LittleBox[]> tempLittleBoxMatrix = new List <LittleBox[]> (); for (int i = 0; i < matrixSize; i++) { tempLittleBoxMatrix.Add(new LittleBox[matrixSize]); } int axis1 = (matrixSize - 1) * Mathf.Abs(firstRandomIndex - secondRandomIndex); int axis1Change = -1 * Mathf.Abs(firstRandomIndex - secondRandomIndex) + 1 - Mathf.Abs(firstRandomIndex - secondRandomIndex);; int axis2Change = 1 - firstRandomIndex - 1 * firstRandomIndex; for (int i = 0; i < matrixSize; i++) { int axis2 = (matrixSize - 1) * firstRandomIndex; for (int j = 0; j < matrixSize; j++) { int x = axis2 * secondRandomIndex + axis1 * (1 - secondRandomIndex); int y = axis1 * secondRandomIndex + axis2 * (1 - secondRandomIndex); littleBoxMatrix[i][j].SetPosition(x, y); tempLittleBoxMatrix[x][y] = littleBoxMatrix[i][j]; axis2 += axis2Change; } axis1 += axis1Change; } for (int i = 0; i < matrixSize; i++) { for (int j = 0; j < matrixSize; j++) { littleBoxMatrix[i][j] = tempLittleBoxMatrix[i][j]; } } } // Generate for (float time = 0f; time < generationTime; time += Time.deltaTime) { for (int crunch = 0; crunch < 10; crunch++) { Color randomColor = pathColorList[Random.Range(0, pathCount)]; int randomPathEndInt = Random.Range(0, 2) * (pathDick[randomColor].Count - 1); LittleBox subtractedLB = pathDick[randomColor][randomPathEndInt]; if (pathDick[subtractedLB.pathColor].Count > 3) { List <int> randomNeighborList = new List <int>(); for (int i = 0; i < subtractedLB.neighbors.Count; i++) { randomNeighborList.Add(i); } while (randomNeighborList.Count > 0) { int randomNeighborIndex = Random.Range(0, randomNeighborList.Count); LittleBox randomNeighborLB = subtractedLB.neighbors [randomNeighborList[randomNeighborIndex]]; randomNeighborList.Remove(randomNeighborList[randomNeighborIndex]); // if the neighbor is: // not in the same path that the subtractedLB was // an EndPoint of another path // the only neighbor in its respective path of the subtractedLB if (randomNeighborLB.pathColor != subtractedLB.pathColor && (pathDick[randomNeighborLB.pathColor][0] == randomNeighborLB || pathDick[randomNeighborLB.pathColor][pathDick[randomNeighborLB.pathColor].Count - 1] == randomNeighborLB) && !OtherNeighborInSamePath(subtractedLB, randomNeighborLB)) { pathDick[randomColor].Remove(subtractedLB); if (randomPathEndInt == 0) { pathDick[randomColor][0].images[1].enabled = true; pathDick[randomColor][0].images[1].color = subtractedLB.pathColor; } else { pathDick[randomColor][pathDick[subtractedLB.pathColor].Count - 1].images[1].enabled = true; pathDick[randomColor][pathDick[subtractedLB.pathColor].Count - 1].images[1].color = subtractedLB.pathColor; } randomNeighborLB.images[1].enabled = false; subtractedLB.images[1].enabled = true; subtractedLB.images[1].color = randomNeighborLB.pathColor; subtractedLB.pathColor = randomNeighborLB.pathColor; if (pathDick[randomNeighborLB.pathColor][0] == randomNeighborLB) { pathDick[randomNeighborLB.pathColor].Insert(0, subtractedLB); } else { pathDick[randomNeighborLB.pathColor].Add(subtractedLB); } break; } } } } yield return(null); // yield return new WaitForSeconds (1f); } // Clear paths foreach (Color pathColor in pathDick.Keys) { for (int i = 0; i < pathDick[pathColor].Count; i++) { if (i != 0 && i != pathDick[pathColor].Count - 1) { pathDick[pathColor][i].Unoccupy(); } else { pathDick[pathColor][i].SetAsEndPoint(pathColor); } } pathDick[pathColor].Clear(); } yield return(new WaitForSeconds(0.25f)); StartCoroutine(PuzzleTimer()); }
public virtual void DoAction(LittleBox littleBox) { }
protected override IEnumerator PuzzleGenerator() { int xPos = Random.Range(0, columnCount), yPos = Random.Range(0, rowCount); finishEndPoint = littleBoxMatrix [xPos] [yPos]; cellList.Add(finishEndPoint); finishEndPoint.occupied = true; // finishEndPoint.images [0].color = finishEndPointColor; finishEndPoint.pathPosition = 0; // finishEndPoint.debugText.text = finishEndPoint.pathPosition.ToString (); LittleBox startEndPoint = finishEndPoint; List <LittleBox> deadEndList = new List <LittleBox> (); // Dictionary<LittleBox, LittleBox> forkDeadEndDick = new Dictionary<LittleBox, LittleBox> (); // Generate Maze while (cellList.Count > 0) { for (int z = 0; z < cellsPerFrame && cellList.Count > 0; z++) { LittleBox currentCell = cellList[GetCellListIndex()]; if (!connectedCells.ContainsKey(currentCell)) { connectedCells.Add(currentCell, new List <LittleBox>()); } List <int> neighborIndices = new List <int> { 0, 1, 2, 3 }; if (currentCell.neighbors.Count < neighborIndices.Count) { neighborIndices.RemoveRange(currentCell.neighbors.Count, neighborIndices.Count - currentCell.neighbors.Count); } // Randomly selects neighbors until it finds an unoccupied one, if all are occupied, // removes cell from cellList for (int i = 0; i < currentCell.neighbors.Count; i++) { int randNeighborInt = neighborIndices[Random.Range(0, neighborIndices.Count)]; neighborIndices.Remove(randNeighborInt); LittleBox neighborCell = currentCell.neighbors[randNeighborInt]; // if neighbor is unoccupied, add neighbor to list if (!neighborCell.occupied) { AddLBtoCellList(neighborCell, currentCell); // keeps track of forks (cells with 3 or more connected neighbors) if (connectedCells[currentCell].Count == 3) { currentCell.images[0].color = Color.yellow; forkDeadEndDick.Add(currentCell, currentCell); } break; } else if (i == currentCell.neighbors.Count - 1) { cellList.Remove(currentCell); if (connectedCells[currentCell].Count == 2) { currentCell.images[0].color = Color.clear; } else if (connectedCells[currentCell].Count == 1) { // if (currentCell != startEndPoint && currentCell != finishEndPoint) // { currentCell.images[0].color = Color.cyan; // } deadEndList.Add(currentCell); } break; } } } yield return(null); // yield return new WaitForSeconds (1f); } // Get deadEnd Paths LittleBox startFork = null; int furthestDisance = 0; for (int z = 0; z < deadEndList.Count; z++) { LittleBox deadEnd = deadEndList[z]; LittleBox fork = ConnectFurthestDeadEndToFork(deadEnd, deadEnd.pathPosition, connectedCells[deadEnd][0].pathPosition - deadEnd.pathPosition); if (fork) { if (forkDeadEndDick[fork] != fork) { forkDeadEndDick[fork].images[0].color = Color.blue; } forkDeadEndDick[fork] = deadEnd; forkDeadEndDick[fork].images[0].color = Color.cyan; } if (deadEnd.pathPosition > furthestDisance) { furthestDisance = deadEnd.pathPosition; startEndPoint = deadEnd; startFork = fork; } yield return(null); } // Find longest path List <LittleBox> tempForkList = new List <LittleBox> (); float longestPathDistance = startEndPoint.pathPosition; float currentPathDistance = startEndPoint.pathPosition - startFork.pathPosition; startFork.images [0].color = Color.green; startFork.isTraversed = true; tempForkList.Add(startFork); while (tempForkList.Count > 0) { yield return(null); for (int z = 0; z < (int)(cellsPerFrame / 5) && tempForkList.Count > 0; z++) { LittleBox currentFork = tempForkList[tempForkList.Count - 1]; for (int i = 0; i < connectedCells[currentFork].Count; i++) { LittleBox neighbor = connectedCells[currentFork][i]; if (!neighbor.isTraversed) { LittleBox connectedFork = FindConnectedForks(neighbor, currentFork); float forkedPathDis = Mathf.Abs(connectedFork.pathPosition - currentFork.pathPosition); currentPathDistance += forkedPathDis; float deadEndPathDis = Mathf.Abs(connectedFork.pathPosition - forkDeadEndDick[connectedFork].pathPosition); if (currentPathDistance + deadEndPathDis > longestPathDistance) { longestPathDistance = currentPathDistance + deadEndPathDis; finishEndPoint.images[0].color = Color.blue; finishEndPoint = forkDeadEndDick[connectedFork]; } connectedFork.images[0].color = Color.green; tempForkList.Add(connectedFork); break; } if (i == connectedCells[currentFork].Count - 1) { currentFork.images[0].color = Color.magenta; if (tempForkList.Count > 1) { currentPathDistance -= Mathf.Abs(currentFork.pathPosition - tempForkList[tempForkList.Count - 2].pathPosition); } tempForkList.Remove(currentFork); } } } } for (int i = 0; i < columnCount; i++) { for (int j = 0; j < rowCount; j++) { littleBoxMatrix[i][j].Unoccupy(); } } startEndPoint.SetAsEndPoint(pathColor); // startEndPoint.images [1].sprite = ExperimentPanel.puzzleSprites [0]; // startEndPoint.images [1].color = Color.red; startEndPoint.images [1].sprite = mazeSprites [0]; startEndPoint.images [1].color = Color.white; finishEndPoint.images [1].enabled = true; // finishEndPoint.images [1].sprite = ExperimentPanel.puzzleSprites [0]; // finishEndPoint.images [1].color = Color.blue; finishEndPoint.images [1].sprite = mazeSprites [1]; StartCoroutine(PuzzleTimer()); }
private LittleBox ReturnBoxValue(LittleBox lb) { return(lb); }