動的

2017-02-15 42 views
5

次の多次元配列の要素のソート/取得します私は現在、偉大な古い脳のおならを走ることだし、動的に下記のラウンドの勝者は、に進みますことを、次の「回戦」を選択立ち往生しています:動的

generated ladder


上記のラダーを動的に生成し、私は何をしたいのは次の試合のIDを把握されています。私は現在、POCとしてこれを持っているが、競争のはしごは64 /以上まで実行した場合、それは持続可能ではありません。

$ar = [ 
1 => [ 
    ['id' => 1,'name' => 'round1, pair 1'], 
    ['id' => 2,'name' => 'round1, pair 2'], 
    ['id' => 3,'name' => 'round1, pair 3'], 
    ['id' => 4,'name' => 'round1, pair 4'], 
], 
2 => [ 
    ['id' => 5,'name' => 'round2, pair 1'], 
    ['id' => 6,'name' => 'round2, pair 2'], 
] 
]; 

$cases = [0, 0, 1, 1, 2, 2]; 

foreach($ar as $i => $round) { 

    foreach($round as $_i => $r) { 
     echo $r['name'] . " & NEXT_MATCH_ID::> " . $ar[($i + 1)][$cases[$_i]]['id'] . "<br /> "; 
    } 
} 

はハードずに何上記を達成するためのより簡単な方法はあります例えば、コード化された変数($cases)。

本質的に、「一致/対」の量は、ラダーが4->2->1のように半分になります。

上記は正しいIDを生成しますが、拡張可能でも動的でもありません。上記コードの

round1, pair 1 & NEXT_MATCH_ID::> 5 
round1, pair 2 & NEXT_MATCH_ID::> 5 
round1, pair 3 & NEXT_MATCH_ID::> 6 
round1, pair 4 & NEXT_MATCH_ID::> 6 
round2, pair 1 & NEXT_MATCH_ID::> ... 
round2, pair 2 & NEXT_MATCH_ID::> ... 
//......etc etc... 

Demo/ Example必要な場合。


ノート

  • あり、 "選手/チーム" の試合に制限はありません、これは指数関数的にすることができ、4, 6, 8, 10, 12, 14, 16, 18....32, 34...64...etc
  • 最終ラウンド(グランドファイナル - 1マッチ)にはこれ以上は進まないため、これは決して起こらない/適用されません。 (は容易にif($i == count($rounds)) {.... do not continue...によって制限されます)。
  • 複数の一致が同時に実行される可能性があるため、「次のラウンドID」はではなく、lastId + 1となります。
+1

次にマッチIDがラウンドの最後のIDと現在のペアインデックスから計算することができる : '$ NEXTID = $ roundLastId + 1 +(INT )floor($ pairIndex/2); '。 Btw。ペアの数は2のべき乗(2,4,8,16 ...)にする必要があります。 – shudder

+0

@shudderそれはほぼ正しいだろう!しかし、次回のIDとは異なるIDで複数のマッチが実行される可能性がありますので、POCを通過しました:P – Darren

+0

この場合、確定的ではないため、次の一致IDをアルゴリズムで把握することはできません。まずデータレベルで解決する必要があります。共通のラダー/ラウンドID(同じラダー/ラウンド内で一致IDを計算できるように)を使用して、またはリレーショナルデータベースを扱っていない場合は、ツリー構造に直接進むことで、next_matchフィールドをペアにすることもできます。 – shudder

答えて

1

だけの数学

すべてのラウンドは、チームのpow(2, Rounds - Round + 1)と試合のpow(2, Rounds - Round)が含まれていることを、覚えておいてください。幾何学的な進歩としてまとめてください。ラウンド$round前に撮影した試合の

a=2^(rounds-1)r=1/2n=round-1geometric progression2^(rounds-1) + 2^(rounds-2) + ... 2^(rounds - round + 1)です。その合計は2^(rounds) - 2^(rounds+1-round)です。

したがって、一致するIDと次の一致IDは、単に,roundroundsの3つの引数の関数に過ぎません。私はその計算を関数getMatchIdgetNextIdに移しました。

<?php 
// just matchesInPreviousRounds + parnum 
function getMatchId($pairnum, $round, $rounds) { 
    // matchesInPreviousRounds - is a sum of a geometric progression 
    // 2^(rounds-1) + 2^(rounds-2) + ... 2^(rounds - round + 1) 
    // with a=2^(rounds-1), r=1/2, n = round-1 
    // its sum is 2^(rounds) - 2^(rounds+1-round) 
    $inPreviousRounds = $round > 1 ? (pow(2, $rounds) - pow(2, $rounds + 1 - $round)) : 0; 

    $id = $inPreviousRounds + $pairnum; 

    return (int)$id; 
} 

// next id is last id of a round + half a pairnum. 
function getNextId($pairnum, $round, $rounds) { 
    if($round === $rounds) { 
     return false; 
    } 

    $matchesInThisAndPreviousRounds = pow(2, $rounds) - pow(2, $rounds - $round); 

    $nextid = $matchesInThisAndPreviousRounds + ceil($pairnum/2); 

    return (int)$nextid; 
} 

$divide = 64; // for 1/64 at the start 
$power = round(log($divide)/log(2)); // get 6 for 64 
$rounds = (int) $power + 1; 


for($round = 1; $round <= $rounds; $round++) { 
    // every round contains 2^($rounds - $round + 1) of teams 
    // and has 2^($rounds - $round) of matches 
    $teamsLeft = pow(2, $rounds - $round + 1); 
    $pairsLeft = pow(2, $rounds - $round); 


    for($pairnum = 1; $pairnum <= $pairsLeft; $pairnum++) { 
     $id = getMatchId($pairnum, $round, $rounds); 
     $nextid = getNextId($pairnum, $round, $rounds); 

     echo "Round $round, pair $pairnum, id $id "; 
     echo "winner goes to " . $nextid ? $nextid : "A BAR" . "\n"; 
    } 
} 

その結果

Round 1, pair 1, id 1, winner goes to 65 
Round 1, pair 2, id 2, winner goes to 65 
... 
Round 1, pair 62, id 62, winner goes to 95 
Round 1, pair 63, id 63, winner goes to 96 
Round 1, pair 64, id 64, winner goes to 96 
Round 2, pair 1, id 65, winner goes to 97 
Round 2, pair 2, id 66, winner goes to 97 
... 
Round 2, pair 29, id 93, winner goes to 111 
Round 2, pair 30, id 94, winner goes to 111 
Round 2, pair 31, id 95, winner goes to 112 
Round 2, pair 32, id 96, winner goes to 112 
Round 3, pair 1, id 97, winner goes to 113 
Round 3, pair 2, id 98, winner goes to 113 
... 
Round 3, pair 13, id 109, winner goes to 119 
Round 3, pair 14, id 110, winner goes to 119 
Round 3, pair 15, id 111, winner goes to 120 
Round 3, pair 16, id 112, winner goes to 120 
Round 4, pair 1, id 113, winner goes to 121 
Round 4, pair 2, id 114, winner goes to 121 
... 
Round 4, pair 7, id 119, winner goes to 124 
Round 4, pair 8, id 120, winner goes to 124 
Round 5, pair 1, id 121, winner goes to 125 
Round 5, pair 2, id 122, winner goes to 125 
Round 5, pair 3, id 123, winner goes to 126 
Round 5, pair 4, id 124, winner goes to 126 
Round 6, pair 1, id 125, winner goes to 127 
Round 6, pair 2, id 126, winner goes to 127 
Round 7, pair 1, id 127, winner goes to A BAR 
+0

うわー、元気いっぱい! – Darren