<!--------------------------------------------------------------------------->
<!-- INTRODUCTION
The Code Project article submission template (HTML version)
Using this template will help us post your article sooner. To use, just
follow the 3 easy steps below:
1. Fill in the article description details
2. Add links to your images and downloads
3. Include the main article text
That's all there is to it! All formatting will be done by our submission
scripts and style sheets.
-->
<!--------------------------------------------------------------------------->
<!-- IGNORE THIS SECTION -->
<html>
<head>
<title>The Code Project</title>
<style type="text/css">
BODY, P, TD { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt;
margin-left: 40px;
}
H2,H3,H4,H5 { color: #ff9900; font-weight: bold; }
H2 { font-size: 13pt; }
H3 { font-size: 12pt; }
H4 { font-size: 10pt; color: black; }
PRE { BACKGROUND-COLOR: #FBEDBB; FONT-FAMILY: "Courier New", Courier, mono; WHITE-SPACE: pre; }
CODE { COLOR: #990000; FONT-FAMILY: "Courier New", Courier, mono; }
</style>
<link rel="stylesheet" type="text/css" href="http://www.codeproject.com/styles/global.css" />
</head>
<body bgcolor="#FFFFFF" color="#000000">
<!--------------------------------------------------------------------------->
<!------------------------------- STEP 1 --------------------------->
<!-- Fill in the details (CodeProject will reformat this section for you) -->
<pre>
Title: The Object-Oriented Text Star Trek Game in C++
Author: James M. Curran
Email: jamescurran@mvps.org
Member ID: 2094
Language: C++
Platform: Windows, Console mode.
Technology: Good old Text
Level: Beginner
Description: The Classic Super Star Trek Game rewritten in modern Object-oriented C++
Section Game Development
SubSection Game
License: Choose your license (<a href="http://www.codeproject.com/info/licenses.aspx">CPOL, CPL, MIT, etc</a>)
</pre>
<!------------------------------- STEP 2 --------------------------->
<!-- Include download and sample image information. -->
<ul class="download">
<li><a href="Article_src.zip">Download source - XXX Kb</a></li>
</ul>
<p></p>
<pre>
------*------
------------- `--- ------'
`-------- --' / /
\\------- --
'-----------'
The USS Enterprise --- NCC - 1701
Your mission begins with your starship located
in the galactic quadrant Canopus.
Your orders are as follows:
Destroy the 24 Klingon warships which have invaded
the galaxy before they can attack Federation Headquarters
on stardate 3732. This gives you 32 days. There are
3 starbases in the galaxy for resupplying your ship.
Hit any key to accept command.
------------------------
. . . . * . . . Stardate 3700
. . . . . . . . Condition GREEN
. * . . . . . . Quadrant (5, 4)
. . . * . . . . Sector (5, 2)
. <E> . . . . . . Photon Torpedos 10
. * . . . . . . Total Energy 3000
* . * * . . . . Shields 0
. . . . . . . * Klingons 24
------------------------
Command?
</pre>
<!------------------------------- STEP 3 --------------------------->
<!-- Add the article text. Please use simple formatting (<h2>, <p> etc) -->
<h2>Introduction</h2>
<p>Here's is my entry into the Code Project Random Programming Competition for Summer 2008. It seems that Michael
Birken beat me to being the first to post a Star Trek article. Since the two programs have a shared history and
game play, which he covers extensively in his article, it's best if you read that one first.
(It's OK.... I'll wait)</p>
<ul><li><a href="http://www.codeproject.com/KB/game/startrek_1971_text.aspx">Star Trek 1971 Text Game</a></li></ul>
<p>I'll just concentrate on the difference in my design.</p>
<h2>Background</h2>
<p>This port took a roundabout journey to the pages of CodeProject. As Mr. Birken describes, Mike Mayfield's program
was ported to PCs by Creative Computing Magazine, and appeared in David Ahl's "<i>Basic Computer Games,
Personal Computer Edition</i>" (I still have my autographed copy) in the late 1970's. Due to the limitations of
the language, it was a nightmare of spagetti code. Then, in 1996, a man named Chris Nystrom took that code, and ported
it to Standard C. However, it was a very literal port (much like Mr. Birken's C# version) : virtually all variable
are globals, maintaining their cryptic one or two letter names, and nearly all functions pass
their parameters and return value using global varaibles.
</p>
<p>I liked the idea, but figured since we now had a powerful language to work with, we should use it to it's full
advantage. So, in 1997, I set out to written a fully object-oriented version. The goal was
for it to have the same look & feel and game-play as the original, but with a modern OO design.
</p>
<p>Unfortunately, Visual C++ 5.0 was a bit lacking back then -- templates were a bit flakey, and the STL didn't
work at all. I gave up after a day's work. A year later, now armed with the more stable Visual C++ 6.0, I put
in all of three days work, before getting bored and abandoning it again.
</p>
<p>When the contest was announced, I remebered this long forgotten project, and to my surprise, I was able to find the code
on one of the PCs on my home network (old computers never die, they are just repurposed).
</p>
<p>So, with 10 years more experience with OO design (if somewhat rusty C++ skills)
and a C++ compiler that mostly works (Visual Studio 2008 comes Microsoft C++ v15), I set out to finally complete
the task.</p>
<h2>Using the Code</h2>
<p>There's nothing particularly notable about the code, unless you want to track how
my views on Hungrian notation and instance member prefixes changed over the
years. Or how my native C++ coding look a lot like managed C# code with
thing like Interfaces and Properties.</p>
<p>The key design principle is Separation of Concerns. The whole universe of
the game is divided up into little objects, and they handle everything about
their little piece of the world, and, with few exceptions, know nothing about
anything outside of it.</p>
<p>At the heart of this is an object called, naturally enough, <b>Game. </b>
The Game object knows about our Ship, the Galaxy, and the Current Time, and not
much else. It's major task is to handle setting up the game,
determining when the game is won, and accepting the players commands. It
also handles the major "cheat" in the design -- having the Enemy respond to
something the Ship did.</p>
<p>Next we have the <b>Ship </b>object. The Ship knows a bit more, but only
things related to the ship as a whole. It knows it's position in the
galaxy, how much energy it has, and if it is docked to a starbase. It also
knows that it has several subsystems, each of which derived from the base class
<b>ShipSystem</b>.</p>
<b>
<img alt="" src="ShipSystem.png" style="float: left" /></b><p> </p>
<p> </p>
<p> </p>
<p><b>
ShipSystem</b> and it's derived classes are the core of the game. The is
one derived class for each of the game's commands. (One of those commands
is "Resignation" which is not really a ship's system but is a <b>ShipSystem</b> derived class,
so analogy breaks a bit, but we'll ignore that).</p>
<p>Via the base class, each <b>ShipSystem</b> knows it's name, description (these
are used to display the help output when you type a bad command) and it's
command keyword, and what ship owns it. <b>Ship</b> doesn't even know how
many subsystems it have, or what their keyword are -- It just asks each, in
turn, if the user's input activates them.</p>
<p>The <b>ShipSytem</b> base class also handles the managing of damage and repairs
to each system.</p>
<p>Then, the derived classes handle the details of their own particular system.
For example, the <b>Engines </b>object accepts a direction and distance to
travel, and then ask the <b>Quadrant</b> object to move it. The Quadrant
object reports back where it arrived, and what happend to it along the way (did
it hit something? did it enter a new quadrant?)</p>
<p> <b><img alt="" src="subsystems.png" /></b></p>
<p>Similarly, the <b>LongScan</b> object would look at a small part of the galaxy
and display what it finds. You might think that for the LongScan to look
at the Galaxy object is breaking encapsulation but it is exactly what that
system would do in real life (if you consider Star Trek "real life").</p>
<p>Two other base classes are of interest: ILocatable and IDisplayable.
IDisplayable functions like an Interface in the .Net world; it only has one
method and it's pure virtual: DisplayOn(). Basically, <b>
obj.DisplayOn(cout); </b> would write the object, in it's own unique
format, onto cout. I can then define operator<< for ostream &
IDisplayable.</p>
<p>ILocatable is a implemented base class, despite it's name. It's used
as the base class for any object that has a position in a sector: your ship, the
klingons, stars and torpedos.</p>
<p><i>Note</i>: I've used pieces of the <b>Boost Libraries</b> (<a
href="http://www.boost.org">www.boost.org</a>), so you'll need to have those
installed to re-compile the source code. Boost is an extensive open-source
library of C++ classes written by some of the best minds in the field. If
you do any C++ coding, you should have it installed anyway.</p>
<pre>
Command? srs
-----------------------
. . . . . . . . Stardate 2800
. . . . . . . . Condition GREEN
. . . . . . . . Quadrant (7, 8)
. . . . * . * . Sector (6, 7)
. . . * . . . . Photon Torpedos 10
. . . . . .<E><E>. Total Energy 3000
. . . . . . . . Shields 0
. . . . . . . . Klingons 17
------------------------
Command? lrs
Long Range Scan for Quadrant (7, 8)
--------------------
: 004 : 208 : *** :
--------------------
: 006 : 003 : *** :
--------------------
: 102 : 016 : *** :
--------------------
Command?
</pre>
<h2>Future</h2>
<p>As with every project, along the way I places where a major change would make a
big improvement, but a deadline loomed, and I didn't want to do anything
radical. But a major overhaul is needed. </p>
<p>The first thing is that I realized too late the difference between a Quadrant & a
Sector -- there isn't one -- so those two classes should be merged. That
would make a large swath of code much simpler. </p>
<p> I also decided that this really needs an event-driven design, so the quadrant
would fire an event says an object moved within it, and that Klingons would
listen to the quadrant, and know to fire back. Rather than the current
design, where the Engines tell the Game object to tell the klingons to fire
back. But the Standard C++ library doesn't include a message passing
framework, and I didn't want to build my own.</p>
<p> However, .Net does have such a framework, build right into it's core. I
had first thought I couldn't port this easily to C#, because I had used multiple
inheritance, but I now believe that would be easy to factor out. But, I
put a lot of other work aside for the summer to work on this, so that project
will have to wait for next summer.</p>
<h2>History</h2>
<p> Aug 5th - Considering the history of this program (see above), it's
a bit silly to call it "version 1.0", but, whatever, this is the first release
of this rendition.
</body>
</html>