<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>Independent Developer &#187; programming</title>
	<atom:link href="http://www.independentdeveloper.com/archive/tag/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.independentdeveloper.com</link>
	<description>Code, Art and Everything In-Between</description>
	<lastBuildDate>Tue, 11 Aug 2009 21:32:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Tower Defense as a Thread Model</title>
		<link>http://www.independentdeveloper.com/archive/2009/06/11/tower-defense-as-a-thread-model</link>
		<comments>http://www.independentdeveloper.com/archive/2009/06/11/tower-defense-as-a-thread-model#comments</comments>
		<pubDate>Thu, 11 Jun 2009 23:28:23 +0000</pubDate>
		<dc:creator>Lisa</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[multicore]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[thread model]]></category>
		<category><![CDATA[tower defense]]></category>
		<category><![CDATA[turret defense]]></category>
		<category><![CDATA[video games]]></category>

		<guid isPermaLink="false">http://www.independentdeveloper.com/?p=614</guid>
		<description><![CDATA[I keep having the strangest reoccurring dream. In my dream, I&#8217;m playing a turret defense game. 1 However, it is no ordinary tower game. It&#8217;s actually a thread process model. In the dream, each turret type represents a thread synchronization mechanism, generally some sort of atomic primitive. The units marching along the paths represent the [...]]]></description>
			<content:encoded><![CDATA[<p>I keep having the strangest reoccurring dream. In my dream, I&#8217;m playing a <a href="http://www.newgrounds.com/portal/search/title/tower%20defense">turret defense</a> game. <sup><a href="#footnote">1</a></sup> <strong>However, it is no ordinary tower game. It&#8217;s actually a thread process model.</strong> In the dream, each turret type represents a thread synchronization mechanism, generally some sort of atomic primitive. The units marching along the paths represent the threads that require synchronization. Most are worker threads calculating a result set, but some may be polling threads reading device updates or doing other jobs. The range circle of each tower and the position of the unit along the path represent states when it is appropriate for the thread to be accessed, either quiescent states or work states where the thread has completed some useful task and has a result ready to be read. If the unit\thread moves out of the turret\synchronization range circle, the thread result is lost or held and operation on the next work cycle begins. Shooting a unit\thread with a turret is a read\synchronization operation and gathers the result.</p>
<p>Yeah, I know, clearly I&#8217;ve had threads on the mind a little too much lately. <img src='http://www.independentdeveloper.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I&#8217;ve been working on a major refactor of the engine at work and <a href="http://herbsutter.wordpress.com/?s=effective+concurrency">thread synchronization problems are tricky</a>.</p>
<p>Usually when I have dreams like this I wake up in the morning and have a good laugh wondering what the heck I was thinking. This time, however, I think there may be something to it. Now, I&#8217;m not suggesting a design built literally on the model exactly as it is in the dream but I think there are elements that could be useful.</p>
<p><strong>The part that is intriguing about the turret defense thread model is that you can guarantee completion (all units hit) without guaranteeing really very much at all about either the turrets or the units. </strong>The units are not synchronized with each other; in fact, in many tower games faster units can overtake slower ones. A unit does not guarantee a minimum speed&#8211;it may even be allowed to stop entirely on occasion&#8211;although it does guarantee a maximum speed. The turrets are not synchronized with each other, nor do they communicate any information to each. A turret does not guarantee a specific number of units hit, only that it will hit units at a specific rate. A turret also does not guarantee what order it will fire at units in. A unit likewise guarantees nothing about when or by whom it will be hit except that it can be hit when it is within a valid range.</p>
<p>Yet, with a sufficient number of correctly placed turrets all the units will be hit with none escaping. More importantly, most of the time you can have far fewer turrets than units and still complete successfully. The number of turrets required is a simple function of the <em>maximum</em> speed of the turret, the <em>maximum</em> speed of the unit, and the number of times the turret must hit the unit for completion. Placement is trickier, but with a thread model you could surely replicate that with signals. </p>
<p>Fascinating, isn&#8217;t it?</p>
<p>I&#8217;m not sure yet where to go with all this, but it seems like there&#8217;s a useful idea to be gleaned. It&#8217;s not that different that a traditional worker pool, but the lack of communication seems important. I&#8217;m sure there&#8217;s something useful that can be done with this.</p>
<hr/>
<p><em>1. <a name="footnote"></a>For anyone who cares, the specific game seems to be <a href="http://en.wikipedia.org/wiki/Crystal_Defenders">Crystal Defenders</a>, but with some elements of <a href="http://www.ninjakiwi.com/Games/Tower-Defense/Bloons-Tower-Defense-3.html">Bloons Tower Defense 3</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.independentdeveloper.com/archive/2009/06/11/tower-defense-as-a-thread-model/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>One Dozen Easy C++ Programming Tips</title>
		<link>http://www.independentdeveloper.com/archive/2007/12/12/one-dozen-easy-c-programming-tips</link>
		<comments>http://www.independentdeveloper.com/archive/2007/12/12/one-dozen-easy-c-programming-tips#comments</comments>
		<pubDate>Wed, 12 Dec 2007 16:37:38 +0000</pubDate>
		<dc:creator>Lisa</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[bug tracking]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[crash]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[user interface]]></category>

		<guid isPermaLink="false">http://www.independentdeveloper.com/archive/2007/12/12/one-dozen-easy-c-programming-tips</guid>
		<description><![CDATA[Consistency breeds ease of use. Even if your buttons do some weird thing, as long as they all do the same weird thing users will eventually figure it out. Make the buttons each behave differently, however, and the user is lost forever. Sort data the way it is most likely to be used. The logical [...]]]></description>
			<content:encoded><![CDATA[<ol>
<li>Consistency breeds ease of use. Even if your buttons do some weird thing, as long as they all do the <em>same</em> weird thing users will <em>eventually</em> figure it out. Make the buttons each behave differently, however, and the user is lost forever.</li>
<li>Sort data the way it is most likely to be used. The logical sort order for a date is chronological, not alphabetical. </li>
<li>When in doubt, the default option should be the least destructive \ most undoable choice.</li>
<li>If you have to write an explanation of your code, never assume that the reader has read the same papers you have, unless you told them what else to read&#8211;<em>and where to find it&#8211;</em>first.</li>
<li>Wrong code comments are worse than no code comments.</li>
<li>&#8220;Copy and paste&#8221; errors happen to <em>everyone</em>. Be on the look out for them.</li>
<li>If a bug disappears in debug mode, 90% of the time it will be one of two things: 1) you are overflowing memory or 2) you have a thread race condition. At least two thirds of the time, it will be a memory overflow. It is extremely unlikely it is problem with the compiler.</li>
<li>Releasing your program compiled in &#8220;debug mode&#8221; is not an acceptable &#8220;fix&#8221;. The bug only <em>looks</em> like its gone&#8230; it&#8217;s still there, and can come back to bite you at any time. Don&#8217;t do it, no matter how tempting it might seem.</li>
<li>If the program crashes on exit, you probably freed something you didn&#8217;t properly allocate, or that isn&#8217;t allocated anymore.</li>
<li>Leaking memory is bad. Freeing pointers or objects that don&#8217;t belong to you is worse. If an object you expect to be free is still hanging around, don&#8217;t force it, find out why&#8230; &#8220;fixing&#8221; bugs by patching the symptoms and not the cause makes the code much harder to repair in the long run.</li>
<li>If you have an intermittent bug, and your &#8220;fix&#8221; made it go away and you don&#8217;t know why, the bug is almost certainly still there. If you can&#8217;t explain it, you didn&#8217;t fix it.</li>
<li>Good documentation is more important than more features. Write it down, even if it&#8217;s just a simple &#8220;readme.txt&#8221; file&#8230; the next person who has to work with your stuff will appreciate it!</li>
</ol>
<p>More tips? Ideas you would recommend? Post a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.independentdeveloper.com/archive/2007/12/12/one-dozen-easy-c-programming-tips/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Million Objects</title>
		<link>http://www.independentdeveloper.com/archive/2007/12/09/a-million-objects</link>
		<comments>http://www.independentdeveloper.com/archive/2007/12/09/a-million-objects#comments</comments>
		<pubDate>Sun, 09 Dec 2007 10:43:39 +0000</pubDate>
		<dc:creator>Lisa</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[computer programming]]></category>
		<category><![CDATA[multicore]]></category>
		<category><![CDATA[multithreading]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[zen koan]]></category>

		<guid isPermaLink="false">http://www.independentdeveloper.com/archive/2007/12/09/a-million-objects</guid>
		<description><![CDATA[From a Zen Kōan: Gyosan asked Isan, &#8220;If a million objects come to you, what should you do?&#8221; Isan answered, &#8220;A green object is not yellow. A long object is not short. Each object manages its own fate. Why should I interfere with them?&#8221; Gyosan bowed in homage. It&#8217;s good advice for programming multi-core, too.]]></description>
			<content:encoded><![CDATA[<p>From a <a href="http://www.amazon.com/gp/search?ie=UTF8&#038;keywords=zen%20koans&#038;tag=independevelo-20&#038;index=na-books-us&#038;linkCode=ur2&#038;camp=1789&#038;creative=9325">Zen Kōan</a>:</p>
<blockquote><p>Gyosan asked Isan, &#8220;If a million objects come to you, what should you do?&#8221; Isan answered, &#8220;A green object is not yellow. A long object is not short. Each object manages its own fate. Why should I interfere with them?&#8221; Gyosan bowed in homage.
</p></blockquote>
<p>It&#8217;s good advice for programming multi-core, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.independentdeveloper.com/archive/2007/12/09/a-million-objects/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AC3D Plugin: Bake Texture Layout</title>
		<link>http://www.independentdeveloper.com/archive/2007/12/06/ac3d-plugin-bake-texture-layout</link>
		<comments>http://www.independentdeveloper.com/archive/2007/12/06/ac3d-plugin-bake-texture-layout#comments</comments>
		<pubDate>Thu, 06 Dec 2007 17:33:56 +0000</pubDate>
		<dc:creator>Lisa</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Tools and Plugins]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[ac3d]]></category>
		<category><![CDATA[AC3D plug-ins]]></category>
		<category><![CDATA[bake]]></category>
		<category><![CDATA[cg]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[plug ins]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[texture baking]]></category>
		<category><![CDATA[texture map]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[uv map]]></category>

		<guid isPermaLink="false">http://www.independentdeveloper.com/archive/2007/12/06/ac3d-plugin-bake-texture-layout</guid>
		<description><![CDATA[This plugin &#8220;bakes&#8221; a texture from one object to another, allowing you to transfer texture data between two models with the same geometry but different texture coordinates&#8211;without having to re-paint the texture bitmap! This is useful if you start to paint a texture, and change your mind later about the UV map; or for games [...]]]></description>
			<content:encoded><![CDATA[<p>This plugin &#8220;bakes&#8221; a texture from one object to another, allowing you to transfer texture data between two models with the same geometry but different texture coordinates&#8211;without having to re-paint the texture bitmap! This is useful if you start to paint a texture, and change your mind later about the UV map; or for games when you can&#8217;t change the layout of the map for technical reasons, but would like different UVs for convenience during painting. It&#8217;s also good for fitting text onto a warped surface or other projections that would be difficult or impossible to paint by hand.</p>
<p><strong><a href="/files/baketexture.zip" title="Download">Download the plugin. (Requires Windows XP, AC3D 6.2 or above.)</a></strong>
<p align="center"><a href="/images/bake.jpg" rel="lightbox" title="Texture Baking Example"><img src="/images/bake_thumb.jpg" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.independentdeveloper.com/archive/2007/12/06/ac3d-plugin-bake-texture-layout/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How Games Influence Ideas</title>
		<link>http://www.independentdeveloper.com/archive/2007/11/29/how_games_influence_ideas</link>
		<comments>http://www.independentdeveloper.com/archive/2007/11/29/how_games_influence_ideas#comments</comments>
		<pubDate>Thu, 29 Nov 2007 08:18:00 +0000</pubDate>
		<dc:creator>Lisa</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[computer games]]></category>
		<category><![CDATA[ecology]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[gender issues]]></category>
		<category><![CDATA[national science foundation]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[research]]></category>
		<category><![CDATA[social values]]></category>
		<category><![CDATA[values at play]]></category>
		<category><![CDATA[video games]]></category>
		<category><![CDATA[worldviews]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Games, books, and movies may be entertainment, but they also influence the way people think. The &#8220;Values at Play&#8221; research project, a collaboration between Hunter College&#8217;s innovative Tiltfactor Lab and New York University sponsored by the National Science Foundation, is investigating how game designers unconsciously and consciously express and encourage specific worldviews through their creations. [...]]]></description>
			<content:encoded><![CDATA[<p>Games, books, and movies may be entertainment, but they also influence the way people think. The <a href="http://valuesatplay.org/">&#8220;Values at Play&#8221;</a> research project, a collaboration between Hunter College&#8217;s innovative Tiltfactor Lab and New York University sponsored by the National Science Foundation, is investigating how game designers unconsciously and consciously express and encourage specific worldviews through their creations.</p>
<p>This looks like an interesting project, providing a growing body of useful research for seasoned developers, as well as game design contests and curriculum tools for students and educators.</p>
<p><a href="http://valuesatplay.org/">http://valuesatplay.org/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.independentdeveloper.com/archive/2007/11/29/how_games_influence_ideas/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Ambient Occlusion?</title>
		<link>http://www.independentdeveloper.com/archive/2007/11/27/what_is_ambient_occlusion</link>
		<comments>http://www.independentdeveloper.com/archive/2007/11/27/what_is_ambient_occlusion#comments</comments>
		<pubDate>Tue, 27 Nov 2007 10:07:00 +0000</pubDate>
		<dc:creator>Lisa</dc:creator>
				<category><![CDATA[3D Tutorials]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[3d graphics]]></category>
		<category><![CDATA[ambient occlusion]]></category>
		<category><![CDATA[cg]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[global illumination]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[monte carlo]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ray casting]]></category>
		<category><![CDATA[ray tracing]]></category>
		<category><![CDATA[rendering]]></category>
		<category><![CDATA[ssao]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Ambient occlusion is a lighting technique that is commonly used to create soft shadows on objects. Ambient occlusion isn&#8217;t used to create the type of shadows that are cast from objects with a light shining directly on them. Instead, ambient occlusion generates the type of deep shadows that appear in the corners or creases of [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Ambient occlusion</strong> is a lighting technique that is commonly used to create soft shadows on objects. Ambient occlusion isn&#8217;t used to create the type of shadows that are cast from objects with a light shining directly on them. Instead, ambient occlusion generates the type of deep shadows that appear in the corners or creases of things, where it is hard for the light to reach.</p>
<p>Technically speaking, ambient occlusion is a global illumination technique. However, in common usage of the term it is often referred to as a cheap <em>alternative</em> to global illumination. To clear up any confusion, what most renderers refer to as &#8220;global illumination&#8221; is actually an amalgamation of several techniques such as radiosity, metropolis light transport, image-based lighting or photon mapping. The actual techniques used differ slightly from renderer to renderer. Some renderers include an ambient occlusion term as part of their global illumination calculation; others do not.</p>
<p>Like most global illumination techniques, ambient occlusion is dependent on the other geometry in the scene. Ambient occlusion on its own generates less realistic lighting than &#8220;full&#8221; global illumination. However, ambient occlusion is much faster and less complex to calculate than other methods which is why it is still popular among game developers and in production animation.</p>
<p class="photocaption" align="center"><a href="/images/ambient-occlusion.jpg" rel="lightbox" title=""><img src="/images/ambient-occlusion_thumb.jpg" alt="" /></a><br />
(Left) Without Ambient Occlusion. (Right) With Ambient Occlusion<br />
<em>Click for larger image.</em></p>
<p><span id="more-47"></span></p>
<h4>How Does Ambient Occlusion Work?</h4>
<p>To understand how ambient occlusion works, it&#8217;s helpful to break down the terms.</p>
<p>&#8220;Ambient&#8221; refers to the indirect light in the scene. Ambient light is light that doesn&#8217;t come from an identifiable light source. It is light that is accumulated from stray photons bouncing around the room. Photographers sometimes also call this kind of light source &#8220;available light&#8221; or &#8220;existing light&#8221;.</p>
<p>&#8220;Occlusion&#8221; is the act of one object blocking another object. For example, if you are taking a picture and at the last moment a truck drives in front of you blocking the shot, the truck is &#8220;occluding&#8221; your view of your subject. Occlusion is important to computer graphics for many things besides just shadows. For example, many renderers can optimize the scene and render faster by culling objects that are occluded, or hidden, by other objects.</p>
<p>Ambient occlusion is a measurement of how much ambient light is blocked by nearby things. If an object or surface is occluded, less light can reach it, which means the surface will be in shadow. Creases, corners, hollows and the undersides of things tend to have a lot of ambient occlusion.</p>
<h4>For Artists: Rendering with Ambient Occlusion</h4>
<p>Most modern renderers support either some form of global illumination or ambient occlusion; many support both alone or in combination.</p>
<p>Ambient occlusion improves your render by making small details more visible and improving depth perception. A lot of the way humans perceive depth has to do with the size, darkness, and placement of shadows. Using ambient occlusion to add shading to your scene will help the viewer comprehend the placement of objects better.</p>
<p class="photocaption" align="center"><a href="/images/poser-ao.jpg" rel="lightbox" title=""><img src="/images/poser-ao.thumb.jpg" alt="" /></a><br />
Ambient occlusion in Poser 6</p>
<p>While each renderer is different, often the basic concepts are pretty similar between many of them. As an example, here&#8217;s how you can add ambient occlusion to your render in Poser:</p>
<p>- Click to the Materials tab<br />
- Use the eye dropper to select the material you want to edit<br />
- In the Material Properties node editor, click the Set Up Ambient Occlusion Button. This will add an ambient occlusion node to you material shader.</p>
<p>Poser 6 gives you four properties you can adjust on the ambient occlusion node:</p>
<p><strong>Samples</strong> &#8211; The number of ray samples that will be cast. The more rays, the finer the lighting, but the longer the render. Keep this number as low as you can unless the lighting looks splotchy, in which case, turn it up.</p>
<p><strong>MaxDist</strong> &#8211; The maximum distance the ray will test. Larger values generally result in more shadows.</p>
<p><strong>RayBias</strong> &#8211; The smallest distance the ray will test for objects. If you get black blotches on your figure, very slightly increase your ray bias. If you increase it too much, you will start missing shadows, so don&#8217;t turn it up more than you need to.</p>
<p><strong>Strength</strong> &#8211; The blend weight of the effect.</p>
<h4>For Programmers: Implementing Ambient Occlusion</h4>
<p>As far as bang for the buck, ambient occlusion is one of the fastest ways to improve the quality of your game&#8217;s lighting without a tremendous amount of pain. There are many, many algorithms out there for calculating ambient occlusion and each has advantages and disadvantages depending on your engine features. That said, there are two methods in particular I&#8217;d like to point out.</p>
<p><strong>Monte Carlo Ray Casting Method</strong><br />
I&#8217;m not sure if this is the <em>oldest</em> method for calculating ambient occlusion or not, but it was certainly the first method learned. Assuming you already have some sort of ray-triangle collision code, this method is very easy to understand and implement.</p>
<p>For each point you want to calculate lighting for, shoot out a number of random rays. (In truth, you don&#8217;t really want <em>completely</em> random rays&#8230; small random offsets from your surface normal seem to work best.) If the ray hits something, increase the shadow value proportional to the distance along the ray where the collision occurred.</p>
<p>With enough samples per point, this method can produce very good results. The downside is that this method can be very slow. Many games utilizing this method don&#8217;t calculate the ambient occlusion in real-time&#8230; the ambient occlusion is calculated when the level is loaded or off-line when the level is built, and the results are stored in a texture map.</p>
<p><em>[Incidentally... yes... looking at Poser's settings, I'm guessing this or something very similar is the method they are using!]</em></p>
<p><strong>Screen Space Method</strong><br />
Screen space ambient occlusion has been generating a lot of buzz lately. Screen space rendering methods in general are always desirable to real-time programmers because they run in constant time and constant memory, and aren&#8217;t inhibited by scene complexity. A good starting point for learning about screen space ambient occlusion is <a href="http://delivery.acm.org/10.1145/1290000/1281671/p97-mittring.pdf?key1=1281671&amp;key2=9942678811&amp;coll=ACM&amp;dl=ACM&amp;CFID=15151515&amp;CFTOKEN=6184618" title="Siggraph Paper" target="_blank">Martin Mittring of Crytek&#8217;s Siggraph 2007 presentation</a>.</p>
<p>Basically, the notion is that by sampling surrounding points in the depth buffer you can estimate shadowed areas. Think of it (sort of) like running an edge-detection routine on the z-buffer. The method can produce some artifacts, but there are number of ways to combat them&#8230; and the benefit of course is that this method is fast even on complex scenes.</p>
<p>Again, there are many methods out there and numerous variations, but hopefully this is enough to get you pointed in the right direction!</p>
<p>Questions? Ideas? Links to your favorite method? Post a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.independentdeveloper.com/archive/2007/11/27/what_is_ambient_occlusion/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sony Cuts Price of PS3 Dev Kits</title>
		<link>http://www.independentdeveloper.com/archive/2007/11/20/sony_cuts_price_of_ps3_dev_kit</link>
		<comments>http://www.independentdeveloper.com/archive/2007/11/20/sony_cuts_price_of_ps3_dev_kit#comments</comments>
		<pubDate>Tue, 20 Nov 2007 10:17:00 +0000</pubDate>
		<dc:creator>Lisa</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[dev kit]]></category>
		<category><![CDATA[game developments]]></category>
		<category><![CDATA[playstation]]></category>
		<category><![CDATA[price]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ps3]]></category>
		<category><![CDATA[video games]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[According to Information Week: In the latest move, Sony Computer Entertainment, the unit responsible for the PlayStation, reduced the price of the SDK, which SCE calls the Reference Tool, to $10,250 in North America, $8,600 in Japan, and $11,250 in Europe. In addition, SCE said it would enhance the development environment by integrating programming tools [...]]]></description>
			<content:encoded><![CDATA[<p>According to Information Week:</p>
<blockquote><p>In the latest move, Sony Computer Entertainment, the unit responsible for the PlayStation, reduced the price of the SDK, which SCE calls the Reference Tool, to $10,250 in North America, $8,600 in Japan, and $11,250 in Europe. In addition, SCE said it would enhance the development environment by integrating programming tools from SN Systems. SN&#8217;s core tool is called ProDG.</p></blockquote>
<p><a href="http://www.informationweek.com/news/showArticle.jhtml?articleID=203103277">Read the rest of the article here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.independentdeveloper.com/archive/2007/11/20/sony_cuts_price_of_ps3_dev_kit/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GDC Already?</title>
		<link>http://www.independentdeveloper.com/archive/2007/11/10/gdc_already</link>
		<comments>http://www.independentdeveloper.com/archive/2007/11/10/gdc_already#comments</comments>
		<pubDate>Sat, 10 Nov 2007 10:10:00 +0000</pubDate>
		<dc:creator>Lisa</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Totally Off-Topic]]></category>
		<category><![CDATA[gamasutra]]></category>
		<category><![CDATA[game developers conference]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[gdc]]></category>
		<category><![CDATA[idga]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[In case you missed it, the annual Game Developer&#8217;s Conference is early this year! The conference is usually held in March, but this year it runs from February 18-22 in San Francisco, California. Alumni early-registration deadline is December 5.]]></description>
			<content:encoded><![CDATA[<p>In case you missed it, the annual <a href="http://www.gdconf.com/">Game Developer&#8217;s Conference</a> is early this year! The conference is usually held in March, but this year it runs from February 18-22 in San Francisco, California. Alumni early-registration deadline is December 5. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.independentdeveloper.com/archive/2007/11/10/gdc_already/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Your Own Turing Machine: Part II</title>
		<link>http://www.independentdeveloper.com/archive/2007/10/07/your_own_turing_machine_part_i</link>
		<comments>http://www.independentdeveloper.com/archive/2007/10/07/your_own_turing_machine_part_i#comments</comments>
		<pubDate>Sun, 07 Oct 2007 08:53:00 +0000</pubDate>
		<dc:creator>Lisa</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Fun Projects]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[addition]]></category>
		<category><![CDATA[computer]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[subtraction]]></category>
		<category><![CDATA[turing]]></category>
		<category><![CDATA[turing machine]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Last time we built a Turing machine as a rainy-day project. Today, I&#8217;ll show you how to use it to do something useful, like add two numbers together. Single-Head Addition Now that we have our Turing machine, it&#8217;s time to write some programs for it! Last time, we wrote a simple incrementer to learn how [...]]]></description>
			<content:encoded><![CDATA[<p>Last time we built a <a href="/archive/2007/10/07/build_a_2_computer" title="Read the previous article">Turing machine</a> as a rainy-day project. Today, I&#8217;ll show you how to use it to do something useful, like add two numbers together.<span id="more-8"></span></p>
<h4>Single-Head Addition</h4>
<p>Now that we have our Turing machine, it&#8217;s time to write some programs for it! Last time, we wrote a simple incrementer to learn how the machine works. This time, we&#8217;ll write something (slightly) more complicated. Let&#8217;s try some basic arithmetic, starting with addition.</p>
<p>First, you will need to set up your Turing machine. Since we&#8217;re still working with only one tape, we&#8217;ll need to put both numbers we want to add on the same tape. Separate each number with a blank. For example, if I wanted to add the numbers 3 + 5, my tape would read 1-1-1-0-1-1-1-1-1. As before, place the head at the start of the first number.</p>
<p>Let&#8217;s assume we want to overwrite the tape with the answer. So, when we start the program, the tape will contain the two numbers we want to add. When we finish running the program, the tape will contain the sum of the two numbers.</p>
<p>There are lots of ways we could go about adding the two numbers, but there&#8217;s one very obvious way to do it. Squish the two numbers together! Sure, it sounds cheesy, but it&#8217;s a pretty efficient way to accomplish our task.</p>
<p>Here&#8217;s the outline:</p>
<p>- Move the head past the first number to the blank.<br />
- Move the head past the blank.<br />
- Move the head to the last digit of the second number.<br />
- Remove the one and change it to a blank.<br />
- Move the head to the blank between the numbers.<br />
- Write a one.<br />
- Reset the head to the start.</p>
<p>Each of these steps can be accomplished with simple loops to move past the written values, similar to the incrementer. However, we&#8217;ll need to add a little more logic to our loops, to handle the case when either operand is a zero.</p>
<p class="photocaption" align="center"><img src="/images/turing-singletape-add.gif" alt="Adder" /><br />
Watch the adder program in action</p>
<p>Start by moving the head past the first number. This loop is similar to the one in the incrementer, however, we&#8217;ll need to &#8220;flip the loop&#8221; by moving the head <em>before</em> we check for the value. That way, we&#8217;ll be sure our head ends up where we mean it to be, regardless of what the first number is.</p>
<p>#1: Move the head right one space. Go to the next instruction.</p>
<p>#2: If the current value is 1, go to instruction #1. Otherwise, go to instruction number #3.</p>
<p>Next, we must move past the blank:</p>
<p>#3: Move the head right one space. Go to the next instruction.</p>
<p>Once past the blank, we need to find our way to the end of the second number. Use a loop to move the head past the second number, then retreat one space so that the head is on the last digit:</p>
<p>#4: If the current value is 1, go to instruction #5. Otherwise, go to instruction #6.</p>
<p>#5: Move the head right one space. Go to instruction #4.</p>
<p>#6: Move the head left one space. Go to the next instruction.</p>
<p>Now, we need to replace the last digit with a blank&#8230; but not so fast! What if the second number is a zero? We need to handle this special case. If the second number is a zero, we&#8217;ll just skip to the end and reset the head. Otherwise, we&#8217;ll merge the two numbers.</p>
<p>#7: If the current value is a 1, go to instruction #8. Otherwise, go to instruction #13.</p>
<p>#8: Write a blank. Go to the next instruction.</p>
<p>#9: Move the head left one space. Go to the next instruction.</p>
<p>Now, it is time to start moving the head back in the opposite direction. Seek to the left until we find the blank separating the values:</p>
<p>#10: If the current value is a 1, go to instruction #11. Otherwise, go to instruction #12.</p>
<p>#11: Move the head left one space. Go to instruction #10.</p>
<p>To combine the numbers together, replace the blank with a 1. This doesn&#8217;t change the value, because we already removed a 1 from the end of the second number:</p>
<p>#12: Write a 1. Go to the next instruction.</p>
<p>#13: Move the head left one space. Go to the next instruction.</p>
<p>We&#8217;ve calculated the sum! The tape should now read the sum of the two numbers. But, for good practice, we aren&#8217;t quite done&#8230; to do this right, we want to make sure we restore the head so that it points at the start of the value written to the tape. We can do this exactly the same way as we did in the incrementer, by moving until we find a blank, then stepping to the right.</p>
<p>#14: If the current value is a 1, go to instruction #15. Otherwise, go to instruction #16.</p>
<p>#15: Move the head to the left one space. Go to instruction #14.</p>
<p>#16: Move the head right one space. End program.</p>
<p>And we&#8217;re done. Try it out and see how it works!<br />
Here&#8217;s the complete program listing:</p>
<p class="codeblock"><code><strong>-Adder-</strong><br />
<strong>#1:</strong> Move the head right one space. Go to the next instruction.<br />
<strong>#2:</strong> If the current value is 1, go to instruction #1. Otherwise, go to instruction number #3.<br />
<strong>#3:</strong> Move the head right one space. Go to the next instruction.<br />
<strong>#4:</strong> If the current value is 1, go to instruction #5. Otherwise, go to instruction #6.<br />
<strong>#5:</strong> Move the head right one space. Go to instruction #4.<br />
<strong>#6</strong>: Move the head left one space. Go to the next instruction.<br />
<strong>#7:</strong> If the current value is a 1, go to instruction #8. Otherwise, go to instruction #13.<br />
<strong>#8:</strong> Write a blank. Go to the next instruction.<br />
<strong>#9:</strong> Move the head left one space. Go to the next instruction.<br />
<strong>#10:</strong> If the current value is a 1, go to instruction #11. Otherwise, go to instruction #12.<br />
<strong>#11:</strong> Move the head left one space. Go to instruction #10.<br />
<strong>#12:</strong> Write a 1. Go to the next instruction.<br />
<strong>#13:</strong> Move the head left one space. Go to the next instruction.<br />
<strong>#14:</strong> If the current value is a 1, go to instruction #15. Otherwise, go to instruction #16.<br />
<strong>#15:</strong> Move the head to the left one space. Go to instruction #14.<br />
<strong>#16:</strong> Move the head right one space. End program.</code></p>
<h4>Multi-Head Addition</h4>
<p>A multiple-head Turing machine doesn&#8217;t have any computational advantage over a single-head Turing machine&#8211;anything you can compute on one, you can compute on the other&#8211;but multi-head Turing machines can sometimes make your programs easier to conceptualize, especially for complicated algorithms.</p>
<p>From here on out, we&#8217;ll use a multi-head machine. To show how a multi-head\multi-tape machine works in comparison to a single-head machine, we&#8217;ll implement addition again, this time using three heads and three tapes instead of one.</p>
<p>To setup your Turing machine for a multi-headed operation, we need to create one tape for each head. Lay out your tape index cards in three rows. Place one head above each tape row.</p>
<p>The first and second tapes will hold our two &#8220;addends&#8221;, or numbers we want to add together. Place the first number you want to add in the first tape. Place the second number you want to add in the second tape. The third tape will hold the result. Set the third tape to blank.</p>
<p>The outline for a multi-head addition operation is much easier to follow:</p>
<p>- Copy the value from the first tape to the third tape<br />
- Append the value from the second tape to the third tape<br />
- Reset all three heads</p>
<p>Not only is this program more intuitive to read, it also eliminates the need to check for zero values.</p>
<p class="photocaption" align="center"><img src="/images/turing-multitape-add.gif" alt="Adder" /><br />
Example of the multi-head adder</p>
<p>Let&#8217;s walk through the code:</p>
<p>#1: If the current value of head #1 is 1, then go to instruction #2. Otherwise, go to instruction #5.</p>
<p>#2: With head #3, write a 1.</p>
<p>#3: Move head #3 to the right one space.</p>
<p>#4: Move head #1 to the right one space. Go to instruction #1.</p>
<p>What this segment does is copy the value of the first tape to the third tape. Every time head #1 reads a 1, head #3 writes a 1. This segment keeps looping until tape #1 runs out of 1&#8242;s.</p>
<p>The next section is essentially the same, except that it copies data from tape #2 to tape #3:</p>
<p>#5: If the current value of head #2 is 1, then go to instruction #6. Otherwise, go to instruction #9.</p>
<p>#6: With head #3, write a 1.</p>
<p>#7: Move head #3 to the right one space.</p>
<p>#8: Move head number #2 to the right one space. Go to instruction #5.</p>
<p>Tape #3 now holds our result! All that&#8217;s left is to reset the heads to start. Here is the complete listing:</p>
<p class="codeblock"><code><strong>-Multi-head Adder-</strong><br />
<strong>#1:</strong> If the current value of head #1 is 1, then go to instruction #2. Otherwise, go to instruction #5.<br />
<strong>#2:</strong> With head #3, write a 1.<br />
<strong>#3:</strong> Move head #3 to the right one space.<br />
<strong>#4:</strong> Move head #1 to the right one space. Go to instruction #1.<br />
<strong>#5:</strong> If the current value of head #2 is 1, then go to instruction #6. Otherwise, go to instruction #9.<br />
<strong>#6:</strong> With head #3, write a 1.<br />
<strong>#7:</strong> Move head #3 to the right one space.<br />
<strong>#8:</strong> Move head number #2 to the right one space. Go to instruction #5.<br />
<strong>#9:</strong> Move head #1 to the left one space.<br />
<strong>#10:</strong> If the current value of head #1 is 1, go to instruction #11. Otherwise, go to instruction #12.<br />
<strong>#11:</strong> Move head #1 to the left one space. Go to instruction #10.<br />
<strong>#12:</strong> Move head #1 to the right one space.<br />
<strong>#13:</strong> Move head #2 to the left one space.<br />
<strong>#14:</strong> If the current value of head #2 is 1, go to instruction #15. Otherwise, go to instruction #16.<br />
<strong>#15:</strong> Move head #2 to the left one space. Go to instruction #14.<br />
<strong>#16:</strong> Move head #2 to the right one space.<br />
<strong>#17:</strong> Move head #3 to the left one space.<br />
<strong>#18:</strong> If the current value of head #3 is one, go to instruction #17. Otherwise, go to instruction #19.<br />
<strong>#19:</strong> Move head #3 to the right one space. End program.</code></p>
<h4>Subtraction</h4>
<p>Hopefully, by now you&#8217;re starting to get a grasp on how to write simple programs for your Turing machine.</p>
<p>I&#8217;m not going to post a complete program listing for subtraction, as it&#8217;s pretty much the same as addition (if you can&#8217;t figure it out, ask me and I&#8217;ll help you out). If you build a three-tape setup, where tapes #1 and #2 store the numbers you want to subtract and tape #3 stores the result, then you can implement subtraction by copying the value from tape #1 to tape #3, then decrementing tape #3 by the value of tape #2. Very straight-forward.</p>
<p>There&#8217;s only one gotcha you need to look out for in subtraction: what happens if the second number is larger than the first number? You could, of course, choose to ignore this case. A better thing to do would be to finally bust out those paperclips and use the state register. If you decrement a value from tape #3, but the current value of tape #3 is already zero, change the state by adding a paperclip to indicate that the result you are writing on tape #3 is a negative value. If you don&#8217;t want to use a state register, you can accomplish the same thing by using the first cell on tape #3 to indicate whether the value is positive or negative. If the value is blank, the result is positive. If the value is 1, the result is negative. That is more-or-less what modern computers do. In any case, there are lots of good ways to handle the scenario&#8211;the important thing is that you check for it.</p>
<p><strong>Questions? Ideas? More programs you&#8217;d like to see? Post a comment!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.independentdeveloper.com/archive/2007/10/07/your_own_turing_machine_part_i/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Build a $2 Computer</title>
		<link>http://www.independentdeveloper.com/archive/2007/10/01/build_a_2_computer</link>
		<comments>http://www.independentdeveloper.com/archive/2007/10/01/build_a_2_computer#comments</comments>
		<pubDate>Tue, 02 Oct 2007 01:01:00 +0000</pubDate>
		<dc:creator>Lisa</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Fun Projects]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[computer]]></category>
		<category><![CDATA[fun projects]]></category>
		<category><![CDATA[kids projects]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[turing]]></category>
		<category><![CDATA[turing machine]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[utm]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[In 1936, Alan Turing conceived of a machine that helped lay the groundwork for modern computing. These extremely simple machines, dubbed Turing machines, are in theory able to simulate the logic of any known computer that can possibly be constructed. While Turing originally conceived of his machine only as a thought exercise and never constructed [...]]]></description>
			<content:encoded><![CDATA[<p>In 1936, Alan Turing conceived of a machine that helped lay the groundwork for modern computing. These extremely simple machines, dubbed Turing machines, are in theory able to simulate the logic of any known computer that can possibly be constructed. While Turing originally conceived of his machine only as a thought exercise and never constructed it, it is possible to build and program a simple version of Turing&#8217;s computer for only a few dollars worth of basic office supplies.</p>
<p><strong>This is a fun afternoon project for all ages, and a great way to introduce both kids and adults to the fundamentals of computer programming.</strong></p>
<p><span id="more-7"></span></p>
<h4>Supplies</h4>
<p>You will need:</p>
<p>- A magic marker<br />
- A deck of 3&#215;5 index cards<br />
- Paperclips (optional)</p>
<p>One deck of index cards will allow you to build the machine and write several programs for it. If you want to write more programs, you will need more index cards. If you don&#8217;t have index cards, you can use paper instead.</p>
<h4>Understanding Numeral Systems</h4>
<p>Before you can build and use a Turing machine, you need to know a little bit about number systems. For example, most people know that modern computers are &#8220;binary&#8221; or that the numbers we ordinarily use to count with are &#8220;decimal&#8221;, but what does that mean exactly?</p>
<p>The words &#8220;binary&#8221; and &#8220;decimal&#8221; refer to the base of the number; the base is how many digits you must count before you roll over to the next digit. <strong>Decimal means base ten.</strong> Think <em>dec</em>athlon or <em>dec</em>imeter to remember that decimal means 10. Base ten means you must count ten digits&#8211;zero through nine&#8211;before you start re-using them and move to the next column. For example, if you start at zero and count all the way up to nine, when you reach nine you run out of digits, so the next number becomes one-zero. <em>(Ten is just a fancy word for one-zero!)</em> Likewise, when you get to 99 you run out of digits again, so the next number becomes one-zero-zero. i.e. 100</p>
<p><strong>Binary means base two.</strong> Think of the words <em>bi</em>cycle or <em>bi</em>plane to remember that binary means two. To count in binary, you count two digits&#8211;zero through one&#8211;before you start re-using them and move to the next column. To count in binary, first you count zero, then one, then you&#8217;ve run out of digits so the next number becomes one-zero. Just like in decimal, every time you run out of digits you roll over to the next column.</p>
<p align="center"><img src="/images/counting-binary.gif" /><br />
<strong>Modern computers use binary, but the Turing machine we are about to build uses an even simpler numeral system known as &#8220;unary&#8221;!</strong> A unary numeral system is a <strong>base one</strong> numeral system. Think of the words <em>un</em>o or <em>uni</em>cycle to remember that unary means one. In a base-one system, you start at one, and then immediately move to the next column with one-one, then one-one-one, then one-one-one-one, and so on. This may seem strange, but you use this system all the time even if you do not realize it! For example, tally marks are a popular everyday usage of a unary numeral system&#8211;one line equals one number. Counting on your fingers, or using candies or pennies to track points in a playground game are other good examples of everyday unary counting.</p>
<p align="center"><img src="/images/tallymarks.gif" /><br />
<em>[Side note: For those who may be concerned, grouping your tally marks by five isn't the same thing as a new digit... it's just like using a comma between thousands-places in decimal. It doesn't change the number; grouping the digits simply makes it easier to read.]</em></p>
<h4>How a Turing Machine Works</h4>
<p>A Turing machine consists of an infinitely long <strong>tape</strong>, upon which &#8220;symbols&#8221; (i.e. numbers) can be written by an attached read\write recording <strong>head</strong>. The head has both a finite <strong>state</strong> and a finite <strong>set of instructions</strong>, telling the head what to do when it encounters a symbol on the tape. At each instruction, the machine may modify the symbol on the active location of the tape, change the state of the head, or move the head left or right on the tape.</p>
<p>The <em>finite set of instructions</em> is the key to it all. The idea in a nutshell is that if can you write down the exact procedure for doing something, you can hand that set of instructions to someone else and they&#8217;ll be able to do exactly what you did and come up with the same result. Note, that your list of instructions must be <em>finite</em>. &#8220;Finite&#8221; means &#8220;limited&#8221;. This is important, because it means that you can&#8217;t just go on writing new instructions forever. It means that once you&#8217;ve written down your steps, that&#8217;s it, you&#8217;re done. You&#8217;ve got to write down all the instructions about what <em>could</em> happen and how to handle it up front, before the other person tries to follow the instructions. You can&#8217;t add more later. A set of instructions like this is called an <em>algorithm</em>.</p>
<p>Think of an algorithm like a recipe. If you follow the exact steps, you&#8217;ll get the right answer to the computation; in the same way that if you follow the exact recipe for baking a chocolate cake, you&#8217;ll get a chocolate cake. Sometimes, as part of the recipe, you might be asked to make a decision and follow a different procedure based on the current state. For example, in a recipe for chocolate cake, you might be asked to stick a toothpick in the cake and see if it comes out clean. If the toothpick has sticky cake on it when you pull it out, the recipe will tell you to put the cake back in the oven for a bit. If the toothpick is clean, however, the cake is done and the recipe will ask you to take the cake out of the oven. In a computer program, this is called a <em>&#8220;branch instruction&#8221;</em>. It means, &#8220;read the current state, and do either A or B depending on what the state is&#8221;. Branch instructions let you make very powerful algorithms.</p>
<p>A Universal Turing machine, or UTM, is a Turing machine that is able to simulate another Turing machine. This is a <em>very</em> powerful concept, as it relates to the modern idea of stored programs. Basically, what Turing said is that if you could come up with a set of instructions to solve a problem, instead of just coding them permanently into a Turing machine, you could write those instructions into a table or tape in a way such that another Turing machine could play them back. This &#8220;universal machine&#8221; could then be used to solve many different kinds of computations depending on the program written in its instruction table. Neat, eh?</p>
<h4>How to Make a Turing Machine</h4>
<p>Take about half of your index cards, maybe a little more if you want to compute larger numbers. Turn the index cards so they are taller than they are wide; this will make your Turing machine a bit easier to fit on your desk later. Write a &#8220;1&#8243; on the front of each index card. Write a &#8220;0&#8243; on the back of each index card. This stack of index cards will form your <strong>&#8220;tape&#8221;</strong>.</p>
<p>Now, take three index cards and draw an arrow pointing down on them. These will be your read\write <strong>&#8220;heads&#8221;</strong>. In the corner, label each head 1, 2, and 3. In theory, any computation that can be solved on a machine with multiple heads can also be solved on a machine with a single head, but in practice some things are a lot easier to program if you have multiple heads.</p>
<p>The rest of your index cards we&#8217;ll use for writing programs. This is your table of commands or <strong>&#8220;instruction stack&#8221;</strong>.<br />
<strong>Advanced Stuff: <em>What about the &#8220;state register&#8221;?</em></strong> Earlier, we mentioned that a Turing machine consists of a tape, a head, a state, and a set of instructions. We have the tape, the head and the instructions&#8230; but what about state? Well, for simple programs you don&#8217;t really need it&#8211;the &#8220;instruction pointer&#8221; (aka. what instruction you are on) and multiple heads gives you all the state you need so I&#8217;ve omitted a separate register. But, if you really want one, you can use paperclips. Simply attach or remove paperclips from the heads to track your state; the number of paperclips denotes what state you are in. Again, you don&#8217;t really need it unless you are going to get fancy, so I won&#8217;t be using it in the examples.</p>
<h4>Set Up</h4>
<p>To set up your Turing machine, first you must lay out your &#8220;tape&#8221;. Place the tape index cards face-down in a long row, so that they all read zero.</p>
<p>Next, place the first &#8220;head&#8221; index card above the tape. Position the head at least three index cards over from left hand side of the tape. This is your starting position. You don&#8217;t want your starting position to be on the first index card, because you need to be able to move the head to the left of start for some programs.</p>
<p align="center"><a href="/images/turing-machine-numbertwo.jpg" rel="lightbox" title="My Turing machine displaying the number '2'."><img src="/images/turing-machine-numbertwo.thumb.jpg" /></a><br />
My Turing machine displaying the number &#8217;2&#8242;.</p>
<p>You can now enter an &#8220;initial value&#8221; into your machine. Your initial value is whatever number you want to perform an operation on. Starting at the head position, flip over index cards so they read &#8220;1&#8243; until you reach whatever value you want to start with. Remember, a Turing machine uses a unary number system. This means that each 1 written on the tape increases the value of the number by one. For example, if you wanted to perform an operation on the number four, you would flip over four index cards. Your tape would read 1-1-1-1.</p>
<h4>How to Program the Computer</h4>
<p>To program your Turing machine, you must write a set of instructions on a stack of numbered index cards. The rules for programming a Turing machine are simple. One index card = one instruction. Be sure to number your index cards in case you drop your program on the floor so they don&#8217;t get mixed up.</p>
<p><strong>On each instruction you may do any of the following things:</strong></p>
<p>- Read the current value of the tape. The current value is whatever number is written on the tape at the position that the arrow on the head is pointing to.</p>
<p>- Write a new value to the tape. You can flip over the index card that the head is pointing to, to change its value.</p>
<p>- Move the head a specified number of spaces to the left or to the right. For example, you could say &#8220;move the head three spaces to the right&#8221;.**</p>
<p>- Specify the next instruction to execute. You may &#8220;branch&#8221; instructions, and specify a different instruction to execute based on the current value of the tape. For example, you can say &#8220;If the value of the tape is 1, go to instruction #10. Otherwise, go to instruction #12.&#8221;. If you don&#8217;t specify an instruction, it is assumed that you mean to run the next instruction in the sequence. If the current instruction can&#8217;t be executed, the machine halts.</p>
<p><strong>Additional programming tips:</strong></p>
<p>- <em>Always</em> finish your programs in a &#8220;known state&#8221;. That means, return your head to the start position after you finish your calculation, don&#8217;t just leave it dangling out there. You should always be able to run the next program immediately after you finish your last without resetting anything.</p>
<p>- Geeze-o-pete, don&#8217;t do the &#8220;if the current instruction can&#8217;t be executed, the machine halts&#8221; thing! That&#8217;s called &#8220;crashing the computer&#8221;. If you mean your program to finish, write &#8220;end program&#8221; on the instruction index card instead.</p>
<p>Yes, folks, even a computer based on a deck of index cards can crash!</p>
<h4>Your First Program: The Incrementer</h4>
<p>A great program to start with is an incrementer. It&#8217;s a very simple operation, and a good example of how the machine works. It&#8217;s also a very useful operation, as modern computers use incrementers all the time for many different things.</p>
<p><strong>An incrementer takes the current value, and adds one to it.</strong></p>
<p>So, what is the algorithm for an incrementer? Well, remember that our Turing machine uses a unary counting system. That means, if we want to increase the value of a number by one, all we need to do is write another 1 to the tape.</p>
<p>I always like to break programs down into their steps before I write them, so here&#8217;s the general outline for the program:</p>
<p>- From the starting position, move the head past the end of whatever value is written on the tape.<br />
- Write a one.<br />
- Move the head back to the starting position, so we finish in a known state.</p>
<p>Ok! Now all we need to do is break it down into the individual instructions. So, first things first, let&#8217;s find the end of the value on the tape. Here&#8217;s how to do this, using the rules we outlined earlier:</p>
<p>#1: If the current value is 1, go to instruction #2. Otherwise go to instruction #3.</p>
<p>#2: Move the head to the right one space. Go to instruction #1.</p>
<p>Excellent. These two instructions will cause the program to loop until the head moves to the first blank space. Once we&#8217;ve reached the end of the value we want to increment, we have to add a one.</p>
<p>#3: Write a 1. Go to the next instruction.</p>
<p>Ok! We&#8217;ve done it, we&#8217;ve incremented the number. That wasn&#8217;t bad at all, was it? Some programmers would just end the program here, since it&#8217;s completed the task. However, a good programmer will leave the system in a known state and ready to run the next program. We&#8217;re good programmers, so we&#8217;re going to move the head back to the start.</p>
<p>#4: If the current value is a 1, go to instruction #5. Otherwise, go to instruction #6.</p>
<p>#5: Move the head to the left one space. Go to instruction #4.</p>
<p>#6: Move the head right one space. End program.</p>
<p>This code segment is another loop, similar to our first two instructions. It causes the head to keep moving left until it reaches a blank. When the head reaches a blank, we know we must be immediately before the start. So, we move the head back one step to the right leaving the head pointing at the start position and ready to run the next program. That&#8217;s all there is to it!<br />
In summary, here&#8217;s the complete program listing:</p>
<p class="codeblock"><code><strong>-Incrementer-</strong><br />
<strong>#1:</strong> If the current value is 1, go to instruction #2. Otherwise go to instruction #3.<br />
<strong>#2:</strong> Move the head to the right one space. Go to instruction #1.<br />
<strong>#3:</strong> Write a 1. Go to the next instruction.<br />
<strong>#4:</strong> If the current value is a 1, go to instruction #5. Otherwise, go to instruction #6.<br />
<strong>#5:</strong> Move the head to the left one space. Go to instruction #4.<br />
<strong>#6:</strong> Move the head right one space. End program.</code></p>
<p><strong>To &#8220;run&#8221; the program, simply do exactly what it says on the index cards.</strong> If the card says move the head to the right, for example, move the index card representing the head one space to the right, so that it&#8217;s pointing at the next index card in the tape. Be sure to follow along <em>exactly</em>.</p>
<p>You can run this program again and again, and increment the number as many times as you like&#8230; or, at least until you run out of tape! A theoretical Turing machine has an infinite tape, so it doesn&#8217;t have problems with limited storage. Of course, that&#8217;s impossible to build in real life. Our tape is limited by the number of index cards we have, but I&#8217;m sure you can already see it doesn&#8217;t take a very long tape for the machine to be useful. If you want to operate on larger numbers, add more index cards. Be warned though: just because a Turing machine is a very powerful computer, that doesn&#8217;t mean it&#8217;s a very fast one! <img src='http://www.independentdeveloper.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Next time, we&#8217;ll implement some basic arithmetic, and learn how to use our Turing machine to add two numbers together.</strong></p>
<hr />** Note: By the book, a Turing machine can only move 1 step to the left or right in a given instruction. I suggest that you allow it to take more than one step at a time; this doesn&#8217;t change much computationally, but like multiple heads it makes the programs a little shorter and easier.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.independentdeveloper.com/archive/2007/10/01/build_a_2_computer/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
