Esempio n. 1
0
    public void Transition(ITrackIndividualGameState state)
    {
        var player1Shuffled = ShuffleForPlayerIfNecessary(state.FirstPlayerState, 1);
        var player2Shuffled = ShuffleForPlayerIfNecessary(state.SecondPlayerState, 2);

        if (player1Shuffled || player2Shuffled)
        {
            state.Tick(Constants.GamePlayParameters.TimeToShuffle);
        }

        if (_logger.IsEnabled(LogLevel.Trace))
        {
            _logger.LogTrace($"After transition, player 1 has {state.FirstPlayerState.PlayPile.Cards.Count} card(s) in their play pile");
            _logger.LogTrace($"After transition, player 1 has {state.FirstPlayerState.GatherPile.Cards.Count} card(s) in their gather pile");
            _logger.LogTrace($"After transition, player 1 has {state.FirstPlayerState.PlayedCards.Cards.Count} card(s) in their played cards pile");

            _logger.LogTrace($"After transition, player 2 has {state.SecondPlayerState.PlayPile.Cards.Count} card(s) in their play pile");
            _logger.LogTrace($"After transition, player 2 has {state.SecondPlayerState.GatherPile.Cards.Count} card(s) in their gather pile");
            _logger.LogTrace($"After transition, player 2 has {state.SecondPlayerState.PlayedCards.Cards.Count} card(s) in their played cards pile");
        }
        _logger.LogDebug($"Total time taken so far is {state.TimeElapsed.TotalSeconds} seconds");

        if (state.TimedOut)
        {
            _logger.LogDebug($"Game over after {state.TimeElapsed.TotalSeconds} seconds because max mins is {state.MaxDuration.TotalMinutes}");
        }
    }
 public CoordinateGameplay(ILogger <CoordinateGameplay> logger, IPlayWar gamePlay,
                           IPointsCalculator pointsCalculator, ITrackIndividualGameState individualGameStateTracker,
                           IDateTime dateTime, ICompileStats statsCompiler)
 {
     _logger                     = logger;
     _gamePlay                   = gamePlay;
     _pointsCalculator           = pointsCalculator;
     _individualGameStateTracker = individualGameStateTracker;
     _dateTime                   = dateTime;
     _statsCompiler              = statsCompiler;
 }
Esempio n. 3
0
    private void HandlePlayerWinning(ITrackIndividualGameState state, int warDepth, int winner,
                                     Card firstCard, Card secondCard)
    {
        _logger.LogDebug($"Player {winner} wins");
        var loser       = (3 - winner);
        var winnerState = (winner == 1) ? state.FirstPlayerState : state.SecondPlayerState;
        var loserState  = (winner == 2) ? state.FirstPlayerState : state.SecondPlayerState;

        state.Tick(Constants.GamePlayParameters.TimeToGatherCards);
        if (warDepth > 0)
        {
            if (winner == 1)
            {
                state.Stats.Player1WarWinsByDepth[warDepth]++;
            }
            else
            {
                state.Stats.Player2WarWinsByDepth[warDepth]++;
            }

            foreach (var card in winnerState.PlayedCards.Cards)
            {
                _logger.LogDebug($"Player {winner} saves: {card}");
                winnerState.GatherPile.Cards.Add(card);
            }
            foreach (var card in loserState.PlayedCards.Cards)
            {
                _logger.LogDebug($"Player {loser} loses: {card}");
                winnerState.GatherPile.Cards.Add(card);
            }
            winnerState.PlayedCards.Cards.Clear();
            loserState.PlayedCards.Cards.Clear();
        }
        winnerState.GatherPile.Cards.Add(firstCard);
        winnerState.GatherPile.Cards.Add(secondCard);
    }
Esempio n. 4
0
    public void War(ITrackIndividualGameState state, int warDepth = 0)
    {
        var firstPlayerCard  = state.FirstPlayerState.PlayPile.Draw();
        var secondPlayerCard = state.SecondPlayerState.PlayPile.Draw();

        state.Tick(Constants.GamePlayParameters.TimeToPlayCard);

        _logger.LogDebug($"War: {firstPlayerCard} vs {secondPlayerCard}...");
        var warOnThisTurn = firstPlayerCard == secondPlayerCard;

        if (warOnThisTurn)
        {
            _logger.LogDebug($"War with depth {warDepth} on this turn.");
            (int firstPlayerFacedownCardsAlready, bool player1Shuffled)  = ShuffleForWarIfNecessary(state.FirstPlayerState, 1);
            (int secondPlayerFacedownCardsAlready, bool player2Shuffled) = ShuffleForWarIfNecessary(state.SecondPlayerState, 2);
            if (player1Shuffled || player2Shuffled)
            {
                state.Tick(Constants.GamePlayParameters.TimeToShuffle);
            }

            if (PlayerHasEnoughCardsForWar(state.FirstPlayerState, firstPlayerFacedownCardsAlready) &&
                PlayerHasEnoughCardsForWar(state.SecondPlayerState, secondPlayerFacedownCardsAlready))
            {
                for (int i = 0; i < Constants.NumberOfFaceDownCardsForWar - firstPlayerFacedownCardsAlready; i++)
                {
                    state.FirstPlayerState.PlayedCards.Cards.Add(state.FirstPlayerState.PlayPile.Draw());
                }
                for (int i = 0; i < Constants.NumberOfFaceDownCardsForWar - secondPlayerFacedownCardsAlready; i++)
                {
                    state.SecondPlayerState.PlayedCards.Cards.Add(state.SecondPlayerState.PlayPile.Draw());
                }

                state.FirstPlayerState.PlayedCards.Cards.Add(firstPlayerCard);
                state.SecondPlayerState.PlayedCards.Cards.Add(secondPlayerCard);
                state.Tick(Constants.GamePlayParameters.TimeToPlayCardsFaceDownForAWar);
                War(state, warDepth + 1);
            }
            else
            {
                if (PlayerHasEnoughCardsForWar(state.FirstPlayerState, firstPlayerFacedownCardsAlready) == false &&
                    PlayerHasEnoughCardsForWar(state.SecondPlayerState, secondPlayerFacedownCardsAlready) == false)
                {
                    throw new Exception("This code path is almost statistically impossible with a shuffled, full deck of cards!");
                }
                if (PlayerHasEnoughCardsForWar(state.FirstPlayerState, firstPlayerFacedownCardsAlready) == false)
                {
                    _logger.LogDebug("Player 1 does not have enough cards for war...");
                    state.FirstPlayerState.CannotContinueBecauseCantPlayEnoughCardsForWar = true;
                }
                else
                {
                    _logger.LogDebug("Player 2 does not have enough cards for war...");
                    state.SecondPlayerState.CannotContinueBecauseCantPlayEnoughCardsForWar = true;
                }
            }
        }
        else if (firstPlayerCard > secondPlayerCard)
        {
            HandlePlayerWinning(state, warDepth, winner: 1, firstPlayerCard, secondPlayerCard);
        }
        else
        {
            HandlePlayerWinning(state, warDepth, winner: 2, firstPlayerCard, secondPlayerCard);
        }
    }