private bool CalculateDepthForEachTile() { gridAPI.ResetDepthValues(); Queue <Tile> nextLookAtTile = new Queue <Tile>(); gridAPI.GetStartTile().SetTileDepth(0); nextLookAtTile.Enqueue(gridAPI.GetStartTile()); numTilesChecked = 1; Tile dequeTile; while (nextLookAtTile.Count > 0) { dequeTile = nextLookAtTile.Dequeue(); if (dequeTile != gridAPI.GetGoalTile()) { List <Tile> inRangeTiles = GetInRangeTiles(dequeTile, 0); //Debug.Log(inRangeTiles.Count); for (int i = 0; i < inRangeTiles.Count; i++) { if (inRangeTiles[i].GetTileDepth() == -1) { inRangeTiles[i].SetTileDepth(dequeTile.GetTileDepth() + 1); numTilesChecked++; inRangeTiles[i].pfParent = dequeTile; nextLookAtTile.Enqueue(inRangeTiles[i]); if (inRangeTiles[i] == gridAPI.GetGoalTile()) { break; } //Debug.Log(inRangeTiles[i].x + ", " + inRangeTiles[i].y); } } } else { break; } } if (gridAPI.GetGoalTile().GetTileDepth() == -1) { gridAPI.GetGoalTile().SetTileDepth(-(gridAPI.CurNumTiles() - numTilesChecked)); } return(true); }
// Update is called once per frame void Update() { if (gridAPI.generateBoard && gridAPI.boardGenerated) { if (gridAPI.ResetDepthValues()) { //PART 2 OF PROJECT stopwatch.Start(); if (SetLegalMovesForAllTiles()) { //PART 3 OF PROJECT if (CalculateDepthForEachTile()) { //Debug.Log("Created Board With Initial Values"); gridAPI.SetTimerText(stopwatch.Stop()); gridAPI.SetNumTilesChecked(numTilesChecked); newPf = true; TValueToChoose = 0; curPTile = gridAPI.GetGoalTile(); lerpValue = 1.0f; } else { stopwatch.Stop(); gridAPI.SetTimerText("Failed"); } } else { stopwatch.Stop(); gridAPI.SetTimerText("Failed"); } } else { stopwatch.Stop(); gridAPI.SetTimerText("Failed"); } gridAPI.generateBoard = false; } //Just separated BFS to see how it performs if (gridAPI.boardGenerated && gridAPI.doBFS) { gridAPI.ResetDepthValues(); stopwatch.Start(); if (BFS()) { gridAPI.doBFS = false; gridAPI.SetTimerText(stopwatch.Stop()); gridAPI.SetNumTilesChecked(numTilesChecked); newPf = true; gridAPI.ResetTileColours(); TValueToChoose = 0; curPTile = gridAPI.GetGoalTile(); lerpValue = 1.0f; } else { stopwatch.Stop(); gridAPI.SetTimerText("Failed"); } } //PART 4 Of The Project if (gridAPI.boardGenerated && gridAPI.doHillClimb) { stopwatch.Start(); totalPathNumMoves = 0; if (HillClimb(gridAPI.hilClimbNumIter)) { gridAPI.doHillClimb = false; gridAPI.SetTimerText(stopwatch.Stop()); gridAPI.ResetTileColours(); newPf = true; TValueToChoose = 0; curPTile = gridAPI.GetGoalTile(); lerpValue = 1.0f; } else { stopwatch.Stop(); gridAPI.SetTimerText("Failed"); } } //PART 5 Of The Project if (gridAPI.boardGenerated && gridAPI.doSPF) { gridAPI.ResetSpfRValues(); stopwatch.Start(); if (ShortestPathFirst()) { gridAPI.doSPF = false; gridAPI.SetTimerText(stopwatch.Stop()); gridAPI.SetNumTilesChecked(numTilesChecked); newPf = true; gridAPI.ResetTileColours(); TValueToChoose = 1; curPTile = gridAPI.GetGoalTile(); lerpValue = 1.0f; } else { stopwatch.Stop(); gridAPI.SetTimerText("Failed"); } } //PART 6 Of The Project if (gridAPI.boardGenerated && gridAPI.doAStar) { gridAPI.ResetAStarValues(); stopwatch.Start(); if (AStar()) { gridAPI.doAStar = false; gridAPI.SetTimerText(stopwatch.Stop()); gridAPI.SetNumTilesChecked(numTilesChecked); newPf = true; gridAPI.ResetTileColours(); TValueToChoose = 2; curPTile = gridAPI.GetGoalTile(); lerpValue = 1.0f; } else { stopwatch.Stop(); gridAPI.SetTimerText("Failed"); } } //PART 7 Of The Project if (gridAPI.boardGenerated && gridAPI.doGeneticMating && gridAPI.GridSize() <= 16) { gridAPI.doGeneticMating = false; stopwatch.Start(); Genetics(); gridAPI.SetTimerText(stopwatch.Stop().ToString()); gridAPI.ResetDepthValues(); BFS(); } //Path tile animation if (newPf && Input.GetMouseButton(0)) { if (curPTile.pfParent != null && curPTile.pfParent != gridAPI.GetStartTile()) { totalPathNumMoves += curPTile.pfParent.GetNumMoves(); //Debug.Log(curPTile.pfParent.GetNumMoves()); if (TValueToChoose == 0) { lerpValue -= 1.0f / gridAPI.GetGoalTile().GetTileDepth(); curPTile.pfParent.SetSpriteColor(Color.Lerp(gridAPI.gridStartElementColour, gridAPI.gridGoalElementColour, lerpValue)); } else if (TValueToChoose == 1) { lerpValue -= 1.0f / gridAPI.GetGoalTile().GetSpfValue(); curPTile.pfParent.SetSpriteColor(Color.Lerp(gridAPI.gridStartElementColour, gridAPI.gridGoalElementColour, lerpValue)); } else if (TValueToChoose == 2) { lerpValue -= 1.0f / gridAPI.GetGoalTile().GetAStarFV(); curPTile.pfParent.SetSpriteColor(Color.Lerp(gridAPI.gridStartElementColour, gridAPI.gridGoalElementColour, lerpValue)); } curPTile = curPTile.pfParent; } else { totalPathNumMoves += gridAPI.GetStartTile().GetNumMoves(); //Debug.Log(totalPathNumMoves); totalPathNumMoves = 0; } } }