>
USS Enterprise

TO BOLDLY GO

WHERE NO TBA

HAD GONE BEFORE

Klingon Starship
alien in viewscreen

Welcome back, Captain.

Journey back to the halcyon days of 1971, when men coded on paper tape and punch cards, and Star Trek was two years dead. A man named Mike Mayfield developed the first Trek video game as a TBA (Text-Based Adventure), a text-based interactive written for the Sigma 7 and later ported into HP Basic.

Sad you missed it? Don't be, because a few intrepid modern-day hackers have ported the entire codebase into C# and compiled to run on your modern PC. All the ASCII-based adventures of the Starship Enterpise are yours for the asking, months before J.J. Abrams finishes rebooting the franchise. Relive the past before they rebuild the future. Starfleet demands it.

(From the article "Play the original Star Trek video game from 1971," by Jay Garmon in Geekend, August 5, 2008.)

Star Trek

1971 Text Game

 command insignia

by Michael Birken, 29 July 2008

A Bit of History

Two years after the original series was canceled in 1969, high school senior Mike Mayfield was busy keeping the Star Trek universe alive by feeding punched paper tape into a Sigma 7 in an effort to bring the crew of the Enterprise and the Klingon Empire to life on a 10 character-per-second teletype terminal. Soon after Mike ported his game to HP BASIC, it entered the public domain. From there, early computer enthusiasts enhanced and rewrote the game for every flavor of mini and microcomputer BASIC imaginable and beyond.

I remember encountering versions of the game back in the early 80s when I was a little kid trying to learn BASIC on my IBM PCjr. Back then, computer books and magazines distributed programs in printed form. Meaning, you had to type them in to play the games. It was a pain in the ass, but the process encouraged you to tinker. It motivated you to learn to code and to tweak or even improve the programs you were entering in.

Every BASIC game book that I picked up contained some version of the Star Trek game. I recall loading it up a few times, but each time I ended up staring at the screen in utter confusion. "How the heck is this Star Trek?" I remember thinking. I couldn’t figure out how to play it.

By the time I entered high school, I had graduated from BASIC and moved onto to bigger and better things like C and C++. But, on occasion, I often wondered about the Star Trek text game. What made it so popular? After learning about the history that I touched upon above, I decided to dig it up and take a second look.

After a bit of web surfing, I came across Mike Mayfield’s original port to HP BASIC. Here’s a (screenshot of a) snippet of the code:

This is the type of code I grew up with.

Ah, good old line-numbered BASIC. It’s all coming back to me now. Those line numbers were there to provide targets for GOTO and GOSUB statements. But, line numbers made editing a tad difficult. It was convention to enter in line numbers that were multiples of 10. That way, as you developed the program, you could go back and insert up to 9 additional statements in between existing lines without reworking all the GOTO/GOSUB references. If you needed to insert more than 9 lines, I remember a special feature in the BASIC editor on my PCjr. It would append a zero to all line numbers and all line number references throughout the program. Meaning, you could now insert up to 99 lines. Couldn’t they just renumber the program in multiples of 10? Nah. The PCjr wasn’t powerful enough for that.

If you’re wondering about “Centerline Engineering,” it was an imaginary company that Mike Mayfield coined to give his BASIC projects a level of prominence to those reading the remarks section.

With code in hand, I really wanted to play the game. I’m sure that there are HP BASIC interpreters out there for modern machines, but what fun would that be. Before I played it, I wanted do my own port. This game was born in the hobbyist era. It was made to be reinterpreted and enhanced as it traded handed. I wanted to bring back part of those long-lost magical days of type-in programs.

My first impression of the code was "what’s with all the single letter variable names?" First I thought it was a limitation of HP BASIC, but then I noticed the occasional 2-letter names. I guess 2 is better than 1. Everything is also in caps. Take a look at this line:

That line increments T. But, due to the caps, I feel like the code is screaming at me. ASIGN THE SUM OF T AND 1 BACK TO T DAMN IT! Also, I’m so used to writing t++ or t += x that I forgot about the expanded notation. In fact, entering 7th grade having mastered BASIC, I found myself really confused when my math teacher introduced us to solving simultaneous equations. For instance, find the value of X in this equation:

That was the first time I was introduced to the concept of operator overloading. The equals-sign can mean variable assignment or numerical equivalence depending on the context.

Here’s a cool block of code that I noticed:

These are not executable statements. They’re strings that can be referenced in PRINT commands. The unquoted symbols get substituted with values of variables. It’s conceptually similar to C-style printf() format placeholders. I didn’t realize that BASIC offered such a rich numerical formatting notation.

As I continued to examine the source, I found some statements that didn’t make sense. For instance, even though you don’t have to declare variables before you use them, you still need to specify the dimensions of arrays. I came across some arrays that were never allocated as such. Ultimately, I decided to seek out a better basis for my port.

After a bit of Googling, I found a cleaned up version that maintained the majority of Mike Mayfield’s code. Some of it was reworked, probably to enable it to run on modern versions of BASIC. For instance, those cool IMAGE statements were dropped and replaced with sets of simpler PRINT commands. The variable names appear virtually identical, but at least they are all accounted for in this version.

Porting the Game

Next, I had to decide what language to port it to. Staring at that BASIC code reminded me that C# brought goto back into the mainstream. Would it be possible to do an exact line-by-line port from BASIC to C#? Apparently so... and the result is some of the sickest code I’ve ever keyed into a computer. Want a comparison? Here’s a segment of BASIC code:

Ode to BASIC: Oh, how neat and clean you look!

And the C# version:

It may look messy, but it works!

To simulate line numbers, each line starts with a label consisting of an underscore followed by a number. That works fine for GOTO, but what about GOSUB? Examine line 2992. Subroutines were replaced with methods. That almost worked. In BASIC, you’re not forced to RETURN from subroutines. You can leave them via GOTO. That was used only in the case that the player is destroyed to send them back to the beginning of the program to start over. I replaced that GOTO with a return statement that passes a flag back to the caller. The caller inspects the flag and jumps back to the program start if need be. I also discovered that at one point, there is a GOTO that jumps into a FOR loop. C# won’t let you jump to a label in a sub-block of code. I transformed the FOR loop into a GOTO loop to make C# happy.

All the variables in the BASIC program, including the arrays, are real number type. However, in BASIC, an array and a scalar can share the same name; the interpreter is able to sort it all out. But, C# is less kind. To solve the problem, I prefixed array names with underscores. Also, arrays in BASIC are indexed from 1 instead of 0. To compensate, I increased the length of all arrays by 1. Index 0 is never used.

When I started testing my port, I noticed some string formatting problems. Examine the following BASIC line:

That means: Print 41 spaces followed by left-parenthesis. That was easy to translate, but the intension was to push the left-parenthesis onto the next line by letting it wrap around the console. I cleaned some of this stuff up. There are also some tables that get printed in the game. I reformatted them a bit to make them easier to read.

One other thing: notice that in this type of BASIC, # indicates not-equal-to. It took me a while to realize why they chose that symbol. # resembles ≠.

And now, I was ready to play the game!

warping across screen