TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-18-2008, 09:54 PM   #1 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default Calculating an unknown variable

Dear PHPers,

I'm currently developing a cupsystem for a new gaming ladder called "ClanFight". Now, I hit a snag.

I am trying to determine how many brackets should be played before the end is 'near', so to speak. For example:

PHP Code:
$totalPlayers 64;
$totalBrackets $totalPlayers 1/* 128 minus 1, 127th is the winner */ 
Now, of course I know that we have to play:
64 players, remainder 32, remainder 16, remainder 8, remainder 4, remainder 2, winner 1. A total of 127 brackets (64 + 32 + 16 + 8 + 4 + 2 + 1 = 127).
Now, this means, that there will be played 7 ROUNDS.
Round 1 :: 64 (32 matches, 32 dropouts)
Round 2 :: 32 (16 matches, 16 dropouts)
Round 3 :: 16 (8 matches, 8 dropouts)
Round 4 :: 8 (4 matches, 2 dropouts)
Round 5 :: 4 (2 matches, 2 dropouts
Round 6 :: 2 (1 match, 1 winner)
Round 7 :: 1 (cookie?)

Anyhow, this all looks rather simple because we can calculate the yet unknown variable. We know by seeing the nummers 64 to 1, that 7 rounds are played, with the 7th round being the winner. So most fairly, you could say 6 rounds with the result 1 winner. Either way. I rather have it displayed in 6 rounds though, because the 6th round, 1 on1 will spit out the winner anyway.

Now, how am I supposed to calculate it? 8 players requires 3 rounds. (8 - 4 - 2 (- 1)) So how am I supposed to draw from 8, 3 and from 64, 6.

64 / 8 = 8 - 2 = 6... But that doesn't work on the 8 players.
8 / 8 = 0 - 2 = -2...

I am still ignorant to this variable or calculationmethod. Perhaps you guys could take a crack at it.

The total players will increase to 512 players total, 1023 brackets with A LOT of ROUNDS. :)

Help? :)
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 05-19-2008, 12:31 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

To find the number of rounds from players, it looks like you could use the log2 value. In PHP we don't have this function available (though it is in C) but we do have a more general log function which can take a variable base value and for our purposes that base should be 2.

To quote the PHP Manual page:
float log ( float $arg [, float $base ] )

If the optional base parameter is specified, log() returns logbase arg , otherwise log() returns the natural logarithm of arg.
In practice, we can use it like so:
PHP Code:
$players  64;
$brackets = ($players 2) - 1;     // 127
$rounds   = (int) log($players2); // 6 
Salathe is offline  
Reply With Quote
Old 05-19-2008, 02:32 PM   #3 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

I must say, I didn't know that there was a function like that. However, the code you supplied displays 5 instead of 6. :)

Perhaps any other methods you care to try out?
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 05-19-2008, 09:06 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I think you must have copy/pasted incorrectly and/or altered something because log(64, 2) is 6, fact. :)
Salathe is offline  
Reply With Quote
Old 05-19-2008, 09:21 PM   #5 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
I think you must have copy/pasted incorrectly and/or altered something because log(64, 2) is 6, fact. :)
I'm not kidding. :) It seriously returns 5. :) Running version 5.2.4.

http://markernst.eu/clanfight/test.php
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 05-20-2008, 12:08 AM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

OK, looks like there may be a rounding problem on your machine or version of PHP (mine's 5.2.5). Simply round the value rather than casting it to integer.

$rounds = round(log($players, 2)); // 6

As a fun check try: echo (int) 5.999999999999999; that's what PHP might be seeing rather than 6 as we expect.
Salathe is offline  
Reply With Quote
Old 05-20-2008, 04:47 PM   #7 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Worked like a charm Salathe. Thanks a loOot.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:32 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design