Randomizer

 avatar
user_5771351
csharp
2 months ago
4.0 kB
7
Indexable
Never
Console.OutputEncoding = System.Text.Encoding.UTF8;

string[] Names = new string[]
{
            "Name1",
            "Name2",
            "Name3",
            "Name4",
            "Name5",
            "Name6",
            "Name7",
            "Name8",
            "Name9",
            "Name10",
            "Name11",
 };


// расчет количества команд
int teamsAmount = CalculateTeamsAmount();

Console.WriteLine("Добро пожаловать в Рандомайзер!");
Console.WriteLine("0. Список участников");
Console.WriteLine("============================");
Console.WriteLine("Выберите способ генерации: ");
Console.WriteLine("1. По порядку");
Console.WriteLine("2. По случайности");

Console.Write("Выбор: ");
int inputPoint = Convert.ToInt32(Console.ReadLine());

switch (inputPoint)
{
    case 0:
        ShowParticipantList();
        break;
    case 1:
        GenerateTeamsByOrder(teamsAmount);
        break;
    case 2:
        GenerateTeamsByRandom(teamsAmount);
        break;
    default:
        break;
}



void GenerateTeamsByOrder(int teamsAmount)
{
    string[][] teams = new string[teamsAmount][];

    int j = 0;

    for (int i = 0; i < teams.Length; i++, j += 2)
    {
        if ((j + 1) < Names.Length) // 11 < 10
        {
            teams[i] = new string[] { Names[j], Names[j + 1] };
        }
        else
        {
            teams[i] = new string[] { Names[j] };
        }
    };

    for (int i = 0; i < teams.Length; i++)
    {
        foreach (var player in teams[i])
        {
            Console.WriteLine($"Команда {i}: {player}");
        }

        Console.WriteLine();
    }
}

void GenerateTeamsByRandom(int teamAmount)
{
    Random random = new Random();

    // Shuffle the Names array using Fisher-Yates algorithm
    for (int i = Names.Length - 1; i > 0; i--)
    {
        int j = random.Next(0, i + 1);
        string temp = Names[i];
        Names[i] = Names[j];
        Names[j] = temp;
    }

    // Create teams based on the shuffled Names array
    string[][] teams = new string[teamsAmount][];
    int currentIndex = 0;

    for (int i = 0; i < teams.Length; i++)
    {
        int teamSize = (i < teamsAmount - 1) ? 2 : 1;
        teams[i] = new string[teamSize];

        for (int j = 0; j < teamSize; j++)
        {
            teams[i][j] = Names[currentIndex++];
        }
    }

    // Print the generated teams
    for (int i = 0; i < teams.Length; i++)
    {
        foreach (var player in teams[i])
        {
            Console.WriteLine($"Команда {i}: {player}");
        }

        Console.WriteLine();
    }


}

int CalculateTeamsAmount()
{
    int teamsAmount = 0;
    if (Names.Length > 0)
    {
        teamsAmount = Names.Length / 2;
        if (Names.Length % 2 > 0)
        {
            teamsAmount += 1;
            Console.WriteLine("количество команд: " + teamsAmount);
        }
        else
        {
            Console.WriteLine("количество команд: " + teamsAmount);
        }

    }
    else
    {
        Console.WriteLine("No players");
    }

    return teamsAmount;
}

void GetRandomNames()
{
    Random random = new Random();

    string first = Names[random.Next(Names.Length)];
    Console.WriteLine(first);

    while (true)
    {
        string second = Names[random.Next(Names.Length)];

        if (first != second)
        {
            Console.WriteLine(second);
            break;
        }
        else
        {
            continue;
        }
    }
}

void ShowParticipantList()
{
    Console.WriteLine("Список участников:");
    Console.WriteLine("===================");

    foreach (var name in Names)
    {
        Console.WriteLine(name);
    }
}
Leave a Comment