Esempio n. 1
0
        private async Task <GameState> MakeTurnAsync()
        {
            _tokenSource = new CancellationTokenSource();

            var(Player, Side) = _playersControl.GetNextPlayer();

            // Get turn
            var newTurn = await Task.Run(() => Player.MakeTurn(_modelController.Field, _tokenSource.Token))
                          .ConfigureAwait(continueOnCapturedContext: false);

            Logger.Info(newTurn);

            // Check if game stoped
            if (FinalGameState != null)
            {
                return(FinalGameState.Value);
            }

            // Check if returned value the reason of history rolling
            if (!_isHistoryRolling)
            {
                // Check turn
                var success = TryUpdateField(newTurn);

                // Check end of game
                if (!success || GameRules.HasPlayerWon(_modelController.Field, Side))
                {
                    FinishGame(success ? Side : Side.ToOpposite());
                    return(FinalGameState.Value);
                }
            }

            _isHistoryRolling = false;

            return(new GameState(_modelController.Field, StateType.Turn, Side));
        }