I have a problem. Think of a football league and there are 16 teams in this football league.
$teams = array(
"Team1", "Team2", "Team3", "Team4",
"Team5", "Team6", "Team7", "Team8",
"Team9", "Team10", "Team11", " Team12",
"Team13", "Team14", "Team15", "Team16"
);
Each team can only play one match against each other and each team can only play one match per week.
With 8 matches per week, this makes 120 matches in total.
Some of these matches have been played before.
The matches of the 1st week and the 2nd week are as follows:
1 week
Team14 vs Team9,
Team1 vs Team10,
Team13 vs Team3,
Team11 vs Team5,
Team2 vs Team6,
Team4 vs Team15,
Team16 vs Team12,
Team7 vs Team8
2 weeks
Team6 vs Team10,
Team13 vs Team2,
Team9 vs Team8,
Team14 vs Team1,
Team3 vs Team4,
Team15 vs Team7,
Team16 vs Team5,
Team11 vs Team12
How can I find the remaining matches of the fixture so that the matches that have been played or will be played do not repeat?
Even though I tried many times with PHP, I could not succeed.
2
Answers
In your case, I would start by creating a table with all the possible occurences.
I would create a unique key for each match, e.g. Team1_Team2 for Team1 vs Team2 and so on.
Using a key can be easier to compare afterwards if a match has already been played.
To create the occurrences, we use a nested loop.
The inner loop starts at 1 because you can’t play the same team twice.
Note: An error has crept in on Team12 – there’s one space too many.
We have all the occurrences, each key always has the team with the lowest number to the left of the "_".
For each match already played, we now need to create the key corresponding to the match and compare it with our occurences.
As you can see in your example matches: Team14 vs Team9.
The weakest team is not necessarily on the left.
We therefore need to sort before creating the key:
The 2 matches have been deleted and $occurences now only has 118 matches.
I hope I’ve been able to help you.
there can be more optimal solutions for this problem but here is a approach i think you can use to solve this problem
Generate the Complete Round-Robin Schedule where each team plays against every other team exactly once we can use Rotation MEchanism that will make sure that every team plays against every other team.
Remove Played Matches
Output the Remaining Schedule
you can do something like this