- Edited
This is the simplest yet most specific solution I could come up with, will see if I can generalize it more C#
[Edit]Edited to make less bulky and more organized[/Edit]
[Edit]This code now can be switched to more dimensions, I tried 14 players split into 7 player teams and it worked nicely[/Edit]:
[Edit]Edited to make less bulky and more organized[/Edit]
[Edit]This code now can be switched to more dimensions, I tried 14 players split into 7 player teams and it worked nicely[/Edit]:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace SplitTeams
{
class Program
{
class Player
{
public string Name { get; set; }
public int Rating { get; set; }
}
const int PLAYER_COUNT = 10;
static void Split(Player[] original, Player[] team1, Player[] team2)
{
int[, , , ,] matrix = new int[PLAYER_COUNT, PLAYER_COUNT, PLAYER_COUNT, PLAYER_COUNT, PLAYER_COUNT];
int length = PLAYER_COUNT;
int[] matches = new int[PLAYER_COUNT / 2];
for (int i = 0; i < matches.Length; i++)
matches[i] = i;
float midPoint = ((float)original.Sum(x => x.Rating)) / 2;
for (int i = 0; i < length; i++)
{
for (int j = i + 1; j < length; j++)
for (int k = j + 1; k < length; k++)
for (int l = k + 1; l < length; l++)
for (int m = l + 1; m < length; m++)
{
if(m != l && m != l && m != k && m != j && l != k && l != j && l != i && k != j && k != i && j != i)//avoid duplicate
{
matrix[i, j, k, l, m] = original[i].Rating + original[j].Rating + original[k].Rating + original[l].Rating + original[m].Rating;
if (Math.Abs(matrix[matches[0], matches[1], matches[2], matches[3], matches[4]] - midPoint) > Math.Abs(matrix[i, j, k, l, m] - midPoint))
{
matches[0] = i;
matches[1] = j;
matches[2] = k;
matches[3] = l;
matches[4] = m;
}
}
}
}
int team1Counter = 0, team2Counter = 0;
for (int i = 0; i < original.Length; i++)
{
if (matches.Contains(i))
team1[team1Counter++] = original[i];
else
team2[team2Counter++] = original[i];
}
}
static void Main(string[] args)
{
Player[] players = new Player[]
{
new Player{Name="A", Rating=1567},
new Player{Name="B", Rating=1696},
new Player{Name="C", Rating=1697},
new Player{Name="D", Rating=1653},
new Player{Name="E", Rating=1813},
new Player{Name="F", Rating=1664},
new Player{Name="G", Rating=1664},
new Player{Name="H", Rating=1620},
new Player{Name="I", Rating=1665},
new Player{Name="J", Rating=1747},
};
Player[] team1 = new Player[PLAYER_COUNT / 2];
Player[] team2 = new Player[PLAYER_COUNT / 2];
Split(players, team1, team2);
float sum1 = team1.Sum(x => x.Rating);
float sum2 = team2.Sum(x => x.Rating);
float perc1 = (sum1 * 100) / (sum1 + sum2);
float perc2 = (sum2 * 100) / (sum1 + sum2);
Console.WriteLine(string.Format("Team 1: ({0:f2}%)", perc1));
foreach (Player p in team1)
Console.WriteLine(string.Format("Name: {0}, Rating: {1}", p.Name, p.Rating));
Console.WriteLine(string.Format("Team 2: ({0:f2}%)", perc2));
foreach (Player p in team2)
Console.WriteLine(string.Format("Name: {0}, Rating: {1}", p.Name, p.Rating));
Console.Read();
}
}
}