Reputation: 37
I am creating a basic, quite bare-bones Football Manager game in VB.NET. The game runs on a console window. I have already set up all the foundations and now I need to schedule the 20 clubs in the tournament to play each other once and away for a total of 38 matches per club, randomly. However, I cannot for the life of me get it right. I've spent three days working on the scheduling algorithm, and am wondering if someone can help me out.
The algorithm should do this:
This is what I have done so far, which is wrong and failed to achieve my objectives:
Sub CreateFixtures()
Randomize()
Dim totalRounds = (ClubList.Count - 1)
Dim matchID As Integer = 0
Dim rnd As New Random()
For round = 1 To 38
Debug.WriteLine($"Round {round}")
Dim availableTeams = New List(Of ClubClass)(ClubList)
While availableTeams.Count > 1 ' Ensure there are at least 2 teams left
Dim homeTeamIndex = rnd.Next(0, availableTeams.Count)
Dim homeTeam = availableTeams(homeTeamIndex)
' Remove homeTeam from available teams to prevent it from playing against itself
availableTeams.RemoveAt(homeTeamIndex)
' Select a random away team that hasn't already played against the home team
Dim availableAwayTeams = availableTeams.Where(Function(team) Not homeTeam.HasPlayedAgainst(team)).ToList()
If availableAwayTeams.Count > 0 Then
Dim awayTeamIndex = rnd.Next(0, availableAwayTeams.Count)
Dim awayTeam = availableAwayTeams(awayTeamIndex)
CreateMatch(matchID, homeTeam, awayTeam, round)
availableTeams.Remove(awayTeam)
Debug.WriteLine($"{homeTeam.name} vs {awayTeam.name}")
matchID += 1
End If
End While
Next
End Sub
Upvotes: 2
Views: 426
Reputation: 3
Let the teams be 0, 1, 2,...,19
For round i (i=0,...,18), the ten matchups are (with all addition done mod 19):
After these first 19 rounds, each team will have played each team once.
Repeat the 19 rounds with home and away switched.
You can permute the rounds to make everything more random (otherwise team 19 will play 19 home games followed by 19 away games).
Upvotes: 0