title: A small lottery game project and challenges I have. C #
body: <p>EDIT: All challenges completed. Final Code added.</p>
  <p>While I wait for my programming education to begin (.NET / C #), I practice myself in the meantime. I'm still a Padawan coder.</p>
  <p>Right now I am coding a small lottery game that should work something like this:</p>
  <ul>
  <li>Enter name and 7 numbers between 1-35.</li>
  <li>Once you have entered all the players and their numbers, just press ENTER to continue.</li>
  <li>Generate the correct lottery line (11 numbers in total)</li>
  <li>Data are sorted in numerical order.</li>
  <li>Data is corrected.</li>
  <li>Data is presented as the example below:</li>
  </ul>
  <pre><code>Correct lottery line: 7,8,9,10,11,12,15 
  Additional numbers: 21,22,23,24
  James: 1,4,6,7,8,20,21      2 correct, 1 additional number    
  Jane: 1,6,8,12,14,15,35     3 correct, zero additional number*
  </code></pre>
  <ul>
  <li>Data is saved in a file on the hard disk</li>
  </ul>
  <p>Complete code added below:</p>
  <pre><code>    internal class Program
      {
          static List&lt;Player&gt; listOfPlayers = new List&lt;Player&gt;();
          static List&lt;int&gt; lotto35 = new List&lt;int&gt;();
          static List&lt;int&gt; lotto11 = new List&lt;int&gt;();
          static List&lt;int&gt; lotto7 = new List&lt;int&gt;();
          static List&lt;int&gt; lotto4 = new List&lt;int&gt;();
          static Random rnd = new Random();
          static string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + &quot;\\lottorad.txt&quot;;
  
          static void Header()
          {
              Console.WriteLine(&quot;\n*****************************************************************&quot; +
              &quot;\n*                                                               *&quot; +
              &quot;\n*\t\tWelcome to The Little Lottery Game\t\t*&quot; +
              &quot;\n*                                                               *&quot; +
              &quot;\n*****************************************************************&quot;);
          }
          static void LottoNumbers()
          {
              // Generate numbers 1-35 and add them to lotto35:
              for (int i = 1; i &lt;= 35; i++)
                  lotto35.Add(i); 
  
              // Generate 11 random numbers from lotto35. No duplicates allowed!:
              for (int i = 0; i &lt; 11; i++)
              {
                  // Gets a random number from lotto35 and stores it in &quot;index&quot;:
                  int index = rnd.Next(lotto35.Count);
                  //Console.Write(lottoNumbers[index] + &quot;, &quot;);
                  
                  lotto11.Add(lotto35[index]);
  
                  // Removes the last number retrieved from lotto35 to prevent duplicates:
                  lotto35.RemoveAt(index);
              }
              //lotto11.Sort();
  
              //Console.ReadKey();
          }
  
          static void RunGame()
          {
              //Console.WriteLine(string.Join(&quot;, &quot;, lotto11.GetRange(0, 11)));
  
              Console.Write(&quot;\nType in your Name or press ENTER to continue.&quot; +
                      &quot;\nName: &quot;);
              // Wait for name input:
              string nameInput = Console.ReadLine();
  
              if (!string.IsNullOrEmpty(nameInput))
              {
                  int[] numberInput = new int[7];
                  for (int i = 0; i &lt; 7; i++)
                  {
                      // Use this bool to keep looping until user input correct number:
                      bool moveon = false;
                      do
                      {
                          Console.Write($&quot;Type in lotto nr {i + 1}: &quot;);
                          string input = Console.ReadLine();
  
                          // First check if input is a number, next check that input is between 1 and 35.
                          // The function TryParse takes the string input and tries to parse it as an int.
                          // If it's parsed, it's returned as a new int 'useinput':
  
                          if (int.TryParse(input, out int useinput) &amp;&amp; useinput &gt;= 1 &amp;&amp; useinput &lt;= 35 &amp;&amp; (!numberInput.Contains(useinput)))
                          {
                              // Add number to int[]:
                              numberInput[i] = useinput;
                              // Allow user to skip loop and enter new number.
                              // 'Continue' breaks directly out of loop:
                              moveon = true; continue;
                          }
                          else { Console.WriteLine($&quot;Error: Only numbers between 1 and 35 and no duplicate:&quot;); }
                      }
                      // Keep looping until moveon = true:
                      while (moveon == false);
                  }
  
                  // Sort every input in numberInput ascending:
                  Array.Sort(numberInput);
  
                  // Add name and sorted numbers to list of players:
                  listOfPlayers.Add(new Player(nameInput, numberInput));
              }
  
              else if (string.IsNullOrEmpty(nameInput))
              {
                  // Add numbers to lotto7 and lotto4 and sort them
                  lotto7.AddRange(lotto11.GetRange(0, 7));
                  lotto7.Sort();
                  lotto4.AddRange(lotto11.GetRange(7, 4));
                  lotto4.Sort();
                  lotto11.Sort();
  
                  Console.Clear();
                  Header();
                  
                  // Output the result
                  Console.ForegroundColor = ConsoleColor.Green;
                  Console.WriteLine(&quot;\nLottery numbers: &quot; + string.Join(&quot;, &quot;, lotto7));
                  Console.WriteLine(&quot;Additional numbers: &quot; + string.Join(&quot;, &quot;, lotto4));
  
                  // List players and their lottery numbers:
                  foreach (Player player in listOfPlayers)
                  {
                      Console.WriteLine();
                      Console.ForegroundColor = ConsoleColor.Gray;
                      Console.WriteLine(&quot;{0} {1}&quot;, player.Name + &quot;, Your numbers: &quot;, string.Join(&quot;, &quot;, player.Numbers));
  
                      // Save result in lottorad.txt
                      using (StreamWriter outputFile = new StreamWriter(path, true))
                      {
                          outputFile.WriteLine(DateTime.Now);
                          outputFile.WriteLine(&quot;{0} {1}&quot;, player.Name + &quot;, Your numbers: &quot;, string.Join(&quot;, &quot;, player.Numbers));
                      }
  
                      // Check how many correct numbers each player have
                      int count1 = 0;
                      int count2 = 0;
                      for (int i = 0; i &lt; player.Numbers.Length; i++)
                      {
                          if (lotto7.Contains(player.Numbers[i]))
                              count1++;
                          if (lotto4.Contains(player.Numbers[i]))
                              count2++;
                      }
  
                      Console.Write(&quot;You have &quot;);
                      Console.ForegroundColor = ConsoleColor.Green;
                      Console.Write(count1);
                      Console.ForegroundColor = ConsoleColor.Gray;
                      Console.Write(&quot; correct lottery numbers plus &quot;);
                      Console.ForegroundColor = ConsoleColor.Green;
                      Console.Write(count2);
                      Console.ForegroundColor = ConsoleColor.Gray;
                      Console.Write(&quot; additional numbers.&quot;);
                      Console.WriteLine();
  
                  }
                  // Ask player to quit or play again
                  bool moveon = false;
                  do
                  {
                      Console.ResetColor();
                      Console.Write(&quot;\nTo quit press Q : To play again press ENTER : &quot;);
                      string letter = Console.ReadLine();
                      letter = letter.ToUpper();
                      
                      if ( letter == &quot;Q&quot;)
                      {
                          Console.WriteLine(&quot;Thanks for playing!&quot;);
                          Environment.Exit(0);
                      }
                      else
                      {
                          // Clear all data before new game starts
                          Console.Clear();
                          Console.ResetColor();
                          listOfPlayers.Clear();
                          lotto35.Clear();
                          lotto11.Clear();
                          lotto7.Clear();
                          lotto4.Clear();
                          Header();
                          moveon = true; continue;
                      }
                      
                  }
                  while (moveon == false);
                  
              }
  
              // Start over again.
              LottoNumbers();
              RunGame();
          }
  
          static void Main(string[] args)
          {
  
              Header();
              LottoNumbers();
              RunGame();
          }
  
          public class Player
          {
              public string Name { get; set; }
              public int[] Numbers { get; set; }
              public Player(string name, int[] numbers)
              {
                  Name = name;
                  Numbers = numbers;
              }
          }
      }
  }
  </code></pre>