<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Joacim's view on stuff : c#, OOD</title><link>http://msmvps.com/blogs/joacim/archive/tags/c_2300_/OOD/default.aspx</link><description>Tags: c#, OOD</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>When OOP stinks and when you instead should leave a smell behind…</title><link>http://msmvps.com/blogs/joacim/archive/2010/08/06/when-oop-stinks-and-when-you-instead-should-leave-a-smell-behind.aspx</link><pubDate>Fri, 06 Aug 2010 14:45:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1775480</guid><dc:creator>Joacim Andersson</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/joacim/rsscomments.aspx?PostID=1775480</wfw:commentRss><comments>http://msmvps.com/blogs/joacim/archive/2010/08/06/when-oop-stinks-and-when-you-instead-should-leave-a-smell-behind.aspx#comments</comments><description>&lt;p&gt;I just saw an interesting video blog post by the CTO of &lt;a target="_blank" href="http://www.devexpress.com/"&gt;Devexpress&lt;/a&gt; entitled &lt;a target="_blank" href="http://tv.devexpress.com/#PacManCode.movie"&gt;Pac-Man and Object-Oriented Programming&lt;/a&gt;, in which Julian talks about how we&amp;rsquo;ve been doing object-oriented programming for 20 years now (or even more than that if you come from the SmallTalk world) and how we, as OOP/OOD programmers and designers think about programming. He then quickly moves on to comparing that to a game of Pac-Man.&lt;/p&gt;
&lt;p&gt;Now Pac-Man was a great video game developed by Namco in Japan back in 1979 and it was released in Europe and the US the year after. The game was an instant success, maybe because it was different from most of the other video games of the time, such as Space Invaders, Galaxy, and Asteroids, which was mainly shoot &amp;lsquo;em up games in which you should kill and/or destroy everything you saw (much unlike video games of today where we have 3D FPS games in which you walk around and kill/destroy everything you see&amp;hellip; Oh, wait there isn&amp;rsquo;t that much of a difference, is there?). In Pac-Man you play this pie-chart looking character that walks around in a maze eating dots at the same time as you&amp;rsquo;re trying to avoid getting caught by one of the four colorful ghosts that are, for some reason, hunting you.&lt;/p&gt;
&lt;p&gt;Whoops&amp;hellip; Sorry! I got a bit nostalgic there for a moment and sort of strayed away from the topic. This blog post was supposed to be about object oriented programming and design and not about gaming. So let&amp;rsquo;s get back on track, shall we?&lt;/p&gt;
&lt;p&gt;If you, as an OOP developer, would design the Pac-Man game today, how would you go about it? You should probably create a class for Pac-Man and another for the ghosts that hunts him. Well, they might actually be subclasses of a common &lt;em&gt;character&lt;/em&gt; class since they do share some very basic logic such as they can&amp;rsquo;t walk through walls and other basic elements that you can think of. But they would still be 2 different classes. You might also have a Maze class that draws the actual maze and have some other logic like knowing which level you&amp;rsquo;re playing and such. The dots and power ups might also be different classes.&lt;/p&gt;
&lt;p&gt;But the logic of the ghosts aren&amp;rsquo;t that easy. How will they go about to chase Pac-Man? They obviously need to know about his position but how do you calculate the closest path to get to him? The ghosts can&amp;rsquo;t go through a wall so you must calculate the nearest path around the maze. Doing that in every game cycle on a modern day computer is not such a big deal, but Pac-Man came out back in 1979 and the CPUs used at the time couldn&amp;rsquo;t do that and still produce a fast pacing game.&lt;/p&gt;
&lt;p&gt;The solution Julian suggested was that maybe the ghosts in the game are like blood-hounds and Pac-Man is given away a scent as he moves around in the maze. The square in the maze Pac-Man just left gets a value that as Pac-Man moves on decreases in value until the scent has evaporated. This way the ghosts has no real knowledge about Pac-Man at all since they are just following a scent. The scent itself is now a property of the maze object and not of the ghost object. Julian called this anti-object oriented programming since the logic is not in the &amp;ldquo;real-world&amp;rdquo; object of the ghost (is that really a real-world object?). So by changing the logic of the ghost not to actually hunt Pac-Man but to follow a scent when they find it and also added some logic to the maze class, the anti-object, which you probably only used for drawing before. &lt;/p&gt;
&lt;p&gt;I love this example since it&amp;rsquo;s an example of thinking-out-of-the-box when it comes to OOP and OOD. Instead of always trying very hard to make the classes as close to real-world objects as possible make a few changes and simplify the whole idea.&lt;/p&gt;
&lt;p&gt;To be completely fair here, this is actually not how the AI of the ghosts in the original game was designed. If you&amp;rsquo;re interested I invite you to read &lt;a target="_blank" href="http://home.comcast.net/~jpittman2/pacman/pacmandossier.html"&gt;the Pac-Man Dossier&lt;/a&gt; especially chapter 3 and chapter 4 that explains how the actual AI for the ghosts was created. But the fact that the actual design was different is beside the point. Julian showed a simple solution to a hard problem you might face when you&amp;rsquo;re doing object oriented design.&lt;/p&gt;
&lt;p&gt;Have fun!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1775480" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/joacim/archive/tags/vb/default.aspx">vb</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/visual+basic/default.aspx">visual basic</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/.net/default.aspx">.net</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/c_2300_/default.aspx">c#</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/OOD/default.aspx">OOD</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/OOP/default.aspx">OOP</category></item></channel></rss>