Seeding a Single Elimination Cup

During the past few days I have been working on a Cup Script for skilled.ch. I will be posting a few points I had to work on during the process since I have search for a reference concerning this subject and came up empty.

The following is a script to create the matches for the previously seeded contestants. The $post variable is what is transferred via $_POST. It’s content looks like this:

$_POST['id'] array(1, 2, 3, …); // User ID
$_POST['seed'] array (8, 4, 1, …); // Seeding data

The variable $grid['size'] is the total number of contestants in the bracket.

This is the code I used to evaluate this data and generate matches:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Check if seeding where made correctly + Connect Contestant ID with Seeding
for($c = 1; $c<=$grid['size']; $c++){
if(!in_array($c, $post['seed'])) die("You must use all numbers from 1 - ".$grid['size'].".");
    $contestants[$c - 1] = $post['seed'][$c - 1].".".$post['id'][$c - 1];
}

// Sort the contestants by seeding. Note that the ID is saved as the decimal, so it will never affect seeding. Do not use decimals in the seeding!!!
sort($contestants);

$NrOfMatchesRound1 = ($grid['size'] / 2);

$jumper = 0;

for($c = 0; $c<$NrOfMatchesRound1; $c++){

// Make sure the Match is generated at the top or the bottom of the tree, according to seeding
if($jumper==0){ $MatchNr = $c + 1; $jumper = 1; }
else{ $MatchNr = $NrOfMatchesRound1 - $c + 1; $jumper = 0; }

//Player with the higher seeding
$HighSeed = $contestants[$c];
$CalcLowSeed = $grid['size'] - $c - 1;
//Player with the lower seeding
$LowSeed = $contestants[$CalcLowSeed];

// This IF makes sure the Higher and Lower Seedings are placed as the correct contestant on the Cup tree
if($jumper==1){
    $tmp1 = explode(".", $HighSeed); //Seperate Seed and ID again
    $tmp2 = explode(".", $LowSeed);
}else{
    $tmp2 = explode(".", $HighSeed);
    $tmp1 = explode(".", $LowSeed);
}

//Contestant ID
$cont_1_ID = $tmp1[1];
//Contestant Seeding
$cont_1_SD = $tmp1[0];

$cont_2_ID = $tmp2[1];
$cont_2_SD = $tmp2[0];

$replacementset['Matches'] .= "Match $MatchNr is: $cont_1_ID ($cont_1_SD) vs. $cont_2_ID ($cont_2_SD)<br />";
}

I hope that I could help someone somehow :)

By the way: All the userinput is from an admin. The permissions are checked by a class before this part gets executed. If you for some reason do not trust the people that use this script, you may want to filter the input a little more.


Dragony   |  Coding, PHP   |  07 1st, 2010    |  No Comments »

WordPress now on dragony.ch

Hey there,

I decided I would install something already finished on this website rather then just using my already way outdated CMS. If you care you will be able to find some information about me and what I do on the internet on this website.

Check back soon for some new stuff :)


Dragony   |  Uncategorized   |  06 16th, 2010    |  No Comments »