//Coroutines let you run code asynchronously in Unity (i.e. while other code is running)
    //This lets you, for instance, tell a bit of code to run in the background while the game
    //keeps going. We use it here to start a process that alternates asking each AI to take a
    //turn, and then waits for a second so the viewer (that's you) can see what happens.
    IEnumerator VersusMode()
    {
        //No humans allowed
        allowInteraction = false;
        while (true)
        {
            botPlayer1.TakeTurn(currentGame, TimeAllottedPerTurn);
            yield return(new WaitForSeconds(timeBetweenActions));

            yield return(new WaitUntil(() => !animatingAction));

            if (currentGame.endStatus != 0)
            {
                break;
            }

            botPlayer2.TakeTurn(currentGame, TimeAllottedPerTurn);
            yield return(new WaitForSeconds(timeBetweenActions));

            yield return(new WaitUntil(() => !animatingAction));

            if (currentGame.endStatus != 0)
            {
                break;
            }
        }
        SetEndState();
        Debug.Log("done");
    }
Exemple #2
0
    public IEnumerator PlayGame(Game game, BaseAgent player1, BaseAgent player2, int turnLimit = 100)
    {
        int turn = 0;

        //? Always reset the game before playing.
        game.ResetState();

        var time = Stopwatch.StartNew();

        // game.PrintBoard();
        while (turn < turnLimit)
        {
            if (!player1.TakeTurn(game, TimeAllottedPerTurn))
            {
                break;
            }
            if (game.endStatus > 0)
            {
                break;
            }
            // game.PrintBoard();
            turn++;

            //Take a break when we reach 16ms (approx. 1 frame)
            if (time.ElapsedMilliseconds > 16)
            {
                time.Restart();
                yield return(0);
            }

            if (!player2.TakeTurn(game, TimeAllottedPerTurn))
            {
                break;
            }
            if (game.endStatus > 0)
            {
                break;
            }
            // game.PrintBoard();
            turn++;

            //Take a break when we reach 16ms (approx. 1 frame)
            if (time.ElapsedMilliseconds > 16)
            {
                time.Restart();
                yield return(0);
            }
        }

        // game.PrintBoard();

        if (turn >= turnLimit)
        {
            // Debug.Log("Game tied: turn limit exceeded.");
        }
        else
        {
            switch (game.endStatus)
            {
            case 1:
                // Debug.Log("Player 1 wins (in "+turn+" turns)");
                break;

            case 2:
                // Debug.Log("Player 2 wins (in "+turn+" turns)");
                break;

            case 3:
                // Debug.Log("Game tied (in "+turn+" turns)");
                break;
            }
        }

        yield return(0);
    }
Exemple #3
0
    public int PlayGame(Game game, BaseAgent player1, BaseAgent player2, int turnLimit = 100)
    {
        int turn = 0;

        //? Always reset the game before playing.
        game.ResetState();

        // game.PrintBoard();
        while (turn < turnLimit)
        {
            if (!player1.TakeTurn(game))
            {
                break;
            }
            if (game.endStatus > 0)
            {
                break;
            }
            // game.PrintBoard();
            turn++;
            if (!player2.TakeTurn(game))
            {
                break;
            }
            if (game.endStatus > 0)
            {
                break;
            }
            // game.PrintBoard();
            turn++;
        }

        // game.PrintBoard();

        if (turn >= turnLimit)
        {
            // Debug.Log("Game tied: turn limit exceeded.");
        }
        else
        {
            switch (game.endStatus)
            {
            case 1:
                // Debug.Log("Player 1 wins (in "+turn+" turns)");
                return(1);

                break;

            case 2:
                // Debug.Log("Player 2 wins (in "+turn+" turns)");
                return(2);

                break;

            case 3:
                // Debug.Log("Game tied (in "+turn+" turns)");
                return(3);

                break;
            }
        }
        return(0);
    }