From 5b7f06f8b75c2072070d19fcaf1d414f68a9b45a Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 26 Sep 2024 21:06:48 +0100 Subject: [PATCH] add base Generator class, it implements IGenerator Generate() methods call FillNumbers to get number values. Numbers are randomized, sized according to limits, unique (no duplicates) and sorted. --- Lottery/Generator.cs | 103 ++++++++++++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 31 deletions(-) diff --git a/Lottery/Generator.cs b/Lottery/Generator.cs index 89950e1..bbf0142 100644 --- a/Lottery/Generator.cs +++ b/Lottery/Generator.cs @@ -1,56 +1,97 @@ namespace Lottery { - /// - /// - /// - class UKGenerator : IGenerator - { - public int Count { get; } = 6; + record Limits(int Count, int Lower, int Upper); - public List Generate() + /// + /// Abstract base class for generators + /// It implements the IGenerator interface + /// Provides some default implementation + /// + class Generator : IGenerator + { + public virtual Numbers Generate() => throw new NotImplementedException(); + + protected static void FillNumbers(List NumberList, Limits Limits) { - List numbers = []; - for (int i = 0; i < Count; i++) + List Candidates = Enumerable.Range(Limits.Lower, Limits.Upper).ToList(); + + for (int i = 0; i < Limits.Count; i++) { - numbers.Add(Random.Shared.Next(60)); + int RandomIndex = Random.Shared.Next(Candidates.Count); + NumberList.Add(Candidates[RandomIndex]); + Candidates.RemoveAt(RandomIndex); } - return numbers; + NumberList.Sort(); } } /// - /// + /// Concrete UKGenerator class. + /// Generates six balls from 1 to 59 /// - class EuroGenerator : IGenerator + class UKGenerator : Generator { - public int Count { get; } = 8; + private readonly Limits Limits = new(6, 1, 59); - public List Generate() + public override Numbers Generate() { - List numbers = []; - for (int i = 0; i < Count; i++) - { - numbers.Add(Random.Shared.Next(60)); - } - return numbers; + Numbers Numbers = new(KindOfLotto.Uk, []); + Generator.FillNumbers(Numbers.Normal, Limits); + return Numbers; } } /// - /// + /// Concrete EuroGenerator class. + /// Generates five balls from 1 to 50 and two balls from 1 to 12 /// - class SetForLifeGenerator : IGenerator + class EuroGenerator : Generator { - public int Count { get; } = 10; + private readonly Limits NormalLimits = new(5, 1, 50); + private readonly Limits SpecialLimits = new(2, 1, 12); - public List Generate() + public override Numbers Generate() { - List numbers = []; - for (int i = 0; i < Count; i++) - { - numbers.Add(Random.Shared.Next(60)); - } - return numbers; + NumbersWithSpecial Numbers = new(KindOfLotto.Euro, [], []); + Generator.FillNumbers(Numbers.Normal, NormalLimits); + Generator.FillNumbers(Numbers.Special, SpecialLimits); + return Numbers; + } + } + + /// + /// Concrete SetForLifeGenerator class. + /// Generates five balls from 1 to 47 and one ball from 1 to 10 + /// + class SetForLifeGenerator : Generator + { + private readonly Limits NormalLimits = new(5, 1, 47); + private readonly Limits SpecialLimits = new(1, 1, 10); + + public override Numbers Generate() + { + NumbersWithSpecial Numbers = new(KindOfLotto.SetForLife, [], []); + Generator.FillNumbers(Numbers.Normal, NormalLimits); + Generator.FillNumbers(Numbers.Special, SpecialLimits); + return Numbers; + } + } + + /// + /// Concrete ThunderBallGenerator class. + /// Generates fives balls from 1 to 39 and one ball from 1 to 14 + /// + class ThunderBallGenerator : Generator + { + private readonly Limits NormalLimits = new(5, 1, 39); + private readonly Limits SpecialLimits = new(1, 1, 14); + + public override Numbers Generate() + { + NumbersWithSpecial Numbers = new(KindOfLotto.Thunderball, [], []); + Generator.FillNumbers(Numbers.Normal, NormalLimits); + Generator.FillNumbers(Numbers.Special, SpecialLimits); + return Numbers; } } }