Beispiel #1
0
 void Start()
 {
     movesAndScores = FindObjectOfType <MovesAndScores>();
     animator       = GetComponent <Animator>();
     targetPosition = transform.position;
     scores         = GameObject.FindGameObjectWithTag("Scores");
 }
    public int minimax(int depth, int turn)
    {
        if (hasXWon())
        {
            return(+1);
        }
        if (hasOWon())
        {
            return(-1);
        }

        List <MNode> pointsAvailable = getMoves();

        if (pointsAvailable.Capacity == 0)
        {
            return(0);
        }

        List <int> scores = new List <int>();

        for (int i = 0; i < pointsAvailable.Count; i++)
        {
            MNode point = pointsAvailable[i];


            if (turn == 1)
            {
                MNode x = new MNode();
                x.x      = point.x;
                x.y      = point.y;
                x.player = 2;
                gameBoard[point.x, point.y] = x;

                int currentScore = minimax(depth + 1, 2);
                scores.Add(currentScore);

                if (depth == 0)
                {
                    MovesAndScores m = new MovesAndScores(currentScore, point);
                    m.point = point;
                    m.score = currentScore;
                    bestPositions.Add(m);
                }
            }

            else if (turn == 2)
            {
                MNode o = new MNode();
                o.x      = point.x;
                o.y      = point.y;
                o.player = 1;
                gameBoard[point.x, point.y] = o;
                int currentScore = minimax(depth + 1, 1);
                scores.Add(currentScore);
            }
            gameBoard[point.x, point.y] = null;
        }
        return(turn == 1 ? returnMax(scores) : returnMin(scores));
    }
Beispiel #3
0
    public int minimax(int depth, int turn)
    {
        if (hasXWon())
        {
            return(+1);                   //+1 for a player win
        }
        if (hasOWon())
        {
            return(-1);                   //-1 for a computer win
        }
        List <MNode> pointsAvailable = getMoves();

        if (pointsAvailable.Capacity == 0)
        {
            return(0);
        }

        List <int> scores = new List <int>();

        for (int i = 0; i < pointsAvailable.Count; i++)
        {
            MNode point = pointsAvailable[i];

            //Select the highest from the minimax call on X's turn
            if (turn == 1)
            {
                MNode x = new MNode();
                x.x      = point.x;
                x.y      = point.y;
                x.player = 2;
                grid[point.x, point.y] = x;

                int currentScore = minimax(depth + 1, 2);
                scores.Add(currentScore);

                if (depth == 0)
                {
                    MovesAndScores m = new MovesAndScores(currentScore, point);
                    m.point = point;
                    m.score = currentScore;
                    rootsChildrenScores.Add(m);
                }
            }
            //Select the lowest from the minimax call on O's turn
            else if (turn == 2)
            {
                MNode o = new MNode();
                o.x      = point.x;
                o.y      = point.y;
                o.player = 1;
                grid[point.x, point.y] = o;
                int currentScore = minimax(depth + 1, 1);
                scores.Add(currentScore);
            }
            grid[point.x, point.y] = null;             //reset the point
        }
        return(turn == 1 ? returnMax(scores) : returnMin(scores));
    }
 void Start()
 {
     soulCreator       = FindObjectOfType <SoulCreator>();
     audioSource       = GetComponent <AudioSource>();
     movesAndScores    = FindObjectOfType <MovesAndScores>();
     soulListToDestroy = new List <GameObject>();
     InstantiateAllGrids();
     CenterTheGrid();
     gridManager = new GridManager(soulsGrid, soulCreator);
     gridManager.GetStartGrid();
     ChangeStatus(DROP_SOULS);
 }
Beispiel #5
0
    public int Minimax(int[] IAlugar, int turno, int state)
    {
        puntaje = CheckIAWinner(IAlugar, turno);
        if (puntaje != 0)
        {
            return(puntaje);
        }

        CheckEmpty(IAlugar);

        List <int> scores   = new List <int>();
        List <int> posMoves = AvailablePlaces();

        if (posMoves.Capacity == 0)
        {
            return(0);
        }

        for (int i = 0; i < posMoves.Count; i++)
        {
            int move = posMoves[i];

            if (turno == computadora)
            {
                IAlugar[move] = turno;
                int newScore = Minimax(IAlugar, player, state + 1);
                scores.Add(newScore);

                if (state == 0)
                {
                    MovesAndScores mAndS = new MovesAndScores(newScore, move);
                    mAndS.score = newScore;
                    mAndS.move  = move;
                    childrenScores.Add(mAndS);
                }
            }
            else if (turno == player)
            {
                IAlugar[move] = turno;
                int newScore = Minimax(IAlugar, computadora, state + 1);
                scores.Add(newScore);
            }

            IAlugar[move] = 2;
        }

        return(turno == computadora?CheckMaxScores(scores) : CheckMinScores(scores));
    }