Jump to content

From ruby to PHP


mr_murph

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
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.