Click here to Skip to main content
15,884,177 members
Articles / Programming Languages / C++

The Object-Oriented Text Star Trek Game in C++

Rate me:
Please Sign up or sign in to vote.
4.74/5 (10 votes)
6 Aug 2008CPOL6 min read 42.4K   816   34  
The Classic Super Star Trek Game rewritten in modern Object-oriented C++
<!--------------------------------------------------------------------------->  
<!--                           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)
 . &lt;E&gt; .  .  .  .  .  .     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.&nbsp; 
	(It&#39;s OK.... I&#39;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&#39;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),&nbsp; I set out to finally complete 
	the task.</p>
<h2>Using the Code</h2>
	<p>There&#39;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.&nbsp; 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.&nbsp; 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.&nbsp; </b>
		The Game object knows about our Ship, the Galaxy, and the Current Time, and not 
		much else. It&#39;s major task is to handle setting up the game, 
		determining when the game is won, and accepting the players commands.&nbsp; It 
		also handles the major &quot;cheat&quot; in the design -- having the Enemy respond to 
		something the Ship did.</p>
	<p>Next we have the <b>Ship </b>object.&nbsp; The Ship knows a bit more, but only 
		things related to the ship as a whole.&nbsp; It knows it&#39;s position in the 
		galaxy, how much energy it has, and if it is docked to a starbase.&nbsp; 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>&nbsp;</p>
	<p>&nbsp;</p>
	<p>&nbsp;</p>
	<p><b>
		ShipSystem</b> and it&#39;s derived classes are the core of the game.&nbsp; The is 
		one derived class for each of the game&#39;s commands.&nbsp; (One of those commands 
		is &quot;Resignation&quot; which is not really a ship&#39;s system but is a <b>ShipSystem</b> derived class, 
		so analogy breaks a bit, but we&#39;ll ignore that).</p>
	<p>Via the base class, each <b>ShipSystem</b> knows it&#39;s name, description (these 
		are used to display the help output when you type a bad command) and it&#39;s 
		command keyword, and what ship owns it.&nbsp; <b>Ship</b> doesn&#39;t even know how 
		many subsystems it have, or what their keyword are -- It just asks each, in 
		turn, if the user&#39;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.&nbsp; 
		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.&nbsp; 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.&nbsp; 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 &quot;real life&quot;).</p>
	<p>Two other base classes are of interest:&nbsp; ILocatable and IDisplayable.&nbsp; 
		IDisplayable functions like an Interface in the .Net world; it only has one 
		method and it&#39;s pure virtual:&nbsp; DisplayOn().&nbsp; Basically, <b>
		obj.DisplayOn(cout);&nbsp; </b>&nbsp; would write the object, in it&#39;s own unique 
		format, onto cout.&nbsp;&nbsp; I can then define operator&lt;&lt; for ostream &amp; 
		IDisplayable.</p>
	<p>ILocatable is a implemented base class, despite it&#39;s name.&nbsp;&nbsp; It&#39;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>:&nbsp; I&#39;ve used pieces of the <b>Boost Libraries</b> (<a 
			href="http://www.boost.org">www.boost.org</a>), so you&#39;ll need to have those 
		installed to re-compile the source code.&nbsp; Boost is an extensive open-source 
		library of C++ classes written by some of the best minds in the field.&nbsp; 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>&lt;E&gt;. 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&#39;t want to do anything 
	radical.  But a major overhaul is needed.&nbsp;</p>
	
<p>The first thing is that I realized too late the difference between a Quadrant &amp; a 
	Sector -- there isn&#39;t one -- so those two classes should be merged.&nbsp; That 
	would make a large swath of code much simpler.&nbsp;</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.&nbsp; Rather than the current 
		design, where the Engines tell the Game object to tell the klingons to fire 
		back.&nbsp; But the Standard C++ library doesn&#39;t&nbsp; include a message passing 
		framework, and I didn&#39;t want to build my own.</p>
	<p>	However, .Net does have such a framework, build right into it&#39;s core.&nbsp; I 
		had first thought I couldn&#39;t port this easily to C#, because I had used multiple 
		inheritance, but I now believe that would be easy to factor out.&nbsp; 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>&nbsp; Aug 5th -&nbsp; Considering the history of this program (see above), it&#39;s 
	a bit silly to call it &quot;version 1.0&quot;, but, whatever, this is the first release 
	of this rendition.
</body>

</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) NovelTheory LLC
United States United States
20+ years as a developer : Assembly, C, C++ and C# (in that order) with sidelines in ASP/VBScript, ASP.Net, JavaScript, Perl, QuickBasic, VisualBasic, plus a few others which I'm not going to mention because if I did someone might ask me to use them again (shudder)

Microsoft MVP in VC++ (1994-2004)

I also run www.NJTheater.com as a hobby.

Full resume & stuff at NovelTheory.com

Underused blog at HonestIllusion.com

Comments and Discussions