Reputation: 23
I need some help. I want to create one tournament. Let's say I have 6 players. 1 2 3 4 5 6
I want to create some.. lets' say stages... Every player will play 5 matches(number of players - 1), in 5 different stages. In one stage, all the players must appear only once.
For example, with 6 players I want to generate these results:
Squad 1:
1-2
3-4
5-6
Squad 2:
1-3
2-5
4-6
Squad 3:
1-4
2-6
3-5
Squad 4:
1-5
2-4
3-6
Squad 5:
1-6
2-3
4-5
So, in every stage, the matches must be unique, and every player must play with every player.
I want one algorithm that will work even if I want 8 players, or 12, or 16, or 28.
Thanks
Upvotes: 1
Views: 1964
Reputation: 287825
<?php
$numplayers = 6;
if ($numplayers % 2 != 0) $numplayers++; // Dummy
for ($round = 0;$round < $numplayers - 1;$round++) {
echo 'Squad ' . ($round+1) . ":\n\n1-";
for ($i = 0;$i < $numplayers-1;$i++) {
if ($i % 2 == 0) {
$player = ($numplayers-2) - ($i/2) - $round;
} else {
$player = ((($i-1)/2) - $round);
}
if ($player < 0) $player += $numplayers - 1;
echo ($player+2);
echo ($i % 2 == 0) ? "\n" : '-';
}
echo "\n\n";
}
Upvotes: 3