mirror of
https://github.com/onyx-and-iris/Lottery.git
synced 2024-11-21 21:50:48 +00:00
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.
This commit is contained in:
parent
1b4e0baebb
commit
5b7f06f8b7
@ -1,56 +1,97 @@
|
||||
namespace Lottery
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
class UKGenerator : IGenerator
|
||||
{
|
||||
public int Count { get; } = 6;
|
||||
record Limits(int Count, int Lower, int Upper);
|
||||
|
||||
public List<int> Generate()
|
||||
/// <summary>
|
||||
/// Abstract base class for generators
|
||||
/// It implements the IGenerator interface
|
||||
/// Provides some default implementation
|
||||
/// </summary>
|
||||
class Generator : IGenerator
|
||||
{
|
||||
public virtual Numbers Generate() => throw new NotImplementedException();
|
||||
|
||||
protected static void FillNumbers(List<int> NumberList, Limits Limits)
|
||||
{
|
||||
List<int> numbers = [];
|
||||
for (int i = 0; i < Count; i++)
|
||||
List<int> 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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// Concrete UKGenerator class.
|
||||
/// Generates six balls from 1 to 59
|
||||
/// </summary>
|
||||
class EuroGenerator : IGenerator
|
||||
class UKGenerator : Generator
|
||||
{
|
||||
public int Count { get; } = 8;
|
||||
private readonly Limits Limits = new(6, 1, 59);
|
||||
|
||||
public List<int> Generate()
|
||||
public override Numbers Generate()
|
||||
{
|
||||
List<int> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// Concrete EuroGenerator class.
|
||||
/// Generates five balls from 1 to 50 and two balls from 1 to 12
|
||||
/// </summary>
|
||||
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<int> Generate()
|
||||
public override Numbers Generate()
|
||||
{
|
||||
List<int> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete SetForLifeGenerator class.
|
||||
/// Generates five balls from 1 to 47 and one ball from 1 to 10
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concrete ThunderBallGenerator class.
|
||||
/// Generates fives balls from 1 to 39 and one ball from 1 to 14
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user