private void InitializeSimulationParamaters(Match match, BallPositionEnum ballPosition, int attempts, bool shootAttempt)
 {
     _match        = match;
     _attempts     = attempts;
     _ballPosition = ballPosition;
     _shootAttempt = shootAttempt;
 }
        public BallPositionEnum InvertBallPosition(BallPositionEnum ballPosition)
        {
            BallPositionEnum defenseBallPosition = 0;

            // if ballPosition not mid use inverted position for defense
            if (ballPosition != BallPositionEnum.Mid)
            {
                defenseBallPosition = ballPosition == BallPositionEnum.Front ? BallPositionEnum.Back : BallPositionEnum.Front;
            }
            else
            {
                defenseBallPosition = ballPosition;
            }

            return(defenseBallPosition);
        }
        public int CalculateDefendingPoinstAtBallPosition(Team team, BallPositionEnum ballPosition, bool includeGoalKeeper)
        {
            BallPositionEnum defenseBallPosition = InvertBallPosition(ballPosition);

            var playersAtBallPosition = GetPlayersAtPosition(team, defenseBallPosition);
            var totalPlayerScore      = playersAtBallPosition.Select(x => x.Defense).ToList().Sum();

            // Include goalkeeper score when attacking team tries to shoot
            if (includeGoalKeeper)
            {
                var goalKeeper = team.Players.Where(x => x.Posistion == PlayerPosistionEnum.GoalKeeper).FirstOrDefault();
                totalPlayerScore += goalKeeper == null ? 0 : goalKeeper.Defense;
            }

            return(totalPlayerScore);
        }
        public Match RunSimulation(Match match, BallPositionEnum ballPosition = BallPositionEnum.Mid, int attempts = 42, bool shootAttempt = false)
        {
            InitializeSimulationParamaters(match, ballPosition, attempts, shootAttempt);

            while (_attempts > 0)
            {
                var attackingTeam = _match.FirstTeam.IsAttacking ? _match.FirstTeam : _match.SecondTeam;
                var defendingTeam = !_match.FirstTeam.IsAttacking ? _match.FirstTeam : _match.SecondTeam;

                // Get playerScore at ballposition
                var attackingPlayerPoints = CalculateAttackingPoinstAtBallPosition(attackingTeam, _ballPosition);
                // When _shootAttempt is true defendingPlayerPoints = playersPoint + goalKeerpPoints
                var defendingPlayerPoints = CalculateDefendingPoinstAtBallPosition(defendingTeam, _ballPosition, _shootAttempt);

                // differenceBonusPlayerPoints is a banus for the players with higher playerPoint at the position
                var differenceBonusPlayerPoints = attackingPlayerPoints - defendingPlayerPoints;

                // attackingPlayerPoints and defendingPlayerPoints are used to generate a number
                var attackPoints  = GenerateNumber(attackingPlayerPoints, differenceBonusPlayerPoints > 0 ? differenceBonusPlayerPoints : 0);
                var defensePoints = GenerateNumber(defendingPlayerPoints, differenceBonusPlayerPoints < 0 ? -differenceBonusPlayerPoints : 0);

                if (_shootAttempt)
                {
                    // if attack is higher then defense attakcing team scores
                    if (attackPoints > defensePoints)
                    {
                        if (_match.FirstTeam.IsAttacking)
                        {
                            _match.ScoreFirstTeam += 1;
                        }
                        else
                        {
                            _match.ScoreSecondTeam += 1;
                        }
                    }
                    // When attacking team does not score, switch defending team to attack and adjust parameters
                    PrepareAttackingAttempt();
                    continue;
                }

                if (attackPoints > defensePoints)
                {
                    // When already at the front the next Attempt will be a shooting attempt
                    if (_ballPosition == BallPositionEnum.Front)
                    {
                        PrepareShootAttempt();
                    }
                    else
                    {
                        PrepareMoveForwardAttempt();
                    }
                    continue;
                }

                // When defending team wins the attempt, switch defending team to attack and adjust parameters
                PrepareAttackingAttempt();
            }

            var processedMatchResults = ProcesMatchResult(_match);

            return(processedMatchResults);
        }
 // should return the combined number of all players at the sameposition of the ball
 public List <Player> GetPlayersAtPosition(Team team, BallPositionEnum ballPosition)
 {
     return(team.Players.Where(x => (int)x.Posistion == ((int)ballPosition)).ToList());
 }
        public int CalculateAttackingPoinstAtBallPosition(Team team, BallPositionEnum ballPosition)
        {
            var playersAtBallPosition = GetPlayersAtPosition(team, ballPosition);

            return(playersAtBallPosition.Select(x => x.Attack).ToList().Sum());
        }
 public void ChangeState(BallPositionEnum ballState)
 {
     _ballStateMain = ballState;
 }