Exemplo n.º 1
0
    private IEnumerator MovePieceEvent()
    {
        AIMinMaxResult aiResult = null;
        Move           nextMove = null;

        yield return(new WaitForSeconds(1f));

        //render the gameboard to the 2d representation
        uiController.RenderBoard(gameBoard);

        while (true)
        {
            foreach (Player p in players)
            {
                currentPlayer             = p;
                currentPlayer.playerState = Player.PlayerState.Thinking;

                StartCoroutine(uiController.FadeText(uiController.PlayerTurnText, "Go " + currentPlayer.playerNumber.ToString() + "!", currentPlayer.PieceTint));

                if (currentPlayer.playerType == Player.PlayerType.Human)
                {
                    while (currentPlayer.playerState == Player.PlayerState.Thinking)
                    {
                        yield return(new WaitForSeconds(MoveTime));

                        if (currentPlayer.playerType == Player.PlayerType.Computer)
                        {
                            break;
                        }
                    }

                    if (currentPlayer.playerType == Player.PlayerType.Human)
                    {
                        nextMove = currentPlayer.selectedMove;
                    }
                }

                if (currentPlayer.playerType == Player.PlayerType.Computer)
                {
                    //start thinking asyncronously
                    gameBoard.AIMinMaxSearchAsyncBegin(currentPlayer.AIThoughtDepth + 1, currentPlayer.playerNumber);

                    while (AIMinMax.jobStatus == AIMinMaxJobStatus.Started || AIMinMax.jobStatus == AIMinMaxJobStatus.StopRequested)
                    {
                        yield return(new WaitForSeconds(MoveTime));

                        if (AIMinMax.jobStatus == AIMinMaxJobStatus.Started && AIMinMax.StatesSearched > MaxStatesToSearch)
                        {
                            gameBoard.AiMinMaxSearchAsyncStopRequest();
                        }
                    }
                    //finish thinking and get the move
                    aiResult   = gameBoard.AIMinMaxSearchAsyncEnd();
                    lastResult = aiResult;
                    nextMove   = aiResult.Move;
                    gameBoard.GetPiece(nextMove.piece).ZoomToPiece();
                    yield return(new WaitForSeconds(1f));

                    currentPlayer.selectedMove = aiResult.Move;
                    currentPlayer.playerState  = Player.PlayerState.Moving;
                }

                //this is the best place to stop if the game should be paused
                while (Paused)
                {
                    yield return(new WaitForSeconds(MoveTime));
                }

                Piece movedPiece = gameBoard.GetPiece(nextMove.piece);
                gameBoard.Move(nextMove);
                movedPiece.ZoomToPiece(true);
                //render the gameboard to the 2d representation
                uiController.RenderBoard(gameBoard);

                yield return(new WaitForSeconds(2f));


                currentPlayer.playerState = Player.PlayerState.Waiting;

                if (CheckGameOver())
                {
                    yield break;
                }
            }
        }
    }