Jump to content

Match Making


Go to solution Solved by mac_gyver,

Recommended Posts

I am having a dilemma.  I am working on a script for a client.  It is a match making system.  It takes all the teams, minimum of 20 teams. 10 matches a season.  No team can play the same team in the season.  Here is my code I have so far.  If it does not make sense ask me, I will try to explain more in depth.

<?php
error_reporting(E_ALL);
include("security/config.php");

//create season for testing purposes
$sql_season = "insert into season (`season`) values ('1')";
$conn->query($sql_season) or die($conn->error);

$season = 1; //testing purpose
echo "Season 1:<br />"; //testing purpose

//get number of teams
$sql_teams = "select * from teams";
$rs = $conn->query($sql_teams) or die($conn->error);
$num_of_teams = $rs->num_rows;
echo "  Number of teams this season: {$num_of_teams}<br />"; //testing purpose
//put all teams in array
$teams = array();
while($ts = $rs->fetch_array(MYSQLI_ASSOC)){
	$teams[] = $ts;
}


if($num_of_teams % 2 != 0){
	$teams[] = array('name' => 'bye week', 'id' => 0);
	$sql_dummy = "insert into teams (`name`) values ('bye week')";
	$conn->query($sql_dummy);
}

//how many matches
$weeks = 10;
$matches = 10;
echo "  This Seaon will be {$weeks} consisting of {$matches} matches<br /><br />"; //testing purpose
for($i = 0; $i < $weeks; $i++){
	//mix up teams
	$teams1 = $teams;
	$teams2 = $teams;
	shuffle($teams1);
	shuffle($teams2);
	
	$week = $i + 1;
	
	//$sql_week = "insert into weeks (`number`, `season_id`) values ('{$week}', '{$season}')";
	//$conn->query($sql_week) or die($conn->error);
	//$weekID = $conn->insert_id;
	echo "    Week {$week} Created:<br />";
	
	//$matchup = array();

	for($x = 0; $x < $matches; $x++){
		shuffle($teams1);
		shuffle($teams2);
		$team_1 = array_pop($teams1);
		$team_2 = array_pop($teams2);

		$rs = false;

		$rs = ($team_1['id'] == $team_2['id'] ? true : false);
		$sql_search = "select * from matches where season_id = '{$season}' "
		$sql_search .= "and team_1 = ('{$team_1['id']}' or '{$team_2['id']}') ";
		$sql_search .= "and team_2 = ('{$team_1['id']}' or '{$team_2['id']}')";
		$rss = $conn->query($sql_search) or die($conn->error);
		if(!$rs || !$rss){
			
			$sql_create = "insert into matches (`team_1`, `team_2`, `week`, `season_id`) values ('{$team_1['id']}', '{$team_2['id']}', '{$week}', '{$season}')";
			$rs = $conn->query($sql_create);
			if($rs){
				echo "        "; //testing purpose
				echo "{$team_1['name']} vs {$team_2['name']}<br />"; //testing purpose
			}
			else{
				echo 'error: ' . $conn->error . '<br />'; //testing purpose
			}
		}
		else{
			$teams1[] = $team_1;
			$teams2[] = $team_2;
			$x--;
		}
		
		
	}
}
 
Link to comment
https://forums.phpfreaks.com/topic/289987-match-making/
Share on other sites

I noticed i had some errors in my code.  Updated code and still no luck

<?php
error_reporting(E_ALL);
include("security/config.php");

//create season for testing purposes
$sql_season = "insert into season (`season`) values ('1')";
$conn->query($sql_season) or die($conn->error);

$season = 1;
echo "Season 1:<br />";

//get number of teams
$sql_teams = "select * from teams";
$rs = $conn->query($sql_teams) or die($conn->error);
$num_of_teams = $rs->num_rows;
echo "  Number of teams this season: {$num_of_teams}<br />";
//put all teams in array
$teams = array();
while($ts = $rs->fetch_array(MYSQLI_ASSOC)){
	$teams[] = $ts;
}


if($num_of_teams % 2 != 0){
	$teams[] = array('name' => 'bye week', 'id' => 0);
	$sql_dummy = "insert into teams (`name`) values ('bye week')";
	$conn->query($sql_dummy);
}

//how many matches
$weeks = 10;
$matches = 10;
echo "  This Seaon will be {$weeks} consisting of {$matches} matches<br /><br />";
for($i = 0; $i < $weeks; $i++){
	//mix up teams
	$teams1 = $teams;
	$teams2 = $teams;
	shuffle($teams1);
	shuffle($teams2);
	
	$week = $i + 1;
	
	$sql_week = "insert into weeks (`number`, `season_id`) values ('{$week}', '{$season}')";
	$conn->query($sql_week) or die($conn->error);
	//$weekID = $conn->insert_id;
	echo "    Week {$week} Created:<br />";
	
	$matchup = array();

	for($x = 0; $x < $matches; $x++){
		shuffle($teams1);
		shuffle($teams2);
		$team_1 = array_pop($teams1);
		$team_2 = array_pop($teams2);

		$rs = true;

		$rs = ($team_1['id'] != $team_2['id'] ? true : false);
		$sql_search = "select * from matches where season_id = '{$season}' ";
		$sql_search .= "and team_1 = ('{$team_1['id']}' or '{$team_2['id']}') ";
		$sql_search .= "and team_2 = ('{$team_1['id']}' or '{$team_2['id']}')";
		$rss = $conn->query($sql_search) or die($conn->error);
		if($rs && !$rss){
			
			$sql_create = "insert into matches (`team_1`, `team_2`, `week`, `season_id`) values ('{$team_1['id']}', '{$team_2['id']}', '{$week}', '{$season}')";
			$rs = $conn->query($sql_create);
			if($rs){
				echo "        ";
				echo "{$team_1['name']} vs {$team_2['name']}<br />";
			}
			else{
				echo 'error: ' . $conn->error . '<br />';
			}
		}
		else{
			$teams1[] = $team_1;
			$teams2[] = $team_2;
			$x--;
		}
		
		
	}
}

Link to comment
https://forums.phpfreaks.com/topic/289987-match-making/#findComment-1485684
Share on other sites

the way this is normally done is to have your teams in an array, make a copy of that array, rotate the second array, taking the end entry and putting it back into the start, match it up with the original team array, ignoring the match-up between the identical team in both arrays.

 

if i can find an example among my archived code, i will post it.

Link to comment
https://forums.phpfreaks.com/topic/289987-match-making/#findComment-1485691
Share on other sites

thanks, i made it work 

 

Data printed:

 

Season 1:
  Number of teams this season: 42
  This Seaon will be 10 consisting of 10 matches

    Week 1 Created:
        Team 12 vs Team 31
        Team 3 vs Team 40
        Team 8 vs Team 6
        Team 23 vs Team 5
        Team 25 vs Team 1
        Team 18 vs Team 39
        Team 9 vs Team 14
        Team 21 vs Team 15
        Team 36 vs Team 38
        Team 17 vs Team 7
        Team 19 vs Team 11
        Team 16 vs Team 20
        Team 29 vs Team 32
        bye week vs Team 2
        Team 22 vs Team 10
        Team 27 vs Team 13
        Team 24 vs Team 34
        Team 4 vs Team 35
        Team 33 vs Team 37
        Team 28 vs Team 20
        Team 26 vs Team 30
    Week 2 Created:
        Team 10 vs Team 23
        Team 27 vs Team 29
        Team 1 vs Team 13
        Team 40 vs Team 34
        Team 35 vs Team 24
        Team 37 vs Team 2
        Team 25 vs Team 39
        Team 20 vs Team 32
Team 36 vs Team 38 ==== Played already find new match up
        Team 28 vs Team 7
        Team 33 vs Team 31
        Team 16 vs Team 11
        Team 19 vs Team 8
        Team 20 vs Team 9
        Team 6 vs Team 14
        Team 12 vs Team 26
        Team 5 vs Team 22
        Team 4 vs Team 30
        Team 17 vs Team 36
        bye week vs Team 21
        Team 18 vs Team 3
        Team 38 vs Team 15
    Week 3 Created:
        Team 16 vs Team 18
        bye week vs Team 32
        Team 2 vs Team 11
        Team 23 vs Team 29
        Team 20 vs Team 36
        Team 21 vs Team 10
        Team 9 vs Team 24
        Team 13 vs Team 6
        Team 7 vs Team 26
        Team 40 vs Team 39
        Team 33 vs Team 12
        Team 8 vs Team 3
        Team 38 vs Team 27
        Team 34 vs Team 14
        Team 4 vs Team 31
        Team 22 vs Team 19
        Team 5 vs Team 30
        Team 1 vs Team 20
        Team 28 vs Team 17
        Team 15 vs Team 37
        Team 25 vs Team 35
    Week 4 Created:
        Team 20 vs Team 11
        Team 27 vs Team 28
        bye week vs Team 15
        Team 17 vs Team 2
        Team 9 vs Team 21
        Team 16 vs Team 13
        Team 39 vs Team 5
        Team 24 vs Team 6
        Team 32 vs Team 19
        Team 22 vs Team 37
        Team 26 vs Team 10
        Team 4 vs Team 36
        Team 23 vs Team 20
        Team 8 vs Team 34
        Team 18 vs Team 14
        Team 12 vs Team 1
        Team 31 vs Team 7
        Team 25 vs Team 40
        Team 30 vs Team 33
        Team 29 vs Team 35
        Team 38 vs Team 3
    Week 5 Created:
        Team 18 vs Team 8
        Team 22 vs Team 35
        Team 19 vs Team 39
        Team 12 vs Team 40
Team 37 vs Team 33 ==== Played already find new match up
        Team 6 vs Team 20
        Team 24 vs Team 5
Team 32 vs Team 20 ==== Played already find new match up
        Team 3 vs Team 32
Team 20 vs Team 1 ==== Played already find new match up
bye week vs Team 15 ==== Played already find new match up
Team 13 vs Team 16 ==== Played already find new match up
        Team 7 vs Team 13
        Team 4 vs Team 2
        Team 30 vs Team 11
        Team 1 vs Team 10
        Team 29 vs Team 38
Team 15 vs Team 21 ==== Played already find new match up
        Team 27 vs Team 20
        Team 28 vs Team 37
        Team 25 vs Team 34
        Team 9 vs bye week
Team 21 vs Team 15 ==== Played already find new match up
        Team 33 vs Team 26
        Team 16 vs Team 31
        Team 36 vs Team 21
        Team 15 vs Team 14
        Team 23 vs Team 17
    Week 6 Created:
        Team 14 vs Team 36
        Team 7 vs Team 40
        Team 20 vs Team 30
        Team 24 vs Team 4
Team 28 vs Team 37 ==== Played already find new match up
        Team 13 vs Team 22
Team 39 vs Team 5 ==== Played already find new match up
        bye week vs Team 33
        Team 8 vs Team 10
        Team 25 vs Team 18
        Team 12 vs Team 27
        Team 21 vs Team 28
        Team 38 vs Team 9
        Team 5 vs Team 32
        Team 23 vs Team 16
        Team 34 vs Team 20
        Team 17 vs Team 3
        Team 11 vs Team 1
        Team 37 vs Team 31
        Team 26 vs Team 2
        Team 19 vs Team 6
        Team 29 vs Team 15
        Team 35 vs Team 39
    Week 7 Created:
        Team 11 vs Team 17
        Team 4 vs Team 22
        Team 39 vs Team 1
        Team 20 vs Team 24
        Team 33 vs Team 7
Team 12 vs Team 26 ==== Played already find new match up
        Team 6 vs Team 35
        Team 2 vs Team 36
        Team 37 vs Team 19
        Team 5 vs Team 14
        Team 38 vs Team 10
        Team 28 vs Team 3
        Team 40 vs Team 32
        Team 21 vs Team 23
        Team 15 vs Team 20
        Team 26 vs Team 27
        Team 8 vs Team 9
        Team 16 vs Team 34
        Team 29 vs Team 31
        Team 25 vs bye week
        Team 18 vs Team 12
        Team 30 vs Team 13
    Week 8 Created:
Team 26 vs Team 33 ==== Played already find new match up
        Team 40 vs Team 20
        Team 25 vs Team 26
        Team 10 vs Team 2
        Team 35 vs Team 5
        Team 30 vs Team 14
        Team 15 vs Team 7
Team 20 vs Team 24 ==== Played already find new match up
        Team 33 vs Team 27
        Team 9 vs Team 20
Team 8 vs Team 6 ==== Played already find new match up
Team 29 vs Team 23 ==== Played already find new match up
        Team 23 vs Team 19
        Team 31 vs Team 18
        Team 4 vs bye week
        Team 11 vs Team 29
        Team 1 vs Team 32
        Team 6 vs Team 22
        Team 16 vs Team 37
        Team 12 vs Team 28
        Team 36 vs Team 3
        Team 38 vs Team 34
        Team 24 vs Team 8
        Team 17 vs Team 39
        Team 21 vs Team 13
    Week 9 Created:
        Team 2 vs Team 20
Team 31 vs Team 33 ==== Played already find new match up
        Team 21 vs Team 22
        Team 17 vs Team 27
Team 12 vs Team 26 ==== Played already find new match up
        Team 39 vs Team 16
        Team 11 vs Team 25
        Team 35 vs Team 19
        Team 13 vs Team 32
Team 34 vs Team 20 ==== Played already find new match up
        Team 6 vs Team 18
Team 7 vs Team 28 ==== Played already find new match up
        Team 28 vs Team 4
        Team 36 vs Team 8
Team 33 vs Team 31 ==== Played already find new match up
        Team 30 vs Team 37
        Team 40 vs Team 9
        Team 26 vs bye week
        Team 24 vs Team 12
Team 14 vs Team 34 ==== Played already find new match up
        Team 34 vs Team 7
        Team 29 vs Team 10
        Team 38 vs Team 14
        Team 15 vs Team 5
Team 20 vs Team 23 ==== Played already find new match up
        Team 31 vs Team 3
        Team 1 vs Team 20
        Team 23 vs Team 33
    Week 10 Created:
        Team 38 vs Team 20
        Team 40 vs Team 30
        Team 34 vs Team 28
Team 20 vs Team 1 ==== Played already find new match up
Team 22 vs Team 10 ==== Played already find new match up
        Team 25 vs Team 24
        Team 29 vs Team 20
Team 19 vs Team 11 ==== Played already find new match up
        Team 1 vs Team 4
        Team 37 vs Team 36
        Team 7 vs Team 2
        bye week vs Team 18
Team 14 vs Team 6 ==== Played already find new match up
Team 9 vs Team 21 ==== Played already find new match up
        Team 21 vs Team 31
        Team 5 vs Team 26
Team 23 vs Team 19 ==== Played already find new match up
        Team 10 vs Team 33
Team 6 vs Team 14 ==== Played already find new match up
        Team 17 vs Team 6
Team 35 vs Team 22 ==== Played already find new match up
        Team 39 vs Team 23
        Team 32 vs Team 12
        Team 22 vs Team 8
        Team 14 vs Team 3
Team 27 vs Team 13 ==== Played already find new match up
        Team 16 vs Team 35
        Team 13 vs Team 15
        Team 11 vs Team 9
        Team 19 vs Team 27

Link to comment
https://forums.phpfreaks.com/topic/289987-match-making/#findComment-1485692
Share on other sites

my code 

<?php
error_reporting(E_ALL);
include("security/config.php");

//create season for testing purposes
$sql_season = "insert into season (`season`) values ('1')";
$conn->query($sql_season) or die($conn->error);

$season = 1;
//echo "Season 1:<br />";

//get number of teams
$sql_teams = "select * from teams";
$rs = $conn->query($sql_teams) or die($conn->error);
$num_of_teams = $rs->num_rows;
//echo "  Number of teams this season: {$num_of_teams}<br />";
//put all teams in array
$teams = array();
while($ts = $rs->fetch_array(MYSQLI_ASSOC)){
	$teams[] = $ts;
}


if($num_of_teams % 2 != 0){
	$teams[] = array('name' => 'bye week', 'id' => 0);
	$sql_dummy = "insert into teams (`name`) values ('bye week')";
	$conn->query($sql_dummy);
}

//how many matches
$weeks = 10;
$matches = 10;
//echo "  This Seaon will be {$weeks} consisting of {$matches} matches<br /><br />";
for($i = 0; $i < $weeks; $i++){
	//mix up teams
	$teams1 = $teams;
	shuffle($teams1);
	
	$number_of_matches = (count($teams) / 2);
	shuffle($teams);
	$teams1 = array_slice($teams, 0, $number_of_matches);
	$teams2 = array_slice($teams, $number_of_matches, $number_of_matches);
	$teams2 = array_reverse($teams2);
	shuffle($teams1);
	shuffle($teams2);
	$week = $i + 1;
	
	$sql_week = "insert into weeks (`number`, `season_id`) values ('{$week}', '{$season}')";
	$conn->query($sql_week) or die($conn->error);
	$weekID = $conn->insert_id;
	//echo "    Week {$week} Created:<br />";
	
	$matchup = array();

	$x = 0;
	do{		
		shuffle($teams1);
		shuffle($teams2);
		$team_1 = array_pop($teams1);
		$team_2 = array_pop($teams2);

		$sql_search = "select * from matches where (`team_1` = '{$team_1['id']}' or `team_1` = '{$team_2['id']}') and (`team_2` = '{$team_1['id']}' or `team_2` = '{$team_2['id']}') and `season_id` = '{$season}'";
		$rs = $conn->query($sql_search) or die($conn->error);

		if($rs->num_rows == 0){
			$sql_create = "insert into matches (`team_1`, `team_2`, `week`, `season_id`) values ('{$team_1['id']}', '{$team_2['id']}', '{$weekID}', '{$season}')";
			$rs = $conn->query($sql_create);
			$x++;
		}
		else{
			//echo "{$team_1['name']} vs {$team_2['name']} ==== Played already find new match up<br />";
			$teams2[] = $team_1;
			$teams1[] = $team_2;
			array_reverse($teams2);
			shuffle($teams1);
		}
	} while($x < $number_of_matches);
}

Link to comment
https://forums.phpfreaks.com/topic/289987-match-making/#findComment-1485693
Share on other sites

  • Solution

if that's the code that produced the above schedule, team 20 is going to be very tired by the end of the season as they are playing two games each week all season long.
 
here's an example of the array shift method (add enough 'bye' entries to make the array an even number of entries, plus groups of two 'bye' entries if you want to reduce the number of games in any week, to make the season longer) -  

<?php

$num_teams = 41;
$teams = range(1,$num_teams);

if(count($teams) % 2){
$teams[] = 'bye'; // make even
}

$numteams = sizeof($teams);
$team_list = array_keys($teams);
$tmp_list = array_splice($team_list, 1, sizeof($team_list) - 1);
$count = sizeof($tmp_list) + 1;
$days = array();
for ($i = 0;$i < $numteams - 1;$i++) {
$days[$i] = array();
$day_list = array_merge(array($team_list[0]), $tmp_list);
for ($j = 0;$j < $count / 2;$j++) {
$days[$i][] = $teams[$day_list[$j]] . ' vs ' . $teams[$day_list[$count - $j - 1]];
}
// rotate $tmp_list
$elm = array_shift($tmp_list);
array_push($tmp_list, $elm);
}
echo "<pre>";
print_r($days);
echo "</pre>";
?>

btw - if you want every team to play every other team at least once, it will take many more weeks then 10 for 42 teams (you actually only show 40 teams in the result.)

 

and because the bye's, if you have more than one, would be grouped if added to the end of the team array, you would need to randomize the $days array in the above code so that teams don't sit out multiple weeks in a row. edit: if you have more than one 'bye', it would be better to just evenly distribute them in the original array.

Link to comment
https://forums.phpfreaks.com/topic/289987-match-making/#findComment-1485694
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.