    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: My Pet Project Monopoly 2</title>
        <link>https://members.accu.org/index.php/articles/507</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Design of applications and programs + Overload Journal #35 - Jan 2000</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c67/">Design</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c169/">35</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c67-169/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c67+169/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;My Pet Project Monopoly 2</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 January 2000 16:50:56 +00:00 or Wed, 26 January 2000 16:50:56 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>This is the second, of a series of three articles. In the
previous article I gave a rundown of stages that a 'pet project'
(Monopoly&reg;) has been through over the many years I've been
learning C, C++, Windows programming, OOP etc.</p>
<p>This time we take a closer look at one of the completed C++
programs, what classes were involved, how they worked and some the
consequences behind the design.</p>
<p>UML diagrams have been used to show the hierarchies and
interaction of the classes. These have been created for this
article and UML itself wasn't used during the design process.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e26" id=
"d0e26"></a>Introduction</h2>
</div>
<p>Let us start with a couple of caveats. Firstly, I don't intend
to describe each and every class here, but restrict things to some
of the classes which are of interest to the article. Secondly, this
isn't intended to be a perfect design by any means.</p>
<p>Because of the nature of the project, i.e. just for myself, I
just dived-in and 'hacked' things (something I don't recommend and
certainly don't practice at work!). But this approach raises issues
quickly and, one hopes that because they are raised through the
process of &quot;doing&quot;, the consequences are better understood.</p>
<p>I've subsequently found out many of the points I came up against
have been discussed in various OO design and Pattern books - which
at the time of coding were not known to me. Everyday practitioners
of C++ will look at these, probably recognise them, sigh deeply
(and know solutions instantly)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e35" id="d0e35"></a>Basic
Classes</h2>
</div>
<p>The main text of this article flows around the following
classes:</p>
<div class="variablelist">
<dl>
<dt><span class="term">Board Class:</span></dt>
<dd>
<p>This (misnamed) class managed the game. It provided methods for
moving players around the board, picking cards, quitting players
from the game and other miscellaneous tasks.</p>
</dd>
<dt><span class="term">Player Class:</span></dt>
<dd>
<p>Each instantiation of this class represented each of the players
taking part in the game. (Also see implementation note 1)</p>
</dd>
<dt><span class="term">Site Class:</span></dt>
<dd>
<p>This formed the top class in a hierarchy of different types of
site one finds while touring the Monopoly board. (Also see
implementation note 2). As implemented the full class hierarchy is
shown in figure 1.</p>
</dd>
<dt><span class="term">Card Class:</span></dt>
<dd>
<p>This formed the top class in a hierarchy of different types of
Chance and Community Chest cards. The full class hierarchy is shown
in figure 2.</p>
</dd>
</dl>
</div>
<div class="figure"><a name="d0e65" id="d0e65"></a>
<div class="mediaobject c2"><img src=
"/var/uploads/journals/resources/Site%20class%20hierarchy.png" align="middle" alt=
"Site class hierarchy"></div>
<p class="title c3">Figure 1. Site class hierarchy</p>
</div>
<div class="figure"><a name="d0e71" id="d0e71"></a>
<div class="mediaobject c2"><img src=
"/var/uploads/journals/resources/Card%20class%20hierarchy.png" align="middle" alt=
"Card class hierarchy"></div>
<p class="title c3">Figure 2. Card class hierarchy</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e77" id=
"d0e77"></a>Implementation</h2>
</div>
<p>The board (an array of <tt class="classname">Site*</tt>), the
participants (an array of <tt class="classname">Player*</tt>) and
the chance/community chest cards (arrays of <tt class=
"classname">Card*</tt>) were all global variables (see
implementation note 3).</p>
<p>One of the <tt class="classname">Site</tt>'s virtual methods was
called <tt class="methodname">Action</tt> (see implementation note
4). This is called when a player lands on a site in order to
perform a site-dependent action, i.e. collect tax, collect rent,
pick a chance or community chest card etc.</p>
<p>Similarly one of <tt class="classname">Card</tt>'s virtual
methods (also called <tt class="methodname">Action</tt>) was called
when a card is picked after a player lands on a chance or community
chest site. This performed the card-dependent action, i.e.
collecting fees, moving to other locations on the board,
celebrating birthdays etc.</p>
<p>Figure 3 shows what happens when a player rolls the dice and
lands on a chance card site, and the card picked up is a <tt class=
"classname">CashCard</tt> to collect doctor's fees for example.</p>
<div class="figure"><a name="d0e112" id="d0e112"></a>
<div class="mediaobject c2"><img src=
"/var/uploads/journals/resources/A%20players%20move.png" align="middle" alt=
"A player&rsquo;s move"></div>
<p class="title c3">Figure 3. A player's move</p>
</div>
<p>Similarly figure 4 depicts the sequence of events when a player
buys a site.</p>
<div class="figure"><a name="d0e120" id="d0e120"></a>
<div class="mediaobject c2"><img src=
"/var/uploads/journals/resources/A%20player%20buys%20a%20site.png" align="middle" alt=
"A player buys a site"></div>
<p class="title c3">Figure 4. A player buys a site</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e126" id=
"d0e126"></a>Implementation Notes</h2>
</div>
<div class="orderedlist">
<ol type="1">
<li>
<p>The intention was to have Player as an abstract class with
inherited classes of <tt class="classname">HumanPlayer</tt> and
<tt class="classname">ComputerPlayer</tt>. In fact the game was
completed for human players only and <tt class=
"classname">Player</tt> remained as a concrete class.</p>
</li>
<li>
<p>The only &quot;<tt class="classname">Site</tt>&quot; missing from this
hierarchy is the <tt class="classname">JustVisitingSite</tt>. The
&quot;<tt class="methodname">Action</tt>&quot; for this site is to do
nothing, which was the default <tt class="methodname">Action</tt>
provided by <tt class="classname">Site</tt>. One could argue that
<tt class="classname">Site</tt> should have been an abstract class,
with <tt class="methodname">Action</tt> as a pure virtual method. A
concrete <tt class="classname">JustVisitingSite</tt> class would
then be required to implement the do nothing action.</p>
</li>
<li>
<p>Global variables humph! Yes, I know now good design eliminates
global variables, but I must have been overly familiar in using
them when this program was first written. These are probably better
encapsulated in a class such as <tt class=
"classname">MonopolyDocument</tt>.</p>
</li>
<li>
<p>&quot;<tt class="methodname">Action</tt>&quot; could be better named but
it achieved its purpose.</p>
</li>
</ol>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e181" id="d0e181"></a>Some Design
Points</h2>
</div>
<p>On the face of it the above might look all right, but there are
issues here. Some of these can be deduced from the diagrams, others
need a little more elaboration.</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e186" id="d0e186"></a>Copy
semantics</h3>
</div>
<p>The Site and Player are reference, rather than value, objects,
i.e. it does not make sense to take a copy of the 'Car' player,
debit &pound;50 cash from the copy, and expect that the 'Car'
player itself is &pound;50 worse off (although that probably would
have been the intention had it been coded that way). It must be
confessed that this implementation did not declare a private copy
constructor, and assignment operator. Rather it relied on the
programmer instead of the compiler to enforce the correct copy
rules.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e191" id="d0e191"></a>Class
dependencies</h3>
</div>
<p>The following relationships exist between Players and Sites.</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>A <tt class="classname">Player</tt> is located at a <tt class=
"classname">Site</tt>.</p>
</li>
<li>
<p>A <tt class="classname">Site</tt>, potentially, has visitors -
i.e. a number of <tt class="classname">Player</tt>s.</p>
</li>
<li>
<p>An <tt class="classname">AssetSite</tt> may have an owner - i.e.
a <tt class="classname">Player</tt>.</p>
</li>
<li>
<p>A <tt class="classname">Player</tt>, will have assets - i.e. a
number of <tt class="classname">Site</tt>s.</p>
</li>
</ul>
</div>
<p>Bearing the required copy semantics in mind, it seems quite
natural to want to write:</p>
<pre class="programlisting">
Site&amp; aSite (aPlayer.Location ());  
  // or&hellip;
Player&amp; aPlayer (anAssetSite.Owner ());
</pre>
<p>This means, though, that neither <tt class="classname">Site</tt>
nor <tt class="classname">Player</tt> is a standalone class; each
is dependent on the other. I wouldn't want that dependency if I
could help it, but in this case I haven't really come across a
better solution. References [1] and [2] address this issue in depth
- perhaps I should go back and re-read them)</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e245" id="d0e245"></a>More Class
dependencies</h3>
</div>
<p>Another interesting dependency involved the AssetSites and the
colour group to which they belong. In this implementation
<tt class="classname">ColourGroup</tt> and <tt class=
"classname">Site</tt> were referred to in two ways each(!), (yes, I
did say two!). First they both were enumerated types. The
<tt class="classname">ColourGroup</tt> enumerated type was passed
to an AssetSite when the object was created.</p>
<p>Secondly, they were in global (again!!) arrays. The <tt class=
"classname">Site*</tt> array, already mentioned, representing the
board, and a <tt class="classname">ColourGroup*</tt> array indexed
by the enumerated type for a given colour group, for each set of
the <tt class="classname">Site</tt>'s enumerated types.</p>
<p>If one were to add new sites or colour groups then two places in
the code would need changing, one - the addition of the appropriate
enumerated type and two - the creation of the object itself.
Although this works it clearly isn't good practise - it would be
easy to forget (or overlook) changes one of those places.</p>
<p>Colour groups need to know about the sites that make up the
colour group. It's debatable if sites need to know about their
colour group, but (ignoring necessary casts) it seems natural to
want to write:</p>
<pre class="programlisting">
AssetSite&amp; anAssetSite 
                (aPlayer.Location ());
ColourGroup&amp; colourGroup
          (anAssetSite.ColourGroup ());
</pre>
<p>But we cannot have the following constructors:</p>
<pre class="programlisting">
class AssetSite {
public:
  AssetSite (ColourGroup&amp;, etc&hellip;);
};
class ColourGroup {
public:
  ColourGroup (AssetSite&amp;, AssetSite&amp;);
// Utility, Mayfair &amp; OldKentRd
  ColourGroup (AssetSite&amp;, AssetSite&amp;, AssetSite&amp;); // Most BuildingSites
  ColourGroup (AssetSite&amp;, AssetSite&amp;, AssetSite&amp;, AssetSite&amp;); // Stations
};
</pre>
<p>because an asset site cannot be created until its colour group
exists, and vice versa. So do we have a default constructor for a
colour group, then add the sites later, or ignore the colour group
when the site is constructed and assign it later? Which, if any, of
these is appropriate should come out of the design process and this
gets discussed in the next article.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e282" id="d0e282"></a>What are
Sites?</h3>
</div>
<p>In this implementation it can been seen that, when we look at
the hierarchy under <tt class="classname">AssetSite</tt>, that the
<tt class="classname">AssetSite</tt>s represent two things. One is
the <tt class="classname">Site</tt> itself - the player lands on
the site and something happens. The other is they represent an
<tt class="classname">Asset</tt> that is owned by a player, i.e.
the <tt class="classname">TitleDeed</tt> cards. The distinction
between these representations was not made in this
implementation.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e302" id=
"d0e302"></a>Model-View-Controller</h3>
</div>
<p>This application lends itself naturally to the
Model-View-Controller pattern. However this design and
implementation wasn't based on the pattern and any similarity was
pure happenstance.</p>
<p>The purpose of the pattern is to separate the data from the user
control and views of the data. More details on the pattern in
general can be found in references [3] and [4]. It is also
discussed, with respect to this project, in the next article.</p>
<p>This implementation described here partly failed in this
respect.</p>
<p>As you can see from the diagrams opposite, whenever a user
issued a command, the controller (<tt class=
"varname">theApplicationWindow</tt>) had an internal knowledge of
the data (by direct access to those global variables), and
manipulated them accordingly.</p>
<p>The viewing part of the system was slightly better organised.
The main view onto the status of the game was known as the
<tt class="classname">BoardView</tt>. This had it's own knowledge
of the bitmaps representing the sites, and the icons representing
each player, which means there's no dependency of the game objects
(Site, Player etc) on the view, but the view (a window) still
directly accessed the appropriate global variables. The same was
also true of player status and auction dialog windows.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e321" id="d0e321"></a>A thought on
STL</h3>
</div>
<p>The standard template library wasn't around at the time this
version of the program was implemented, but it would have been
useful to use the container classes. I.e. all the current players
could be stored in <tt class=
"classname">std::list&lt;Player&gt;</tt>, the assets belonging to a
player referenced through <tt class=
"classname">std::set&lt;AssetSite&gt;</tt> etc.</p>
<p>But wait&hellip; we've already mentioned that <tt class=
"classname">Player</tt> and <tt class="classname">Site</tt> are
reference based objects and not value based objects required by the
STL. How we get round this and a different look at the game's
design will be given in the final part of this article. Also we
have a first stab at applying some Patterns.</p>
</div>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e340" id="d0e340"></a>References</h2>
</div>
<div class="bibliomixed">
<p class="bibliomixed"><span class="citetitle"><i class=
"citetitle">Analysis Patterns - Reusable Object Models</i></span>
Martin Fowler; Addison Wesley; ISBN 0-201-89542-0</p>
</div>
<div class="bibliomixed">
<p class="bibliomixed"><span class="citetitle"><i class=
"citetitle">Designing Object Oriented C++ Applications Using the
Booch Method</i></span> Robert Martin; Prentice Hall; ISBN
0-13-203837-4</p>
</div>
<div class="bibliomixed">
<p class="bibliomixed"><span class="citetitle"><i class=
"citetitle">Design Patterns - Elements of
ReusableSsoftware</i></span> Gamma et al; Addison Wesley; ISBN
0-201-63361-2</p>
</div>
<div class="bibliomixed">
<p class="bibliomixed"><span class="citetitle"><i class=
"citetitle">A System of Patterns (Pattern - Oriented Software
Architecture)</i></span> Buschmann et al; Wiley; ISBN
0-471-95869-7</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
