<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9</title>
    <atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Niners/c4f.Bryan-Dougherty/Posts/RSS"></atom:link>
    <itunes:summary></itunes:summary>
    <itunes:author>Microsoft</itunes:author>
    <itunes:subtitle></itunes:subtitle>
    <image>
      <url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
      <title>Channel 9</title>
      <link>http://channel9.msdn.com/Niners/c4f.Bryan-Dougherty/Posts</link>
    </image>
    <itunes:image href=""></itunes:image>
    <itunes:category text="Technology"></itunes:category>
    <description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
    <link>http://channel9.msdn.com/Niners/c4f.Bryan-Dougherty/Posts</link>
    <language>en</language>
    <pubDate>Sun, 19 May 2013 17:56:01 GMT</pubDate>
    <lastBuildDate>Sun, 19 May 2013 17:56:01 GMT</lastBuildDate>
    <generator>Rev9</generator>
    <c9:totalResults>2</c9:totalResults>
    <c9:pageCount>1</c9:pageCount>
    <c9:pageSize>25</c9:pageSize>
  <item>
      <title>Your Code Sunk My Battleship!</title>
      <description><![CDATA[
<p></p>
<span id="c4fmetadata">
<table cellspacing="0" cellpadding="1" width="100%" border="0">
<tbody>
<tr class="entry_overview">
<td><span class="entry_description">Clarity Battleship is a multi-player game in which players code the &quot;artificial intelligence&quot; needed to command their fleet to victory on the high seas. The game is built using WPF and WCF.</span></td>
</tr>
<tr>
<td colspan="2">
<div class="entry_author">Bryan Dougherty</div>
<div class="entry_company"><a href="http://blogs.claritycon.com/blogs/bryan_dougherty">Bryan's Blog</a></div>
<br>
<div class="entry_details"><b>Difficulty: </b><span class="entry_details_input">Intermediate</span></div>
<div class="entry_details"><b>Time Required:</b> <span class="entry_details_input">
1-3 hours to create your own fleet</span></div>
<div class="entry_details"><b>Cost: </b><span class="entry_details_input">Free</span></div>
<div class="entry_details"><b>Software: </b><span class="entry_details_input"><a href="http://msdn.com/express/">Visual Basic or Visual C# Express Editions</a></span></div>
<div class="entry_details"><b>Hardware: </b><span class="entry_details_input"></span></div>
<div class="entry_details"><b>Download: </b><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/8892343/claritybattleship.zip">Download</a>
<ul>
</ul>
</div>
</td>
</tr>
</tbody>
</table>
</span>
<h2>Clarity Battleship</h2>
<p>At Clarity Consulting we’re always looking for a little friendly competition. That’s why a few times a year we hold coding competitions that we call Tech Challenges. I recently organized one based on the classic board game Battleship.</p>
<p>The flow of the game is based loosely on the board game. The simple goal is to sink all of your opponents’ ships. Since we’re writing code though, I decided to step up the game play and make it a more interesting, requiring more strategy than luck.&nbsp; Just
 like the board game, the battlefield is a grid. Unlike the classic, however, multiple teams can battle at the same time. Each team’s fleet is assigned to a region of the Battlefield.
</p>
<p>In our tournament we had six teams battle at a time and held one “Battle Royale” with eleven teams.&nbsp; Below you can see a sample screen shot of a game with three teams. Blocks in red indicate damage. The bottom of the screen displays messages broadcast or
 information about fleets that have been sunk.&nbsp; </p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/8892343/screenshotLarge.jpg"><img height="154" alt="Screen Shot" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/8892343/screenshotLarge_thumb.jpg" width="244" border="0"></a>
</p>
<p>Teams position ships at the beginning of the game and give ships orders to execute including:</p>
<ul>
<li>Attacking locations on the battlefield </li><li>Performing reconnaissance on an area of the battlefield </li><li>Broadcasting messages to opponents or conspirators via an “open radio frequency” (collusion and deception are allowed and encouraged!)
</li></ul>
<p>Game play is pseudo turn-based. Teams are notified by the game when their ships are ready to execute an order. Teams are also notified as ships are damaged or sunk. The game ends when only one team remains.</p>
<h2>Application Architecture</h2>
<p>Now that you have a feel for the game play, let’s talk about the code. One reason for building this game was to give myself a fun way to learn more about Windows Communication Foundation (WCF) and Windows Presentation Foundation (WPF), new features of the
 .NET Framework 3.0.</p>
<p>WCF provides a new unified mechanism for client-server communication that greatly simplifies that amount of work necessary to by standardizing the way you build your communication pieces. I recommend reading
<a href="http://www.code-magazine.com/Article.aspx?QuickID=0705041">this article</a> as a good place to start learning about WCF.</p>
<p>WPF enables richer user interfaces and better integration between developer and designer through the use of XAML. As you can see by my simple (though I think pretty cool) UI, I focused my interest on WCF, but did take advantage of a bit of what WPF has to
 offer.</p>
<h3>Overview</h3>
<p>The Battleship application is divided into three parts. The Battleship.Components project is a shared assembly that contains the classes which manage the communication and the logic of the game as well as class definitions for all of the ships (i.e. AircraftCarrier,
 Battleship, etc.). The Battleship.Gameboard uses WPF to display the action on the battlefield. Finally, console applications are built by each team wishing to compete.</p>
<p>The diagram below shows how the pieces fit together.</p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/8892343/ArchitectureDiagramOverview.jpg"><img height="200" alt="Architecture Diagram - Overview" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/8892343/ArchitectureDiagramOverview_thumb.jpg" width="244" border="0"></a>
</p>
<h3>WCF Communication</h3>
<p>As I mentioned, my focus in development was using WCF to manage the communication between the fleets and the game. I was very interested in exploring the bidirectional communication that can be achieved with WCF because it seemed like a perfect fit for the
 scenario. A traditional web service can receive messages from clients and send back responses. However, it can’t originate a message on its own to send to the client. For example, with a web service I could easily have a fleet tell the game about an order
 to execute (i.e. “I want my Battleship to attack 7,2”). However, I can’t have that web service inform another team that its ship was just sunk.</p>
<p>With WCF I was able to achieve this without writing a lot of custom code. There is some complexity in the code, but most of that is because my goal was to hide the communication layer from the building of a fleet so that players could simply focus on writing
 the code to implement their strategy. The following sections will walk through the flow of the communication.</p>
<h4>Server Side</h4>
<p>While the Battleship.GameBoard is Windows Forms App, it actually should be considered the server in this application. That’s because it hosts the WCF service that the Fleets call.</p>
<p>To define a WCF service, you start by defining the interface for the service. Similar to the WSDL of a web service, this is going to tell consumers what the contract, or inputs and outputs, of the service are. Callers will call the different operations exposed
 by the service.</p>
<p>In this application, the IBattleshipGameService interface seen below defines the contract for the service.</p>
<pre class="csharpcode">[ServiceContract(CallbackContract=<span class="kwrd">typeof</span>(IBattleshipGameServiceCallback))]
<span class="kwrd">public</span> <span class="kwrd">interface</span> IBattleshipGameService
{
    [OperationContract(IsOneWay = <span class="kwrd">true</span>)]
    <span class="kwrd">void</span> RegisterFleet(Fleet fleet);

    [OperationContract(IsOneWay = <span class="kwrd">true</span>)]
    <span class="kwrd">void</span> ExecuteOrder(<span class="kwrd">string</span> shipID, Order order);
}</pre>
<style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style>
<p>The interface shows that there are two methods for consumers to call, RegisterFleet and ExecuteOrder. The OperationContract attribute indicates that these are methods. The IsOneWay parameter of the attributes indicates that no response is sent back to callers.
 That probably seems straightforward for the RegsterFleet call, but you might expect that ExecuteOrder might return a result.</p>
<p>Actually what happens is that when a fleet is registered, the service holds on to a channel for calling back to the fleets. And the ExecuteOrder method only queues up an order for processing, but doesn’t immediately execute it. Before queuing it up, the
 game logic ensures that there aren’t already too many pending orders in the queue for the ship. This prevents a team from cheating by blasting the server with orders. We’ll delve more into how the communication back to the client is achieved later.</p>
<p>With the interface to the service defined, let’s turn our attention to a portion of the application configuration file.</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">configuration</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">system.serviceModel</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">services</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">service</span> <span class="attr">name</span><span class="kwrd">=&quot;Clarity.Battleship.Components.GameSide.BattleshipGameService&quot;</span> 
                    <span class="attr">behaviorConfiguration</span><span class="kwrd">=&quot;BattleshipGameServiceBehavior&quot;</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">endpoint</span> <span class="attr">address</span><span class="kwrd">=&quot;http://localhost:8088/BattleshipGameService&quot;</span>
                  <span class="attr">binding</span><span class="kwrd">=&quot;wsDualHttpBinding&quot;</span>
                  <span class="attr">contract</span><span class="kwrd">=&quot;Clarity.Battleship.Components.GameSide.IBattleshipGameService&quot;</span><span class="kwrd">/&gt;</span>
      <span class="kwrd">&lt;/</span><span class="html">service</span><span class="kwrd">&gt;</span></pre>
<p>This shows the definition of the service. The contract attribute indicates the service will adhere to the IBattleshipGameService interface. The binding attribute indicates that the WSDualHttpBinding will be used. This is the plumbing that allows both services
 and clients to send and receive messages.</p>
<p>With the interface and configuration in place, a concrete implementation has to be built. The BattleshipGameService class provides this. This class implements the IBattleshipGameService interface described above. The actual code for registering a fleet and
 executing an order are therefore defined here.</p>
<pre class="csharpcode">[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,UseSynchronizationContext = <span class="kwrd">false</span>)]
<span class="kwrd">public</span> <span class="kwrd">class</span> BattleshipGameService : IBattleshipGameService</pre>
<p>Note the ServiceBehavior attribute and its InstanceContextMode property that decorate the BattleshipGameService class. WCF uses this attribute to decide how to execute the operation when a call comes into the service. In this case, the attributes indicate
 that the service should run as a singleton. In other words, all calls will be executed by a single instance of the BattleshipGameService. You can further see this in the following code taken from the constructor of the Game class that shows how the service
 is actually brought to life.</p>
<pre class="csharpcode">Uri u = <span class="kwrd">new</span> Uri(<span class="str">&quot;http://localhost:8088/BattleshipService&quot;</span>);
_gameHost = <span class="kwrd">new</span> ServiceHost(<span class="kwrd">new</span> BattleshipGameService(), <span class="kwrd">new</span> Uri[] { u });
_gameHost.Open();</pre>
<p>The ServiceHost (which does what it sounds like and hosts the service) is created by passing in an instance of our service object and the uri for the address of the service (I know the uri seems redundant since it’s in the config, but as far as I saw I had
 to use this constructor). Since we know the service host will just use the single instance of the BattleShipGameService, we can track that instance through a private property for convenience and hook its events as seen in the code snippets below.</p>
<pre class="csharpcode"><span class="kwrd">private</span> BattleshipGameService BattleshipGameService
{
    get
    {
        <span class="kwrd">return</span> (BattleshipGameService)_gameHost.SingletonInstance;
    }
}

<span class="kwrd">this</span>.BattleshipGameService.FleetRegistered &#43;=
   <span class="kwrd">new</span> FleetRegisteredEventHandler(BattleshipGameService_FleetRegistered);
<span class="kwrd">this</span>.BattleshipGameService.OrderExecuted &#43;= 
   <span class="kwrd">new</span> OrderExecutedEventHandler(BattleshipGameService_OrderExecuted);</pre>
<style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style><style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style><style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style>
<p>This allows us to elegantly pass off the calls to the Game class. The service then acts as a simple pass through and leaves the Game class to manage the game.</p>
<h4>Client Side</h4>
<p>Now that we’ve covered the basics of the server side, let’s look at what happens on the client side. In each console app, a class is created that derives from the FleetCommander class (there are complete directions on how to do this with the downloadable
 source). The FleetCommander class exposes all of the ships of the Fleet for the team to program against.</p>
<p>To avoid cheating, (some would call it creative programming) each ship gets a unique identifier set by the Game at startup. That way the Game knows which orders are legitimate. To hide that id and details from the FleetCommander and make a very simple and
 intuitive interface, the method for executing an order is exposed off of the ship classes. The code snippet below shows how FleetCommander could react to an event indicating that a ship is ready for orders. In this case it is saying that the ship should attack
 the coordinate at location _targetX, _targetY.</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnShipAwaitingOrders(Ship ship, Order lastOrder)
{
      Order orderToExecute = <span class="kwrd">new</span> Order();
      orderToExecute.Coordinate = <span class="kwrd">new</span> Coordinate(_targetX, _targetY);
      orderToExecute.Type = OrderType.Attack;
      ship.ExecuteOrder(orderToExecute);</pre>
<p>Under the covers, the Ship.ExecuteOrder method raises an event which is handled by the base FleetCommander class. The FleetCommander calls the appropriate method on a WCF client class which makes the call to the BattleshipGameService.</p>
<p>A portion of the definition of the WCF client can be seen below. You’ll see that it is a class that derives from the DuplexClientBase. This class manages the channels that pass messages back and forth. You can think of it as similar to a web service proxy
 class.</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">partial</span> <span class="kwrd">class</span> BattleshipGameServiceClient : 
    System.ServiceModel.DuplexClientBase&lt;IBattleshipGameService&gt;, IBattleshipGameService
{</pre>
<p>So that covers how messages are sent to the server, but you may be asking yourself “How did a FleetCommander find out it that a ship was awaiting orders?” or for that matter “How does it get notified about anything?”</p>
<p>Well, each FleetCommander has an instance of a listener class which receives callbacks from the game service. The way this is set up for the listener is very similarly to how we set up the service. Once again we start with an interface, in this case the
 IBattleshipGameServiceCallback interface.</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IBattleshipGameServiceCallback
{
    [OperationContract(IsOneWay = <span class="kwrd">true</span>)]
    <span class="kwrd">void</span> OnFleetUpdated(Fleet fleet, FleetUpdateReason reason);

    [OperationContract(IsOneWay = <span class="kwrd">true</span>)]
    <span class="kwrd">void</span> OnGameStateChanged(GameInfo gameInfo);

    [OperationContract(IsOneWay=<span class="kwrd">true</span>)]
    <span class="kwrd">void</span> OnOrderExecuted(<span class="kwrd">string</span> shipID, Order lastOrder);

    [OperationContract(IsOneWay = <span class="kwrd">true</span>)]
    <span class="kwrd">void</span> OnMessageReceived(<span class="kwrd">string</span> data, Coordinate c);

    [OperationContract(IsOneWay = <span class="kwrd">true</span>)]
    <span class="kwrd">void</span> OnFleetSunk(<span class="kwrd">string</span> fleetName);
}</pre>
<p>Just like before the OperationContract attributes are used to indicate the methods available. And just like before we need a concrete implementation of the interface. Enter the FleetCommanderListener.</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> FleetCommanderListener : IBattleshipGameServiceCallback</pre>
<p>The FleetCommanderListener is used when creating the BattleshipGameServiceClient. As you can see in the constructor definition below, a callbackInstance parameter must be passed in. The BattleshipGameServiceClient can then handle communication to the server
 via the IBattleshipGameService interface and also listen for communication back that adheres to the IBattleshipGameServiceCallback interface!
</p>
<pre class="csharpcode"><span class="kwrd">public</span> BattleshipGameServiceClient(System.ServiceModel.InstanceContext callbackInstance) :
     <span class="kwrd">base</span>(callbackInstance)</pre>
<p>The last little piece of the puzzle is the client configuration file.</p>
<pre class="csharpcode">  <span class="kwrd">&lt;</span><span class="html">system.serviceModel</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">client</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">endpoint</span> <span class="attr">name</span><span class="kwrd">=&quot;Clarity.Battleship.Components.IBattleshipGameService&quot;</span>
                <span class="attr">address</span><span class="kwrd">=&quot;http://localhost:8088/BattleshipGameService&quot;</span>
                <span class="attr">binding</span><span class="kwrd">=&quot;wsDualHttpBinding&quot;</span>
                <span class="attr">bindingConfiguration</span><span class="kwrd">=&quot;BattleshipBinding&quot;</span>
                <span class="attr">contract</span><span class="kwrd">=&quot;Clarity.Battleship.Components.GameSide.IBattleshipGameService&quot;</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">client</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">bindings</span><span class="kwrd">&gt;</span>
      <span class="rem">&lt;!-- configure a binding that support duplex communication --&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">wsDualHttpBinding</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">binding</span> <span class="attr">name</span><span class="kwrd">=&quot;BattleshipBinding&quot;</span>
                 <span class="attr">clientBaseAddress</span><span class="kwrd">=&quot;http://localhost:6088/BattleshipGameClient/&quot;</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">binding</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;/</span><span class="html">wsDualHttpBinding</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">bindings</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">system.serviceModel</span><span class="kwrd">&gt;</span></pre>
<p>As you can see from the example above, the configuration is fairly straightforward. Just like in the server config, we say we’re using the wsDualHttpBinding. The one additional piece of info is the clientBaseAddress which tell the server how to call back
 into the client.</p>
<h4>The Complete Picture</h4>
<p>So after reading all of this, you might be thinking “I thought he said there wasn’t much custom code needed.” I think that is still true in terms of the WCF. While certainly not trivial, it really boils down to building a couple interfaces, their concrete
 implementations, and filling out some config files. Most of the complexity came in was in building out the eventing model to simplify the interface for teams.</p>
<p>The following diagram helps pull everything together by showing an example of the flow of the communication for execution of an order.</p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/8892343/ArchitectureDiagramFlow.jpg"><img height="135" alt="Architecture Diagram - Flow" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/8892343/ArchitectureDiagramFlow_thumb.jpg" width="244" border="0"></a>
</p>
<p>1. The derived FleetCommander makes calls the ExecuteOrder method on a Ship (i.e. “I want my Battleship to attack 7,2”)</p>
<p>2. The Ship raises an event which is handled by the FleetCommander base class which in turn calls to the BattleshipGameServiceClient</p>
<p>3. This WCF client class makes the WCF call to execute the order</p>
<p>4. The single instance of the service raises an event to the Game class with the order information
</p>
<p>5. The Game class which controls all the game flow enqueues the order</p>
<p>6. After processing an order from the queue, the Game calls the OnOrderExecuted WCF callback which gets invoked on the FleetCommanderListener</p>
<p>7. Finally the FleetCommanderListener raises an OrderExecuted event to the derived FleetCommander class (i.e. “Your Battleship’s attack at 7,2 resulted in a Hit”) which is turned calls the OnShipAwaitingOrders method and the cycle starts over</p>
<h3>WPF User Interface</h3>
<p>The last thing to cover with the architecture is the UI. It is pretty simple, but I wanted to experiment a little with WPF. The Gameboard assembly contains a class called Battlefield that is a WPF Window (comparable to a Winform Form). A Grid control makes
 up the main component of the gameboard. The grid lent itself to building the UI since everything stretches nicely to fit the screen, regardless of the number of teams competing.</p>
<p>Once the game starts, the game board is setup by adding rectangles and images to the grid control. Throughout the game, the colors of the rectangles are changed to indicate shots, recon, or hit targets.</p>
<p>Below is an example of some of the Battlefield code behind in which the rectangle for the location of the order is painted a different color depending on the result of an order.</p>
<pre class="csharpcode">Rectangle r = _rectangles[_lastOrderStarted.Order.Coordinate];
<span class="kwrd">if</span> (e.Order.Result == OrderResult.ShipHit)
{
    r.Fill = Brushes.Red;
    _rectangleFills[e.Order.Coordinate] = r.Fill;
}
<span class="kwrd">else</span> <span class="kwrd">if</span> (e.Order.Result == OrderResult.ShipSunk)
{
    r.Fill = Brushes.Red;
    _rectangleFills[e.Order.Coordinate] = r.Fill;
}
<span class="kwrd">else</span> <span class="kwrd">if</span> (e.Order.Result == OrderResult.Miss)
{
    r.Fill = _rectangleFills[e.Order.Coordinate];
}
<span class="kwrd">else</span>
{
    r.Fill = _rectangleFills[e.Order.Coordinate];
}</pre>
<h2>Conclusion</h2>
<p>Hopefully you enjoyed this game and learned a little in process. I encourage you to download the code and build your own fleet. The download includes a document called “Clarity Battleship – Building a FleetCommander” which gives you step by step instructions.
 Feel free to post your own builds here for other people to challenge and good luck with your battles!</p>
<p>Bryan Dougherty is a Principal with Clarity Consulting, a software development consulting firm based in Chicago, IL. He is an MCP with several years of professional software engineering experience. Bryan earned a B.S. in Computer Science from Northwestern
 University and writes about topics related to software development on his <a href="http://blogs.claritycon.com/blogs/bryan_dougherty">
blog</a>.</p>
<style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style><style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style><style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style><style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style><style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style><style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style><style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style> <img src="http://m.webtrends.com/dcs1wotjh10000w0irc493s0e_6x1g/njs.gif?dcssip=channel9.msdn.com&dcsuri=http://channel9.msdn.com/Niners/c4f.Bryan-Dougherty/Posts/RSS&WT.dl=0&WT.entryid=Entry:RSSView:90259fc35bd54167afba9e7600ce8e02">]]></description>
      <comments>http://channel9.msdn.com/coding4fun/articles/Your-Code-Sunk-My-Battleship</comments>
      <itunes:summary>
 




Clarity Battleship is a multi-player game in which players code the &amp;quot;artificial intelligence&amp;quot; needed to command their fleet to victory on the high seas. The game is built using WPF and WCF.



Bryan Dougherty
Bryan&#39;s Blog

Difficulty: Intermediate
Time Required: 
1-3 hours to create your own fleet
Cost: Free
Software: Visual Basic or Visual C# Express Editions
Hardware: 
Download: Download








Clarity Battleship
At Clarity Consulting we’re always looking for a little friendly competition. That’s why a few times a year we hold coding competitions that we call Tech Challenges. I recently organized one based on the classic board game Battleship. 
The flow of the game is based loosely on the board game. The simple goal is to sink all of your opponents’ ships. Since we’re writing code though, I decided to step up the game play and make it a more interesting, requiring more strategy than luck.&amp;nbsp; Just
 like the board game, the battlefield is a grid. Unlike the classic, however, multiple teams can battle at the same time. Each team’s fleet is assigned to a region of the Battlefield.
 
In our tournament we had six teams battle at a time and held one “Battle Royale” with eleven teams.&amp;nbsp; Below you can see a sample screen shot of a game with three teams. Blocks in red indicate damage. The bottom of the screen displays messages broadcast or
 information about fleets that have been sunk.&amp;nbsp;  

 
Teams position ships at the beginning of the game and give ships orders to execute including: 

Attacking locations on the battlefield Performing reconnaissance on an area of the battlefield Broadcasting messages to opponents or conspirators via an “open radio frequency” (collusion and deception are allowed and encouraged!)

Game play is pseudo turn-based. Teams are notified by the game when their ships are ready to execute an order. Teams are also notified as ships are damaged or sunk. The game ends when only one team remains. 
Application Architecture
Now</itunes:summary>
      <link>http://channel9.msdn.com/coding4fun/articles/Your-Code-Sunk-My-Battleship</link>
      <pubDate>Sun, 24 Aug 2008 15:00:14 GMT</pubDate>
      <guid isPermaLink="false">http://channel9.msdn.com/coding4fun/articles/Your-Code-Sunk-My-Battleship</guid>
      <media:thumbnail url="http://ecn.channel9.msdn.com/o9/c4f/images/8892343_100.jpg" height="75" width="100"></media:thumbnail>
      <media:thumbnail url="http://ecn.channel9.msdn.com/o9/c4f/images/8892343_220.jpg" height="165" width="220"></media:thumbnail>      
      <dc:creator>Bryan Dougherty</dc:creator>
      <itunes:author>Bryan Dougherty</itunes:author>
      <slash:comments>3</slash:comments>
      <wfw:commentRss>http://channel9.msdn.com/coding4fun/articles/Your-Code-Sunk-My-Battleship/RSS</wfw:commentRss>
      <category>Gaming</category>
      <category>card and board games</category>
    </item>
  <item>
      <title>Pac-Man for the Smartphone</title>
      <description><![CDATA[<span id="c4fmetadata">
<table cellspacing="0" cellpadding="1" width="100%" border="0">
<tbody>
<tr class="entry_overview">
<td width="50">&nbsp;</td>
<td><span class="entry_description">The article expains writing a game, Pac-Man for Windows Mobile 5.0 Smartphone platform</span></td>
</tr>
<tr>
<td colspan="2">
<div class="entry_author">Bryan Dougherty</div>
<div class="entry_company"><a href="http://blogs.claritycon.com/blogs/bryan_dougherty">Bryan Dougherty's Blog</a></div>
<br>
<div class="entry_details"><b>Difficulty: </b><span class="entry_details_input">Intermediate</span></div>
<div class="entry_details"><b>Time Required:</b> <span class="entry_details_input">
3-6 hours</span></div>
<div class="entry_details"><b>Cost: </b><span class="entry_details_input">$50-$100</span></div>
<div class="entry_details"><b>Software: </b><span class="entry_details_input"><a href="http://msdn.com/express/">Visual Basic or Visual C# Express Editions</a></span></div>
<div class="entry_details"><b>Hardware: </b><span class="entry_details_input"><a href="http://direct.motorola.com/ENS/Web_ProductHome.asp?country=USA&amp;language=ENS&amp;productid=29303">Motorola MPX220</a></span></div>
<div class="entry_details"><b>Download: </b><a href="http://employees.claritycon.com/bdougherty/blog/code/MobilePacMan/build/">Download</a>
<ul>
</ul>
</div>
</td>
</tr>
</tbody>
</table>
</span>
<h2>Introduction</h2>
<p>I've always loved mobile technology and I wanted to try my hand at mobile development. So a little ways back I bought a Smartphone, the
<a href="http://direct.motorola.com/ENS/Web_ProductHome.asp?country=USA&amp;language=ENS&amp;productid=29303">
Motorola MPX220</a>, which runs the Windows Mobile 2003 OS. I hope to upgrade to a new Smartphone soon, so this article will discuss building an application targeting the Windows Mobile 5.0 Smartphone platform.
</p>
<p>When deciding what to do, I thought of a variety of useful applications I could build. Then I decided that making a game would be more fun. I settled on a classic: Pac-Man.</p>
<p><img alt="" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/1044050/pacman_1.png" border="0"></p>
<p><b>Figure 1: Pac-Man running on Windows Mobile 5.0 Smartphone Emulator</b></p>
<h2>Getting Started</h2>
<p>Before diving into code, I thought I'd mention some of the pieces involved in developing for the Smartphone.</p>
<h3>Development Environment</h3>
<p>In order to create applications for the Windows Mobile 5.0 Smartphone platform, the following applications need to be installed:
</p>
<ul>
<li>Visual Studio .NET 2005 Professional Edition<br>
<br>
</li><li>Windows Mobile 5.0 Smartphone SDK<br>
<br>
</li><li>ActiveSync Version 4.1.0 </li></ul>
<h3>New Project</h3>
<p>Once the development environment is set up, the new Mobile 5.0 Smartphone templates appear under the New Project dialog in Visual Studio.
</p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/1044050/pacman_2.png" target="_Top"><img alt="Click here for larger image" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/1044050/pacman_2_thumb.png" border="0"></a></p>
<p><b>Figure 2: New Project dialog in Visual Studio (click image to zoom)</b></p>
<p>Note that you choose which version of the Compact Framework you are targeting when you select the project template. Those targeting CF1.0 have 1.0 after the name, while the others target the 2.0 Framework.
</p>
<p>Development, in general, feels just like development of any WinForm application. You do need to keep in mind, however, that some functionality supported by the standard .NET Framework is not supported by the Compact Framework. For more on support for classes
 you can look <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_evtuv/html/etconnetcompactframeworkclasses.asp">
in the MSDN Library</a>.</p>
<h3>The Emulator</h3>
<p>While developing, you don't have to deploy your application to an actual phone when you want to test. Instead, a built-in emulator, as seen in the Figure 1, is available. With the click of a button, you can deploy to the emulator and test your application
 with full debugging capability, just like you would a WinForm application. The behavior and speed of the emulator seemed to provide a reasonable indication of how the application would perform on an actual phone.</p>
<h2>Development</h2>
<p>Instead of rushing in and trying to crank out the code for Pac-Man, I took a more structured approach. With a few proof of concepts under my belt, I decided to build a framework that would provide the common building blocks needed to create 2-D games for
 a Smartphone. I tried to focus on the following functionality as goals: </p>
<ul>
<li>A game board that controls efficient painting and game flow<br>
<br>
</li><li>A basis for defining game characters supported by the game board<br>
<br>
</li><li>Built-in support for common interactions between characters </li></ul>
<p>A basic litmus test for success would be whether someone (like you) could use this framework to build a classic 2-D video game, like Frogger, by only writing the new code managing interactions between the characters specific to the game.</p>
<p>The remainder of the article will highlight a few of the key concepts I built into the framework.
</p>
<h3>Sprites</h3>
<p>The Sprite class provides the basis for all characters in the game. In Pac-Man, this includes ghosts, dots, walls, and Pac-Man himself. The class is abstract, forcing all derived classes to implement&nbsp;two methods:</p>
<pre><code>public abstract void Update();
public abstract void Draw(Graphics gameSurface);</code></pre>
<p>Not surprisingly, they expect the derived class to update their state and draw themselves to the game board, respectively. Methods are provided for getting the instance of the GameBoard the Sprite has been added to, and detecting collisions between one Sprite
 and a list of other Sprites. In addition,&nbsp;a Sprite instance is expected to manage several state properties:
</p>
<ul>
<li>ID—must be unique per GameBoard <br>
<br>
</li><li>Bounds—a rectangle that defines the perimeter of the object. A future version could use a Region or other construct to more accurately track the area taken up by the Sprite. This property is used for collision detection and drawing.
<br>
<br>
</li><li>Invalid—a boolean that tells the GameBoard whether a Sprite needs to be redrawn
<br>
<br>
</li><li>Visible—a boolean that determines whether a Sprite should be drawn or not </li></ul>
<pre></pre>
<h3>GameBoard</h3>
<p>The GameBoard class controls the flow of the game. It derives from System.Windows.Forms.Form, and like the Sprite class, it is not creatable. This inheritance structure allows the developer to handle any Form events needed&nbsp;for his game. Below is an example
 in which the Pac-Man application's GameBoard class listens to user input by overriding the OnKeyDown method.</p>
<pre><code>protected override void OnKeyDown(KeyEventArgs e)
{
    _KeyPressed = true;
    base.OnKeyDown (e);
    if(e.KeyCode == Keys.Right || e.KeyCode == Keys.Left || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
    {
        switch(e.KeyCode)
        {
            case Keys.Right:
            {
                _Pacman.MovementDirection = MovementDirections.Right;
                break;
            }
            case Keys.Left:
            {
                _Pacman.MovementDirection = MovementDirections.Left;
                break;
            }
            case Keys.Down:
            {
                _Pacman.MovementDirection = MovementDirections.Down;
                break;
            }
            case Keys.Up:
            {
                _Pacman.MovementDirection = MovementDirections.Up;
                break;
            }
        }
    }
}
</code></pre>
<p>Each game application needs to create its own game board class that derives from GameBoard. All character Sprites should then be created and added to the GameBoard's list of Sprites. Then, after the GameBoard loads, a Timer is used to call the Update and
 Draw methods on Sprites as mentioned above.</p>
<h3>Painting</h3>
<p>So how exactly does the drawing of the Sprites work? From a Sprite's perspective, it's simple. All a Sprite needs to know is how to draw itself. A Dot Sprite for example, simply draws a rectangle to the game board graphics object.</p>
<pre><code>public override void Draw(Graphics gameBoard)
{
    if(!_IsEaten)
    {
        gameBoard.FillRectangle(new SolidBrush(Color.LightBlue), _Bounds);
    }
}

</code></pre>
<p>The Draw methods for Pac-Man and Ghost Sprites are more complicated and draw one of a series of bitmap images to the game board. The changing images, along with updated Bounds create animation. When drawn, a color is chosen (magenta in the Figure 3 below)
 to indicate portions of the image that should be treated as transparent.</p>
<p><img alt="" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/1044050/pacman_3.png" border="0"></p>
<p><b>Figure 3: Ghost Sprite image enlarged</b></p>
<p>The GameBoard class makes the screen painting as efficient as possible. It overrides the Form OnPaint and OnPaintBackground methods since it controls all the updating of the screen. The OnPaint method makes sure that only the portions of the screen that
 have changed get repainted. In addition, it uses a back buffer to minimize any flicker.</p>
<pre><code>protected override void OnPaint(PaintEventArgs e)
{
    if(_BackBuffer == null) 
    { 
        return;
    }

    Graphics g = Graphics.FromImage(_BackBuffer);

    ArrayList changedSprites = new ArrayList();
    // blank out any invalid Sprites previous position in the back buffer
    foreach(Sprite sprite in _SpritesByZIndex)
    {
        if(sprite.Invalid)
        {
            g.FillRectangle(new SolidBrush(_BackgroundColor), sprite.PreviousBounds);
            changedSprites.Add(sprite);
        }
    }

    // now draw them to the back buffer
    foreach(Sprite sprite in changedSprites)
    {
        if(sprite.Visible)
        {
            sprite.Draw(g);
        }
    }

    if(_FullRepaint)
    {
        // draw the entire back buffer 
        e.Graphics.DrawImage(_BackBuffer, 0, 0);
        _FullRepaint = false;

        foreach(Sprite sprite in _SpritesByZIndex)
        {
            // reset Invalid flag
            sprite.Invalid = false;
        }
    }
    else
    {
        // only draw the parts that have changed
        foreach(Sprite sprite in changedSprites)
        {
            e.Graphics.DrawImage(_BackBuffer, sprite.Bounds, sprite.Bounds, GraphicsUnit.Pixel);
            e.Graphics.DrawImage(_BackBuffer, sprite.PreviousBounds, sprite.PreviousBounds, GraphicsUnit.Pixel);

            // reset Invalid flag
            sprite.Invalid = false;
        }
    }
}
</code></pre>
<h3>Mobile Development Considerations</h3>
<p>As I've said, from a coding perspective, developing mobile applications in .NET is very similar to building standard WinForm apps. However, this little project did bring to light a number of things you have to keep in mind with mobile applications, most
 centering around the word &quot;small&quot;. </p>
<p>The most obvious consideration is that the device has a small screen. That means you have to design a user interface that is easy to use within the minimal real estate. That tends to translate into keeping things simple. One other screen consideration is
 that from phone to phone, screen size and resolution may change. I didn't address that with my first cut of Pac-Man, but it's on the list. In my testing I targeted a lower resolution that would work on my phone.</p>
<p>With any type of application, but especially with a game, you have to think about how a user will interact with the small keys of his phone, possibly using no more that a thumb! I consciously made controlling Pac-Man a little different than the typical arcade
 feel to make up for some of the clumsiness of using the phone's directional pad. That change has received mixed reviews from the people who have tried it out. That's what testing is for, though, right?</p>
<p>The other &quot;small&quot; consideration is the phone's horsepower. While a Smartphone blows away my old Tandy 64K Color Computer, the computer I started playing games on, I definitely found myself needing to tweak the code for performance. I was sure ahead of time
 that highly efficient painting logic would be necessary. I was surprised, however, to find that the code that prepares the possible travel paths for ghosts takes a considerable amount of time to crunch. For that reason, I made sure that this logic is only
 processed the first time the splash screen is displayed. It is then cached for later reuse.</p>
<h2>Conclusion</h2>
<p>So there it is...well, the first version, at least. I plan to periodically update the game making it more efficient and more fun—smarter ghosts for starters. Maybe I'll even eat my own dog food and try building Frogger to see if that little framework I put
 together holds water. I really want to add a feature to report high scores to a web site. What's a game with out a little healthy competition, right?
</p>
<p>I recently started a <a href="http://blogs.claritycon.com/blogs/bryan_dougherty/default.aspx">
blog</a> about this and other topics related to software development on <a href="http://blogs.claritycon.com/">
my company's blog site</a>. The <a href="http://employees.claritycon.com/bdougherty/blog/code/MobilePacMan/source/">
code</a> and <a href="http://employees.claritycon.com/bdougherty/blog/code/MobilePacMan/build/">
compiled game</a> for the all platforms will be available there, too. Feel free to check for updates and post any feedback you may have.</p>
 <img src="http://m.webtrends.com/dcs1wotjh10000w0irc493s0e_6x1g/njs.gif?dcssip=channel9.msdn.com&dcsuri=http://channel9.msdn.com/Niners/c4f.Bryan-Dougherty/Posts/RSS&WT.dl=0&WT.entryid=Entry:RSSView:47256638bf7c40ebb1d19e7600d8d714">]]></description>
      <comments>http://channel9.msdn.com/coding4fun/articles/Pac-Man-for-the-Smartphone</comments>
      <itunes:summary>



&amp;nbsp;
The article expains writing a game, Pac-Man for Windows Mobile 5.0 Smartphone platform



Bryan Dougherty
Bryan Dougherty&#39;s Blog

Difficulty: Intermediate
Time Required: 
3-6 hours
Cost: $50-$100
Software: Visual Basic or Visual C# Express Editions
Hardware: Motorola MPX220
Download: Download








Introduction
I&#39;ve always loved mobile technology and I wanted to try my hand at mobile development. So a little ways back I bought a Smartphone, the

Motorola MPX220, which runs the Windows Mobile 2003 OS. I hope to upgrade to a new Smartphone soon, so this article will discuss building an application targeting the Windows Mobile 5.0 Smartphone platform.
 
When deciding what to do, I thought of a variety of useful applications I could build. Then I decided that making a game would be more fun. I settled on a classic: Pac-Man. 
 
Figure 1: Pac-Man running on Windows Mobile 5.0 Smartphone Emulator 
Getting Started
Before diving into code, I thought I&#39;d mention some of the pieces involved in developing for the Smartphone. 
Development Environment
In order to create applications for the Windows Mobile 5.0 Smartphone platform, the following applications need to be installed:
 

Visual Studio .NET 2005 Professional Edition

Windows Mobile 5.0 Smartphone SDK

ActiveSync Version 4.1.0 
New Project
Once the development environment is set up, the new Mobile 5.0 Smartphone templates appear under the New Project dialog in Visual Studio.
 
 
Figure 2: New Project dialog in Visual Studio (click image to zoom) 
Note that you choose which version of the Compact Framework you are targeting when you select the project template. Those targeting CF1.0 have 1.0 after the name, while the others target the 2.0 Framework.
 
Development, in general, feels just like development of any WinForm application. You do need to keep in mind, however, that some functionality supported by the standard .NET Framework is not supported by the Compact Framework. For more on support for classes
 you</itunes:summary>
      <link>http://channel9.msdn.com/coding4fun/articles/Pac-Man-for-the-Smartphone</link>
      <pubDate>Thu, 09 Nov 2006 11:06:04 GMT</pubDate>
      <guid isPermaLink="false">http://channel9.msdn.com/coding4fun/articles/Pac-Man-for-the-Smartphone</guid>      
      <dc:creator>Bryan Dougherty</dc:creator>
      <itunes:author>Bryan Dougherty</itunes:author>
      <slash:comments>8</slash:comments>
      <wfw:commentRss>http://channel9.msdn.com/coding4fun/articles/Pac-Man-for-the-Smartphone/RSS</wfw:commentRss>
      <category>Gaming</category>
      <category>arcade</category>
    </item>    
</channel>
</rss>