Beispiel #1
0
        static void Main(string[] args)
        {
            LotteryNumberSet set;

            PrintHeader(); // Some TUI spam

            for (;;)
            {
                switch (Console.ReadKey().KeyChar) // Wait for command
                {
                case 'g':                          // Something along the lines of "(g) generate"
                    set = LotteryNumberSet.Create();
                    IEnumerable <LotteryNumberSetPrinter.PrintInstruction> sheet;
                    sheet = new LotteryNumberSetPrinter().Format(set);

                    foreach (var instruction in sheet)
                    {
                        instruction.Execute();
                    }
                    break;

                case 'q': // Something along the lines of "(q) quit"
                    Environment.Exit(0);
                    break;

                default:
                    break;
                }
            }
            ;
        }
Beispiel #2
0
        public void SetPrinter_Prints_OnesSet()
        {
            // Setup
            LotteryNumberSetPrinter printer = new LotteryNumberSetPrinter();
            LotteryNumberSet        set     = new LotteryNumberSet
            {
                Balls = Enumerable.Range(0, LotteryNumberSet.BallCount)
                        .Select(_ => 1u)
                        .ToArray()
            };

            // Act
            var formatted = printer.Format(set);

            // Assert
            Assert.True(
                // Try to read each number from the corresponding print instruction,
                //  then check the values correspond, and the count is correct
                (from instruction in formatted
                 where instruction is LotteryNumberSetPrinter.WriteTextPrintInstruction writeText &&
                 Int32.TryParse($"{writeText.Text[writeText.Text.Count() - 1]}", out var ballNumber) &&
                 ballNumber == 1
                 select(bool?) null).Count() == set.Balls.Count()
                );
        }
Beispiel #3
0
        public IEnumerable <PrintInstruction> Format(LotteryNumberSet set)
        {
            var numbers = set.Balls
                          .OrderBy(x => x)
                          .GroupBy(num => (int)(num / 10))
                          .Select(g => (g.Key, g));

            var instructions = new List <PrintInstruction>();

            instructions.Add(new WriteTextPrintInstruction {
                Text = Prelude
            });
            instructions.Add(new NewLinePrintInstruction());
            instructions.Add(new NewLinePrintInstruction());

            bool firstNumber = true;

            foreach (var group in numbers)
            {
                if (ColourMapping.TryGetValue(group.Item1, out ConsoleColor colour))
                {
                    instructions.Add(new SetColourPrintInstruction {
                        Colour = colour
                    });
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }

                foreach (var num in group.Item2)
                {
                    instructions.Add(new WriteTextPrintInstruction {
                        Text = firstNumber ? $"{num}" : $", {num}"
                    });
                    firstNumber = false;
                }
                ;

                instructions.Add(new SetColourPrintInstruction {
                    Colour = null
                });
            }
            ;

            instructions.Add(new WriteTextPrintInstruction {
                Text = " !"
            });
            instructions.Add(new NewLinePrintInstruction());

            return(instructions.ToArray());
        }
Beispiel #4
0
        public void LotteryNumberSet_Generates_SixValidNumbers()
        {
            // Setup
            LotteryNumberSet set;

            // Act
            set = LotteryNumberSet.Create();

            // Assert
            Assert.True(set.Balls.All(num => num >= 0), "NonZero numbers should be generated"); // See also: unsigned
            Assert.True(set.Balls.All(num => num <= LotteryNumberSet.MaxNumber), "All numbers should be less than MaxNumber");
            Assert.Equal(set.Balls.Count(), LotteryNumberSet.BallCount);
        }
Beispiel #5
0
        public void LotteryNumberSet_Generates_OrderedNumbers()
        {
            // Setup
            LotteryNumberSet set;

            // Act
            set = LotteryNumberSet.Create();

            // Assert
            foreach (var index in Enumerable.Range(1, set.Balls.Count()))
            {
                Assert.True(set.Balls[index - 1] < set.Balls[index], "Balls should be ordered");
            }
        }
Beispiel #6
0
        public void LotteryNumberSet_Generates_UniqueNumbers()
        {
            // Setup
            LotteryNumberSet set;

            // Act
            set = LotteryNumberSet.Create();

            // Assert
            foreach (var index in Enumerable.Range(1, LotteryNumberSet.BallCount))
            {
                // Balls are a monotonically increasing set
                //  That means pairwise uniqueness inductively guarantees total uniqueness
                Assert.True(set.Balls[index - 1] != set.Balls[index], "Balls should be unique");
            }
        }