Problem creating a hockey season.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pugg09
Forum Newbie
Posts: 1
Joined: Mon Aug 18, 2008 10:34 pm

Problem creating a hockey season.

Post by pugg09 »

Newbi Alert!!!

Sorry, but I'm really struggling to finish this project of mine. I have a hockey Division of 6 teams (but really, it could be any amount). I use a 3D array to store the Game #,Team # and HomeOrAway game.

I don't have any problem keeping track of it as I fill the array, but what happens is, by Game index 1 I wind up with the same order or team vs team. For example, Team 13 always has a Home game against team 4 and an Away game against team 8. Is there any fix for this?

I've posted the code below - please excuse my ignorance.

Code: Select all

 
 
 
        $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;
                                }
                            }
                    }
                    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<$numGames; $i++){
        
 
 
 
Post Reply