Esempio n. 1
0
    public override IEnumerator GetDecision(TTTBoard board)
    {
        currentTime = System.DateTime.Now;

        isDoneMoving = false;

        //Reset previous data
        anticipatedResult = new MoveScore(0, 0);
        statesSampled     = 0;
        culledStates      = 0;
        moveChoices.Clear();
        viableChoices.Clear();

        //Copy the board to avoid affecting the real one
        TTTBoard testBoard = new TTTBoard(board);

        //Get all free squares - the player may cheat, but the AI does not
        testBoard.PopulateMoveQueue(moveChoices);

        //Error checking for if we try to edit a board that's complete
        if (moveChoices.Count > 0)
        {
            chosenMove = moveChoices.Peek();
        }
        else
        {
            UnityEngine.Debug.LogError("TRYING TO PLAY ON ALREADY COMPLETE BOARD");
            isDoneMoving = true;
            chosenMove   = Vector2Int.one * -1;
        }

        //Allows us to check whether or not the following coroutine has finished, by saving a reference to a bool
        BoolCheck newBc = new BoolCheck();

        StartCoroutine(GetBestMovesRecursive(testBoard, playingAs, 0, anticipatedResult, newBc, 2));

        //Keep waiting for previous functions to complete before moving on
        while (!newBc.complete)
        {
            yield return(new WaitForEndOfFrame());
        }

        //Debug info
        Debug.Log("Computer player " + playingAs.ToString() + " processed " + statesSampled + " unique states, " +
                  "best case scenario: " + anticipatedResult.outcome + " after " + anticipatedResult.depth + " turns");

        string tot = "";

        for (int i = 0; i < viableChoices.Count; ++i)
        {
            Vector2Int vChoice = viableChoices.Dequeue();
            viableChoices.Enqueue(vChoice);
            tot += vChoice;
            tot += " ";
        }

        Debug.Log("Choices: " + tot);
    }