Exemple #1
0
 public PacManRunner(IAgent agent1, IAgent agent2, PacManGameState gs, float speed)
 {
     this.agent1 = agent1;
     this.agent2 = agent2;
     this.gs     = gs;
     this.speed  = speed;
 }
Exemple #2
0
    public MovementIntent Act(PacManGameState gs, int playerNumber)
    {
        var movementActionValues = (MovementIntent[])Enum.GetValues(typeof(MovementIntent));
        var actionIndex          = UnityEngine.Random.Range(0, movementActionValues.Length);

        return((MovementIntent)movementActionValues.GetValue(actionIndex));
    }
Exemple #3
0
    public Grid MakeMapDiscrete(PacManGameState gs)
    {
        Grid grid = new Grid();

        grid.nodeRadius = 0.5f;
        grid.gridSizeX  = gs.GetXSize();
        grid.gridSizeZ  = gs.GetZSize();

        Node n = new Node();

        for (int i = 0; i < grid.gridSizeX; i++)
        {
            for (int j = 0; j < grid.gridSizeZ; j++)
            {
                bool walkable;
                if (gs.GetEtatCase()[i, j] == 1)
                {
                    walkable = false;
                }
                else
                {
                    walkable = true;
                }
                n.walkable      = walkable;
                n.wordlPosition = new Vector2(i, j);

                grid.grid[i, j] = n;
            }
        }
        return(grid);
    }
Exemple #4
0
    public bool[] RunFrame()
    {
        var action1 = agent1.Act(gs, 1);
        var action2 = agent2.Act(gs, 2);

        bool[] frameResult = PacManGameState.Step(gs, action1, action2, speed);

        agent1.Obs((frameResult[0] ? 1F : 0F), frameResult[2]);
        agent1.Obs((frameResult[1] ? 1F : 0F), frameResult[2]);

        return(frameResult);
    }
Exemple #5
0
 private static void SearchContactWithGum(PacManGameState p)
 {
     if (Vector3.Distance(p.P1, p.GumBall) <=
         0.5f * 1.9f)
     {
         p.GumActive = false;
         p.P1Killer  = true;
     }
     else if (Vector3.Distance(p.P2, p.GumBall) <=
              0.5f * 1.9f)
     {
         p.GumActive = false;
         p.P2Killer  = true;
     }
 }
Exemple #6
0
 private static void SearchContactBetweenPlayer(PacManGameState p)
 {
     if (Vector3.Distance(p.P1, p.P2) <= 0.70f)
     {
         if (p.P1Killer)
         {
             p.GameEnd  = true;
             p.P1Winner = true;
         }
         else if (p.P2Killer)
         {
             p.GameEnd  = true;
             p.P2Winner = true;
         }
     }
 }
Exemple #7
0
    // Initialisation des agents, du runner et du GameState
    public void InitializeGame(int agent1, int agent2)
    {
        switch (agent1)
        {
        case 0:
            PlayerOne.GetComponent <HumanPlayerScript>().ControlType = 0;
            agentP1 = new HumanPlayerAgent(PlayerOne.GetComponent <HumanPlayerScript>());
            break;

        case 1:
            PlayerOne.GetComponent <HumanPlayerScript>().ControlType = 1;
            agentP1 = new HumanPlayerAgent(PlayerOne.GetComponent <HumanPlayerScript>());
            break;

        case 2:
            agentP1 = new RandomAgent();
            break;

        case 3:
            agentP1 = new RandomRolloutAgent(RandomRNbIteration, x, z);
            break;
        }
        switch (agent2)
        {
        case 0:
            PlayerTwo.GetComponent <HumanPlayerScript>().ControlType = 0;
            agentP2 = new HumanPlayerAgent(PlayerTwo.GetComponent <HumanPlayerScript>());
            break;

        case 1:
            PlayerTwo.GetComponent <HumanPlayerScript>().ControlType = 1;
            agentP2 = new HumanPlayerAgent(PlayerTwo.GetComponent <HumanPlayerScript>());
            break;

        case 2:
            agentP2 = new RandomAgent();
            break;

        case 3:
            agentP2 = new RandomRolloutAgent(RandomRNbIteration, x, z);
            break;
        }
        gs     = new PacManGameState(x, z, PlayerOne.transform.position, PlayerTwo.transform.position, Obstacles, Doors);
        runner = new PacManRunner(agentP1, agentP2, gs, speed);
        InGame = true;
        GumBall.transform.position = gs.GetGumVector();
    }
Exemple #8
0
    public MovementIntent Act(PacManGameState gs, int playerNumber)
    {
        int[,] grid;
        Grid g = MakeMapDiscrete(gs);

        var movementActionValues = (MovementIntent[])Enum.GetValues(typeof(MovementIntent));

        PacManGameState gsCopy = new PacManGameState(gs);

        if (gsCopy.GetGumStatus())
        {
            //Heuristique recompense quand il est au plus pres de la gum ball


            return((MovementIntent)movementActionValues.GetValue(1));
        }
        else
        {
            //Si on est le joueur 1
            if (playerNumber == 1)
            {
                //Heuristique recompence quand il est pres du joeur
                if (gs.GetP1Status())
                {
                    return((MovementIntent)movementActionValues.GetValue(1));
                }
                //Heuristique recompence quand il est loin du joueur
                else
                {
                    return((MovementIntent)movementActionValues.GetValue(1));
                }
            }
            else
            {
                if (gs.GetP2Status())
                {
                    return((MovementIntent)movementActionValues.GetValue(1));
                }
                else
                {
                    return((MovementIntent)movementActionValues.GetValue(1));
                }
            }
        }
    }
Exemple #9
0
    /**
     * Returns whether passed GameObject collides with a wall
     */
    private static bool CollidesWithWalls(Vector3 player, PacManGameState p)
    {
        float borderLeft  = player.x - 0.375f;
        float borderRight = player.x + 0.375f;
        float borderUp    = player.z - 0.375f;
        float borderDown  = player.z + 0.375f;

        if ((int)(borderLeft - .5) < 0 || (int)(borderRight + .5) >= p.XSize ||
            (int)(borderUp - .5) < 0 || (int)(borderDown + .5) >= p.ZSize)
        {
            return(true);
        }

        // Works because we are axis aligned and player is not wider than walls
        return(p.EtatCase[(int)(borderLeft + .5), (int)(borderUp + .5)] == 1 ||
               p.EtatCase[(int)(borderLeft + .5), (int)(borderDown + .5)] == 1 ||
               p.EtatCase[(int)(borderRight + .5), (int)(borderUp + .5)] == 1 ||
               p.EtatCase[(int)(borderRight + .5), (int)(borderDown + .5)] == 1);
    }
    public MovementIntent Act(PacManGameState gs, int playerNumber)
    {
        int            bestActionScore = 0;
        int            j;
        MovementIntent bestAction = 0;

        foreach (var action in movementIntentValues)
        {
            int actionScore = 0;

            for (var i = 0; i < RolloutIterations; i++)
            {
                gsCopy.CopyGS(gs);

                var            randActionIndex = UnityEngine.Random.Range(0, movementIntentValues.Length);
                MovementIntent randAction      = (MovementIntent)movementIntentValues.GetValue(randActionIndex);

                var result = PacManGameState.Step(gsCopy, action, randAction, Speed);

                for (j = 0; !result[2] && j < ExplorationFrames; j++)   // While not terminal state
                {
                    randActionIndex = UnityEngine.Random.Range(0, movementIntentValues.Length);
                    MovementIntent randAction1 = (MovementIntent)movementIntentValues.GetValue(randActionIndex);
                    randActionIndex = UnityEngine.Random.Range(0, movementIntentValues.Length);
                    MovementIntent randAction2 = (MovementIntent)movementIntentValues.GetValue(randActionIndex);

                    result = PacManGameState.Step(gsCopy, randAction1, randAction2, Speed);
                }

                actionScore += result[playerNumber] ? 1 : 0;
            }

            if (actionScore > bestActionScore)
            {
                bestActionScore = actionScore;
                bestAction      = action;
            }
        }

        return(bestAction);
    }
Exemple #11
0
 // Récupère Intent et Applique changement de position
 public static bool[] Step(PacManGameState p, MovementIntent action1, MovementIntent action2, float Speed)
 {
     if (p.P1Killer)
     {
         IntentManagement(action1, 0, Speed * 1.2f, p);
         IntentManagement(action2, 1, Speed, p);
     }
     else if (p.P2Killer)
     {
         IntentManagement(action1, 0, Speed, p);
         IntentManagement(action2, 1, Speed * 1.2f, p);
     }
     else
     {
         IntentManagement(action1, 0, Speed, p);
         IntentManagement(action2, 1, Speed, p);
     }
     SearchContactWithGum(p);
     SearchContactBetweenPlayer(p);
     return(new bool[3] {
         p.P1Winner, p.P2Winner, p.GameEnd
     });
 }
Exemple #12
0
    // Methode Copie du GameState
    public void CopyGS(PacManGameState PMGS)
    {
        // Initialisation de Etat case
        for (int i = 0; i < PMGS.XSize; i++)
        {
            for (int j = 0; j < PMGS.ZSize; j++)
            {
                this.EtatCase[i, j] = PMGS.EtatCase[i, j];
            }
        }

        this.XSize     = PMGS.XSize;
        this.ZSize     = PMGS.ZSize;
        this.P1        = PMGS.P1;
        this.P2        = PMGS.P2;
        this.P1Killer  = PMGS.P1Killer;
        this.P2Killer  = PMGS.P2Killer;
        this.GumActive = PMGS.GumActive;
        this.GumBall   = PMGS.GumBall;
        this.P1Winner  = PMGS.P1Winner;
        this.P2Winner  = PMGS.P2Winner;
        this.GameEnd   = PMGS.GameEnd;
    }
 public RandomRolloutAgent(float explorationFrames, int xSize, int zSize)
 {
     this.ExplorationFrames = (int)explorationFrames;
     this.gsCopy            = new PacManGameState(xSize, zSize);
 }
Exemple #14
0
 public MovementIntent Act(PacManGameState gs, int playerNumber)
 {
     return(script.Intent);
 }
Exemple #15
0
    // Setter mouvement
    private static void IntentManagement(MovementIntent Intent, int playerIndex, float Speed, PacManGameState p)
    {
        if ((Intent & MovementIntent.WantToMoveForward) != 0)
        {
            tryMovingInDirection(playerIndex, 0, Speed, p);
        }

        if ((Intent & MovementIntent.WantToMoveBackward) != 0)
        {
            tryMovingInDirection(playerIndex, 1, Speed, p);
        }

        if ((Intent & MovementIntent.WantToMoveLeft) != 0)
        {
            tryMovingInDirection(playerIndex, 3, Speed, p);
        }
        if ((Intent & MovementIntent.WantToMoveRight) != 0)
        {
            tryMovingInDirection(playerIndex, 2, Speed, p);
        }
        p.P1 = Portal(p.P1, 0.0f, (float)p.XSize - 1.0f, 0.0f, (float)p.ZSize - 1.0f);
        p.P2 = Portal(p.P2, 0.0f, (float)p.XSize - 1.0f, 0.0f, (float)p.ZSize - 1.0f);
    }
Exemple #16
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            InGame = !InGame;
        }
        if (!InGame)
        {
            return;
        }
        //Run a frame in the runner
        bool[] frameResult = runner.RunFrame();
        gs = runner.GetState();
        PlayerOne.transform.position = gs.GetP1Vector();
        PlayerTwo.transform.position = gs.GetP2Vector();
        // Test des booléen
        if (gs.GetGumStatus() == false)
        {
            Timetokill -= Time.deltaTime;
            if (gs.GetP1Status() && GumBall.activeInHierarchy)
            {
                GumBall.SetActive(false);
                Timetokill = TimeKiller;
                gs.SetPositionGumball(-100, -100, -100);
                GumBall.transform.position = gs.GetGumVector();
                PlayerOne.GetComponent <Renderer>().material = MatKiller;
            }
            else if (gs.GetP2Status() && GumBall.activeInHierarchy)
            {
                GumBall.SetActive(false);
                Timetokill = TimeKiller;
                gs.SetPositionGumball(-100, -100, -100);
                GumBall.transform.position = gs.GetGumVector();
                PlayerTwo.GetComponent <Renderer>().material = MatKiller;
            }
        }
        else
        {
            PlayerOne.GetComponent <Renderer>().material = MatOne;
            PlayerTwo.GetComponent <Renderer>().material = MatTwo;
        }

        if (gs.getP1Winner())
        {
            InGame = false;
            WinOne.SetActive(true);
            LoseTwo.SetActive(true);
            EndMenu.SetActive(true);
        }
        else if (gs.getP2Winner())
        {
            InGame = false;
            WinTwo.SetActive(true);
            LoseOne.SetActive(true);
            EndMenu.SetActive(true);
        }
        if (Timetokill <= 0)
        {
            Timetokill = 0.1f;
            gs.SetBoolP1(false);
            gs.SetBoolP2(false);
            gs.RandomizeGumball();
            GumBall.transform.position = gs.GetGumVector();
            gs.SetGumStatus(true);
            GumBall.SetActive(true);
        }
        if (frameResult[2])//if state is terminal
        {
            InGame = false;
            //TODO Affichage du joueur/agent gagnant et perdant
        }
    }
Exemple #17
0
    /**
     * Tries to move a player in a direction, cancelling movement on collision
     */
    private static void tryMovingInDirection(int playerIndex, int direction, float Speed, PacManGameState p)
    {
        Vector3 newPosition = p.GetPositionForPlayer(playerIndex);

        newPosition += directions[direction] * Speed * Time.deltaTime;

        if (CollidesWithWalls(newPosition, p))
        {
            return;
        }

        p.SetPositionForPlayer(playerIndex, newPosition);
    }