<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Structured Randomness</title>
	
	<link>http://diovo.com</link>
	<description>Startups, Programming and stuff...</description>
	<lastBuildDate>Mon, 16 Jan 2012 18:23:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Diovo" /><feedburner:info uri="diovo" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>Diovo</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Chess programming – The 0x88 board representation</title>
		<link>http://feedproxy.google.com/~r/Diovo/~3/c1RYdRR5-rU/</link>
		<comments>http://diovo.com/2012/01/chess-programming-the-0x88-board-representation/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 18:49:02 +0000</pubDate>
		<dc:creator>Niyaz</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Chess]]></category>

		<guid isPermaLink="false">http://diovo.com/?p=2257</guid>
		<description><![CDATA[Welcome back. Sorry that this installment is a little late. I&#8217;ve got a day job you know. This is part 2 of the series: Chess programming. Read part 1 here. In this part we will be discussing the 0x88 board representation method. Some major changes When I said we will be changing the design decisions [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #888888;"><em>Welcome back. Sorry that this installment is a little late. I&#8217;ve got a day job you know.</em></span></p>
<p>This is part 2 of the series: <strong>Chess programming</strong>. Read <a title="Chess Programming in JavaScript – Part 1" href="http://diovo.com/2011/12/chess-programming-in-javascript-part-1/">part 1 here</a>.</p>
<p>In this part we will be discussing the <strong>0x88 board representation method</strong>.</p>
<h3>Some major changes</h3>
<p>When I said we will be changing the design decisions if required, I did not anticipate that I will be changing these too soon; but there you go &#8211; We have to change the board representation of our game. I will tell you why.</p>
<p>If you remember, we were using a two-dimensional array representation for our chess board:</p>
<div id="gist-1524950" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">var</span> <span class="nx">board</span> <span class="o">=</span> <span class="p">[[</span><span class="nx">BLACK_ROOK</span><span class="p">,</span> <span class="nx">BLACK_KNIGHT</span><span class="p">,</span> <span class="nx">BLACK_BISHOP</span><span class="p">,</span> <span class="nx">BLACK_QUEEN</span><span class="p">,</span> <span class="nx">BLACK_KING</span><span class="p">,</span> <span class="nx">BLACK_BISHOP</span><span class="p">,</span> <span class="nx">BLACK_KNIGHT</span><span class="p">,</span> <span class="nx">BLACK_ROOK</span><span class="p">],</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">],</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">],</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="nx">WHITE_ROOK</span><span class="p">,</span> <span class="nx">WHITE_KNIGHT</span><span class="p">,</span> <span class="nx">WHITE_BISHOP</span><span class="p">,</span> <span class="nx">WHITE_QUEEN</span><span class="p">,</span> <span class="nx">WHITE_KING</span><span class="p">,</span> <span class="nx">WHITE_BISHOP</span><span class="p">,</span> <span class="nx">WHITE_KNIGHT</span><span class="p">,</span> <span class="nx">WHITE_ROOK</span><span class="p">]];</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524950/857feb9c3748631099c3282fe41afcd153db282a/chess.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524950#file_chess.js" style="float:right;margin-right:10px;color:#666">chess.js</a>
            <a href="https://gist.github.com/1524950">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This was an obvious choice because it is so simple and it is the first data-structure that comes to mind when you think about representing a chess board in memory. The problem with this approach is that a two-dimensional array is not very efficient when we want to do a lot of number crunching.</p>
<p>In chess programs, every cycle we save is very important. Later when discuss <em>game trees and search</em> you will see that the performance of your chess engine depends on the ability of the program to crunch numbers. The faster it can calculate, the better the chess engine will do. Keeping this in mind, we are going to use the 0x88 representation for our chess board.</p>
<h3>The 0x88 board representation</h3>
<p>The first difference is between 0x88 representation and the two-dimensional array representation is that the 0x88 is a one-dimensional array. This gives us a little bit of performance improvement over the two-dimensional arrays because since there is only one index, the CPU will have to do less work to do the look-up.</p>
<p>The second and the most important difference is that the 0x88 representation contains 128 squares instead of the 64 squares in our previous representation. This means that it has double the number of squares of a normal chess board. What are these extra squares and why are they used? Let us see how it looks:</p>
<p><img class="alignnone size-full wp-image-2271" title="0x88 chess board representation" src="http://diovo.com/wp-content/uploads/2012/01/0x88.png" alt="" width="550" height="277" /></p>
<p>It looks like the previous representation, except that we have an extra hidden (empty) chess board fused to the right of the original board.</p>
<p>Since this is a one-dimensional representation, the squares are numbered sequentially from 0 to 127. The first row starts from 0 and the second row starts from 16. The numbers 8 through 15 belong to the hidden board squares. Let us see how this numbering scheme looks on the board in <em>hexadecimal notation</em>:</p>
<p><img class="alignnone size-full wp-image-2275" title="0x88 representation with HEX numbers" src="http://diovo.com/wp-content/uploads/2012/01/0x88-hex.png" alt="" width="550" height="278" /></p>
<p>Let us see how this bizarre scheme is better than the two-dimensional array approach. Let us say you looking at a rook on square 7. You want to find out where this rook can move to. As you know, a rook can move to any square in the same file or in the same rank. We can find out the squares on the same file by repeatedly adding/subtracting 16 to the current square:</p>
<div id="gist-1593161" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="mh">0x07</span> <span class="o">=</span> <span class="mi">7</span></div><div class='line' id='LC2'><span class="mh">0x17</span> <span class="o">=</span> <span class="mi">7</span> <span class="o">+</span> <span class="mi">16</span></div><div class='line' id='LC3'><span class="mh">0x27</span> <span class="o">=</span> <span class="mi">7</span> <span class="o">+</span> <span class="mi">16</span> <span class="o">+</span><span class="mi">16</span></div><div class='line' id='LC4'><span class="mh">0x37</span> <span class="o">=</span> <span class="mi">7</span> <span class="o">+</span> <span class="mi">16</span> <span class="o">+</span> <span class="mi">16</span> <span class="o">+</span> <span class="mi">16</span></div><div class='line' id='LC5'><span class="mh">0x47</span> <span class="o">=</span> <span class="mi">7</span> <span class="o">+</span> <span class="mi">16</span> <span class="o">+</span> <span class="mi">16</span> <span class="o">+</span> <span class="mi">16</span> <span class="o">+</span> <span class="mi">16</span></div><div class='line' id='LC6'><span class="mh">0x57</span> <span class="o">=</span> <span class="mi">7</span> <span class="o">+</span> <span class="mi">16</span> <span class="o">+</span> <span class="mi">16</span> <span class="o">+</span> <span class="mi">16</span> <span class="o">+</span> <span class="mi">16</span> <span class="o">+</span> <span class="mi">16</span></div><div class='line' id='LC7'><span class="c1">// etc</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1593161/f663a05eb190c246011edd7cff06cf4f7bef92ff/gistfile1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1593161#file_gistfile1.js" style="float:right;margin-right:10px;color:#666">gistfile1.js</a>
            <a href="https://gist.github.com/1593161">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Similarly we can find out the squares on the same rank by repeatedly adding/subtracting 1 to the current square:</p>
<div id="gist-1593175" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="mh">0x07</span> <span class="o">=</span> <span class="mi">7</span></div><div class='line' id='LC2'><span class="mh">0x06</span> <span class="o">=</span> <span class="mi">7</span> <span class="o">-</span> <span class="mi">1</span></div><div class='line' id='LC3'><span class="mh">0x05</span> <span class="o">=</span> <span class="mi">7</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="mi">1</span></div><div class='line' id='LC4'><span class="mh">0x04</span> <span class="o">=</span> <span class="mi">7</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="mi">1</span></div><div class='line' id='LC5'><span class="mh">0x03</span> <span class="o">=</span> <span class="mi">7</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="mi">1</span></div><div class='line' id='LC6'><span class="mh">0x02</span> <span class="o">=</span> <span class="mi">7</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="mi">1</span></div><div class='line' id='LC7'><span class="c1">// etc</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1593175/8754a2c15350a26750e16bdf37960ff69da2a235/gistfile1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1593175#file_gistfile1.js" style="float:right;margin-right:10px;color:#666">gistfile1.js</a>
            <a href="https://gist.github.com/1593175">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>So instead of working with the row numbers or column numbers in the two-dimensional approach, we just work with the square number in the 0x88 approach. But the major advantage of the 0x88 representation has something to do with the hexadecimal number 0x88. You see, when we search for squares in the same file or the same rank, we will eventually overflow the numbers in the current rank/file and go beyond the valid chess board.</p>
<p>If you are using two-dimensional array notation, you have to multiple checks to figure whether your newly calculated row and column are inside the board or not:</p>
<div id="gist-1593251" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">if</span> <span class="p">(</span><span class="nx">row</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nx">row</span> <span class="o">&lt;</span> <span class="mi">8</span> <span class="o">&amp;&amp;</span> <span class="nx">column</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nx">column</span> <span class="o">&lt;</span> <span class="mi">8</span> <span class="p">){</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// valid move</span></div><div class='line' id='LC3'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1593251/4a221aaf489b36460cb6f827be1d63a72a206d65/gistfile1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1593251#file_gistfile1.js" style="float:right;margin-right:10px;color:#666">gistfile1.js</a>
            <a href="https://gist.github.com/1593251">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Here we have 4 comparison operations &#8211; which means 4 branches in the machine opcode and that can be a huge performance hit.</p>
<p>If you are using a single-dimensional array representation with just 64 squares, you will have to do:</p>
<div id="gist-1594896" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">if</span> <span class="p">(</span><span class="nx">square</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nx">square</span> <span class="o">&lt;</span> <span class="mi">64</span><span class="p">){</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// valid move</span></div><div class='line' id='LC3'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1594896/66ff7007eb224c21668f7691b456059468dc21a7/gistfile1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1594896#file_gistfile1.js" style="float:right;margin-right:10px;color:#666">gistfile1.js</a>
            <a href="https://gist.github.com/1594896">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>That is still two tests. Can we reduce it to a single test? Turns out, we can.</p>
<p>It is easy to know if you are in the current board by just checking whether the 7th bit is set or not:</p>
<div id="gist-1594941" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">Decimal</span> <span class="mi">62</span> <span class="o">=</span> <span class="mi">0111110</span> <span class="nx">Binary</span></div><div class='line' id='LC2'><span class="nx">Decimal</span> <span class="mi">63</span> <span class="o">=</span> <span class="mi">0111111</span> <span class="nx">Binary</span></div><div class='line' id='LC3'><span class="nx">Decimal</span> <span class="mi">64</span> <span class="o">=</span> <span class="mi">1000000</span> <span class="nx">Binary</span></div><div class='line' id='LC4'><span class="nx">Decimal</span> <span class="mi">65</span> <span class="o">=</span> <span class="mi">1000001</span> <span class="nx">Binary</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1594941/a013aad58ab007abf8c0da08ed6c9468208c6c1b/gistfile1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1594941#file_gistfile1.js" style="float:right;margin-right:10px;color:#666">gistfile1.js</a>
            <a href="https://gist.github.com/1594941">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>As you can see, for the immediate numbers greater than 63, the 7th bit is 1 while for numbers smaller than or equal to 63, the bit is 0. Turns out that this test is enough to check whether the number is lesser than 0 too. This is attributed to the the way numbers are represented in <em>Two&#8217;s complement</em> representation.</p>
<div id="gist-1595834" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">Decimal</span>  <span class="mi">2</span> <span class="o">=</span> <span class="mi">00000010</span> <span class="nx">Binary</span></div><div class='line' id='LC2'><span class="nx">Decimal</span>  <span class="mi">1</span> <span class="o">=</span> <span class="mi">00000001</span> <span class="nx">Binary</span></div><div class='line' id='LC3'><span class="nx">Decimal</span>  <span class="mi">0</span> <span class="o">=</span> <span class="mi">00000000</span> <span class="nx">Binary</span></div><div class='line' id='LC4'><span class="nx">Decimal</span> <span class="o">-</span><span class="mi">1</span> <span class="o">=</span> <span class="mi">11111111</span> <span class="nx">Binary</span></div><div class='line' id='LC5'><span class="nx">Decimal</span> <span class="o">-</span><span class="mi">2</span> <span class="o">=</span> <span class="mi">11111110</span> <span class="nx">Binary</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1595834/a2745e425ecbf766fbfbff131557253f49b61097/gistfile1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1595834#file_gistfile1.js" style="float:right;margin-right:10px;color:#666">gistfile1.js</a>
            <a href="https://gist.github.com/1595834">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This means that just by checking whether the 7th bit is set, we can decide whether a square is inside the chess board or not:</p>
<div id="gist-1595848" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="nx">square</span> <span class="o">&amp;</span> <span class="mh">0x40</span><span class="p">)){</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// valid move</span></div><div class='line' id='LC3'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1595848/1adf8868a2f01509a93dcd7829af0c74012307d6/gistfile1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1595848#file_gistfile1.js" style="float:right;margin-right:10px;color:#666">gistfile1.js</a>
            <a href="https://gist.github.com/1595848">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>With this simple test we can now figure out whether a square is inside the chess board or not. Even with this simple test, there are some other problems we want to address. Assume that our rook is in the square 7 and you want to generate all the rook moves from this position. If you add 1 to 7, you <em>wrap around</em> to the next rank (row) and this is not a valid square where your rook can move to.</p>
<p><img class="alignnone size-full wp-image-2292" style="border-style: initial; border-color: initial;" title="" src="http://diovo.com/wp-content/uploads/2012/01/07-082.png" alt="" width="350" height="317" /></p>
<p>There is no elegant way to solve this <em>wrapping around problem</em> using the 64-square board. This is where the 0x88 representation comes into picture.</p>
<p>In the 0x88 representation, since we have 128 squares instead of 64, we will have to check whether the 8th bit is set or not to know whether a square is inside the chess board or not. For this purpose we use the constant 0x80 instead of 0x40. Further, if you look closely at our initial representations of the 0x88 boards, you can see that the squares numbers in the hidden board has the 4th bit set while the square numbers in the actual valid board all have the 4th bit as 0. So we can use the constant 0x08 to test whether a square is in the valid part of 0x88 representation or not. Combining both the constants 0x80 and 0x08 we get the new constant 0x88.</p>
<div id="gist-1595963" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="nx">square</span> <span class="o">&amp;</span> <span class="mh">0x88</span><span class="p">)){</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// valid move</span></div><div class='line' id='LC3'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1595963/17bde6a550cbd03efb0b88883db12d733bb40af2/gistfile1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1595963#file_gistfile1.js" style="float:right;margin-right:10px;color:#666">gistfile1.js</a>
            <a href="https://gist.github.com/1595963">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>So using a simple elegant test, we are able to figure out whether the square is valid or not. The wrap-around problem is also solved here in this case because even if you move right from the square 7, you get to square 8 which is an invalid square and your move in that direction will end there.</p>
<p>Another advantage of using the 0x88 representation is that it is easy to figure out the relationship between two squares if you have the square numbers. For example, if the difference between two squares is 1, you can always assume that they are next to each other in the same rank. If the difference is 16, they are next to each other on the same file (column). Look at our initial 0x88 representation to verify this. This is an important concept which we will use throughout the chess program.</p>
<h3>Piece Values</h3>
<p>Taking into account <a href="http://diovo.com/2011/12/chess-programming-in-javascript-part-1/comment-page-1/#comment-16609">a comment from Alejandro on Part 1</a>, I am changing the values of the chess pieces. The idea is that the value of chess pieces should not be substituted for the pieces themselves. We can figure out the values of the pieces later as required when we do evaluation. Right now we want better values to represent the pieces so that the representation will some way help us write better/faster code for the chess program.</p>
<p>Here are the new values that I am going to use:</p>
<div id="gist-1596077" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">var</span> <span class="nx">WHITE_PAWN</span> <span class="o">=</span> <span class="mh">0x01</span><span class="p">;</span></div><div class='line' id='LC2'><span class="kd">var</span> <span class="nx">WHITE_KNIGHT</span> <span class="o">=</span> <span class="mh">0x02</span><span class="p">;</span></div><div class='line' id='LC3'><span class="kd">var</span> <span class="nx">WHITE_KING</span> <span class="o">=</span> <span class="mh">0x03</span><span class="p">;</span></div><div class='line' id='LC4'><span class="kd">var</span> <span class="nx">WHITE_BISHOP</span> <span class="o">=</span> <span class="mh">0x05</span><span class="p">;</span></div><div class='line' id='LC5'><span class="kd">var</span> <span class="nx">WHITE_ROOK</span> <span class="o">=</span> <span class="mh">0x06</span><span class="p">;</span></div><div class='line' id='LC6'><span class="kd">var</span> <span class="nx">WHITE_QUEEN</span> <span class="o">=</span> <span class="mh">0x07</span><span class="p">;</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="kd">var</span> <span class="nx">BLACK_PAWN</span> <span class="o">=</span> <span class="mh">0x09</span><span class="p">;</span></div><div class='line' id='LC9'><span class="kd">var</span> <span class="nx">BLACK_KNIGHT</span> <span class="o">=</span> <span class="mh">0x0A</span><span class="p">;</span></div><div class='line' id='LC10'><span class="kd">var</span> <span class="nx">BLACK_KING</span> <span class="o">=</span> <span class="mh">0x0B</span><span class="p">;</span></div><div class='line' id='LC11'><span class="kd">var</span> <span class="nx">BLACK_BISHOP</span> <span class="o">=</span> <span class="mh">0x0D</span><span class="p">;</span></div><div class='line' id='LC12'><span class="kd">var</span> <span class="nx">BLACK_ROOK</span> <span class="o">=</span> <span class="mh">0x0E</span><span class="p">;</span></div><div class='line' id='LC13'><span class="kd">var</span> <span class="nx">BLACK_QUEEN</span> <span class="o">=</span> <span class="mh">0x0F</span><span class="p">;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1596077/dc5bef944624b573712f045b645d3035fd3b27a9/gistfile1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1596077#file_gistfile1.js" style="float:right;margin-right:10px;color:#666">gistfile1.js</a>
            <a href="https://gist.github.com/1596077">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Notice that we are no longer using negative values for the black pieces.</p>
<p>Notice two things:</p>
<ol>
<li>The difference between any white piece and the corresponding black piece is that the 4th bit is set in the binary representation of the numbers. This means that I can find out whether a piece is black or white by doing something like <code>piece &amp; 0x8</code></li>
<li>The sliding pieces (the Queen, the Bishop and the Rook ) all have the 3rd bit set. This allows us to use the test <code>piece &amp; 0x4</code> to find out whether a piece is a sliding piece or not.</li>
</ol>
<p>We will have to use all these small things to our advantage if we want a decent chess playing program. The scariest part is that bit-wise operations are not very fast in JavaScript thanks to a few terrible things it does in the background to convert numbers between 64-bit and 32-bit before applying bit-wise operations. We will see how it goes.</p>
<p>As always, get the latest code from the <a href="https://github.com/niyazpk/Chess.js">Chess.js</a> repository.</p>
<p>Wait for the next episode &#8211; <em>Part 3: Validating User Moves</em></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Diovo?a=c1RYdRR5-rU:anNBd47i_Ts:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=c1RYdRR5-rU:anNBd47i_Ts:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=c1RYdRR5-rU:anNBd47i_Ts:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=c1RYdRR5-rU:anNBd47i_Ts:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Diovo?i=c1RYdRR5-rU:anNBd47i_Ts:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=c1RYdRR5-rU:anNBd47i_Ts:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Diovo?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=c1RYdRR5-rU:anNBd47i_Ts:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=c1RYdRR5-rU:anNBd47i_Ts:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Diovo?i=c1RYdRR5-rU:anNBd47i_Ts:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Diovo/~4/c1RYdRR5-rU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://diovo.com/2012/01/chess-programming-the-0x88-board-representation/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		<feedburner:origLink>http://diovo.com/2012/01/chess-programming-the-0x88-board-representation/</feedburner:origLink></item>
		<item>
		<title>Chess Programming in JavaScript – Part 1</title>
		<link>http://feedproxy.google.com/~r/Diovo/~3/AJ5B1HFwpJc/</link>
		<comments>http://diovo.com/2011/12/chess-programming-in-javascript-part-1/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 21:01:33 +0000</pubDate>
		<dc:creator>Niyaz</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Chess]]></category>
		<category><![CDATA[Programm]]></category>

		<guid isPermaLink="false">http://diovo.com/?p=2193</guid>
		<description><![CDATA[Just for kicks, I am trying to write a chess playing program in JavaScript. I will document the process of development so that anyone who wants to learn the basics of chess programming will have an end to end reference to look at before they start. [This article is a draft version. Definitely needs a spell check] I will try to document [...]]]></description>
			<content:encoded><![CDATA[<p><em style="color: #888888;">Just for kicks, I am trying to write a chess playing program in JavaScript. I will document the process of development so that anyone who wants to learn the basics of chess programming will have an end to end reference to look at before they start. <em><span style="color: #888888;">[This article is a draft version. Definitely needs a spell check]</span></em></em></p>
<p>I will try to document and discuss every small detail of the implementation so that a reader will not have any difficulty in writing his/her own chess playing program after reading this. At the same time, I don&#8217;t want to go into the details of the game itself.</p>
<p><a title="chess by nestor galina, on Flickr" href="http://www.flickr.com/photos/nestorgalina/3707322819/"><img src="http://farm3.staticflickr.com/2641/3707322819_f336ff8aac.jpg" alt="chess" width="500" height="290" /></a></p>
<p><strong>Prerequisites</strong>: I would assume that you have a good grasp about the rules of the game and have experience in playing chess. You should definitely be experienced in computer programming. You need not be experienced in JavaScript itself. As long as you have experience in some programming language, I hope that you can read the JavaScript code here and understand what is going on. I am going to use a bit of HTML/CSS in this tutorial, but that will be mainly for drawing the chess board output to the screen. This is independent of the chess programming logic, so you need not worry much if you are not well versed in HTML/CSS.</p>
<p>Of course the fact that we are using JavaScript for this programming problem is a bit of a challenge. I am not sure that JavaScript is as fast as other languages in raw computation speed which we will require abundantly for number crunching in a chess playing program.</p>
<h3>Our Goal</h3>
<p>At the end of this exercise, we hope to have at least:</p>
<ul>
<li>A chess UI with which the user can interact with</li>
<li>A chess AI against with the user can play</li>
</ul>
<p>Note: I don&#8217;t have a completed source code for this article series (even though I have <a title="Chess programming stories" href="http://diovo.com/2010/01/chess-programming-and-such/">previous experience</a> in chess programming). I am writing the code as I go. What this means is that I will make a lot of mistakes in the code and as well as in the decisions I make on how to go about structuring the whole application. If you find something wrong, let me know in the comments.</p>
<p>Okay now, lets dive in.</p>
<h3>Displaying the board</h3>
<p>First, let us draw a chess board on the screen. At the start of the game,  a chess board consists of 64 squares and 32 chess pieces arranged in a predefined manner. We are going to do two things here:</p>
<ol>
<li>Find a way to represent the board and the chess pieces in our program.</li>
<li>Write a function to display the current board on the screen.</li>
</ol>
<p>Here is our initial take on the HTML file:</p>
<div id="gist-1523913" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">&lt;!doctype html&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;html&gt;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;head&gt;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;meta</span> <span class="na">charset=</span><span class="s">&#39;utf-8&#39;</span><span class="nt">&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;title&gt;</span>JavaScript Chess<span class="nt">&lt;/title&gt;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">&quot;stylesheet&quot;</span> <span class="na">type=</span><span class="s">&quot;text/css&quot;</span> <span class="na">href=</span><span class="s">&quot;css/style.css&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/head&gt;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;body&gt;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&#39;board&#39;</span><span class="nt">&gt;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/div&gt;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/body&gt;</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;script </span><span class="na">src=</span><span class="s">&#39;scripts/jquery-1.7.1.min.js&#39;</span><span class="nt">&gt;&lt;/script&gt;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;script </span><span class="na">src=</span><span class="s">&#39;scripts/chess.js&#39;</span><span class="nt">&gt;&lt;/script&gt;</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'><span class="nt">&lt;/html&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1523913/f263897df4019e05e38703933e819dc10ea83143/index.html" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1523913#file_index.html" style="float:right;margin-right:10px;color:#666">index.html</a>
            <a href="https://gist.github.com/1523913">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>There is nothing much in the file. We have a single <em>div</em> to hold the chess board. We have included <em>style.css</em> for giving styles to the chess board. We have included jQuery for DOM manipulation, which right now mainly means <em>drawing the board</em>. For this purpose, including the jQuery is definitely an overkill, but I used it anyway so that our code will be clean. It should be easy for you to remove jQuery and just plain JavaScript to duplicate our function to draw the board.</p>
<h3>Board Representation</h3>
<p>We need a way to represent the chess board and the chess pieces. We will represent the board as a two-dimensional array. This way we can refer to a piece in any row/column (also called rank/file). We have to think about this design decision for some time because there may be performance implications for this decision. For performance reasons, we may later decide to use <a href="http://en.wikipedia.org/wiki/Bitboard">bit-boards</a> to represent the board and the pieces, so we should take to write code in such a way that it will be easy to switch between the array representation and bit-board representation.</p>
<p>So let us start by defining the different pieces in chess. We are just enumerating the possible pieces here:</p>
<div id="gist-1524869" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">var</span> <span class="nx">WHITE_KING</span> <span class="o">=</span> <span class="mi">100</span><span class="p">;</span></div><div class='line' id='LC2'><span class="kd">var</span> <span class="nx">WHITE_QUEEN</span> <span class="o">=</span> <span class="mi">90</span><span class="p">;</span></div><div class='line' id='LC3'><span class="kd">var</span> <span class="nx">WHITE_ROOK</span> <span class="o">=</span> <span class="mi">50</span><span class="p">;</span></div><div class='line' id='LC4'><span class="kd">var</span> <span class="nx">WHITE_BISHOP</span> <span class="o">=</span> <span class="mi">31</span><span class="p">;</span></div><div class='line' id='LC5'><span class="kd">var</span> <span class="nx">WHITE_KNIGHT</span> <span class="o">=</span> <span class="mi">30</span><span class="p">;</span></div><div class='line' id='LC6'><span class="kd">var</span> <span class="nx">WHITE_PAWN</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><span class="kd">var</span> <span class="nx">BLACK_KING</span> <span class="o">=</span> <span class="o">-</span><span class="nx">WHITE_KING</span><span class="p">;</span></div><div class='line' id='LC9'><span class="kd">var</span> <span class="nx">BLACK_QUEEN</span> <span class="o">=</span> <span class="o">-</span><span class="nx">WHITE_QUEEN</span><span class="p">;</span></div><div class='line' id='LC10'><span class="kd">var</span> <span class="nx">BLACK_ROOK</span> <span class="o">=</span> <span class="o">-</span><span class="nx">WHITE_ROOK</span><span class="p">;</span></div><div class='line' id='LC11'><span class="kd">var</span> <span class="nx">BLACK_BISHOP</span> <span class="o">=</span> <span class="o">-</span><span class="nx">WHITE_BISHOP</span><span class="p">;</span></div><div class='line' id='LC12'><span class="kd">var</span> <span class="nx">BLACK_KNIGHT</span> <span class="o">=</span> <span class="o">-</span><span class="nx">WHITE_KNIGHT</span><span class="p">;</span></div><div class='line' id='LC13'><span class="kd">var</span> <span class="nx">BLACK_PAWN</span> <span class="o">=</span> <span class="o">-</span><span class="nx">WHITE_PAWN</span><span class="p">;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524869/ccfcc3181a7449ae1094b236ea8714b7b12b7e56/chess.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524869#file_chess.js" style="float:right;margin-right:10px;color:#666">chess.js</a>
            <a href="https://gist.github.com/1524869">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>As you can see we are trying to give different values for each piece in the chess board. I am using ALL_CAPS notation for the variable names to denote that they are constants. JavaScript does not have a way to denote constants (yet).</p>
<p>The pieces are given values based on their relative importance. The Pawn is given a value of 10 and then each other piece is given a value based on how valuable the piece is compared to a Pawn. Think &#8211; how many Pawns do we need to make up for a Queen?</p>
<p>You can see more discussion about it <a href="http://en.wikipedia.org/wiki/Chess_piece_relative_value">here</a>. Normally the value of a Pawn is considered as 1 and the relative values of other pieces are calculated based on that. Here we are using a base value of 10 for Pawn, Just so that the relative values of the Bishop and the Knight can be different at the same time whole numbers. More about this later.</p>
<p>The value of the King is not important because there is no move in chess where you consider sacrificing your King. So you can use any value of the King. We are using 100 here. You can as well use 1.</p>
<p>As you can see, the values given for the black pieces are just the negative values of the values given for the same pieces in white. This is because later in the game when we evaluate board positions, we can just add up all the numbers and see who has an advantage.</p>
<p>Now we have the pieces and what we need is a board to place the pieces:</p>
<div id="gist-1524962" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">var</span> <span class="nx">board</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">]];</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524962/a432ca37d23f95cbd732e7ad0357e247630f2027/chess.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524962#file_chess.js" style="float:right;margin-right:10px;color:#666">chess.js</a>
            <a href="https://gist.github.com/1524962">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>As we already discussed, the board is an 8×8 array. The values represent pieces. A zero value in a cell means that the square in the board is empty. As you can see, the whole board is empty at this point. Let us add the pieces to the board so that it resembles the <a href="http://en.wikipedia.org/wiki/Rules_of_chess#Initial_setup">starting position</a> of a standard chess game:</p>
<div id="gist-1524950" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">var</span> <span class="nx">board</span> <span class="o">=</span> <span class="p">[[</span><span class="nx">BLACK_ROOK</span><span class="p">,</span> <span class="nx">BLACK_KNIGHT</span><span class="p">,</span> <span class="nx">BLACK_BISHOP</span><span class="p">,</span> <span class="nx">BLACK_QUEEN</span><span class="p">,</span> <span class="nx">BLACK_KING</span><span class="p">,</span> <span class="nx">BLACK_BISHOP</span><span class="p">,</span> <span class="nx">BLACK_KNIGHT</span><span class="p">,</span> <span class="nx">BLACK_ROOK</span><span class="p">],</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">,</span> <span class="nx">BLACK_PAWN</span><span class="p">],</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">,</span> <span class="nx">WHITE_PAWN</span><span class="p">],</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="nx">WHITE_ROOK</span><span class="p">,</span> <span class="nx">WHITE_KNIGHT</span><span class="p">,</span> <span class="nx">WHITE_BISHOP</span><span class="p">,</span> <span class="nx">WHITE_QUEEN</span><span class="p">,</span> <span class="nx">WHITE_KING</span><span class="p">,</span> <span class="nx">WHITE_BISHOP</span><span class="p">,</span> <span class="nx">WHITE_KNIGHT</span><span class="p">,</span> <span class="nx">WHITE_ROOK</span><span class="p">]];</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524950/857feb9c3748631099c3282fe41afcd153db282a/chess.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524950#file_chess.js" style="float:right;margin-right:10px;color:#666">chess.js</a>
            <a href="https://gist.github.com/1524950">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>I hope it is not difficult to guess what happened here. We are just placing the pieces we already defined on the chess board according to the rules of the game.</p>
<p><em>Now that looks awesome doesn&#8217;t it?</em></p>
<p><em>No. It does not.</em></p>
<p>We need to show this board in a better way so that people will be able to see the pieces rather than cryptic notations on the screen.</p>
<p>So we define a <em>drawBoard()</em> function which will draw the board to the screen.</p>
<div id="gist-1524996" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">function</span> <span class="nx">drawBoard</span><span class="p">(</span><span class="nx">board</span><span class="p">){</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">for</span><span class="p">(</span> <span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span> <span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="mi">8</span> <span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">){</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">str</span> <span class="o">+=</span> <span class="s1">&#39;&lt;div class=&quot;row&quot;&gt;&#39;</span><span class="p">;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">for</span><span class="p">(</span> <span class="kd">var</span> <span class="nx">j</span> <span class="o">=</span> <span class="mi">0</span> <span class="p">;</span> <span class="nx">j</span> <span class="o">&lt;</span> <span class="mi">8</span> <span class="p">;</span> <span class="nx">j</span><span class="o">++</span> <span class="p">){</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">str</span> <span class="o">+=</span> <span class="s1">&#39;&lt;div class=&quot;column &#39;</span> <span class="o">+</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">(</span> <span class="p">(</span><span class="nx">i</span> <span class="o">+</span> <span class="nx">j</span><span class="p">)</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">===</span> <span class="mi">0</span> <span class="o">?</span> <span class="s1">&#39;light&#39;</span><span class="o">:</span> <span class="s1">&#39;dark&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;&quot;&gt;&#39;</span> <span class="o">+</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="s1">&#39;&lt;div class=&quot;&#39;</span> <span class="o">+</span> <span class="nx">getPieceName</span><span class="p">(</span><span class="nx">board</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="nx">j</span><span class="p">])</span> <span class="o">+</span> <span class="s1">&#39;&quot;&gt;&lt;/div&gt;&#39;</span> <span class="o">+</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="s1">&#39;&lt;/div&gt;&#39;</span><span class="p">;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">str</span> <span class="o">+=</span> <span class="s1">&#39;&lt;/div&gt;&#39;</span><span class="p">;</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#board&#39;</span><span class="p">).</span><span class="nx">append</span><span class="p">(</span><span class="nx">str</span><span class="p">);</span></div><div class='line' id='LC14'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1524996/85884d3875819da7d2c548353ac6b3d3743b44b7/chess.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1524996#file_chess.js" style="float:right;margin-right:10px;color:#666">chess.js</a>
            <a href="https://gist.github.com/1524996">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Here I am constructing an HTML string that will paint the board and then append that HTML string to the board div that we have in our index.html file.</p>
<p>In the first loop (row loop) we are creating a row and in the inner loop (column loop) we are inserting 8 columns to the row.</p>
<p>To understand the code inside the inner loop better, let us see an example output of a single iteration of the inner loop:</p>
<div id="gist-1525007" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;column dark&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;WHITE_KNIGHT&quot;</span><span class="nt">&gt;&lt;/div&gt;</span></div><div class='line' id='LC3'><span class="nt">&lt;/div&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1525007/67cf47a4e1ff1e793dc11220d410556e6c7225ac/exmaple.html" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1525007#file_exmaple.html" style="float:right;margin-right:10px;color:#666">exmaple.html</a>
            <a href="https://gist.github.com/1525007">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>In this example, the outer div has two classes associated with it. One is <em>column</em>, which is generic and it just means that this is a column. The second one is <em>dark</em> which denotes that this is a dark square. As you already know, a chess board contains two different types of squares, dark and light. We use this class to give a dark background color to this square. Similarly, lighter cells will have a <em>light</em> class and will be styled accordingly. To alternate between the dark and light squares, we use the test <code>(i + j) % 2 === 0</code> and it is easy to understand why if you work it out using pen and paper.</p>
<p>The inner div also has a class. This time the class says <em>WHITE_KNIGHT</em>. This denotes the piece which is present in that square in the board. We use this class to show the corresponding piece on the screen:</p>
<div id="gist-1525030" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.WHITE_KNIGHT</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/wKnight.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC3'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1525030/3a0fdee245571aaa31a5f3ebe99f9f53bc6e5982/style.css" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1525030#file_style.css" style="float:right;margin-right:10px;color:#666">style.css</a>
            <a href="https://gist.github.com/1525030">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Similarly we style all the classes so that the icons for the pieces will be loaded whenever the class for corresponding piece is present.</p>
<p>Okay so how did we get this class value as WHITE_KNIGHT ? It is done by this piece of code: <code>getPieceName(board[i][j])</code></p>
<p>What does it do? It gets the value of the piece at the board position [i,j] and then passes this value to a function called <em>getPieceName()</em>. What does <em>getPieceName()</em> do? Well it just returns the name of the piece in string format:</p>
<div id="gist-1525051" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">function</span> <span class="nx">getPieceName</span><span class="p">(</span><span class="nx">pieceValue</span><span class="p">){</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">switch</span> <span class="p">(</span><span class="nx">pieceValue</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nx">WHITE_KING</span><span class="o">:</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;WHITE_KING&#39;</span><span class="p">;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nx">WHITE_QUEEN</span><span class="o">:</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;WHITE_QUEEN&#39;</span><span class="p">;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nx">WHITE_ROOK</span><span class="o">:</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;WHITE_ROOK&#39;</span><span class="p">;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nx">WHITE_BISHOP</span><span class="o">:</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;WHITE_BISHOP&#39;</span><span class="p">;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nx">WHITE_KNIGHT</span><span class="o">:</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;WHITE_KNIGHT&#39;</span><span class="p">;</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nx">WHITE_PAWN</span><span class="o">:</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;WHITE_PAWN&#39;</span><span class="p">;</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nx">BLACK_KING</span><span class="o">:</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;BLACK_KING&#39;</span><span class="p">;</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nx">BLACK_QUEEN</span><span class="o">:</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;BLACK_QUEEN&#39;</span><span class="p">;</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nx">BLACK_ROOK</span><span class="o">:</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;BLACK_ROOK&#39;</span><span class="p">;</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nx">BLACK_BISHOP</span><span class="o">:</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;BLACK_BISHOP&#39;</span><span class="p">;</span></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nx">BLACK_KNIGHT</span><span class="o">:</span></div><div class='line' id='LC35'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;BLACK_KNIGHT&#39;</span><span class="p">;</span></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nx">BLACK_PAWN</span><span class="o">:</span></div><div class='line' id='LC38'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;BLACK_PAWN&#39;</span><span class="p">;</span></div><div class='line' id='LC39'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC40'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC41'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">default</span><span class="o">:</span></div><div class='line' id='LC42'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s1">&#39;EMPTY&#39;</span><span class="p">;</span></div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC44'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC45'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1525051/cbb3541ad2d877bae43215971974447835ba32c9/chess.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1525051#file_chess.js" style="float:right;margin-right:10px;color:#666">chess.js</a>
            <a href="https://gist.github.com/1525051">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Pretty straight forward. Ain&#8217;t it?</p>
<p>Now that we have all the classes ready, we need to style these so that all the icons come up on the board. Let us take a look at the complete <em>style.css</em> file:</p>
<div id="gist-1525037" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nf">#board</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">height</span><span class="o">:</span> <span class="m">400px</span><span class="p">;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">width</span><span class="o">:</span> <span class="m">400px</span><span class="p">;</span></div><div class='line' id='LC4'><span class="p">}</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="nf">#board</span> <span class="nc">.column</span> <span class="p">{</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">height</span><span class="o">:</span> <span class="m">50px</span><span class="p">;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">width</span><span class="o">:</span> <span class="m">50px</span><span class="p">;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="m">#fee472</span><span class="p">;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">float</span><span class="o">:</span> <span class="k">left</span><span class="p">;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">font-size</span><span class="o">:</span> <span class="m">45px</span><span class="p">;</span></div><div class='line' id='LC12'><span class="p">}</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'><span class="nf">#board</span> <span class="nc">.column.dark</span> <span class="p">{</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="m">#00B392</span><span class="p">;</span></div><div class='line' id='LC16'><span class="p">}</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nt">div</span> <span class="p">{</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">height</span><span class="o">:</span> <span class="m">50px</span><span class="p">;</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">width</span><span class="o">:</span> <span class="m">50px</span><span class="p">;</span></div><div class='line' id='LC21'><span class="p">}</span></div><div class='line' id='LC22'><br/></div><div class='line' id='LC23'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.WHITE_KING</span> <span class="p">{</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/wKing.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC25'><span class="p">}</span></div><div class='line' id='LC26'><br/></div><div class='line' id='LC27'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.WHITE_QUEEN</span> <span class="p">{</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/wQueen.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC29'><span class="p">}</span></div><div class='line' id='LC30'><br/></div><div class='line' id='LC31'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.WHITE_ROOK</span> <span class="p">{</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/wRook.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC33'><span class="p">}</span></div><div class='line' id='LC34'><br/></div><div class='line' id='LC35'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.WHITE_BISHOP</span> <span class="p">{</span></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/wBishop.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC37'><span class="p">}</span></div><div class='line' id='LC38'><br/></div><div class='line' id='LC39'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.WHITE_KNIGHT</span> <span class="p">{</span></div><div class='line' id='LC40'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/wKnight.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC41'><span class="p">}</span></div><div class='line' id='LC42'><br/></div><div class='line' id='LC43'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.WHITE_PAWN</span> <span class="p">{</span></div><div class='line' id='LC44'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/wPawn.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC45'><span class="p">}</span></div><div class='line' id='LC46'><br/></div><div class='line' id='LC47'><br/></div><div class='line' id='LC48'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.BLACK_KING</span> <span class="p">{</span></div><div class='line' id='LC49'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/bKing.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC50'><span class="p">}</span></div><div class='line' id='LC51'><br/></div><div class='line' id='LC52'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.BLACK_QUEEN</span> <span class="p">{</span></div><div class='line' id='LC53'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/bQueen.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC54'><span class="p">}</span></div><div class='line' id='LC55'><br/></div><div class='line' id='LC56'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.BLACK_ROOK</span> <span class="p">{</span></div><div class='line' id='LC57'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/bRook.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC58'><span class="p">}</span></div><div class='line' id='LC59'><br/></div><div class='line' id='LC60'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.BLACK_BISHOP</span> <span class="p">{</span></div><div class='line' id='LC61'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/bBishop.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC62'><span class="p">}</span></div><div class='line' id='LC63'><br/></div><div class='line' id='LC64'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.BLACK_KNIGHT</span> <span class="p">{</span></div><div class='line' id='LC65'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/bKnight.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC66'><span class="p">}</span></div><div class='line' id='LC67'><br/></div><div class='line' id='LC68'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.BLACK_PAWN</span> <span class="p">{</span></div><div class='line' id='LC69'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="sx">url(&#39;../images/bPawn.png&#39;)</span><span class="p">;</span></div><div class='line' id='LC70'><span class="p">}</span></div><div class='line' id='LC71'><br/></div><div class='line' id='LC72'><span class="nf">#board</span> <span class="nc">.column</span> <span class="nc">.EMPTY</span> <span class="p">{</span></div><div class='line' id='LC73'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">background</span><span class="o">:</span> <span class="k">none</span><span class="p">;</span></div><div class='line' id='LC74'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1525037/a7e7aa8356b3cde0f16a848b779692734b7fee47/style.css" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1525037#file_style.css" style="float:right;margin-right:10px;color:#666">style.css</a>
            <a href="https://gist.github.com/1525037">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Even though the style.css file is ready, we don&#8217;t have images/icons of the pieces ready yet. So our next task is to get hold of a few images of the chess pieces. You can probably take it from Wikipedia page for <a href="http://en.wikipedia.org/wiki/Chess_piece">chess pieces</a>:</p>
<p><img class="alignnone" src="http://i.imgur.com/sxbDW.png" alt="" width="309" height="462" /></p>
<p>Save these as separate image files. Name them as given in the css file.</p>
<p>We are almost done with Part 1 of this tutorial !!!</p>
<p>The last piece of the puzzle is to call the <em>drawBoard()</em> function on the page load. This is done using the <a href="http://api.jquery.com/ready/">jQuery ready function</a>:</p>
<div id="gist-1525058" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">drawBoard</span><span class="p">(</span><span class="nx">board</span><span class="p">);</span></div><div class='line' id='LC3'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1525058/dc6786c67ecf1c49bded9985b6577132c9a27545/chess.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1525058#file_chess.js" style="float:right;margin-right:10px;color:#666">chess.js</a>
            <a href="https://gist.github.com/1525058">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>So we are ready to see the output of our great program. Go to your favorite web browser (Google Chrome) and then open the index.html file. You will see the following beautiful chess board:</p>
<p><a href="http://diovo.com/wp-content/uploads/2011/12/chess-board.png"><img class="alignnone size-full wp-image-2236" title="chess-board" src="http://diovo.com/wp-content/uploads/2011/12/chess-board.png" alt="" width="408" height="409" /></a></p>
<p>Congratulations. You have completed the first part of this series. It may not seem like much, but in a few small steps like these, you will be ready to write your own little chess program!</p>
<p>You can download the final source code from this tutorial <a href="https://github.com/downloads/niyazpk/Chess.js/chess.zip">here</a>. The latest source code will also be available in the <a href="https://github.com/niyazpk/Chess.js">github repository</a>.</p>
<p>Next &#8211; Part 2: <a href="http://diovo.com/2012/01/chess-programming-the-0x88-board-representation/">The 0x88 board representation</a></p>
<p><span style="color: #888888;"><em>Let me know if you are following this so that I can gauge the interest in these type of articles.</em></span></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Diovo?a=AJ5B1HFwpJc:I7r5LVF-iU4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=AJ5B1HFwpJc:I7r5LVF-iU4:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=AJ5B1HFwpJc:I7r5LVF-iU4:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=AJ5B1HFwpJc:I7r5LVF-iU4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Diovo?i=AJ5B1HFwpJc:I7r5LVF-iU4:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=AJ5B1HFwpJc:I7r5LVF-iU4:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Diovo?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=AJ5B1HFwpJc:I7r5LVF-iU4:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=AJ5B1HFwpJc:I7r5LVF-iU4:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Diovo?i=AJ5B1HFwpJc:I7r5LVF-iU4:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Diovo/~4/AJ5B1HFwpJc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://diovo.com/2011/12/chess-programming-in-javascript-part-1/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://diovo.com/2011/12/chess-programming-in-javascript-part-1/</feedburner:origLink></item>
		<item>
		<title>A better robots.txt for blogs</title>
		<link>http://feedproxy.google.com/~r/Diovo/~3/yJ0mUzOg3tw/</link>
		<comments>http://diovo.com/2011/12/a-better-robots-txt-for-blogs/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 18:35:13 +0000</pubDate>
		<dc:creator>Niyaz</dc:creator>
				<category><![CDATA[Google]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[crawlers]]></category>
		<category><![CDATA[robots.txt]]></category>

		<guid isPermaLink="false">http://diovo.com/?p=2190</guid>
		<description><![CDATA[If you are using a stock WordPress installation, take a look at your robots.txt file and it will most probably say something like: The problem with this is that there are many bots which will hit you very hard if you don&#8217;t specifically block them. Here is a better robots.txt file: Feel free to fork and create better [...]]]></description>
			<content:encoded><![CDATA[<p>If you are using a stock WordPress installation, take a look at your <a href="http://en.wikipedia.org/wiki/Robots_exclusion_standard">robots.txt</a> file and it will most probably say something like:</p>
<div id="gist-1507098" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>User-agent: *</div><div class='line' id='LC2'>Disallow: /wp-admin</div><div class='line' id='LC3'>Disallow: /wp-includes</div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1507098/09a2de72064fbc34d919b55b4071d5b16d184c72/gistfile1.txt" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1507098#file_gistfile1.txt" style="float:right;margin-right:10px;color:#666">gistfile1.txt</a>
            <a href="https://gist.github.com/1507098">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The problem with this is that there are many bots which will hit you very hard if you don&#8217;t specifically block them. Here is a better robots.txt file:</p>
<div id="gist-1507056" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>User-agent: *</div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>Disallow: /wp-content/</div><div class='line' id='LC4'>Disallow: /wp-icludes/</div><div class='line' id='LC5'>Disallow: /trackback/</div><div class='line' id='LC6'>Disallow: /wp-admin/</div><div class='line' id='LC7'>Disallow: /archives/</div><div class='line' id='LC8'>Disallow: /category/</div><div class='line' id='LC9'>Disallow: /tag/*</div><div class='line' id='LC10'>Disallow: /tag/</div><div class='line' id='LC11'>Disallow: /wp-*</div><div class='line' id='LC12'>Disallow: /login/</div><div class='line' id='LC13'>Disallow: /*.js$</div><div class='line' id='LC14'>Disallow: /*.inc$</div><div class='line' id='LC15'>Disallow: /*.css$</div><div class='line' id='LC16'>Disallow: /*.php$</div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>User-agent: All</div><div class='line' id='LC19'>Allow: /</div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'>User-agent: Googlebot-Image</div><div class='line' id='LC22'>Disallow: /</div><div class='line' id='LC23'><br/></div><div class='line' id='LC24'>User-agent: ia_archiver</div><div class='line' id='LC25'>Disallow: /</div><div class='line' id='LC26'><br/></div><div class='line' id='LC27'>User-agent: duggmirror</div><div class='line' id='LC28'>Disallow: /</div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'>User-agent: Yandex</div><div class='line' id='LC31'>Disallow: /</div><div class='line' id='LC32'><br/></div><div class='line' id='LC33'>User-agent: moget</div><div class='line' id='LC34'>User-agent: ichiro</div><div class='line' id='LC35'>Disallow: /</div><div class='line' id='LC36'><br/></div><div class='line' id='LC37'>User-agent: NaverBot</div><div class='line' id='LC38'>User-agent: Yeti</div><div class='line' id='LC39'>Disallow: /</div><div class='line' id='LC40'><br/></div><div class='line' id='LC41'>User-agent: Baiduspider</div><div class='line' id='LC42'>User-agent: Baiduspider-video</div><div class='line' id='LC43'>User-agent: Baiduspider-image</div><div class='line' id='LC44'>Disallow: /</div><div class='line' id='LC45'><br/></div><div class='line' id='LC46'>User-agent: sogou spider</div><div class='line' id='LC47'>Disallow: /</div><div class='line' id='LC48'><br/></div><div class='line' id='LC49'>User-Agent: Slurp</div><div class='line' id='LC50'>Disallow: /</div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1507056/b4248d83dd912061f2972ae7b9aec1cf57e28ea8/gistfile1.txt" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1507056#file_gistfile1.txt" style="float:right;margin-right:10px;color:#666">gistfile1.txt</a>
            <a href="https://gist.github.com/1507056">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Feel free to fork and create better versions.</p>
<p>Just keep in mind that there are still a lot of other bad/useless bots out there which will consume your resources, but many of them do not respect robots.txt. You will have to block their IPs in your .htaccess (or other similar conf files depending on your server).</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Diovo?a=yJ0mUzOg3tw:QzUZZSq-xWA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=yJ0mUzOg3tw:QzUZZSq-xWA:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=yJ0mUzOg3tw:QzUZZSq-xWA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=yJ0mUzOg3tw:QzUZZSq-xWA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Diovo?i=yJ0mUzOg3tw:QzUZZSq-xWA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=yJ0mUzOg3tw:QzUZZSq-xWA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Diovo?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=yJ0mUzOg3tw:QzUZZSq-xWA:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=yJ0mUzOg3tw:QzUZZSq-xWA:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Diovo?i=yJ0mUzOg3tw:QzUZZSq-xWA:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Diovo/~4/yJ0mUzOg3tw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://diovo.com/2011/12/a-better-robots-txt-for-blogs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://diovo.com/2011/12/a-better-robots-txt-for-blogs/</feedburner:origLink></item>
		<item>
		<title>Robert Heinlein on Progress</title>
		<link>http://feedproxy.google.com/~r/Diovo/~3/oKZCACYOib0/</link>
		<comments>http://diovo.com/2011/10/robert-heinlein-on-progress/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 03:12:31 +0000</pubDate>
		<dc:creator>Niyaz</dc:creator>
				<category><![CDATA[Quote]]></category>

		<guid isPermaLink="false">http://diovo.com/?p=2140</guid>
		<description><![CDATA[Progress is made by lazy men looking for easier ways to do things - Robert A. Heinlein]]></description>
			<content:encoded><![CDATA[<div class='quote'>
<p>Progress is made by lazy men looking for easier ways to do things</p>
</div>
<p>- Robert A. Heinlein</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Diovo?a=oKZCACYOib0:W7EMoWMdNEg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=oKZCACYOib0:W7EMoWMdNEg:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=oKZCACYOib0:W7EMoWMdNEg:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=oKZCACYOib0:W7EMoWMdNEg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Diovo?i=oKZCACYOib0:W7EMoWMdNEg:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=oKZCACYOib0:W7EMoWMdNEg:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Diovo?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=oKZCACYOib0:W7EMoWMdNEg:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=oKZCACYOib0:W7EMoWMdNEg:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Diovo?i=oKZCACYOib0:W7EMoWMdNEg:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Diovo/~4/oKZCACYOib0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://diovo.com/2011/10/robert-heinlein-on-progress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://diovo.com/2011/10/robert-heinlein-on-progress/</feedburner:origLink></item>
		<item>
		<title>Feynman and the “map of the cat”</title>
		<link>http://feedproxy.google.com/~r/Diovo/~3/LPXeymXMG9Q/</link>
		<comments>http://diovo.com/2011/10/feynman-and-the-map-of-the-cat/#comments</comments>
		<pubDate>Tue, 11 Oct 2011 07:31:26 +0000</pubDate>
		<dc:creator>Niyaz</dc:creator>
				<category><![CDATA[Excerpt]]></category>

		<guid isPermaLink="false">http://diovo.com/?p=2135</guid>
		<description><![CDATA[From Richard Feynman&#8216;s book: The next paper selected for me was by Adrian and Bronk. They demonstrated that nerve impulses were sharp, single-pulse phenomena. They had done experiments with cats in which they had measured voltages on nerves. I began to read the paper. It kept talking about extensors and flexors, the gastrocnemius muscle, and [...]]]></description>
			<content:encoded><![CDATA[<p>From <strong>Richard Feynman</strong>&#8216;s <a href="http://www.amazon.com/Surely-Feynman-Adventures-Curious-Character/dp/0393316041">book</a>:</p>
<div class='excerpt'>
<p>The next paper selected for me was by Adrian and Bronk. They demonstrated that nerve impulses were sharp, single-pulse phenomena. They had done experiments with cats in which they had measured voltages on nerves.</p>
<p>I began to read the paper. It kept talking about extensors and flexors, the gastrocnemius muscle, and so on. This and that muscle were named, but I hadn’t the foggiest idea of where they were located in relation to the nerves or to the cat. So I went to the librarian in the biology section and asked her if she could find me a map of the cat.</p>
<p>“A map of the cat, sir?” she asked, horrified. “You mean a zoological chart!” From then on there were rumors about some dumb biology graduate student who was looking for a “map of the cat.”</p>
</div>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Diovo?a=LPXeymXMG9Q:y2GloUXblvU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=LPXeymXMG9Q:y2GloUXblvU:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=LPXeymXMG9Q:y2GloUXblvU:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=LPXeymXMG9Q:y2GloUXblvU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Diovo?i=LPXeymXMG9Q:y2GloUXblvU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=LPXeymXMG9Q:y2GloUXblvU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Diovo?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=LPXeymXMG9Q:y2GloUXblvU:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=LPXeymXMG9Q:y2GloUXblvU:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Diovo?i=LPXeymXMG9Q:y2GloUXblvU:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Diovo/~4/LPXeymXMG9Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://diovo.com/2011/10/feynman-and-the-map-of-the-cat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://diovo.com/2011/10/feynman-and-the-map-of-the-cat/</feedburner:origLink></item>
		<item>
		<title>Be good. Be very good</title>
		<link>http://feedproxy.google.com/~r/Diovo/~3/2xZ6xLZJwEA/</link>
		<comments>http://diovo.com/2011/10/be-good-be-very-good/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 07:27:02 +0000</pubDate>
		<dc:creator>Niyaz</dc:creator>
				<category><![CDATA[Excerpt]]></category>

		<guid isPermaLink="false">http://diovo.com/?p=2131</guid>
		<description><![CDATA[User coffeemug writes: 1. Be good. Be very good. Don’t be the “front-end guy” or the “back-end guy”, or some other “guy”. Once you know what you want to build, building software is about five things: algorithms that solve your problem, programming languages that express your algorithms, computer architecture that makes your algorithms run efficiently [...]]]></description>
			<content:encoded><![CDATA[<p>User <strong>coffeemug</strong> <a href="http://news.ycombinator.com/item?id=1747713">writes</a>:</p>
<div class='excerpt'>
<p>1. Be good. Be very good. Don’t be the “front-end guy” or the “back-end guy”, or some other “guy”. Once you know what you want to build, building software is about five things: algorithms that solve your problem, programming languages that express your algorithms, computer architecture that makes your algorithms run efficiently on real hardware, the practical toolchain, and the management of complexity of real software. So study algorithms, and then graduate algorithms, and then advanced graduate algorithms. Do every challenge problem online. Study programming languages to express those algorithms. You can get away with three: C, Lisp, Haskell. Everything else is crud. Study computer architecture and compilers to see how your programs run efficiently. Learn great tools (Emacs/Vim/Visual Studio/bash/Linux/OS X/Windows whatever &#8211; just great ones that you’re damn good at). Learn how complexity is managed. Look at lare open source projects, study how they’re organized, and contribute patches to understand how small changes can effect a large system.</p>
<p>2. Learn what to build. Once you get really good, your time starts to be more valuable than gold. There will be very few people in the world who are as good (the internet will bias you to think that the world is full of great people &#8211; this ain’t so, there isn’t enough of ‘em). You owe it to people and to yourself not to bother with improving something by 1% or 10% because you’re wasting time in opportunity cost and could be improving something by 1000%. Make sure what you’re building is worth building, and make sure every line of code you write is worth writing, otherwise you will fail. Break the NIH syndrome in yourselves now (all good people have it, phenomenal people that build successful companies broke it in themselves). Learn to infer what people want.</p>
<p>3. If you’re that good, you will easily get a $100k job after graduation (probably more by then), and grow to $180k in a few years. That’s very, very comfortable. It’s not worth busting your ass 16 hours a day to build another CRM tool when you can have a $180k job. So don’t start a business to start a business. Start a business to bring a meaningful change in the world. A huge change. A 1000% change. There are lots of hugely successful companies out there that do what’s not meaningful to you &#8211; ignore them. But do make sure that what’s meaningful to you is also meaningful to millions (hopefully billions) of others. You won’t get rich writing Lisp compilers.</p>
<p>This is what matters. Most everything else is fluff.</p>
</div>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Diovo?a=2xZ6xLZJwEA:pS4vr8E-TMM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=2xZ6xLZJwEA:pS4vr8E-TMM:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=2xZ6xLZJwEA:pS4vr8E-TMM:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=2xZ6xLZJwEA:pS4vr8E-TMM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Diovo?i=2xZ6xLZJwEA:pS4vr8E-TMM:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=2xZ6xLZJwEA:pS4vr8E-TMM:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Diovo?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=2xZ6xLZJwEA:pS4vr8E-TMM:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=2xZ6xLZJwEA:pS4vr8E-TMM:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Diovo?i=2xZ6xLZJwEA:pS4vr8E-TMM:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Diovo/~4/2xZ6xLZJwEA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://diovo.com/2011/10/be-good-be-very-good/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://diovo.com/2011/10/be-good-be-very-good/</feedburner:origLink></item>
		<item>
		<title>Immersive experience in JavaScript</title>
		<link>http://feedproxy.google.com/~r/Diovo/~3/xOhlU-78jG8/</link>
		<comments>http://diovo.com/2011/10/immersive-experience-in-javascript/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 04:40:10 +0000</pubDate>
		<dc:creator>Niyaz</dc:creator>
				<category><![CDATA[Quote]]></category>

		<guid isPermaLink="false">http://diovo.com/?p=2129</guid>
		<description><![CDATA[Once while waiting for a badly coded, often-crashing Javascript-heavy page to load I leaned back on my chair too far and smacked my head on a file cabinet. To this day it’s the most immersive experience I’ve had on the web” - mrmessiah]]></description>
			<content:encoded><![CDATA[<div class='quote'>
<p>Once while waiting for a badly coded, often-crashing Javascript-heavy page to load I leaned back on my chair too far and smacked my head on a file cabinet. To this day it’s the most immersive experience I’ve had on the web”</p>
</div>
<p>- <a href="http://www.reddit.com/r/programming/comments/fm5c5/microsofts_response_to_is_ie9_a_modern_browser/c1gznug">mrmessiah</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Diovo?a=xOhlU-78jG8:tfM88Xw9c6I:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=xOhlU-78jG8:tfM88Xw9c6I:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=xOhlU-78jG8:tfM88Xw9c6I:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Diovo?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=xOhlU-78jG8:tfM88Xw9c6I:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Diovo?i=xOhlU-78jG8:tfM88Xw9c6I:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=xOhlU-78jG8:tfM88Xw9c6I:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Diovo?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=xOhlU-78jG8:tfM88Xw9c6I:l6gmwiTKsz0"><img src="http://feeds.feedburner.com/~ff/Diovo?d=l6gmwiTKsz0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Diovo?a=xOhlU-78jG8:tfM88Xw9c6I:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Diovo?i=xOhlU-78jG8:tfM88Xw9c6I:gIN9vFwOqvQ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Diovo/~4/xOhlU-78jG8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://diovo.com/2011/10/immersive-experience-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://diovo.com/2011/10/immersive-experience-in-javascript/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 3.522 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-05-09 11:22:15 --><!-- Compression = gzip -->

