Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace RandomTeamGenerator
- {
- internal static class Program
- {
- private static void Main()
- {
- Console.WriteLine("Random Team Generator\n");
- var numPlayers = ReadNumber("How many players?");
- var numTeams = ReadNumber("How many teams? ");
- Console.WriteLine("Player Names:");
- var playerNames = new List<string>();
- for (var i = 0; i < numPlayers; i++)
- playerNames.Add(Console.ReadLine());
- do
- {
- DisplayTeams(playerNames, numTeams);
- Console.WriteLine();
- Console.WriteLine("\nRerun? y/n");
- } while (Console.ReadLine() == "y");
- }
- private static int ReadNumber(string prompt)
- {
- Console.WriteLine(prompt);
- int number;
- string input;
- do
- {
- input = Console.ReadLine();
- } while (!int.TryParse(input, out number));
- return number;
- }
- /// <summary>
- /// Generate and display teams with the given players
- /// </summary>
- /// <param name="players"></param>
- /// <param name="teams"></param>
- private static void DisplayTeams(List<string> players, int teams)
- {
- var r = new Random();
- var shuffled = players.OrderBy(o => r.Next()).ToList();
- var perTeam = (int)Math.Ceiling((double)players.Count / teams);
- for (var team = 0; team < teams; team++)
- {
- var teamPlayers = shuffled.Skip(team * perTeam).Take(perTeam);
- Console.WriteLine("Team #" + (team + 1) + ": " + string.Join(", ", teamPlayers));
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment