0
votes

I'm currently making a football online manager game, and I'm stuck trying to generate the matches.

This is my SQL structure:

id:     integer
home:   integer (home team)
away:   integer (away team)
date:   integer
league: integer

My leagues will contain many teams, so a team won't be able to compete against all of the other teams.

My solution so far has been to loop across all possible match-ups, and then give it a unique date (based on a certain amount of given dates). However, that can't work now that I have too many teams in the leagues.

Logic:

  • Home and away cannot contain the same id.
  • A match have a unique date if there can't be found a match which have the given date, and one of the teams competing.
  • All teams must have the same amount of matches (to make it fair for all)
  • Teams must have the same amount of home og away games.

League:

  • The amount of teams in the league is uniform.
  • The whole season is generated at once.
  • The season last 1 week and has 28 specific dates within that week, which can be used as match dates.


    I don't have any code to show because the code I have has become irrelevant (because I'm looking for a different solution). I'm not looking for complete code, just guide me the way.

    * UPDATE *

    foreach ($teams as $team_home) {
        foreach ($teams as $team_away) {
            if ($team_home === $team_away) continue;
    
            if (!isset($represented[$team_home->id]['home'])) $represented[$team_home->id]['home'] = 0;                 
            if (!isset($represented[$team_away->id]['away'])) $represented[$team_away->id]['away'] = 0;
    
            if ($represented[$team_away->id]['away'] == count($dates)/2) continue;
    
            $matches[] = array(
                'home' => $team_home->id,
                'away' => $team_away->id,
            );
    
            $represented[$team_home->id]['home']++;
            $represented[$team_away->id]['away']++;
    
            if ($represented[$team_home->id]['home'] == count($dates)/2) break;
        }
    }
    

    The array represented keeps track of how many games the teams play as home and away. Please notice that a team maximum can have 50% of the dates given as home games.

    Example output:

    Array
    (
        [1] => Array
            (
                [home] => 14
                [away] => 14
            )
    
        [2] => Array
            (
                [away] => 14
                [home] => 14
            )
    
        ....
    
    
        ....
    
        [25] => Array
            (
                [away] => 9
                [home] => 9
            )
    )
    

    As you can see, the last teams which are looped doesn't play 14 home and away games as the others.

  • 1
    So you question isn't how to query the database, but rather how to populate the database with pseudo-random match-ups that meet these specific conditions? All all match-ups limited only to within a particular league?Mike Brant
    @MikeBrant, yes exactly.andershagbard
    I think you need to give more details. Does each team need to match each other team a certain number of times (to where number of matches is variable) or is there a specific number of games (such that each team may play other teams a variable number of times)? Are there limits to league sizes? Are league size uniform (or limited to certain sizes choices)? Must every team have equal numbers of home and away games (or as close to equal as possible if there is odd number of games)? Are you generating entire schedule of matches for an entire "season" at once?Mike Brant

    1 Answers

    1
    votes

    Trying to do too much in one go. 24 teams in the league, means each team plays 46 matches (23 other teams one home and one away).

    So now you have 46 slots to put matches in.

    Choose a team at random
    Generate those 46 matches ,assign into the slots in some random order.
    Randomly Choose the next team
    Generate their 46 matches, look for those that have already been assigned and set their slot
    Randomly fill the rest.
    Keep going until you have one team remaining, which should already be done...
    

    Then you can assign dates...

    Could do this in SQL, but almost certainly better expressed in PHP.