Forum Moderators: coopster
Hi Folks
I’m stuck. I suck at algorithms, but I gave it a good try. Now I need your help.
I have to create a season for a minor hockey league with the following particulars
1)A Division can have approximately 6 and up to 15 teams in each.
2)I need to give each team 11 home games, and 11 away games.
3)Store the result in an array that I can then use to insert into my database.
The part I’m stuck on is trying to come up with an algorithm so that when I create a ‘home’ game, the same game is logged as an ‘away’ game with the team the home team is playing against.
So far, this is all I can comfortably code:
$Division = Array();
$Division = {“Team1”, “Team2”, “Team3”,”Team4”,”Team5”,”Team6”,”Team7”,”Team8”,”Team9”,”Team10” };
//Randomize the Teams
shuffle($Teams);
for($i = 0; $i < count($Division); $i++){
//this is the block that I stink at
}
but how do i verify that every home game gets logged as an away game for the other team, also, how do i determine that every team has exactly 11 home and 11 away games?
$games = Array();
$qtyTeams = count($Teams);
$teamIndex = 0;
$counter = 0;
//generate 11 home games
for($j = 1; $j < 12; $j++){
if($teamIndex == $qtyTeams-1 ){ $teamIndex = 0; } else { $teamIndex++ ; }
if($selectedTeam != $Teams[$teamIndex]){
echo "<br>".$selectedTeam."vs".$Teams[$teamIndex];
$games[$counter++] = $selectedTeam."vs".$Teams[$teamIndex];//$j starts at 1 so to start it at index 0 we have to subtract
} else {
$j--;
}
}
//generate 11 away games
for($j = 1; $j < 12; $j++){
if($teamIndex == $qtyTeams-1 ){ $teamIndex = 0; } else { $teamIndex++ ; }
if($selectedTeam != $Teams[$teamIndex]){
echo "<br>".$Teams[$teamIndex]."vs".$selectedTeam;
$games[$counter++] = $Teams[$teamIndex]."vs".$selectedTeam ;//$j starts at 1 so to start it at index 0 we have to subtract
} else {
$j--;
}
}
Now, randomise the order of the games array. For the first half of the season, games[i][0] is the home team and games[i][1] is the away team. in the second half of the season games[i][0] is the away team and games[i][1] is the home team.
Hope that helps!
Thanks.
PS. Code is below - please excuse my ignorance.
$theTeams = Array(13,12,8,7,4,2);
$theTeamPointer = 2;
$homeAway = "";
$numGames = 10;
$SEASON = Array();
for($game = 0; $game<$numGames ; $game++){//game loop, create 12 home and away games for each team
echo "<hr>Setting up Game #".$game;
for($team = 0; $team < count($theTeams); $team++){//setting up each team's home and away game
//SET UP THE HOME GAME FIRST----------------------------------------------------------------
$homeAway = "home";
echo "<BR><B>Setting up ".$homeAway." game ".$game." for Team #".$theTeams[$team]."</B>";
//check to see if home game is set already
if(is_int($SEASON[$game][$team][$homeAway]))
{ echo "<BR>->Game #".$game." for team ".$theTeams[$team]." is set against team #".$SEASON[$game][$team][$homeAway]; }
else
{ echo "<BR>->Game #".$game." for team ".$theTeams[$team]." is empty.";
//it's not set up yet, so find the first available team.
//make sure that the teamPointer is not pointing to the team we're setting a game up with
if($theTeams[$team] == $theTeams[$theTeamPointer]){
echo "<BR>->Oops, ".$theTeams[$team]." is the same as ".$theTeams[$theTeamPointer].". Better increment.";
//make sure that the team pointer isn't already at the end of the Team list, if it is, don't increment, set it back to 0
if($theTeamPointer == count($theTeams)-1){
//it's a the end reset it
echo "<BR>-->Oops again, ".$theTeams[$theTeamPointer]." is at the end of my array, setting the index back to 0";
$theTeamPointer = 0;
}
else {
echo "<BR>-->Incrementing the pointer now from ".$theTeamPointer;
$theTeamPointer++;
echo " to ".$theTeamPointer;
}
}//end if($theTeams[$team] == $theTeams[$theTeamPointer])
//take this team and assign it as the Away team for this Home game
echo "<BR>Team #".$theTeams[$team]." will play against Team #".$theTeams[$theTeamPointer];
//SET THE GAME for the HOME team
$SEASON[$game][$team][$homeAway] = $theTeams[$theTeamPointer];
echo "<BR>Game #".$game." for Team #".$theTeams[$team]."has a ".$homeAway." game against Team #".$theTeams[$theTeamPointer];
echo "<BR>This is the value of HOME the SEASON array: ".$game."-".$team."-".$homeAway."=".$SEASON[$game][$team][$homeAway];
//SET THE GAME for the AWAY team
//make sure that the away team doesn't already have a game set for that game#
if(is_int($SEASON[$game][$theTeamPointer]["away"])){
//a game for that game is set
echo "<BR>Game #".$game." is already taken for team ".$theTeams[$theTeamPointer];
//loop to the end of the season, and find the next available game
for($nextGame = ($game+1); $nextGame < $numGames; $nextGame++){
echo "<BR>Checking to see if Game #".$nextGame." is available.";
if(is_int($SEASON[$nextGame][$theTeamPointer]["away"])){
echo "<BR>Game #".$nextGame." is full.";
}
else {
echo "<BR>Game #".$nextGame." is available.";
echo "<BR>Setting game....";
echo "<BR>This is the value of AWAY the SEASON array:".$nextGame."-".$theTeamPointer."-"."away"." = ".$theTeams[$team];
$SEASON[$nextGame][$theTeamPointer]["away"] = $theTeams[$team];
if($theTeamPointer == count($theTeams)-1){ $theTeamPointer = 0; } else { $theTeamPointer++; }
echo "<BR>Moving the team pointer to the next team in the array, which should be Team #";
echo $theTeams[$theTeamPointer];
$nextGame = $numGames;
}
}
//$SEASON[$game][$theTeamPointer]["away"] = $theTeams[$team];
}
else
{
echo "<BR>This is the value of AWAY the SEASON array:".$game."-".$theTeamPointer."-"."away"." = ".$theTeams[$team];
$SEASON[$game][$theTeamPointer]["away"] = $theTeams[$team];
//this Away team has now been used, so move to the next team.
if($theTeamPointer == count($theTeams)-1){ $theTeamPointer = 0; } else { $theTeamPointer++; }
echo "<BR>Moving the team pointer to the next team in the array, which should be Team #";
echo $theTeams[$theTeamPointer];
}
}
//--------------------------------------
//SET UP THE AWAY GAME NEXT----------------------------------------------------------------
$homeAway = "away";
echo "<BR><B>Setting up ".$homeAway." game ".$game." for Team #".$theTeams[$team]."</B>";
//check to see if home game is set already
if(is_int($SEASON[$game][$team][$homeAway]))
{ echo "<BR>->Game #".$game." for team ".$theTeams[$team]." is set against team #".$SEASON[$game][$team][$homeAway]; }
else
{ echo "<BR>->Game #".$game." for team ".$theTeams[$team]." is empty.";
//it's not set up yet, so find the first available team.
//make sure that the teamPointer is not pointing to the team we're setting a game up with
if($theTeams[$team] == $theTeams[$theTeamPointer]){
echo "<BR>->Oops, ".$theTeams[$team]." is the same as ".$theTeams[$theTeamPointer].". Better increment.";
//make sure that the team pointer isn't already at the end of the Team list, if it is, don't increment, set it back to 0
if($theTeamPointer == count($theTeams)-1){
//it's a the end reset it
echo "<BR>-->Oops again, ".$theTeams[$theTeamPointer]." is at the end of my array, setting the index back to 0";
$theTeamPointer = 0;
}
else {
echo "<BR>-->Incrementing the pointer now from ".$theTeamPointer;
$theTeamPointer++;
echo " to ".$theTeamPointer;
}
}//end if($theTeams[$team] == $theTeams[$theTeamPointer])
//take this team and assign it as the Away team for this Home game
echo "<BR>Team #".$theTeams[$team]." will play against Team #".$theTeams[$theTeamPointer];
//SET THE GAME for the AWAY team
$SEASON[$game][$team][$homeAway] = $theTeams[$theTeamPointer];
echo "<BR>Game #".$game." for Team #".$theTeams[$team]."has a ".$homeAway." game against Team #".$theTeams[$theTeamPointer];
echo "<BR>This is the value of AWAY the SEASON array: ".$game."-".$team."-".$homeAway."=".$SEASON[$game][$team][$homeAway];
//SET THE GAME for the HOME team
//make sure that the away team doesn't already have a game set for that game#
if(is_int($SEASON[$game][$theTeamPointer]["home"])){
//a game for that game is set
echo "<BR>Game #".$game." is already taken for team ".$theTeams[$theTeamPointer];
//loop to the end of the season, and find the next available game
for($nextGame = ($game+1); $nextGame < $numGames; $nextGame++){
echo "<BR>Checking to see if Game #".$nextGame." is available.";
if(is_int($SEASON[$nextGame][$theTeamPointer]["home"])){
echo "<BR>Game #".$nextGame." is full.";
}
else {
echo "<BR>Game #".$nextGame." is available.";
echo "<BR>Setting game....";
echo "<BR>This is the value of HOME the SEASON array:".$nextGame."-".$theTeamPointer."-"."home"." = ".$theTeams[$team];
$SEASON[$nextGame][$theTeamPointer]["home"] = $theTeams[$team];
if($theTeamPointer == count($theTeams)-1){ $theTeamPointer = 0; } else { $theTeamPointer++; }
echo "<BR>Moving the team pointer to the next team in the array, which should be Team #";
echo $theTeams[$theTeamPointer];
$nextGame = $numGames;
}
}
//$SEASON[$game][$theTeamPointer]["away"] = $theTeams[$team];
}
else
{
echo "<BR>This is the value of HOME the SEASON array:".$game."-".$theTeamPointer."-"."home"." = ".$theTeams[$team];
$SEASON[$game][$theTeamPointer]["home"] = $theTeams[$team];
//this Away team has now been used, so move to the next team.
if($theTeamPointer == count($theTeams)-1){ $theTeamPointer = 0; } else { $theTeamPointer++; }
echo "<BR>Moving the team pointer to the next team in the array, which should be Team #";
echo $theTeams[$theTeamPointer];
}
}
//--------------------------------------
}//end for($j = 0; $j < count($theTeams); $j++)
}//end for($i = 0; $i<11 ; $i++){
For more information on object oriented programming refer to the documentation: [php.net...]
Try to implement the classes first yourself, then if you need additional help we'll be here. :)