mr_murph Posted June 17, 2009 Share Posted June 17, 2009 Hello everyone, I´m new at this forum and I don´t know if this is the right place to post this message, so please don´t be angry if I´m in the wrong place I´ve got a Ruby script to distribute a group of teams into a calendar, but it´s written in ruby. I understand what it does but I need some help to translate it to PHP. Please, can anybody help me?? I´d be so thankful. This is the ruby script: def make_rounds(clubs) #If odd insert dummy club if clubs.length % 2 == 1 clubs << "X" end rounds_home = [] rounds_away = [] num_rounds = clubs.length - 2 num_matches = clubs.length / 2 - 1 for i in (0..num_rounds) matches_home = [] matches_away = [] for j in (0..num_matches) matches_home << [clubs[j], clubs[num_rounds - j + 1]] #Home match matches_away << [clubs[num_rounds - j + 1], clubs[j]] #Away match end rounds_home << matches_home rounds_away << matches_away #rotating the teams last = clubs.pop clubs.insert(1, last) end rounds_away.each { |x| rounds_home << x} return rounds_home end def print_rounds(rounds) i = 1 rounds.each do |round| puts "Round: #{i}" round.each do |match| puts "Match: #{match[0]} - #{match[1]}" end i += 1 end end clubs = ("A".."T").collect {|x| x} rounds = make_rounds(clubs) print_rounds(rounds) Thanks in advance Quote Link to comment Share on other sites More sharing options...
mr_murph Posted June 17, 2009 Author Share Posted June 17, 2009 I´ve finally solved this problem. Let me know if you want to know the answer. Quote Link to comment Share on other sites More sharing options...
Maq Posted June 17, 2009 Share Posted June 17, 2009 I´ve finally solved this problem. Let me know if you want to know the answer. Sure, we'd love to know the answer. Quote Link to comment Share on other sites More sharing options...
mr_murph Posted June 18, 2009 Author Share Posted June 18, 2009 Please tell me if I´m doing something wrong or just if it could be done better as I´m also new whit PHP. The main function, make_rounds(): function make_rounds($clubs){ #If odd insert dummy club if (count($clubs) %2 == 1) $clubs[]="X"; $rounds_home = array(); $rounds_away = array(); $num_rounds = count($clubs)-2; $num_matches = count($clubs)/2-1; for ($i=0; $i < $num_rounds+1; $i++) { $matches_home=array(); $matches_away=array(); for ($j=0; $j < $num_matches+1; $j++) { $matches_home[]=array( "home_team" => $clubs[$j], "away_team" => $clubs[$num_rounds - $j + 1] ); $matches_away[]=array( "home_team" => $clubs[$num_rounds - $j + 1], "away_team" => $clubs[$j] ); } $rounds_home[]=$matches_home; $rounds_away[]=$matches_away; # rotating the teams $first = $clubs[1]; foreach ($clubs as $k => $club) { if ($k != 0 && $k != count($clubs)-1) { $next = $k+1; $clubs[$k] = $clubs[$next]; } if ($k == count($clubs)-1) $clubs[$k] = $first; } } # Adding away rounds to the final calendar foreach ($rounds_away as $i => $round) $rounds_home[]=$round; return $rounds_home; } When calling this function, it needs an array with the teams as an argument. I´m not sure if I´m doing the same thing when rotating the teams (line 28). It seems to work, but I need to test it harder. The function print_rounds is just a foreach to display the round values. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.