示例#1
0
    public int[] StartEvaluation()
    {
        // ________________________________________ FIRST POINT __________________________________________________
        // There is only one placed in the game so just randomize it because otherwise it only places where we examine first
        if (pointsInGame.Count < 2)
        {
            System.Random rand = new System.Random();
            IntVector2    random;
            do
            {
                random = new IntVector2(rand.Next(0, 3) - 1, rand.Next(0, 3) - 1);
            } while (random.x == 0 && random.y == 0);

            return(LocalAIToGridPos(pointsInGame[0] + random));
        }

        // _____________________________________ DEFENSE EVAL __________________________________________
        // It' only a defending mechanism so only checks for human pointsinrow
        // try places where we surely have to place
        // it has a smaller chance that it skips that position
        SignInARow humThree = null, humFour = null;
        SignInARow aiThree = null, aiFour = null;

        int   tillI             = pointsInGame.Count;
        float exponentialChance = Mathf.Pow(leaveOutChance, 1.55f);

        for (int i = 0; i < tillI; i++)
        {
            List <SignInARow> list = gameField[pointsInGame[i].x, pointsInGame[i].y].signsInARow;
            for (int k = 0; k < list.Count; k++)
            {
                // skip at random
                if (rand.NextDouble() < exponentialChance)
                {
                    continue;
                }


                int length     = list[k].Length;
                int blockCount = list[k].BlockCount();

                if (length == 3 && blockCount == 0)
                {
                    // Store it so if after this loop we don't find a four (we don't return) place there
                    if (list[k].Type == HumanType)
                    {
                        humThree = list[k];
                    }
                    else
                    {
                        aiThree = list[k];
                    }
                }
                else if (length == 4 && blockCount != 2)
                {
                    // It prioritizes the fours so if it finds one place there
                    if (list[k].Type == HumanType)
                    {
                        humFour = list[k];
                    }
                    else     // There is a 4 length that is AIType so place there
                    {
                        aiFour = list[k];
                    }
                }
            }
        }

        // First if there is a four type ai where we can place win the game
        if (aiFour != null)
        {
            return(LocalAIToGridPos(aiFour.GetUnblockedPos()));
        }
        // If there is a four that is type of human we should really place there
        if (humFour != null)
        {
            return(LocalAIToGridPos(humFour.GetUnblockedPos()));
        }

        // If there is a signinarow with aitype that is length of three decide which is better and return that
        if (aiThree != null)
        {
            return(LocalAIToGridPos(WhichIsBetter(aiThree.GetBlockField1Pos(), aiThree.GetBlockField2Pos(), AIType)));
        }

        // If there is no three with AI type but there is one for the human decide which is better
        if (humThree != null)   // We are going to decide which position is better
        {
            return(LocalAIToGridPos(WhichIsBetter(humThree.GetBlockField1Pos(), humThree.GetBlockField2Pos(), AIType)));
        }

        // Come here is everything else fails
        // ___________________ NORMAL EVAL _____________________________-
        EvaluationResult result = new EvaluationResult();

        try {
            result = EvaluateField(gameField, AIType, 1, pointsInGame, int.MinValue, int.MaxValue);
        } catch (Exception e) {
            UnityEngine.Debug.Log(e.Message + "\n" + e.StackTrace);
        }
        return(LocalAIToGridPos(result.fieldPos));
    }