Forum Moderators: coopster

Message Too Old, No Replies

Algorithm Help – Hockey Season

keeping track of arrays in php

         

pugg09

1:43 am on Aug 16, 2008 (gmt 0)

10+ Year Member



Algorithm Help – Hockey Season

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

}


Please help, as I’ve gone over this for 2 days now. Thanks so much!

malcolmcroucher

11:18 am on Aug 16, 2008 (gmt 0)

10+ Year Member



$i++ must be at the bottom

for($i = 0; $i < count($Division); ){

Echo "hello";

$i++
}

pugg09

12:11 pm on Aug 16, 2008 (gmt 0)

10+ Year Member



thanks, here's what I think I need so far

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--;
}
}

darrenG

6:46 pm on Aug 16, 2008 (gmt 0)

10+ Year Member



What I think would be easier is to generate a 2D array, each entry in the array representing meeting of teams. Don't worry about the order of the games, and dont worry about a game for home and away for each team.

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!

pugg09

3:28 am on Aug 19, 2008 (gmt 0)

10+ Year Member



Alrighty.... I was able to do the array fills, I actually used a 3 dimensional array (1 for game, 2 for Team, 3 for Home or Away. And I have the following code below. The only problem now, is that I am always getting the same assigned games for every team. For example Game 1 thru to Game 10, Team 13 is always playing a Home game against team 4 and an Away game against Team 8. Can anyone help me?

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++){

eelixduppy

1:37 pm on Aug 19, 2008 (gmt 0)



Sorry, I haven't read through your code yet, but I have a suggestion that you might want to take to make this work out much more smoothly and so that it will be easier to code. My suggestion is that you make a few classes for this, say classes Division, Team, and Game. This will make the organization MUCH nicer. The division class obviously have an array of Teams as well as any additional information you'd like to keep there, like division_name, etc... Class Team would have players, name, etc. Game will have Teams, Division, away_info, home_info etc... Then, for what you are going, I'd include in the Division class a function that randomly generates a Game between two teams. Shouldn't be too hard to implement once you have the classes set up.

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. :)