Example #1
0
        private static void DoHandicapForPlayer(string playerName, int year, ScoreResult week0Score, bool isRookie, IEnumerable <ScoreResult> scores)
        {
            Console.WriteLine(playerName + " " + year);

            LinkedList <ScoreResult> copiedScores = new LinkedList <ScoreResult>(scores);

            int numberOfWeekZeroesToAdd = 1;

            if (!isRookie)
            {
                numberOfWeekZeroesToAdd = copiedScores.Count == 4 ? 1 : 4 - copiedScores.Count;
            }

            for (int i = 0; i < numberOfWeekZeroesToAdd; i++)
            {
                copiedScores.AddLast(new ScoreResult(week0Score.Score, 36, 0));
            }

            if (copiedScores.Count >= 5)
            {
                var lastFiveScores = copiedScores.Skip(copiedScores.Count - 5);

                int max         = 0,
                    handicapSum = 0,
                    weekWithMax = 0;

                LinkedList <int> weeksUsed = new LinkedList <int>();

                foreach (var score in lastFiveScores)
                {
                    var handicapSplit = Math.Min(score.Score - score.Par, 20);

                    if (handicapSplit > max)
                    {
                        max         = handicapSplit;
                        weekWithMax = score.Week;
                    }

                    weeksUsed.AddLast(score.Week);
                    handicapSum += handicapSplit;
                }

                weeksUsed.Remove(weekWithMax);

                Console.WriteLine(string.Join(",", weeksUsed));
                Console.WriteLine(CalcHandicapFromScores(handicapSum - max, 4));
            }
            else
            {
                LinkedList <int> weeksUsed = new LinkedList <int>();

                int sum = 0;

                foreach (var score in copiedScores)
                {
                    weeksUsed.AddLast(score.Week);
                    sum += Math.Min(score.Score - score.Par, 20);
                }

                Console.WriteLine(string.Join(",", weeksUsed));
                Console.WriteLine(CalcHandicapFromScores(sum, copiedScores.Count));
            }
        }
        private static HandicapResult HandicapsForResults(IEnumerable <Result> results, int week0Score, bool isRookie)
        {
            LinkedList <ScoreResult> copiedScores = new LinkedList <ScoreResult>(results.Select(x => new ScoreResult(x)));

            int numberOfWeekZeroesToAdd = 0;

            // nasty... look at maybe cleaning up
            if (copiedScores.Count == 4)
            {
                numberOfWeekZeroesToAdd = 1;
            }
            else if (copiedScores.Count < 4)
            {
                if (!isRookie)
                {
                    numberOfWeekZeroesToAdd = copiedScores.Count == 4 ? 1 : 4 - copiedScores.Count;
                }
                else
                {
                    numberOfWeekZeroesToAdd = 1;
                }
            }

            for (int i = 0; i < numberOfWeekZeroesToAdd; i++)
            {
                copiedScores.AddLast(ScoreResult.CreateWeek0Score(week0Score));
            }

            if (copiedScores.Count >= 5)
            {
                var lastFiveScores = copiedScores.Skip(copiedScores.Count - 5);

                int max         = 0,
                    handicapSum = 0,
                    weekWithMax = 0;

                LinkedList <int> weeksUsed = new LinkedList <int>();

                foreach (var score in lastFiveScores)
                {
                    var handicapSplit = score.ScoreOverPar;

                    if (handicapSplit > max)
                    {
                        max         = handicapSplit;
                        weekWithMax = score.Week;
                    }

                    weeksUsed.AddLast(score.Week);
                    handicapSum += handicapSplit;
                }

                weeksUsed.Remove(weekWithMax);

                return(new HandicapResult {
                    Handicap = CalcHandicapFromScores(handicapSum - max, 4), WeeksUsed = weeksUsed
                });
            }
            else
            {
                LinkedList <int> weeksUsed = new LinkedList <int>();

                int sum = 0;

                foreach (var score in copiedScores)
                {
                    weeksUsed.AddLast(score.Week);
                    sum += score.ScoreOverPar;
                }

                return(new HandicapResult {
                    Handicap = CalcHandicapFromScores(sum, copiedScores.Count), WeeksUsed = weeksUsed
                });
            }
        }