PHP Chess Rank Algorithm from Facebook Movie

Last Updated on Wednesday, 28 December 2011 08:37pm Written by spunky Wednesday, 28 December 2011 08:29pm

Following on from the previous post, here is the algorithim used for ranking chess players as seen on The Social Network when Mark Zuckerberg is developing Facemash.

The algorithm shown in the movie looked a bit like this:

  • Ea = 1 / (1 + 10 ^ ((Rb-Ra)/400))
  • Eb = 1 / (1 + 10 ^ ((Ra-Rb)/400))

Here is something a bit more practical in the form of a PHP script:

/**
 * Calculates odds of winning or new score.
 * New players start on 2000 points.
 *
 * @param int $player1 Player 1 Score
 * @param int $player2 Player 2 Score
 * @param int $result  Determine the result
 *                     0 = Calculate Odds (%)
 *                     1 = Player 1 Won
 *                     2 = Player 2 Won
 *                     3 = It's a Draw
 */
function calculateRank($player1, $player2, $result=0)
{
	$odds_p1 = 1/(1+pow(10,(($player2-$player1)/400)));
	$odds_p2 = 1/(1+pow(10,(($player1-$player2)/400)));

	if ($result==0)
		return array('player1' => $odds_p1*100, 'player2' => $odds_p2*100);

	$result_p1 = ($result==1) ? 1 : (($result==2) ? 0 : 0.5);
	$result_p2 = ($result==1) ? 0 : (($result==2) ? 1 : 0.5);

	$score_p1 = $player1 + (32*($result_p1-$odds_p1));
	$score_p2 = $player2 + (32*($result_p2-$odds_p2));

	return array('player1' => $score_p1, 'player2' => $score_p2);
}

Enjoy!

PHP Experience Points (XP) Level System

Last Updated on Friday, 8 July 2011 11:07pm Written by spunky Friday, 8 July 2011 11:05pm

Here’s a fun little snippet that lets you create a XP leveling system similar to World of Warcraft.

*ding!*

function calculateXP($myPoints) {

        $calcStart = 0; // Level 1 Start XP
        $calcEnd = 10;  // Level 1 End XP
        $calcInc = 0;   // Increase by extra how many per level?
        $calcLevel = 6; // Multiply by how many per level? (1- easy / 20- hard)

        /* Calculate Level */
        $myLevel = 0;
        $calcCount = 0;
        do {
                $calcCount = $calcCount+1;
                if ($calcCount % 2 == 0 ) { $calcInc = $calcInc + $calcLevel; }
                if (($myPoints < $calcEnd) && ($myPoints >= $calcStart)) { $myLevel = $calcCount; $myStart = $calcStart; $myEnd = $calcEnd; }
                $calcStart = $calcEnd;
                $calcEnd = $calcEnd + $calcInc;
        } while ($myLevel == 0);
        $myLevel--;

        /* Calculate Percentage to Next Level */
        $myPercent = (($myPoints - $myStart) / ($myEnd - $myStart)) * 100;
        $myPercent = round($myPercent);
        if ($myPercent == 0) { $myPercent = 1; }

        return array('percent'=>$myPercent,'level'=>$myLevel);
}

Please excuse the primitive logic and bad variable naming, this was written many years ago.

Landing pages for dumbasses

Last Updated on Friday, 17 June 2011 10:32am Written by spunky Friday, 17 June 2011 10:31am

If you’re going to make a site that people will actually want to use, your landing page should do no less than answer to and inform of three things:

  1. What is it?
  2. What do I have to do?
  3. What do I get?

Anything else should only relate to the above, for example if there is a potential risk of wasting resources, like the user’s precious time, reinforce the cost:reward or illustrate that other people have taken the risk before them, like the peer pressure effect from a Facebook Like Box.

Half measures for 1140px Grid by Andy Taylor

Last Updated on Thursday, 23 June 2011 11:17am Written by spunky Wednesday, 15 June 2011 06:13pm

Who uses 1024×768 resolution anymore besides your grandma, give me a break. Andy Taylor’s 1140px CSS Grid could be considered the new 960 grid. Although super handy 12 columns doesn’t quite cut it for tight spaces. So using the amazing trickery of maths, this additional CSS beefs it up to potentially 24 by adding “half” columns.

Appendage:

.halfcol { width: 2.425%; float: left; margin-right: 3.8%; }
.onecol.half { width: 9.175%; }
.twocol.half { width: 17.825%; }
.threecol.half { width: 26.475%; }
.fourcol.half { width: 35.125%; }
.fivecol.half { width: 43.775%; }
.sixcol.half { width: 52.425%; }
.sevencol.half { width: 61.075%; }
.eightcol.half { width: 69.725%; }
.ninecol.half { width: 78.375%; }
.tencol.half { width: 87.025%; }
.elevencol.half { width: 93.775%; }

Usage:

<div class="halfcol">Half a column wide</div>
<div class="onecol half">One and a half columns wide</div>
<div class="twocol half">Two and a half columns wide</div>
<div class="threecol half">Three and a half columns wide</div>
<div class="fourcol half">Four and a half columns wide</div>
<div class="fivecol half">Five and a half columns wide</div>
<div class="sixcol half">Six and a half columns wide</div>
<div class="sevencol half">Seven and a half columns wide</div>
<div class="eightcol half">Eight and a half columns wide</div>
<div class="ninecol half">Nine and a half columns wide</div>
<div class="tencol half">Ten and a half columns wide</div>
<div class="elevencol half">Eleven and a half columns wide</div>

Aware this doesn’t cover the IE component, this is intentional because anything < IE9 sux.

Enjoy!

.htaccess redirect to avoid duplicate content penalties

Last Updated on Saturday, 16 April 2011 12:17pm Written by spunky Saturday, 16 April 2011 11:34am

This might seem like an obvious one but it’s something seldom seen in newly launched sites. If your website can be accessed with yourdomain.com and www.yourdomain.com Google will see this as two completely separate sites and penalize you for having duplicated content. You must redirect your visitors to either yourdomain.com or www.yourdomain.com – not allowing both.

It is important to address this problem as early as possible since links can be created outside the scope of your website and the search engines may already have indexed your website under both addresses, this cannot be changed that easily.

The solution is simple: force a 301 redirect for all http requests that are going to the incorrect website URL.

Read more ...

Dreamweaver color scheme for Netbeans editor

Last Updated on Saturday, 2 April 2011 05:26pm Written by spunky Saturday, 2 April 2011 01:55pm

If you’ve stepped up your game and ditched Dreamweaver in favour of using a real PHP IDE like Netbeans then this Dreamweaver-style theme will help make the transition that much easier.

Being unable to find a Dreamweaver color scheme for Netbeans code editor anywhere, this one was built from scratch today using exact color codes from Dreamweaver.

The Netbeans theme also takes a slight influence from Visual Studio’s default colors.

Mmmmm now that’s easy on the eyes.

Making a Side Page Menu in WordPress

Last Updated on Monday, 7 March 2011 08:05am Written by spunky Monday, 7 March 2011 07:26am

For WordPress sites with nested pages a side menu is a valuable element to any design. First we determine if the current page is in fact the parent or a child and collect it’s ID. Then by calling the wp_list_pages command we can list the children and determine if a side menu is even required.

<?php
$parent_id = ($post->post_parent)?$post->post_parent:$post->ID;
$children = wp_list_pages("title_li=&child_of=".$parent_id."&echo=0");
if ($children) { ?>
	<h3><?php echo get_the_title($parent_id)?></h3>
	<ul>
		<li class="<?=($parent_id==$post->ID)?'current_page_item':''?>"><a href="<?=get_permalink($parent_id)?>">Overview</a></li>
		<?php echo $children; ?>
	</ul>
<?php } ?>

Beautifully simple.

Cool story bro

Last Updated on Monday, 7 March 2011 07:18am Written by spunky Saturday, 5 March 2011 11:29am

The story we tell ourselves to justify, reason, explain our lack of…whatever.

“They always…”

“That happened to me because…”

“They are…”

They are a person. That happened to you because it happened. They never more than they always.

How to Recreate/Reset Table ID Fields (1,2,3,4…) using mySQL

Last Updated on Wednesday, 23 February 2011 10:09pm Written by spunky Wednesday, 23 February 2011 07:54am

If you are anal retentive or obsessive–compulsive about having clean ID numbers in your mySQL tables, this code should prove itself useful as it will automatically:

  • clean up, convert, recount, reset, recompile, recreate, repopulate the ID column for every entry
  • modify the auto increment counter for the table

It is important to note this script should not be used on tables with cross referenced identifiers meaning if a different table relies on the IDs of the modified table it will potentially break the references as a result of assigning the new IDs.

The code might also be useful for other purposes, such as cleaning up CMS page orders or providing a package with clean sample data.

Read more ...

The Cultural Conflict of Environment

Last Updated on Wednesday, 23 February 2011 01:58pm Written by spunky Tuesday, 22 February 2011 10:39pm

If you grew up in India you would be a product of that environment. This is probably the first and only time you can blame, or credit, your environment because you have absolutely no say in the matter. Beyond this point your environment becomes increasingly decided. At school you decide to hang out with the stoners or the nerds and you will likely adopt that culture/head space. It is feeble to blame the culture, environment, location or anything else. When in fact you arrived here solely based on your decisions.

Read more ...

Categories

Site Search