Jump to content

why isnt this working?


wildboy211

Recommended Posts

in this code it prints 24 arrays with [0]as a diamond number [1] as team1 and [2] as team2,

only 16 teams can play twice in one week, and the 16 that didnt play needs to play twice next week.When i test the code, it doesnt do that...some teams play twice in one game (which it cant do either)...could someone point out why?

 

<?
function gcd($a, $b)
{
    if ($a == 0 || $b == 0)
        return max($a, $b);
    if ($a > $b)
        return gcd($b, $a);
    if (!($b % $a))
        return $a;
    return gcd($a, $b % $a);
}

function lcm($a, $b)
{
    return $b * $a / gcd($a, $b);
}
?>
<html>
<head>
<style>body{font:10pt Tahoma;}</style>
<body>
<?php
$teams = 32;
$diamonds = 6;
$diamond_slots = 4;
$teams_played = array(); //the teams that have played already=> [0] and how many times =>[1]
$timesplayed = lcm($diamonds * $diamond_slots, $teams) / $teams;
$weeksneeded = lcm($diamonds * $diamond_slots, $teams) / (($diamonds * $diamond_slots) * 2);
$i = 1;

while ($i <= $weeksneeded) {
    $j = 1;
    $slot_count = 0;
    $game = array();

    $d = 1;

    // prefill arrays
    while ($d <= $diamonds * $diamond_slots) {
        $game[$d] = array(1 => 0);
        $d++;
    }
    $d = 1;
    while ($d <= $teams) {
        $teams_played[$d] = array(0 => 0, 1 => 0);
        $d++;
    }


    print "Week " . $i . ":<br>";
    print "<b>Diamond - Team1 - Team2</b><br>";
    while ($j <= $diamonds * $diamond_slots) {
        srand();
        $diamond_rnd = rand(1, $diamonds);

        //check for slots over 5
        for ($row = 1; $row <= count($game); $row++) {
            if ($game[$row][0] == $diamond_rnd) {
                ++$slot_count;
            }
        }

        while ($slot_count > $diamond_slots - 1) {
            srand();
            $diamond_rnd = rand(1, $diamonds);
            $slot_count = 0;

            for ($row = 1; $row <= count($game); $row++) {
                if ($game[$row][0] == $diamond_rnd) {
                    ++$slot_count;
                }
            }
        }
        // randomize teams & check to make sure they arent the same.

        $team1_rnd = rand(1, $teams);
        $team2_rnd = rand(1, $teams);
//        while ($team1_rnd == $team2_rnd) { //checking again in case 2nd or 3rd while changes it
            while ($team1_rnd == $team2_rnd) {
                $team1_rnd = rand(1, $teams);
                $team2_rnd = rand(1, $teams);
            }
            // check to see if teams have already played
            if ($j <= ($teams / 2)) { //since 2 teams will play each game
                while ($teams_played[$team1_rnd][1] >= $i) {
                    $team1_rnd = rand(1, $teams);
                }
                while ($teams_played[$team2_rnd][1] >= $i) {
                    $team2_rnd = rand(1, $teams);
                }
            }
//        }
        // insert random numbers into array
        // check to see if theres an identical array
        for ($row = 1; $row <= count($game); $row++) {
            if ($game[$row] == array($diamond_rnd, $team1_rnd, $team2_rnd)) {
                $result = true;
            } else {
                $result = false;
            }
        }
        for ($d = 1; $d <= count($game); $d++) {
            if (($game[$d][1] && $game[$d][2]) == ($game[$team1_rnd][2] && $game[$team2_rnd][1])) {
                $result2 == true;
            }
        }
        if (($result == true) || ($result2 == true) || ($result3 == true)) {
            $slot_count = 0; //dont add 1 to $j
        } elseif ($result == false) {
            $teams_played[$team1_rnd] = array(0 => $team1_rnd, 1 => 1);
            $teams_played[$team2_rnd] = array(0 => $team2_rnd, 1 => 1);
            $game[$j] = array($diamond_rnd, $team1_rnd, $team2_rnd);
            print '$game[' . $j . ']:';
            print_r($game[$j]);
            print "<br>";
            $j++;
            $slot_count = 0;
        }
        flush();
    }
    print "<br><br>";
    $i++;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/131913-why-isnt-this-working/
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.