<?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>The Home of Jon &#187; Programming</title>
	<atom:link href="http://www.thehomeofjon.net/categories/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thehomeofjon.net</link>
	<description>Welcome home.</description>
	<lastBuildDate>Mon, 07 Dec 2009 06:43:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>YAPB Importer Exporter Plugin</title>
		<link>http://www.thehomeofjon.net/programming/2009/07/yapb-importer-exporter-plugin/</link>
		<comments>http://www.thehomeofjon.net/programming/2009/07/yapb-importer-exporter-plugin/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 19:07:38 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web-Development]]></category>

		<guid isPermaLink="false">http://www.thehomeofjon.net/?p=213</guid>
		<description><![CDATA[This site, The Home of Jon, is built on the most-excellent Wordpress blogging engine. I made the switch from CodeIgniter not very long ago, because it just made more sense for me to use a Content Management System, as opposed to working on my own custom rolled website. For my art site I used a [...]]]></description>
			<content:encoded><![CDATA[<p>This site, The Home of Jon, is built on the most-excellent Wordpress blogging engine. I made the switch from <a title="Code Igniter" href="http://www.codeigniter.com">CodeIgniter</a> not very long ago, because it just made more sense for me to use a <a href="http://en.wikipedia.org/wiki/Web_content_management_system">Content Management System</a>, as opposed to working on my own custom rolled website. For my <a href="http://art.thehomeofjon.net">art site</a> I used a plugin called &#8220;<strong>Yet Another PhotoBlog</strong>&#8220;, or <a href="http://johannes.jarolim.com/blog/wordpress/yet-another-photoblog/">YAPB</a> for short. I seriously love this plugin &#8211; it does just what I need it to do. Plus it&#8217;s author Johannes Jarolim coded it very nicely.</p>
<p>I ran into one problem though using it when I was creating my art site. I had a bunch of pictures, and though Wordpress has an import function, it doesn&#8217;t support YAPB natively. That was a big problem for me &#8211; I could import all my posts, but then I&#8217;d have to go back and manually add pictures to each of my posts (around 100 of them). Um, I don&#8217;t think so.</p>
<p>So I set my mind to it &#8211; I was going to write myself a Wordpress plugin to make it much easier for me to be lazy! So it was written. So let it be done.</p>
<p><span id="more-213"></span></p>
<h2>How I made it</h2>
<p>What I basically did is rip out the exporting code (literally&#8230;like copy and paste) for Wordpress and added one extra field for the YAPB image. Then ripped out the importing code for Wordpress (literally, another copy and paste) and added functionality to read that extra field and save the image along with the post. Then wrapped up both of those in a nice little plugin.</p>
<p> So the importing/exporting works exactly as Wordpress does now in 2.7. The only difference is your images come along with the import/export. </p>
<p> This is the very first beta version of the plugin (my first plugin actually), so sorry if there isn&#8217;t any fancy image stuff. But it works! I used it for my own site.</p>
<h2>Instructions for Use</h2>
<p>The export function is just like Wordpress and is located under Tools -> Export YAPB. The import function is located the same place the other import functions are, Tools -> Import -> YAPB Importer. Use it exactly like you would use the default Wordpress importer. </p>
<p> <span style="font-size: x-small;">(Disclaimer: Of course, in the interest of you-can&#8217;t-blame-me-ism, always backup your DB before doing significant changes like imports!)</span></p>
<div class="buttonwrapper">
<a class="boldbuttons" href='/wp-content/uploads/posts/yapb-import-export.zip'><span>Download YAPB Importer Exporter</span></a></div>
<p><span style="font-size: x-small;"><br />
</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thehomeofjon.net/programming/2009/07/yapb-importer-exporter-plugin/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tic-Tac-Toe Using Minimax and ASP.NET</title>
		<link>http://www.thehomeofjon.net/programming/2007/11/tic-tac-toe-using-minimax-and-asp-net/</link>
		<comments>http://www.thehomeofjon.net/programming/2007/11/tic-tac-toe-using-minimax-and-asp-net/#comments</comments>
		<pubDate>Sun, 18 Nov 2007 13:25:41 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.thehomeofjon.net/blog/title/tic-tac-toe-using-minimax-and-asp-net.html</guid>
		<description><![CDATA[&#8220;I&#8217;m sorry Dave, I can&#8217;t allow you to do that&#8221;. That chilling statement made by the machine HAL in 2001 A Space Odyssey is just one of the many examples of how humans have been fascinated by the idea of artificial intelligence. I myself am very interested in the subject and hope to go back [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;I&#8217;m sorry Dave, I can&#8217;t allow you to do that&#8221;. That chilling statement made by the machine HAL in <a href="http://www.imdb.com/title/tt0062622/quotes">2001 A Space Odyssey</a> is just one of the many examples of how humans have been fascinated by the idea of <a href="http://www-formal.stanford.edu/jmc/whatisai/">artificial intelligence</a>. I myself am very interested in the subject and hope to go back to graduate school for a degree in the field. In the meantime I&#8217;m writing small applications like these to demonstrate what AI can do.</p>
<p><span id="more-22"></span></p>
<p>This is an ASP.NET application that will play a perfect game of Tic-Tac-Toe using the AI algorithm called minimax. First a little about the game Tic-tac-toe. It is a deterministic game, meaning that all actions have a direct and knowable consequence to the game state. It is what theorists like to call a &#8220;<a href="http://en.wikipedia.org/wiki/Zero-sum">Zero Sum</a>&#8221; game &#8211; where the compluter has complete, or perfect information about the game state, the game space, and the environment.</p>
<p>As with the majority of things in AI, what the sitation boils down to is search. Here&#8217;s the components we need to have in order to complete our search of the tic-tac-toe game space:</p>
<ul>
<li>An initial state. For us this is an empty board</li>
<li>A successor function. A method which gives us the resulting states that are accessible by making a particular action against the state we pass in.</li>
<li>A terminal test. A method that determines if we are in a terminal state &#8211; i.e., whether the game is over or not. In our case it would be Player 1, Player 2, or draw (cat&#8217;s game).</li>
<li>A utility function that gives a numeric value for terminal states.</li>
</ul>
<p>Since tic-tac-toe is a turn-based, two player game, it lends itself to using the minimax algorithm to determine the best move for a given state. The minimax algorithm is a little bit confusing.  Here&#8217;s a really simple rundown:</p>
<p>In a normal search situation, you use a initial state and determine a sequence of moves to get you to your desired goal state. However, in turn based games, there is not just one player MAX, but the opponent player MIN that we must worry about. MIN&#8217;s moves can impact the game states that are available to us and indeed can render our desired goal state (where we, MAX win) unreachable. That of course is not satisfactory. What can we do? The MAX player, the first to make a move, must pick a move that has the highest value returned by the utility function.</p>
<p>However, assuming MIN plays optimally, MIN will also try to choose the best move that he can, thus minimizing MAX&#8217;s outcome. Thus, MAX should make the move that minimizes the value of MIN&#8217;s utility function. In other words, the computer MAX, should make the move which leaves its opponent capable of doing the least damage.</p>
<p>Hopefully that sheds some light on how the algorith works. For an in-depth discussion  of the details of how minimax works, see this article at the <a href="http://ai-depot.com/articles/minimax-explained/">AI Depot</a>. (Note that our algorithm makes use of Alpha-Beta pruning to reduce our search tree).</p>
<p>&#8212;&#8212;&#8212;&#8212;-</p>
<p>Now that we have talked about how our AI logic works, lets look at the architecture of our application. Even though this is a small application, I made use of the <a href="http://www.webopedia.com/quick_ref/app.arch.asp">N-Tier</a> approach to web programming. Since our example is simple, we will only have 2 layers: our presentation layer, and our <a href="http://en.wikipedia.org/wiki/Business_logic">business logic</a>. In laymans terms, this is just the layer that displays our information and the layer that contains our logic, respectively. The reason I did this is because it helps us modularize our code for easy re-use and modification, and makes the code much cleaner to read.</p>
<p>I created an ASP.NET User Control to display our tic-tac-toe board, then placed it in an ASP.NET page that configures and displays it. The User Control in turn uses a custom Board object that I created that represents the tic-tac-toe board as an object. I have methods implemented in a separate class that performs the Artificial Intelligence operations on the Board object.</p>
<h2>Board.cs</h2>
<p>This is the class that serves as a wrapper class around a simple integer array. The positions in the array map to positions on the board; e.g. the first three spaces on the first row map to the first 3 positions in the integer array. We also implement the ICloneable and IEnumerable interfaces. ICloneable so that we can make an exact duplicate of a board and manipulate it and not touch the original. The IEnuerable interface is optional, but it lets us use a foreach statement to traverse the board, which is handy. The class also includes helper methods like IsFull, that can tell us some information about a given board.</p>
<h2>MinimaxAlgorithm.cs</h2>
<p>This is where the magic happens. This is the C# implementation of the minimax algorithm with alpha-beta pruning. The class implements an IGameAlgorithm interface which is an interface I created that has on simple method in it named Solve. It take a Board object as an argument and returns the optimal move. I did this so that when calling the algorithm, we can call the interface and not the class itself. That way, if we want to swap out the algorithm for a different one (say with a <a href="http://en.wikipedia.org/wiki/Q-learning">Q-Learning Reinforcement Algorithm</a>), it will be quite easy. Here&#8217;s the meat of the code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.thehomeofjon.net/wp-content/plugins/wp-codebox/wp-codebox.php?p=22&amp;download=MinimaxAlgorithm.txt">MinimaxAlgorithm.txt</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p223"><td class="code" id="p22code3"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> TicTacToe.<span style="color: #0000FF;">Library</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> BoardPlayer
    <span style="color: #000000;">&#123;</span>
        NoPlayer <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>, Player1 <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>, Player2 <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>Serializable<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Board <span style="color: #008000;">:</span> ICloneable, IEnumerable<span style="color: #008000;">&lt;</span>BoardPlayer<span style="color: #008000;">&gt;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> board<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Constructor - initializes empty board and hash key</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #0600FF;">public</span> Board<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            board <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Gets the value at the specified 0-based index</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;index&quot;&gt;Index to retrieve&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;BoardPlayer&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> BoardPlayer <span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">int</span> index<span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>BoardPlayer<span style="color: #000000;">&#41;</span> board<span style="color: #000000;">&#91;</span>index<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> board<span style="color: #000000;">&#91;</span>index<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Gets the value based on x,y coordinates, where 0,0 is</span>
        <span style="color: #008080; font-style: italic;">/// the top left.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;x&quot;&gt;X coordinate&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;y&quot;&gt;Y coordinate&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;BoardPlayer&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> BoardPlayer <span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">int</span> x, <span style="color: #FF0000;">int</span> y<span style="color: #000000;">&#93;</span>
        <span style="color: #000000;">&#123;</span>
            get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>BoardPlayer<span style="color: #000000;">&#41;</span> board<span style="color: #000000;">&#91;</span>y <span style="color: #008000;">*</span> <span style="color: #FF0000;">3</span> <span style="color: #008000;">+</span> x<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            set <span style="color: #000000;">&#123;</span> board<span style="color: #000000;">&#91;</span>y <span style="color: #008000;">*</span> <span style="color: #FF0000;">3</span> <span style="color: #008000;">+</span> x<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Boolean based on if there is a player on the board.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;True if board is empty.&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> IsEmpty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #0600FF;">in</span> board<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>i <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Boolean based on if there the board is full or not.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;True if board is full.&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> IsFull<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #0600FF;">in</span> board<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>i <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Gets all board spots that are not taken.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Array of indices that are free on the board.&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> GetValidMoves<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> moves <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">9</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>board<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                    moves.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> moves.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Evaluates if player has won or not</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;player&quot;&gt;BoardPlayer&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;True if given player has won.&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Evaluate<span style="color: #000000;">&#40;</span>BoardPlayer player<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">int</span> x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// check columns</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span>i, j<span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>player<span style="color: #000000;">&#41;</span> x<span style="color: #008000;">++;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>x <span style="color: #008000;">==</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #008080; font-style: italic;">// check rows</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> j <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span> j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span>j, i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>player<span style="color: #000000;">&#41;</span> x<span style="color: #008000;">++;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>x <span style="color: #008000;">==</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #008080; font-style: italic;">// check diagonal</span>
            x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span>i, i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>player<span style="color: #000000;">&#41;</span> x<span style="color: #008000;">++;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>x <span style="color: #008000;">==</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #008080; font-style: italic;">// check diagonal the other way</span>
            x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span>, j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i<span style="color: #008000;">--</span>, j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#91;</span>i, j<span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>player<span style="color: #000000;">&#41;</span> x<span style="color: #008000;">++;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>x <span style="color: #008000;">==</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Default ToString</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;String representation of object&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            StringBuilder str <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>, j <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">9</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span>, j<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// set player symbol</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>board<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>BoardPlayer.<span style="color: #0000FF;">NoPlayer</span><span style="color: #000000;">&#41;</span>
                    str.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">else</span>
                    str.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>board<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// start a new line</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>j <span style="color: #008000;">==</span> <span style="color: #FF0000;">2</span> <span style="color: #008000;">&amp;&amp;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">6</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    str.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0}-+-+-{0}&quot;</span>, <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Environment</span>.<span style="color: #0000FF;">NewLine</span> <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    j <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">else</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>j <span style="color: #008000;">!=</span> <span style="color: #FF0000;">2</span><span style="color: #000000;">&#41;</span>
                        str.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;|&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> str.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Determines if another board is equal to given board</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;obj&quot;&gt;&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> Equals<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> obj<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> <span style="color: #008000;">==</span> obj<span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #000000;">&#40;</span>obj <a href="http://www.google.com/search?q=is+msdn.microsoft.com"><span style="color: #008000;">is</span></a> Board<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
            Board other <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Board<span style="color: #000000;">&#41;</span>obj<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">9</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>board<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> other.<span style="color: #0000FF;">board</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Get hash code for object</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">int</span> GetHashCode<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">GetHashCode</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#region ICloneable Members</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> Clone<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Board newBoard <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Board<span style="color: #000000;">&#41;</span><span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">MemberwiseClone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            newBoard.<span style="color: #0000FF;">board</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>board.<span style="color: #0000FF;">Clone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> newBoard<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region IEnumerable&lt;BoardPlayer&gt; Members</span>
&nbsp;
        <span style="color: #0600FF;">public</span> IEnumerator<span style="color: #008000;">&lt;</span>BoardPlayer<span style="color: #008000;">&gt;</span> GetEnumerator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// returns board, but as BoardPlayers instead of integers</span>
            BoardPlayer<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> newBoard <span style="color: #008000;">=</span> Array.<span style="color: #0000FF;">ConvertAll</span><span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span>, BoardPlayer<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>board, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Converter<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span>, BoardPlayer<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>
                <span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>BoardPlayer<span style="color: #000000;">&#41;</span>i<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// return iterator for new board</span>
            <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>newBoard <span style="color: #0600FF;">as</span> IEnumerable<span style="color: #008000;">&lt;</span>BoardPlayer<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetEnumerator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #008080;">#region IEnumerable Members</span>
&nbsp;
        <span style="color: #000000;">System.<span style="color: #0000FF;">Collections</span></span>.<span style="color: #0000FF;">IEnumerator</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Collections</span></span>.<span style="color: #0000FF;">IEnumerable</span>.<span style="color: #0000FF;">GetEnumerator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> GetEnumerator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#endregion</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see the code is not very complex or long. It&#8217;s just very recursive, which can be confusing in its own right.</p>
<h2>BoardManager.cs</h2>
<p>This class encapsulates the different actions that we want to perform on the Board. If we want to make a move on the board, or compute the next best minimax move, this class handles that through a bunch of static functions. It helps to have all of this code localized into one place.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.thehomeofjon.net/wp-content/plugins/wp-codebox/wp-codebox.php?p=22&amp;download=BoardManager.txt">BoardManager.txt</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p224"><td class="code" id="p22code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">namespace</span> TicTacToe.<span style="color: #0000FF;">Library</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> BoardManager
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> IGameAlgorithm gameAlgorithm <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> MinimaxAlgorithm<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Sets the algorithm used by ComputeBestMove(). By default it is</span>
        <span style="color: #008080; font-style: italic;">/// the Minimax Algorithm.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;algorithm&quot;&gt;Algorith to use for computing&lt;/param&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> SetGameAlgorithm<span style="color: #000000;">&#40;</span>IGameAlgorithm algorithm<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            gameAlgorithm <span style="color: #008000;">=</span> algorithm<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Determines if the board can be played further.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;board&quot;&gt;Board to evaluate&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;True if the game is over&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> IsGameComplete<span style="color: #000000;">&#40;</span>Board board<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>board.<span style="color: #0000FF;">Evaluate</span><span style="color: #000000;">&#40;</span>BoardPlayer.<span style="color: #0000FF;">Player1</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">||</span>
                board.<span style="color: #0000FF;">Evaluate</span><span style="color: #000000;">&#40;</span>BoardPlayer.<span style="color: #0000FF;">Player2</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">||</span>
                board.<span style="color: #0000FF;">IsFull</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">else</span>
                <span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Determines the player who won the current board.</span>
        <span style="color: #008080; font-style: italic;">/// The result can be no-player.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;board&quot;&gt;Board to evaluate&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> BoardPlayer GetWinner<span style="color: #000000;">&#40;</span>Board board<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>board.<span style="color: #0000FF;">Evaluate</span><span style="color: #000000;">&#40;</span>BoardPlayer.<span style="color: #0000FF;">Player1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> BoardPlayer.<span style="color: #0000FF;">Player1</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>board.<span style="color: #0000FF;">Evaluate</span><span style="color: #000000;">&#40;</span>BoardPlayer.<span style="color: #0000FF;">Player2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> BoardPlayer.<span style="color: #0000FF;">Player2</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">else</span>
                <span style="color: #0600FF;">return</span> BoardPlayer.<span style="color: #0000FF;">NoPlayer</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Make a move on the given board.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;board&quot;&gt;Board to make move on&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;player&quot;&gt;Player who is making the move&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;position&quot;&gt;Positon the player make the move to&lt;/param&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> MakeMoveOnBoard<span style="color: #000000;">&#40;</span>Board board, BoardPlayer player, <span style="color: #FF0000;">int</span> position<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            board<span style="color: #000000;">&#91;</span>position<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> player<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Computes the best move for the board using the algorithm set by</span>
        <span style="color: #008080; font-style: italic;">/// SetGameAlgorithm. If not set, it defaults to Minimax algorithm.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;board&quot;&gt;Board to evaluate.&lt;/param&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Index of the best board move.&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">int</span> ComputeBestMove<span style="color: #000000;">&#40;</span>Board board<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> gameAlgorithm.<span style="color: #0000FF;">Solve</span><span style="color: #000000;">&#40;</span>board<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// Returns a random move o nthe board.</span>
        <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
        <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;Index of the random board move.&lt;/returns&gt;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">int</span> ComputeRandomMove<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Random r <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Random<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> r.<span style="color: #0000FF;">Next</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">8</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>As I mentioned, all the methods that we need to interact with a Board are handled in this class. This is our true &#8220;business logic&#8221; component of the system. It functions more like a <a href="http://en.wikipedia.org/wiki/Facade_pattern">facade design pattern</a> than anything else.</p>
<p>Continue on to the next page to get the rest and the source code&#8230;</p>
<p>
]]></content:encoded>
			<wfw:commentRss>http://www.thehomeofjon.net/programming/2007/11/tic-tac-toe-using-minimax-and-asp-net/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tic-Tac-Toe Applet</title>
		<link>http://www.thehomeofjon.net/programming/2007/10/tic-tac-toe-applet/</link>
		<comments>http://www.thehomeofjon.net/programming/2007/10/tic-tac-toe-applet/#comments</comments>
		<pubDate>Sat, 20 Oct 2007 18:14:57 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.thehomeofjon.net/blog/title/tic-tac-toe-applet.html</guid>
		<description><![CDATA[Training in the remotest parts of the galaxy, TicTacToeTron (also known as T4 for those who are down) has relentlessly sought the highest point of perfection in the mind-bending process that some call the game &#8220;Tic-Tac-Toe&#8221;.
This is the Tic-Tac-Toe Applet. The AI player will play a human player using the AI&#160; minimax algorithm. Using this [...]]]></description>
			<content:encoded><![CDATA[<p>Training in the remotest parts of the galaxy, TicTacToeTron (also known as T4 for those who are down) has relentlessly sought the highest point of perfection in the mind-bending process that some call the game &ldquo;Tic-Tac-Toe&rdquo;.<span id="more-31"></span></p>
<p>This is the Tic-Tac-Toe Applet. The AI player will play a human player using the AI&nbsp; minimax algorithm. Using this algorithm the computer can look forward into the future and determine the consequence of every move it makes &#8211; and since tic-tac-toe is a pretty simplistic game, TicTacToeTron will be able to make his moves with perfect accuracy. He can never lose. See this <a href="http://en.wikipedia.org/wiki/Minimax">link</a> for a deeper explanation of the minimax algorithm.</p>
<div class="buttonwrapper"><a class="boldbuttons" href="/wp-content/uploads/posts/tictactoe_applet.zip"><span>Download Tic-Tac-Toe Applet</span></a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.thehomeofjon.net/programming/2007/10/tic-tac-toe-applet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Puzzle Solver Applet</title>
		<link>http://www.thehomeofjon.net/programming/2007/10/puzzle-solver-applet/</link>
		<comments>http://www.thehomeofjon.net/programming/2007/10/puzzle-solver-applet/#comments</comments>
		<pubDate>Sat, 20 Oct 2007 18:05:25 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.thehomeofjon.net/blog/title/puzzle-solver-applet.html</guid>
		<description><![CDATA[This is a an applet that implements a well-known Artificial Intelligence algorithm called A* search. The &#8216;robot&#8217; is set up in a maze-like structure and must find its way to the goal. The robot has complete knowledge of the environment &#8211; meaning that it knows where the goal is and where the walls are. 
See [...]]]></description>
			<content:encoded><![CDATA[<p>This is a an applet that implements a well-known Artificial Intelligence algorithm called A* search. The &#8216;robot&#8217; is set up in a maze-like structure and must find its way to the goal. The robot has complete knowledge of the environment &#8211; meaning that it knows where the goal is and where the walls are. <span id="more-32"></span></p>
<p>See this <a href="http://en.wikipedia.org/wiki/A%2A_search">link</a> to see a more complete description of A* search. This implementation of A* uses 4 simple heuristics (that is, guesses the robot can make about the best path to take based on knowledge of its environment):</p>
<p>1) the straightline distance between where the robot is and there the goal is.<br />
 2) the distance between where the robot is and there the goal is on the x-axis.<br />
 3) the distance between where the robot is and there the goal is on the y-axis.<br />
 4) the best of all value of any given heuristics.</p>
<div class="buttonwrapper"><a class="boldbuttons" href="/wp-content/uploads/posts/puzzle.zip"><span>Download Puzzle Solver</span></a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.thehomeofjon.net/programming/2007/10/puzzle-solver-applet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Peg Board Solver</title>
		<link>http://www.thehomeofjon.net/programming/2007/10/peg-board-solver/</link>
		<comments>http://www.thehomeofjon.net/programming/2007/10/peg-board-solver/#comments</comments>
		<pubDate>Sat, 20 Oct 2007 17:44:44 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.thehomeofjon.net/blog/title/peg-board-solver.html</guid>
		<description><![CDATA[The Peg Board solver is a depth-first-search algorithm that will iterate through each potential configuration of the peg board that is possible &#8211; it will then store each solution that it finds. Since there are no loops in the search space, this method is complete and when the search is done all possible solutions to [...]]]></description>
			<content:encoded><![CDATA[<p>The Peg Board solver is a depth-first-search algorithm that will iterate through each potential configuration of the peg board that is possible &#8211; it will then store each solution that it finds. Since there are no loops in the search space, this method is complete and when the search is done all possible solutions to the game will have been found. See this <a href="http://www.danobrien.ws/PegBoard.html">link</a> for a more in-depth explanation of the Peg Board game and its solutions.</p>
<p><span id="more-33"></span></p>
<div class="buttonwrapper"><a class="boldbuttons" href="/wp-content/uploads/posts/pegboard.zip"><span>Download Peg Board Solver</span></a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.thehomeofjon.net/programming/2007/10/peg-board-solver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Priority Queue in C</title>
		<link>http://www.thehomeofjon.net/programming/2007/10/a-priority-queue-in-c/</link>
		<comments>http://www.thehomeofjon.net/programming/2007/10/a-priority-queue-in-c/#comments</comments>
		<pubDate>Sat, 20 Oct 2007 15:19:38 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.thehomeofjon.net/blog/title/a-priority-queue-in-c.html</guid>
		<description><![CDATA[So I was writing an implementation of the A* search algorithm for a friend. He has a robot that must navigate a maze &#8211; the robot can obtain &#8220;complete&#8221; knowledge of its environment through an instrument called &#8220;ladar&#8221;. It uses lasers to map out the area around it. Thus, knowing the goal and the obstacles [...]]]></description>
			<content:encoded><![CDATA[<p>So I was writing an implementation of the A* search algorithm for a friend. He has a robot that must navigate a maze &#8211; the robot can obtain &#8220;complete&#8221; knowledge of its environment through an instrument called &#8220;ladar&#8221;. It uses lasers to map out the area around it. Thus, knowing the goal and the obstacles around it, the A* search algorithm proved to be a prime solution for automatically navigating the robot.<span id="more-34"></span></p>
<p>One of the best ways to implement A* is using a priority queue. That is because of the following. As the search generates steps that it can take, it will store each step in a data structure. The most easiest and abstract way to handle this is to have a priority queue, so that when you enter a new step or move the robot can take, it will be inserted automatically (by order of the cost it will take to get there) in the sequence of moves it has already generated. Thus, when you remove the first element from the queue, it will be the best and lowest costing move.</p>
<p>The internals of the priority queue is based on a heap structure. (See a good <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://window.terratron.com/%7Esosman/computers/java/heap/" target="_blank">applet</a> for a visual explanation and this <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://en.wikipedia.org/wiki/Priority_queue" target="_blank">article</a> describing it). This heap is a binary tree which allows for quick access and retrieval. The most attractive feature is that it lets me find the the lowest costing node in constant time because I am not searching for it.</p>
<p>Writing a queue in any real language isn&#8217;t so hard. But writing it in a low level language like C proved to be a challenge. In the case anyone else can use this implementation, I&#8217;ve posted it here for general availability. It should be known that I am no expert C programmer &#8211; so if anyone has any improvements or ideas, let me know!</p>
<div class="buttonwrapper"><a class="boldbuttons" href="/wp-content/uploads/posts/pqueue.zip"><span>Download Priority Queue for C</span></a></div>
]]></content:encoded>
			<wfw:commentRss>http://www.thehomeofjon.net/programming/2007/10/a-priority-queue-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
