Пример #1
0
    public void CalculateOpponentValues()
    {
        GameObject dijkstraGameObject = Instantiate(dijkstraPrefab);
        ModifiedDijkstraAlgorithm dijkstraAlgorithm = dijkstraGameObject.GetComponent <ModifiedDijkstraAlgorithm>();

        dijkstraAlgorithm.Initialize(CurrentNodePosition, MainScript.AllNodes[19], MainScript.CurrentState);
        dijkstraAlgorithm.CalculateModifiedDijkstraAlgorithm();
        preCalculatedTime = endBattleGameController.GetComponent <EndBattleGameController>().timer + (dijkstraAlgorithm.ShortestPath.Count - 1) * stepDuration;
        stepDuration      = 0.001f;
    }
Пример #2
0
    /**
     * <summary>Initalizes the game. Creates the maze and adds the obstacles.</summary>
     */
    public void InitializeGame()
    {
        EnableUserInput = false;
        //Initializes the static variables of the game.
        CurrentState     = 0;
        CurrentStepCount = 0;
        UpdateStepCounter();
        AllNodes   = new Dictionary <int, NodeController>();
        AllEdges   = new List <EdgeController>();
        AllButtons = new List <ButtonController>();
        PlayerPath = new List <NodeController>();

        //Set all possible colors (at least as many as NumberOfObstacles)
        Colors = new List <Color>
        {
            new Color(0, 1, 0),
            new Color(0, 0, 1),
            new Color(1, 0, 0),
            new Color(1, 1, 0)
        };

        //Initializes the number of states.
        NumberOfStates = (int)Math.Pow(2, NumberOfButtons);

        //Moves the player to the start point.
        GameObject.Find("Ruby").GetComponent <RubyController>().SetPositionAndScale();


        //Generates the labyrinth
        GameObject            gameObject = Instantiate(aldousBroderAlgorithmPrefab);
        AldousBroderAlgorithm a          = gameObject.GetComponent <AldousBroderAlgorithm>();

        a.Initialize((int)Math.Floor(1 / ScaleMazeSize * 18), (int)Math.Floor(1 / ScaleMazeSize * 10));
        if (CurrentLevelCount != -1)
        {
            GarbageCollectorGameObjects.Add(gameObject);
        }

        if (!IsBattleGameMode)
        {
            //Generates all obstacles
            gameObject = Instantiate(obstacleGenerationPrefab);
            ObstacleGeneration obstacleGeneration = gameObject.GetComponent <ObstacleGeneration>();
            obstacleGeneration.InsertObstacles();
            if (CurrentLevelCount != -1)
            {
                GarbageCollectorGameObjects.Add(gameObject);
            }

            //Calculates the optimal path and distance.
            gameObject = Instantiate(modifiedDijkstraAlgorithmPrefab);
            ModifiedDijkstraAlgorithm dijkstra = gameObject.GetComponent <ModifiedDijkstraAlgorithm>();
            if (ScaleMazeSize == 0.5f)
            {
                dijkstra.Initialize(AllNodes[0], AllNodes[719]);
            }
            else
            {
                dijkstra.Initialize(AllNodes[0], AllNodes[179]);
            }
            dijkstra.CalculateModifiedDijkstraAlgorithm();
            GameObject stepCounterText = GameObject.Find("OptimalSteps");
            stepCounterText.GetComponent <TextMeshProUGUI>().text = "Optimal: " + dijkstra.ShortestDistance;
            OptimalStepCount = dijkstra.ShortestDistance;
            ShortestPath     = dijkstra.ShortestPath;

            if (CurrentLevelCount != -1)
            {
                GarbageCollectorGameObjects.Add(gameObject);
            }
        }

        //Creates all walls of the maze.
        GameObject  createWallsObject = Instantiate(createWallsPrefab);
        CreateWalls createWallsScript = createWallsObject.GetComponent <CreateWalls>();

        createWallsScript.CreateAllWalls();
        if (CurrentLevelCount != -1)
        {
            GarbageCollectorGameObjects.Add(createWallsObject);
        }

        if (IsBattleGameMode)
        {
            GenerateFreezer();
            GameObject.Find("Opponent").GetComponent <OpponentController>().InitializeOpponent();
        }
        //Enables the user input.
        EnableUserInput = true;
    }
Пример #3
0
    private void InsertOneObstacle(int buttonId, bool onPlayerPath)
    {
        while (true)
        {
            //Calculates the shortest distance before adding the obstacle.
            GameObject algorithmObject = Instantiate(modifiedDijkstraAlgorithmPrefab);
            if (MainScript.CurrentLevelCount != -1)
            {
                MainScript.GarbageCollectorGameObjects.Add(algorithmObject);
            }
            ModifiedDijkstraAlgorithm algorithm = algorithmObject.GetComponent <ModifiedDijkstraAlgorithm>();
            if (onPlayerPath)
            {
                //path between start and target node
                algorithm.Initialize(MainScript.AllNodes[playerStartPosition], MainScript.AllNodes[MainScript.NumberOfNodes - 1]);
            }
            else
            {
                //path between the node in the upper right corner and the node in the lower left corner
                algorithm.Initialize(MainScript.AllNodes[opponentStartPosition], MainScript.AllNodes[MainScript.Height - 1]);
            }
            algorithm.CalculateModifiedDijkstraAlgorithm();
            List <NodeController> shortestPath = algorithm.ShortestPath;

            //Gets randomly a node of the shortest path as obstacle.
            int            randomNumber = Random.Range((int)Math.Floor((double)shortestPath.Count / 4), (int)(shortestPath.Count - Math.Floor((double)shortestPath.Count / 10)));
            NodeController startNode    = shortestPath[randomNumber];
            NodeController targetNode   = shortestPath[randomNumber + 1];

            //Sets the new values of the obstacle
            EdgeController edge = startNode.GetEdgeToNode(targetNode);
            if (edge.Obstacle != -1)
            {
                continue;
            }
            TransformEdge(edge, buttonId);

            //Calculates the shortest distance after adding the obstacle.
            GameObject algorithmObject1 = Instantiate(modifiedDijkstraAlgorithmPrefab);
            if (MainScript.CurrentLevelCount != -1)
            {
                MainScript.GarbageCollectorGameObjects.Add(algorithmObject1);
            }
            ModifiedDijkstraAlgorithm algorithm1 = algorithmObject1.GetComponent <ModifiedDijkstraAlgorithm>();
            if (onPlayerPath)
            {
                algorithm1.Initialize(MainScript.AllNodes[playerStartPosition], MainScript.AllNodes[MainScript.NumberOfNodes - 1]);
            }
            else
            {
                algorithm1.Initialize(MainScript.AllNodes[opponentStartPosition], MainScript.AllNodes[MainScript.Height - 1]);
            }
            algorithm1.CalculateModifiedDijkstraAlgorithm();
            int shortestDistanceWithObstacle = algorithm1.ShortestDistance;

            //Tries to insert a valid button
            if (InsertButton(shortestDistanceWithObstacle, startNode, shortestPath, buttonId, onPlayerPath, edge))
            {
                Destroy(algorithmObject);
                Destroy(algorithmObject1);
                //Changes the color of the obstacle to its initial value.
                edge.ChangeColorOfObstacle(0);
                break;
            }
            else
            {
                //Resets the edge and tries to find another location for the obstacle.
                Destroy(algorithmObject);
                Destroy(algorithmObject1);
                ResetEdge(edge);
            }
        }
    }
Пример #4
0
    /**
     * Tries to insert a button for the corresponding obstacle.
     * <param name="shortestDistance">The shortest distance of the path after adding the obstacle.</param>
     * <param name="nodeAtObstacle">The node in front of the obstacle.</param>
     * <param name="optimalPathWithoutObstacle">The optimal path before adding the obstacle.</param>
     * <param name="buttonId">The id of the button.</param>
     * <param name="onPlayerPath">The location flag of the obstacle.</param>
     * <param name="obstacle">The corresponding obstacle.</param>
     * <returns>Whether the operation was successful.</returns>
     */
    private bool InsertButton(int shortestDistance, NodeController nodeAtObstacle, List <NodeController> optimalPathWithoutObstacle, int buttonId, bool onPlayerPath, EdgeController obstacle)
    {
        //Counts the number of tested nodes.
        int counter = 0;

        while (true)
        {
            //Breaks if it checked too much nodes. Chooses after that another obstacle.
            if (counter == MainScript.NumberOfNodes)
            {
                return(false);
            }

            //Gets a random node as button.
            NodeController node = GetRandomNode();
            //Checks the conditions of the node.
            if (optimalPathWithoutObstacle.Contains(node) || node.Button != -1)
            {
                continue;
            }
            //Sets the values of the new button.
            node.Button = buttonId;
            node.States = SetStates(buttonId);

            //Calculates the shortest distance after adding the button.
            GameObject algorithmObject = Instantiate(modifiedDijkstraAlgorithmPrefab);
            if (MainScript.CurrentLevelCount != -1)
            {
                MainScript.GarbageCollectorGameObjects.Add(algorithmObject);
            }
            ModifiedDijkstraAlgorithm algorithm = algorithmObject.GetComponent <ModifiedDijkstraAlgorithm>();
            if (onPlayerPath)
            {
                algorithm.Initialize(MainScript.AllNodes[playerStartPosition], MainScript.AllNodes[MainScript.NumberOfNodes - 1]);
            }
            else
            {
                algorithm.Initialize(MainScript.AllNodes[opponentStartPosition], MainScript.AllNodes[MainScript.Height - 1]);
            }
            algorithm.CalculateModifiedDijkstraAlgorithm();
            int newShortestDistance = algorithm.ShortestDistance;

            //Calculates the distance between button and obstacle.
            GameObject algorithmObject1 = Instantiate(modifiedDijkstraAlgorithmPrefab);
            if (MainScript.CurrentLevelCount != -1)
            {
                MainScript.GarbageCollectorGameObjects.Add(algorithmObject1);
            }
            ModifiedDijkstraAlgorithm algorithm1 = algorithmObject1.GetComponent <ModifiedDijkstraAlgorithm>();
            algorithm1.Initialize(node, nodeAtObstacle);
            algorithm1.CalculateModifiedDijkstraAlgorithm();
            int distanceBetweenButtonAndObstacle = algorithm1.ShortestDistance;

            //Checks the conditions.
            if (newShortestDistance < shortestDistance && distanceBetweenButtonAndObstacle > (shortestDistance / 10))
            {
                Destroy(algorithmObject);
                Destroy(algorithmObject1);
                //Adds the button at the choosen node.
                GameObject       gameObject = Instantiate(buttonPrefab, node.transform.position, Quaternion.identity);
                ButtonController button     = gameObject.GetComponent <ButtonController>();
                button.Initialize(obstacle, node, buttonId);
                MainScript.AllButtons.Add(button);
                button.gameObject.transform.localScale = new Vector3(0.25f * MainScript.ScaleMazeSize, 0.25f * MainScript.ScaleMazeSize);
                if (MainScript.CurrentLevelCount != -1)
                {
                    MainScript.GarbageCollectorGameObjects.Add(gameObject);
                }
                break;
            }
            //Resets the values and checks another node.
            Destroy(algorithmObject);
            Destroy(algorithmObject1);
            node.Button = -1;
            node.States = null;
            counter++;
        }
        return(true);
    }