Jump to content

Help needed with schedule!


BigBrother

Recommended Posts

Hi need help with this. I am trying to create a schedule for 20 teams, howerver if the number of teams is changed to 20 the script errors out

 

schedule_class.php

<?php
class schedule{
var $history=array();
var $num=8;
var $a=array();
function make_teams(){
	for($i=1;$i<=$this->num;$i++){
		$this->a[]="team ".$i;
	}
}

function remove_history($team1,$team2){
	if(sizeof($this->history)>0){
		foreach($this->history[$team1] as $key => $val){							
			if($val==$team2&&$val){
				$this->history[$team1][$key]=0;
				break;
			}
		}
	}
}

function make_history(){
	for($i=0;$i<$this->num;$i++){
		for($j=0;$j<$this->num;$j++){				
			if($i==$j) $this->history[$this->a[$i]][]=0;
			else $this->history[$this->a[$i]][]=$this->a[$j];
		}
	}
}

function print_teams(){
	print_r($this->teams);
}
function check_temp_history($temphistory,$team){
	if(sizeof($temphistory)>0){
		foreach($temphistory as $val){
			if($val==$team){
				return false;
			}
		}
	}
	return true;
}
function move(){
	$temphistory=array();
	for($i=0;$i<$this->num;$i++){	
		if(sizeof($this->history)>0){		
			foreach($this->history[$this->a[$i]] as $key => $val){
				if($val&&$this->check_temp_history($temphistory,$val)&&$this->check_temp_history($temphistory,$this->a[$i])){	
					$temphistory[]=$this->a[$i];
					$temphistory[]=$val;
					break;
				}
			}
		}
	}
	$this->a=$temphistory;
}

function run(){
	for($j=1;$j<$this->num;$j++){
		for($i=0;$i<$this->num;$i+=2){
			srand();
			$r=rand(0,1);
			if($r==1){
				echo $this->a[$i]."-".$this->a[$i+1];
			}
			else{
				echo $this->a[$i+1]."-".$this->a[$i];
			}
			echo "<br />";
			$this->remove_history($this->a[$i],$this->a[$i+1]);
			$this->remove_history($this->a[$i+1],$this->a[$i]);
		}
		echo "<hr>";
		$this->move();
	}
}
}
?>

 

schedule.php

 

<?php
include("schedule_class.php");

$s=new schedule();
//number of teams
$s->num=8;
//makes the teams
$s->make_teams();
//makes the history-all the possibilities of the teams(team1 can play against all and so on)
$s->make_history();

//prints the schedule
$s->run();

?>

 

Any ideas peeps... thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/144337-help-needed-with-schedule/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.