Пример #1
0
    private bool HillClimb(int numItterations)
    {
        int oldMoves;
        int oldGoalDepth;

        int smallSide = Random.Range(0, 2);
        int avg       = gridAPI.GridSize() / 2;

        //Debug.Log(smallSide);
        for (int i = 0; i < numItterations; i++)
        {
            int randX = Random.Range(0, gridAPI.GridSize());
            int randY = (randX == 0) ? Random.Range(0, gridAPI.GridSize() - 1) : Random.Range(0, gridAPI.GridSize());

            oldMoves     = gridAPI.GetTileByCoord(randX, randY).GetNumMoves();
            oldGoalDepth = gridAPI.GetGoalTile().GetTileDepth();

            if (RandHillClimbTileMove(randX, randY, 0))
            {
                if (CalculateDepthForEachTile())
                {
                    if (gridAPI.GetGoalTile().GetTileDepth() < oldGoalDepth || EvaluateBySumAverage(smallSide, 3) >= avg + 1)
                    {
                        gridAPI.GetTileByCoord(randX, randY).SetNumMoves(oldMoves);
                        numTilesChecked = 0;
                        CalculateDepthForEachTile();
                        gridAPI.GetGoalTile().SetTileDepth(oldGoalDepth);
                    }
                }
            }
        }

        return(true);
    }
Пример #2
0
    // 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;
            }
        }
    }