-1

I'm making a little bot to create games, and I would like to figure out the best approach to creating 2 teams based on players ELO and make it a fair match.

Let's say I have the following:

const players = [
    {name: 'player1', elo:684},
    {name: 'player2', elo:1694},
    {name: 'player3', elo:1234},
    {name: 'player4', elo:1023},
    {name: 'player5', elo:877},
    {name: 'player6', elo:789},
    {name: 'player7', elo:1000},
    {name: 'player8', elo:1300}
];
let team1, team2 = [];

Some how I want to create 2 teams with 4 players each, with balanced elo score on both teams. Is there an algorithm that would help do this? Balance the two teams as much as possible?

Speedy059
  • 629
  • 10
  • 24
  • The question is a bit vague. How will anyone know what is an acceptable algorithm and what isn’t? Do you have an existing algorithm in mind that has a specific flaw you’d like to address? Get familiar with [how to access and process objects, arrays, or JSON](/q/11922383/4642212), and use the static and instance methods of [`Array`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Dec 18 '22 at 07:58

1 Answers1

0

You can attempt to use a greedy algorithm.

Sort your players by elo DESC

players.sort((a, b) => b.elo - a.elo);

now iterate each player in the list

pseudocode
for (player in players)
if (teamElo(team1) < teamElo(team2))
THEN push player to team1
else push player to team2



function teamElo() {
 total = 0
 loop through players on team and sum their elo
 return total
}

Jacob Webb
  • 84
  • 1
  • 8
  • This might not result in equally sized teams. – luk2302 Dec 18 '22 at 08:01
  • True I forgot to account in equal size of teams. Good catch @luk2302. I assume he could check team length and prevent it based off of that. Leaves a bit of room for it to still be possible that the teams will not be balanced on ELO properly – Jacob Webb Dec 18 '22 at 08:04