Ejemplo n.º 1
0
    public static void AiMinMaxSearchAsyncStopRequest(this IGameBoardState gbs)
    {
        if (jobStatus != AIMinMaxJobStatus.Started && jobStatus != AIMinMaxJobStatus.Finished)
        {
            throw new System.Exception("Impossible to end an AI job that has not started.");
        }

        lock (jobLock)
        {
            if (jobStatus == AIMinMaxJobStatus.Started)
            {
                jobStatus = AIMinMaxJobStatus.StopRequested;
            }
        }
    }
Ejemplo n.º 2
0
    public static void JoinThreads()
    {
        if (jobThread != null)
        {
            lock (jobLock)
            {
                if (jobStatus != AIMinMaxJobStatus.Finished)
                {
                    jobStatus = AIMinMaxJobStatus.StopRequested;
                }
            }

            jobThread.Join();
            jobStatus = AIMinMaxJobStatus.None;
        }
    }
Ejemplo n.º 3
0
    public static void AIMinMaxSearchAsyncBegin(this IGameBoardState gbs, int searchLevels, Player.PlayerNumber currentPlayer)
    {
        if (jobStatus != AIMinMaxJobStatus.None)
        {
            throw new System.Exception("Only 1 async job should run at a time. This must be enforced in the game logic.");
        }

        jobStatus      = AIMinMaxJobStatus.Started;
        LevelsSearched = 0;
        StatesSearched = 0;
        MovesSearched  = 0;
        Debug.Log("AI Thread Started");

        //clone gameboardstate to ensure it is not a MonoBehavior - we need to pass it to a new thread
        IGameBoardState gbs_clone = gbs.Clone();

        jobThread = new Thread(() =>
        {
            ///must run at least one level to complete
            jobResult = gbs_clone.AIMinMaxSearch(1, currentPlayer);
            LevelsSearched++;
            //code is in an iterative deepening pattern, but I is initialized to the deepest level for testing
            //usually i should start at 2 and iterate forward
            for (int i = searchLevels; i <= searchLevels; i++)
            {
                AIMinMaxResult res = gbs_clone.AIMinMaxSearch(i, currentPlayer);

                if (jobStatus == AIMinMaxJobStatus.StopRequested)
                {
                    break;
                }

                LevelsSearched++;
                jobResult = res;
            }

            lock (jobLock)
            {
                jobStatus = AIMinMaxJobStatus.Finished;
                Debug.Log("AI Thread Finished");
            }
        });
        jobThread.IsBackground = true;
        jobThread.Start();
    }
Ejemplo n.º 4
0
    /// <summary>
    /// Asyncronous end blocks the calling thread until it's finished.
    /// </summary>
    /// <returns></returns>
    public static AIMinMaxResult AIMinMaxSearchAsyncEnd(this IGameBoardState gbs)
    {
        Debug.Log("Tried ending AI thread in state " + jobStatus.ToString());

        if (jobStatus != AIMinMaxJobStatus.Started && jobStatus != AIMinMaxJobStatus.Finished)
        {
            throw new System.Exception("Impossible to end an AI job that has not started.");
        }

        lock (jobLock)
        {
            if (jobStatus != AIMinMaxJobStatus.Finished)
            {
                jobStatus = AIMinMaxJobStatus.StopRequested;
            }
        }

        jobThread.Join();

        jobStatus = AIMinMaxJobStatus.None;

        return(jobResult);
    }