areyesram

Untitled

May 6th, 2020
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace RandomTeamGenerator
  6. {
  7.     internal static class Program
  8.     {
  9.         private static void Main()
  10.         {
  11.             Console.WriteLine("Random Team Generator\n");
  12.  
  13.             var numPlayers = ReadNumber("How many players?");
  14.             var numTeams = ReadNumber("How many teams? ");
  15.  
  16.             Console.WriteLine("Player Names:");
  17.             var playerNames = new List<string>();
  18.             for (var i = 0; i < numPlayers; i++)
  19.                 playerNames.Add(Console.ReadLine());
  20.  
  21.             do
  22.             {
  23.                 DisplayTeams(playerNames, numTeams);
  24.                 Console.WriteLine();
  25.                 Console.WriteLine("\nRerun? y/n");
  26.             } while (Console.ReadLine() == "y");
  27.         }
  28.  
  29.         private static int ReadNumber(string prompt)
  30.         {
  31.             Console.WriteLine(prompt);
  32.             int number;
  33.             string input;
  34.             do
  35.             {
  36.                 input = Console.ReadLine();
  37.             } while (!int.TryParse(input, out number));
  38.             return number;
  39.         }
  40.  
  41.         /// <summary>
  42.         /// Generate and display teams with the given players
  43.         /// </summary>
  44.         /// <param name="players"></param>
  45.         /// <param name="teams"></param>
  46.         private static void DisplayTeams(List<string> players, int teams)
  47.         {
  48.             var r = new Random();
  49.             var shuffled = players.OrderBy(o => r.Next()).ToList();
  50.             var perTeam = (int)Math.Ceiling((double)players.Count / teams);
  51.             for (var team = 0; team < teams; team++)
  52.             {
  53.                 var teamPlayers = shuffled.Skip(team * perTeam).Take(perTeam);
  54.                 Console.WriteLine("Team #" + (team + 1) + ": " + string.Join(", ", teamPlayers));
  55.             }
  56.         }
  57.     }
  58. }
Add Comment
Please, Sign In to add comment