Chapter 4 page 1 STARTREK THE COMPUTER PROGRAM By Joe Kasser CHAPTER 4 Now that the top and first intermediate level flow charts have been defined they have to be broken down into the low level charts and then finally converted into a program. This would normally be performed in two distinct steps. The first is to lay out the flow chart completely and only then implement the second step which is the writing of the code. In this case, to keep track of what is going on, and to avoid the need for constant references between chapters, the flow chart will be discussed the code will be immediately written and tested. A comparison between the flow chart and the code can then be made directly. It provides for an easier way for the beginner to relate the two together. Each of the modules will be tested on a "stand alone" basis (that is, by itself) and the whole program slowly built up from the bottom within the framework defined by the top level flow chart. If you thus enter the code into your computer as we go, you will slowly build up the game command function by command function and will be able to debug any errors with relative ease. BASIC executes instructions in sequence in ascending line number order. There is also no requirement for the numbers to be contiguous. In fact it is advantageous to leave some spares between the various lines so that additions or changes can later be made to the program without affecting the whole of the program. Most dialects of BASIC however include a renumbering function which can be used to free up space in the event that no space exists to enter a new instruction. The renumber function, however changes everything, and will make the documentation invalid if the text is written about a version which is later renumbered. 4.1 Line Numbering Convention The convention will be established that program lines ending in a 0 are program lines to be used in the final game. Any lines ending with any other number are temporary lines used for some intermediate reason explained at the time of use. 4.2 Allocating Space for the Program A guess is now made as to how much space (how many lines) will be needed to write the various parts of the program and space is allocated accordingly. The main outline of the program showing which routines begin at what lines is thus as shown in figure 4.1. The program starts with the lowest numbered line. Conventionally lines start at number 10 and proceed in 10's. thus the program lines will be numbered as 10, 20, 30 and so on. This allows you to add extra lines if you forget one. This convention grew up before BASIC interpreters recognised the RENUM Copyright (c) Joe Kasser 1989 Chapter 4 page 2 STARTREK THE COMPUTER PROGRAM By Joe Kasser (Renumber) console command. Note that a console command is a command used by the programmer during the program writing and debugging phases and may not execute when put inside a line. The RENUM command renumbers all line numbers and references to line numbers. Thus a short program of the form 1 REM THIS IS LINE 1 2 REM THIS IS LINE 2 3 REM THIS IS LINE 3 after being subjected to the RENUM command would be displayed as 10 REM THIS IS LINE 1 20 REM THIS IS LINE 2 30 REM THIS IS LINE 3 when the LIST statement is invoked. LIST causes BASIC to display a listing of the program statements at the console. A variation LLIST causes the statements to be printed out by the printer attached to the computer. When typing things into the computer you must always end with a "carriage return" character to tell the computer that you have finished. The computer will wait patiently for you to enter your input, then when you touch the carriage return key on your keyboard it knows that you have finished and begins to process your input. From now on, we will assume that you remember about the carriage return key. Copy lines 1, 2 and 3 into the computer, and then type the word LIST followed by a carriage return. Now type in RENUM (carriage return) and after the computer has said "OK" again type in LIST and examine the result. Now type in NEW to clear the memory. Some dialects of BASIC don't recognise the word "NEW", and use something else such as "SCR" (SCRatch). If your computer des not recognise the word NEW, look up the equivalent word in the instruction manual. Before you start entering lines of code into the computer first familiarise yourself with the SAVE and LOAD commands of BASIC. The SAVE command allows you to save what you have entered on disk or cassette. The LOAD command allows you to retreive the program. Read the computer manual to determine exactly how these two commands work and then practice them frequently as you proceed to enter lines. Never walk away from the computer for any reason without performing a SAVE operation. You will feel more than upset if the power fails or your two year old child kills the power or presses the reset switch on the machine the instant you walk away from it after hours of working on it. The commands usually take the form SAVE "PROGRAM.NAME" or LOAD "PROGRAM.NAME" where the program name is included in quotation marks. Variations exist which determine the format of the SAVE/LOAD operation. Read the instruction manual and practice them, even if you don't understand them at this time. Copyright (c) Joe Kasser 1989 Chapter 4 page 3 STARTREK THE COMPUTER PROGRAM By Joe Kasser If you can them work for you , that will be enough for now. The rest of the knowledge will come. We are now ready to begin entering the program sections into the computer. First consider the framework for the BASIC program as shown in figure 4.2. The program starts with a REMark or comment telling us what version it is. The name of the program is given as well as the version. It is good practice to date each version of the the program. When you eventually type up the program, replace the XX-YY-ZZ in line 10 by the date that you do the typing. Later, when you read further into the book, each time you add to the program, change the date in line 10 to show the date that you updated the program. In this manner you will always know which version of the program you are working with when you have a number of different versions or "back ups". Each function then begins at its assigned line number with a REMark or comment statement identifying what is happenning. The subsequent line is a GOTO statement that takes the program down to line 10000. The PRINT statement in line 10001 displays the message "FUNCTION NOT PROGRAMMED AT THIS TIME" at the console. The GOTO statement causes the program to goto a defined line number. Thus lines can be bypassed if required. It will be discussed at length later. The End (of the) game sequence begins at line 9000. The 'END' statement in line 9010 tells BASIC that the program has finished and to stop performing the program. In many dialects of BASIC, if the last line of the program is the last line to be executed, there is no need to use the 'END' statement. If the last line is not the actual last line, as in this case, the 'END' statement is required. The PRINT statement is BASIC's way of getting information to the console. You can PRINT a variable or a constant. The statement PRINT "FUNCTION NOT PROGRAMMED AT THIS TIME" in line 10001 will print a String constant. This String constant is an ASCII string. The PRINT statement appears in many forms as you will see later. Line 10001 ends with a RETURN statment. The RETURN statments cause the program to return from a subroutine to the main flow of the program. We have not yet seen any subroutine calls because each command function is in itself a subroutine and is called by the main command loop as discussed later. Now copy the program statements of figure 4.2 into your computer, because having defined the structure of the program, we are ready to begin to flesh it out. SAVE what you have entered and read on. Copyright (c) Joe Kasser 1989 Chapter 4 page 4 STARTREK THE COMPUTER PROGRAM By Joe Kasser 4.3 Initialising the Variables The first thing to do is to set up all the variables and flags used in the game, and set up the contents of the quadrants in the gfalaxy. However, before they can be set up, we must have some idea of what kind of information is used to describe the contents of the quadrants and how and where that information is stored. Each quadrant may contain different quantities of three different items (Klingons, stars and starbases). These items have to be stored in some kind of matrix or array. We could for example use three different arrays one for the Klingons, one for the stars and one for the location of the starbases. This technique would use up more memory than somehow putting the contents of each quadrant in one array. How can this be done, remembering that there can be numerous stars and Klingons in the Quadrant? Well remember the format in which the contents of the quadrant were displayed. The Long Range Sensor scan display for example was, LONG RANGE SENSORS FOR QUADRANT 4,6 001 012 104 002 206 317 717 001 003 where the contents of each quadrant are described as a three digit number with the following convention. The 100's digit = number of Klingons the 10's digit = number of Starbases the 1's digit = number of Stars, thus for example, 317 means that the quadrant contains 3 Klingons, 1 Starbase and 7 stars. If the contents of the quadrant were stored in the array just as they were displayed, that is the 100's digit = number of Klingons the 10's digit = number of Starbases the 1's digit = number of Stars. As long as the number of stars or the number of Klingons do not each exceed nine, the value stored in the array will be unambiguous. The Long Range Sensor and Map displays can then just display the value of the quadrant directly. Operating on the contents of the quadrant then becomes simple. To remove a Klingon, subtract 100 from the value assigned to the quadrant. For example, for any quadrant in the Q or Quadrant array, which is located in the I th row and J th column , and is referenced as Q(I,J), the BASIC statement LET Q(I,J) = Q(I,J) - 100 will delete one Klingon from the quadrant. Copyright (c) Joe Kasser 1989 Chapter 4 page 5 STARTREK THE COMPUTER PROGRAM By Joe Kasser Detection of the presence of the enemy is also simple. For any quadrant, if there are Klingons present in it, it must have a value greater or equal to 100. Thus the statement IF Q(I,J) => 100 THEN will detect the presence of the enemy. In order to differentiate between quadrants that have been scanned and those that have not, the unscanned ones can be given a negative sign. Thus for example, if Q(I,J) = -317 the contents of the quadrant still show 3 Klingons, 1 starbase and 7 stars, but the negative sign shows that the quadrant has not yet been scanned. The scanning procedure is also made simple. The program does not need to check if a quadrant has been scanned before, it just converts the value stored in the array associated with the quadrant to a positive number. BASIC contains the ABS(X) function which converts any number to its positive value. It does not care what the starting value was, thus LET Q(I,J) = ABS(Q(I,J)) will give the same result no matter if the starting value was negative or positive. For example, if the starting value of Q(I,J) was either -317 or 317 the result will always be 317. Note the use of double parentheses since the ABS( ) function requires them, as does the Q(I,J). BASIC is fussy about their use and will give you a SYNTAX ERROR message if you don't ballance the parentheses across a line. 4.4 Storing the Quadrant Data The galaxy consists of 64 quadrants arranged in eight rows of eight columns. Each quadrant contains at least one star, and may also contain Klingons, and and possibly a starbase. The contents of the quadrants are stored in an array Q(I,J) where I is the row and J the column. I and J may have values between 0 and 7 (8 rows or columns). This array is known as a "Two Dimensional Array" because it has two attributes (I and J). 4.5 Setting Up the Galaxy Having defined how and where the information describing the contents of each of the quadrants within the galaxy are stored (and some of the ramifications of the techniques used) we can now go on to set them up at the beginning of a game. In accordance with good programming practice the flow chart for the operation is first developed and then the language statements performing that operation written. The flow chart for setting up the galaxy is shown in figure 4.3 where the numbers in parantheses in some of the lines, correspond to the numbers of the lines in the BASIC language program. The procedure begins displaying the "start of game messages". It continues by setting up all the constants, variables and parameters that will be used in the game. Next Copyright (c) Joe Kasser 1989 Chapter 4 page 6 STARTREK THE COMPUTER PROGRAM By Joe Kasser follows a loop that is executed once for each quadrant in the galaxy. It is in fact a double loop, one inside the other. The practice of placing one loop inside another is called "nesting". The inside loop sets up all the columns in one row. The outside loop advances the row pointer so that the inside loop can set up all the columns in each row in sequence. For each quadrant, first the number of stars is computed, by choosing a random number between 1 and 7. A test is next performed to see if a starbase is present in the quadrant takes place. This test has a very low probability of success because we want very few starbases in the game. Since we don't want more than say 5 starbases in the galaxy, we could for example put a test in the loop to say that if 5 starbase have already been positioned in the galaxy, not to bother to set up any more. This would however tend to position the bases in the lower number quadrants and the player would soon notice that fact and use it. The technique chosen here is to set the probability of occurrence to be so low, that the probability of having more than 5 starbases in the galaxy is almost non existant. In fact, the probability is so low, that having zero Starbases is a real possibility and so a test has to be put into the flow chart later to determine if that condition is present after the loop terminates. Then the number of Klingons in the quadrant is computed. Here a low probability is used because although there are 64 quadrants in the galaxy, we don't want more than about 30 Klingons in the whole game. The total number of Klingons is then updated because we wish to keep track of how many there are. If no starbases have been allocated, a random quadrant within the galaxy is chosen and a starbase positioned within it. The total number of starbases is then set to 1. The number of Klingons within the galaxy is then examined. If more than 30 are present, the number of stardates available to complete the game is made equal to the number of Klingons, if less than 30 are present, the time is set to 30 Stardates. This allows the player adequate time to search the galaxy for the enemy and also gives extra time if the number of Klingons is large. Putting a Starbase into a quadrant containing Klingons is a philosophical point that has some effect on the game strategy. Exactly what is the function of a starbase? In this game, its purpose is to provide a source of supplies to the Enterprise. It is provided with defensive armament but no offensive armament. The Klingons can thus blockade a base to stop the player reaching it and refuelling. Thus if the Enterprise tries to dock at a starbase when Klingons are present in the quadrant (and shooting) it may take the base commander a short time to synchronise the shields to those of the Enterprise so that the enterprise can approach the base. In the mean time the enemy may be shooting at the ship and cause damage or even destroy it. On the other hand, once docked and protected by the starbase's shields the player can tap into the energy banks of the starbase and can blast the Klingons from within the protection of the starbases's shields. The loops terminate when all the quadrants have been set up. Copyright (c) Joe Kasser 1989 Chapter 4 page 7 STARTREK THE COMPUTER PROGRAM By Joe Kasser A further loop is then performed to ensure that a minimum number of Klingons are set up so as to ensure that the game is not too easy. This loop computes a random quadrant within the galaxy and tests it to determine its contents. If it only contains stars, then a starbase or Klingons can be set up in it at this time. This choice of random quadrants continues until the minimum number (20) of Klingons have been set up. If no starbases have been allocated, a random quadrant within the galaxy is chosen and a starbase positioned within it. The total number of starbases is then set to 1. The Enterprise is then placed in a random quadrant somewhere inside the galaxy. The contents of the quadrant are then set up and the "condition" established to complete the initialisation process. 4.6 Implementing the Flow Chart in BASIC Implementing the flow chart in BASIC could take the form shown in figure 4.4. The program begins at line 10 which has been changed. The REMark is changed to two PRINT statements. CHR$(26) causes a control character to be output to the CRT terminal to clear the screen. Some dialects of BASIC used in computers that have built in screens may use a special word such as CLRS to do the job. The XX-YY-ZZ is replaced by an actual date. The line ends with an error trapping statement which will be discussed later. The colon (:) is used to separate two instructions in the same line. There is no necessity for every instruction to have a separate line number. There are times when it is logical or convenient to have more than on instruction on any given line. Line 20 displays a little more of the credits and then institutes the initialisation process by calling the subroutines starting at lines 4500 and 4660 using the GOSUB (line number) statement. A subroutine is a sequence of instructions that is performed more than once in a program. It is convenient to use just one module of code to perform the operation. This saves memory and facilitates debugging since if the operation is performed by one piece of code no matter when it takes place, if the piece of code is correct (ie. debuged), the operation will always be performed correctly. Subroutines always terminate with a RETURN statement. Lines 30 and 40 display further introductory messages telling the player the scope of the mission. The PRINT K9/100 part displays the number of Klingons that are located within the galaxy but multiplied by 100: hence the K9/100 statement which then displays the actual number. The reason that K9 contains one hundred times the number of Klingons in the galaxy will be discussed later. The semi colon (;) character is a print formatting Copyright (c) Joe Kasser 1989 Chapter 4 page 8 STARTREK THE COMPUTER PROGRAM By Joe Kasser statement. BASIC contains a number of different ways of displaying messages at the console. The two most commonly used formatting statements are the comma and the semi colon. Consider the following statements 11 A=3 : B=6 : C=6 12 PRINT A,B,C when this short program is run, the terminal would display 3 6 9. If on the other hand, the program was changed so that line 12 became 12 PRINT A;B;C when the new version is run, the terminal display would be 3 6 9. The semi colon makes BASIC display the data immediately after any previous piece of data. The comma causes the cursor to advance to the next built in tab position. BASIC will also automatically issue a carriage return line feed sequence after the last data have been displayed. That implies that a 'PRINT' statement by itselft will perform a carriage return line feed sequence, or move the cursor down to the beggining of the next line on the CRT screen. The initialisation module is split into two halves. The first section contained in lines 4500 to 4650 is performed once per session while the second part in lines 4660 to 4830 is performed for every new game that is played. The main sequence is listed in figure 4.4 Consider in detail the task that each line of the subroutine performs. Line 4500 is the comment statement to identify the subroutine. Line 4510 sets up Z as a parameter equal to 1 using the implied LET statement. Different dialects of BASIC may require a different value when called by the RND or random number generation function. The parameter technique is thus used so that it can be readily converted to a different value if necessary. Microsoft BASIC needs Z to be assigned a value of 1. Note that Z is also generally used instead of 1 from now on. If your dialect of BASIC requires a different value for the random number generator, change Z when used in the RND(Z) to what you need. Use Z1 instead of Z for example, and then add the definition for Z1 in line 4510. When you come to run the program, if you don't get random numbers when you expect them, then read the book that came with the computer to see what value to assign to Z1. Since most personal computers speak the Microsoft dialect of BASIC the odds are that you will have no trouble using Z equal to 1. Copyright (c) Joe Kasser 1989 Chapter 4 page 9 STARTREK THE COMPUTER PROGRAM By Joe Kasser A string variable S$ is then defined such that its contents can be used to display the contents of the sectors in the quadrant. It will be described later in the Chapter 6 which deals with the Short Range Sensor Routine. The E0 parameter is then set to 4000 (Units of energy). The parameter C1 is now defined. It is a parameter equal to the number of commands allowed. Using a parameter for the number of commands allowed, allows new ones to be added later without having to make extensive changes in the program. By changing the value assigned to the parameter C1 and later adding the line numbers associated with the new command, any new command can be added with minimal changes to the existing program. Note this technique lets you add a new command as yet undefined, with only two changes to the program. No further changes/debugging of the existing program are required. It can be considered a general rule that any changes made to a working program will introduce bugs into that program. for that reason alone, it is always best to minimise any changes to be made to existing programs. Space is next allocated for the Damage Control arrays D(I) and D$(I) using the DIM (Dimension) statement in line 9020. They are also allocated space as a function of the number of commands in the game using the parameter C1. Their functions will be discussed later. We next allocate space for an array to store the quadrant information (Q(I,J)). The array is a two dimentional array, having rows and columns. Since the galaxy contains 8 rows of 8 columns, the space allocated in the array is for 8 rows and 8 columns. BASIC counts from 0 so that the allocation of 7 places in this line really allocates positions 0,1 ,2 ,3 ,4 ,5 ,6 and 7 or 8 spaces. This means the computer is going to count quadrants from 0 to 7 while the player will see a display of co-ordinates from 1 to 8. The functions performed by the other arrays will be discussed in detail later. For now, just type them into the program. Lines 4530 to 4560 display the start-up message that set the scene for the game. Lines 4570 to 4600 define the command/sub- system strings into the Damage string D$(I) array. Their function will be discussed in Chapter 5 which discusses Damage Control. Line 4610 contain the Command Strings for both the main commands (C1$) and the Computer commands(C2$). Lines 4620 to 4630 define the contents of the computer command function string array (C3$(I)) and line 4650 terminates the first subroutine with a 'RETURN' statement. The second initialisation subroutine starts at line 4660. Everything that should be set to zero at the start of a game is. Most dialects of BASIC will automatically set variables to zero when the 'RUN' statement is invoked to start the program. This line ensures that it is so, and also ensures the zero state for those variables when subsequent games are played. The variables set to zero in this line are C9 The last command excercised Copyright (c) Joe Kasser 1989 Chapter 4 page 10 STARTREK THE COMPUTER PROGRAM By Joe Kasser K4 The number of Klingons captured K5 The number of Klingons that destroyed themselves F9 The end of game flag G9 The Inside/outside the Galaxy flag K9 The number of Klingons in the Galaxy B9 The number of Starbases in the Galaxy L3 The number of Long Range Probes launched. The last item on the line invokes a subroutine beginning at line 3550 which sets the Damage Control status of all the on- board sub-systems to zero. Line 4670 initiates two loops. The loop using J as the loop counter performs the set up operation on each column in one row. The loop using I as the loop counter advances the row counter so that by using the two loops together all the columns in all the rows are set up. Loops can be peformed in a number of ways in BASIC. The FOR/NEXT construction uses a loop counter and tests it automatically. The programmer sets up the limits of the loop in the FOR statement. For example, the statement FOR I = 0 TO 7 sets up a loop. The loop counter is first set to the lower limit (0 in this example). Everything in subsequent lines will be performed until a NEXT statement is encountered. When it is, the value of the loop counter (I in this case) will be incremented and tested to see if it exceeds the second limit (7 in the example). If it is, the loop automatically terminates and the program continues with the instruction immediately following the NEXT statement. If the loop counter is still less than the upper limit, the program flow goes back to the instruction statement immediately following the FOR statement and does the sequence again. The process repeats until the loop counter exceeds the upper limit and the loop times out. Line 4670 calls a subroutine which begins at line 4790. The GOSUB statement is used to invoke a subroutine. The program flow now branches forward to line 4790 and will continue from there interpreting subsequent lines sequentially (such as 4800, 4810 etc) until a RETURN statement is encountered. When the return statement is encountered the program flow returns or comes back to the calling line, and the program continues at the next instruction following the GOSUB statement. The subroutine starting at line 4790 sets up the number of Stars, Starbases and Klingons for each quadrant and will be described below. The next instruction in the line, adds together the numbers assigned to the Klingons (K), the starbase (B) and the stars (S), then makes the number negative (an unscanned quadrant) and stores it in the Quadrant array using the implied LET statement Q(I,J) = -(K + B + S). The NEXT statements for both loops (row and column) which then terminate the loops close out the line. The J loop is terminated first, then the I loop. If the order was inverted, Copyright (c) Joe Kasser 1989 Chapter 4 page 11 STARTREK THE COMPUTER PROGRAM By Joe Kasser there would be a nesting error and the computer would (hopefully) flag it. Line 4710 tests to see if the value representing the total number of Klingons in the game is greater than 20. If it is, the program branches forward to pick up again at line 4740. The value of the variable representing the number of Klingons in the galaxy (K9) was set up to be one hundred times the number of enemy ships in the subroutine starting at line 4790 called in line 4760. Hence the test uses the statement K9>2000. This line tests that a minimum number of Klingons have been set up so as to ensure that the game is not too easy. Remember, the value of K9 is the number of Klingons left multiplied by 100. Line 4720 now invokes a subroutine starting at line 50 (described below) which computes two random co-ordinates within the galaxy. The result is passed as the variables X and Y. The actual quadrant associated with those co-ordinates is Q(X,Y). The contents of the quadrant are tested to see if any Klingons or a Starbase are present in it. If they are, the program branches back to the begining of the line to locate a different quadrant. We are really trying to find a quadrant that does not contain any Klingons, however, on the other hand, the probability of a starbase being present in the quadrant is so low, that if one is, we don't want to delete it. The discussion about how the data is stored in the quadrant showed that there are always less than eight stars in the quadrant. Since the value of the quadrant is also negative (unscanned) a simple test of the form IF Q(X,Y) < -9 THEN will indicate if anything other than stars are present in the quadrant. Line 4730 invokes the subroutine starting at line 4790 to set up a new set of stars, Klingons and starbases, and stores the data in the quadrant matrix using the LET Q(X,Y) = -(K + B + S) statement. The loop then continues setting up more Klingons by virtue of the GOTO 4710 statement at the end of the line until line 4710 determines that the minimum number have been placed in the galaxy. Lines 4710 to 4720 contain a loop that is terminated when the number of Klingons in the game (K9) is greater than a certain minimum. This loop is not programmed by a FOR/NEXT sequence but is taken care of by the programmer with the test in line 4710, and the GOTO at the end of line 4730. Line 4740 sets the amount of time available for the player to complete the game. The variable T is used for Time. The player normally has 30 stardates to complete the game. If however more than 30 Klingons have been positioned in the galaxy, the number of Stardates is set to be the same as the number of Klingons to give the player more time to find and destroy them. Since the variable K9 contains the number of Klingons multiplied Copyright (c) Joe Kasser 1989 Chapter 4 page 12 STARTREK THE COMPUTER PROGRAM By Joe Kasser by 100 (see line 4810) the value of K9/100 has to be compared with 30. An alternative valid way of performing the test would be to state IF K9<3000 THEN T=30 ELSE T=K9/100. Line 4750 sets the initial time factor (T9) and then ensures that at least one starbase is present in the galaxy. B9 is the variable used to store the number of Starbases in the galaxy. If it is still equal to zero at this time, then no Starbases have been set up in any quadrant of the galaxy, and line 4750 proceeds to set one up. A quadrant is chosen at random by the subroutine starting at line 50 and a starbase is placed in that quadrant irrespective of whatever else may already be there. Remember that the sign of the value of the contents of that quadrant is negative, signifying that it has not yet been scanned. Line 4760 determines which quadrant is to be the the starting quadrant for the Enterprise. A random set of co- ordinates is selected by calling the subroutine starting at line 50. The two random values are then loaded into the row (Q1) and column (Q2) co-ordinates of the Enterprise. Q1 and Q2 are variables used to store the position of the Enterprise in the galaxy. If we want to move the Enterprise to another quadrant inside or even outside the galaxy, all we need to do is change the values stored in Q1 and Q2. In order to set up the contents of the quadrant that the Enterprise has been positioned in, line 4770 calls a subroutine beginning at line 3200. Then it continues and calls the subroutine beginning at line 90 to set up the initial state of the Enterprise itself, and sets the amount of energy in the Shields (represented by the variable E1) to a tenth of the total energy allocated to the ship at the start of the game (expressed by the parameter E0). Line 4780 next calls the subroutine starting at line 3400 which determines the condition of the Enterprise (RED, YELLOW or GREEN), and lastly terminates the initialisation subroutine with the RETURN statement. The subroutine to set up the exact number of Stars, Bases and Klingons begins at line 4790. This is an example of a subroutine being called by a subroutine, or "nested subroutines". Line 4790 sets up the number of Stars, represented by the variable S to an integer value between 1 and 7 inclusive by using the expression S = INT(RND(Z)*7+Z). The complex operation is performed in one statement. In order to explain what is being done, let us consider the operation in stages. We require a random number between 1 and 7, for by definition, there cannot be more than 7 stars in a quadrant, and there must always be at least 1 star in any quadrant located inside the galaxy. The procedure starts with the generation of a random number. The RND(X) function returns a floating point random number Copyright (c) Joe Kasser 1989 Chapter 4 page 13 STARTREK THE COMPUTER PROGRAM By Joe Kasser between 0 and 0.9999 ( just less than 1 for all practical purposes). The random number is then multiplied by 7, giving a value between 0 and just less than 7. When a 1 is added to the number, the result is a floating point number between 1 and just less than 8. This floating point number is converted to an integer number between 1 and 7 (inclusive of those values) by the INT(X) function which returns the INTeger part of a floating point number. The whole operation could have been written in individual steps as follows, 4970 S = RND(Z) : REM generate a random number (0 to .9999) 4971 S = S * 7 : REM now convert it to between 0 and 6.99 4972 S = S + 1 : REM now make it something between 1 and 7.99 4973 S = INT(S) : REM and finally make it an integer value between 1 and 7. Line 4800 determines if a Starbase is to be present in the quadrant or not. The value assigned to B is first set to zero to clear up any residue from the previous quadrant. It is good practice to set a variable to a known state at the beginning of a procedure rather than rely on it being cleared at the end of the last time the procedure was performed. A random number between 0 and 0.9999 is then generated. If it is less than 0.02 a starbase is placed in the quadrant, by setting the value of B to 10. The total count of Bases in the galaxy (B9) is then incremented, keeping track of the number of starbases that have been set up. The Klingon sequence begins at line 4810. The number of Klingons in the quadrant is reset to 0 (just like the value of B in the previous line). An other random value is then obtained and if it is greater than 0.06 (a six percent probability), the Klingon sequence is bypassed completely as the program flow branches forward to the next line. There is thus only a six percent probability that at least one Klingon will be present in any quadrant. The statements used to set up the Klingons in the quadrant in line 4810 follow an IF statement. The IF/THEN statement can be used in a number of ways. The test in line 4810 uses IF RND(Z)<.06 THEN yet it was stated in the previous paragraph that the Klingon sequence is bypassed if the random number is greater than 0.06. The two statements say the same thing but in different ways. This construction demonstrates an important difference between the various dialects of BASIC. In the Microsoft dialect if an IF test is performed and it fails, the program flow continues with the next line number in sequence. In other dialects, (such as Northstar BASIC) the program flow may continue with the statement immediately following the IF test in the same line. In Microsoft BASIC, if the IF test fails, the program flow continues on the following line and all statements after the THEN are not performed. In Northstar BASIC the K=INT(RND(Z)*8)*100 is not performed when the test fails, but everything following the ':' is. This difference has to be carefully investigated when Copyright (c) Joe Kasser 1989 Chapter 4 page 14 STARTREK THE COMPUTER PROGRAM By Joe Kasser converting the program to different dialects of BASIC. If line 4810 is changed from 4810 K=0 : IF RND(Z)<.06 THEN K=INT(RND(Z)*8)*100 : K9=K9+K : K8=K9 to 4810 K=0 : IF RND(Z)>.06 THEN 4830 4820 K=INT(RND(Z)*8)*100 : K9=K9+K : K8=K9 4830 Continue here both dialects of BASIC will perform in the same manner because there are no further instructions on line 4810 after the 'THEN'. Another way of doing the job is to use the 'IF/THEN-ELSE' construction. Line 4810 could be written as 4810 K=0:IF RND(Z)>.06 THEN 4830 ELSE K=INT(RND(Z)*8)*100:K9=K9+K:K8=K9 which is also unambiguous. The line now states that if the test is good (ie. RND(Z) is greater than .06) then continue at line 4830, ELSE if it is less than 0.06 set up some Klingons. Northstar BASIC implies the ELSE function by continuing following the : on the same line. This discussion on the IF/THEN statement has been included herein since not all versions of BASIC contain the ELSE capability. The last section of line 4810 sets up the number of Klingons in the quadrant. K is used as a temporary variable containing the exact number of enemy ships in the quadrant. The operation could have been written in a number of lines as 4811 K = RND(Z) : REM Generate a random number (0 to .999) 4812 K = K * 8 : REM convert it to between 0 and 7.99 4813 K = INT(K) : REM now turn it into an integer between 0 and 7 4814 K = K * 100 : REM and multiply it by 100 4815 K9 = K9 + K : REM sum total number of Klingons in the game 4816 K8 = K9 : REM set up the parameter representing the number of Klingons at the start of the game. Line 4810 has thus set up the number of Klingons in the quadrant, and also adjusted the total number for the galaxy. This technique for setting up the enemy ships has one previously unmentioned characteristic. By first deciding if enemy ships are to be placed in a quadrant, and then positioning them there, they will tend to show up in bunches. It is felt that this is not so unreasonable, because it is quite logical for raiding ships to travel in fleets particularly when they know that the Federation (the player) has a slight superiority in weapons technology. The subroutine exits in line 4830. The Initialisation subroutines themselves called a number of other subroutines. These "nested" subroutines are shown in figure 4.5 The subroutine called by the initialisation routine to return two random co-ordinates begins at line 50. The most Copyright (c) Joe Kasser 1989 Chapter 4 page 15 STARTREK THE COMPUTER PROGRAM By Joe Kasser commonly used subroutines are placed into the program with low numbers to speed up the execution time. There are many instances in the program, especially during the initialisation phase where two integer random values are required. They can for example be used to set up co-ordinates of objects. Line 50 contains a REMark to that effect while line 60 does the work. The same procedure is used twice. the first time returns a value for X, the second time returns a value for Y. X and Y are used as temporary variables for the purpose of communicating between the calling program and the subroutine. The code for the procedure is X = INT(RND(Z)*8) which is the joining of the following sequences X = RND(Z) : X = X * 8 : X = INT(X). It is more convenient however to write the operation in one statement. The subroutine starting in line 90 sets up the Enterprise on board parameters. Line 90 contains the REMark comment identifying the task carried out by the subroutine, and the action takes place in line 100. The amount of on-board energy (E) is set to the value represented by the parameter E0 (4000 units as per line 4510). The energy in the shields represented by the variable E1 is set to zero and the number of Photon Torpedoes, represented by the variable P is set to 10. The last statement in the line is the RETURN statement which closes the subroutine. This subroutine is also used in the docking operation described later on. Three other subroutines were called during the initialisation process. The first two set up the contents of the quadrant and the condition of the Enterprise. For now they are "dummied up " by lines 3200, 3340, 3400 and 3540. The last one begins at line 3550 with the usual REMark statement that describes the function of the subroutine. Line 3560 then uses a FOR/NEXT loop to set the stat of all the on-board damageable sub- systems (represented by the contents of the D(I) array) to zero. C1 defines the number of commands, C2 is the number that are not damageable, so the total number that is damageable is C1-C2. These lines insure that if the program is run before the subroutines are inserted, the main sequence will work, but the functions performed by the subroutines will not be carried out. As long as we remember to complete the subroutines before we try to use any data they are supposed to set up, everything will be fine. Now add the lines of program listed in figures 4.4 and 4.5 to the lines of figure 4.2 that you entered earlier. Change the date in line 10 if the date has changed. Carefully check each line you have typed in against the listings, and save the program. You may compare parts of the program by using the LIST instruction to list part of the program (to stop the earlier lines from scrolling uo the screen out of sight). Try typeing 'LIST 10-4500' instead of 'LIST' and see what happens. In the event you get an error message try 'LIST 10,4500' because some Copyright (c) Joe Kasser 1989 Chapter 4 page 16 STARTREK THE COMPUTER PROGRAM By Joe Kasser dialects of BASIC need a comma instead of a hyphen (dash). Once all is well, save the program and read on. 4.7 The Play the Game Module Having laid out the structure of the program and initialised the galaxy we can now begin to do things in it. This is represented by the PLAY.THE.GAME module first outlined in Chapter 3. Consider the flowchart for the module in detail as shown in figure 4.6. The module begins by checking to determine if the last command was a 'MOV' command. If it was, it is assumed that the Enterprise has moved into a new sector, and a Short Range Sensor display is to be shown. The state of the Short Range Sensors is then checked and if they are working, a Short Range Sensor scan is performed. If the sensors are damaged, a Visual scan is performed instead. The Command dialog function now takes place. The player is prompted to input a command, and the computer waits for the reply. The reply is then tested for validity. If it is less than three characters, the player is prompted with a message that signifyies that three characters are to be input to the computer. If the reply is valid it, is carried out by the PERFORM.IT module. If the command is not valid, then the player is prompted with a list of all the valid commands, and a minimal amount of information what they do by the DISPLAY.COMMAND.PROMPT function. At this stage the command has been carried out, and a test is next performed to determine if the game is over. If it isn't, and if the last command carried out by the player was not Short Range Sensor, Visual or Klingon Status. and there are Klingons in the quadrant, the klingons in the sector are moved by the MOVE.KLINGONS module and allowed to take hostile action at the Enterprise in the BACKFIRE module. The loop continues as long as the game is not over. The BASIC language implementation of the flow chart could take the form shown in figure 4.7. The routine begins at line 3000 with the REMark or comment statement. Line 3020 tests the value of the "last command "flag (C9) to see if the last command was a 'MOV' command. If it was, the program flow continues along the line, it if wasn't, the program picks up at the next line number in sequence (line 3040) in accordance with the rules of the IF/THEN statement. If the test passes, the value of I is set to 1 by the I=Z statement. The IF D(Z)=0 THEN statement tests the state of the Short Range Sensors. How it does so will be discussed in Chapter 5 as part of the discussion on the Damage Control function. The D(I) array contains the state of repair of each of the Enterprise on-board systems. If the test passes, that is, the value of D(1) Copyright (c) Joe Kasser 1989 Chapter 4 page 17 STARTREK THE COMPUTER PROGRAM By Joe Kasser is zero, then the program invokes the Short Range Sensor subroutine which begins at line 400. If the test fails, namely, the Short Range Sensors are damaged, the ELSE statement sets the value of I to 11 and the Visual Command subroutine beginning at line 2500 is invoked. The value of I is set to 11, so that a subroutine called by the Visual routine can be invoked correctly. This nested subroutine will be discussed later. Line 3040 contains a PRINT statement. A PRINT statement without anything following it causes the terminal cursor to move down to the beginning of the following line. It is equivalent to a carriage return and line feed combination. The player is prompted for an input in line 3050. The 'INPUT' statement is used by BASIC to accept an input from the console. When it is encountered, BASIC will wait, and accept all characters entered at the keyboard until the 'ENTER' or 'RETURN' key is depressed. It will then process the data that has just been entered. Anything input to the program has to be stored in a variable. The INPUT statement is always followed by a variable. A plain INPUT statement will cause a question mark to be displayed at the console. If something in quotation marks such as "COMMAND " is placed immediately after the word 'INPUT', the computer will display that something before the question mark. Thus INPUT "COMMAND " will cause the computer to prompt the player with COMMAND ?. Notice that in line 3050 there is a space inside the quotation marks after the word COMMAND. BASIC will print the question mark immediately after whatever is inside the quotes, thus if we wanted the prompt to be COMMAND? we would not put the space inside the quotes, but as we do want the prompt to be 'COMMAND ?', we have placed the space inside the pair of quotation marks. The INPUT statement is separated from the variable (A$ in this case) by the semi colon. The characters are entered into the computer and the entry sequence is terminated by the ENTER or the RETURN key. This last action tells the computer that the player is done entering the data, and the computer may start to process the input. The response to the INPUT statement may be numeric or a string. In this case it is a string, and the player input is stored in the string variable A$. The next part of the line tests A$ to see if it is less than 3 characters by using the LEN(A$) function. The LEN(A$) function returns the length of the string. If it less than 3 characters long, the program displays an error message on the console as 'BEG PARDON CAPTAIN (3 letters please)' which not only tells the player that a mistake has been made, but also hints at for the correct reply. The GOTO 3050 instruction at the end of the line makes the program go back to the beginning Copyright (c) Joe Kasser 1989 Chapter 4 page 18 STARTREK THE COMPUTER PROGRAM By Joe Kasser of the line and ask the player to have another try. If the length of A$ is 3 or more characters, the program continues at line 3060 which contains the loop that validates the command entry. There are a number of different techniques that can be used to implement the command matching function, so let us now consider some of them. The possible commands are as follows. NAV WARP ENGINES SRS SHORT RANGE SENSORS LRS LONG RANGE SENSORS PHA PHASERS TOR PHOTON TORPEDOES COM COMPUTER SHE SHIELDS LRP LONG RANGE PROBES TRA TRANSPORTER SHU SHUTTLECRAFT DAM DAMAGE CONTROL VIS VISUAL RES RESIGN SAV SAVE THE STATE OF THE GAME LSG LOAD A SAVED GAME We will also allow the player to directly access three computer commands directly from the main command level. These three commands are as follows MAP DISPLAY A MAP OF THE GALAXY KST SHOW THE KLINGON STATUS SCA SCAN A QUADRANT The simplest technique of finding out which command was entered by the player, and now residing in the string variale called A$, is to try and match them all in turn using something like the following sequence. 3081 IF A$ = "NAV" THEN 1300 3082 IF A$ = "SRS" THEN 400 3083 IF A$ = "LRS" THEN 300 3084 IF A$ = "PHA" THEN 800 3085 IF A$ = "TOR" THEN 1000 3086 IF A$ = "COM" THEN 1900 3087 IF A$ = "SHE" THEN 2700 ETC. That set of instructions tests the contents of A$ and tries in turn to match it with the various allowable commands. This technique is perfectly valid. It does however require the use of a lot of 'matching' instructions. Also, a match in any of the lines will cause a branch to the section of the program that performs the actual command. The command sequence must then end Copyright (c) Joe Kasser 1989 Chapter 4 page 19 STARTREK THE COMPUTER PROGRAM By Joe Kasser with another 'GOTO' instruction which will take the program flow back to the main sequence. A modification of the technique which allows the command modules to be subroutines and just end with a 'RETURN' statement is to use the 'GOSUB' statement as follows. 3081 IF A$ = "NAV" THEN GOSUB 1300 3082 IF A$ = "SRS" THEN GOSUB 400 3083 IF A$ = "LRS" THEN GOSUB 300 3084 IF A$ = "PHA" THEN GOSUB 800 3085 IF A$ = "TOR" THEN GOSUB 1000 3086 IF A$ = "COM" THEN GOSUB 1900 3087 IF A$ = "SHE" THEN GOSUB 2700 ETC. Using this approach, as long as the command modules don't change the contents of A$, or at least don't set them to a value equal to an other command, the only module that will be performed is the desired one. One exception however exists. Since we want a Shthe command modules don't change the contents of A$, or at least don't set them to a value equal to an other command, the only module that will be performed is the desired one. One exception however exists. Since we want a Shce will perform the SRS command immediately after performing the "MOV" because the test for 'MOV' is in line 3081 while the test for 'SRS' follows it in line 3082. (It still doesn't automatically switch to 'VIS' in the event that the Short Range Sensors are damaged, but that can be taken care of using a statement such as IF D(Z) = 0 THEN A$ = "SRS" ELSE A$ = "VIS" somewhat similar to the statemnt in line 3020 already discussed. If this test technique is to be used, a better version is to bypass any undesired lines using a 'GOTO' statement. Here the test on A$ is again performed sequentially for each command, and, when a match has been found and the command carried (using the 'GOSUB/RETURN' structure), the 'GOTO' then bypasses the rest of the tests. This modification also speeds up the operation of the program, since once the match is found, further un-necessary matching tests are not carried out. The 'GOTO' statement would be added in the following manner 3081 IF A$ = "NAV" THEN GOSUB 1300 : GOTO 3090 3082 IF A$ = "SRS" THEN GOSUB 400 : GOTO 3090 3083 IF A$ = "LRS" THEN GOSUB 300 : GOTO 3090 3084 IF A$ = "PHA" THEN GOSUB 800 : GOTO 3090 3085 IF A$ = "TOR" THEN GOSUB 1000 : GOTO 3090 3086 IF A$ = "COM" THEN GOSUB 1900 : GOTO 3090 3087 IF A$ = "SHE" THEN GOSUB 2700 : GOTO 3090 ETC. The FOR/NEXT loop in lines 3060 and 3070 tries to match the command entered by the player with the ones stored in memory. An aother technique which seems more complicated at first but is really simpler uses the 'ON' statement. Before we look at the 'ON' statemnt, let consider how it can be used. Each of the three letter commands was stored in a string (C1$) back in line 4610 by the statement Copyright (c) Joe Kasser 1989 Chapter 4 page 20 STARTREK THE COMPUTER PROGRAM By Joe Kasser 4610 C1$="NAVSRSLRSPHATORCOMSHELRPTRASHUDAMVISRESSAVLSGMAPKSTSCA" Since each of the commands are three letters long, the length of the string is equal to the number of possible choices available to the player multiplied by three. Since the player counts from 1 to the length of the string, and the computer counts from zero, the loop has to be equal to the number of commands minus one. That is done in the FOR I=0 TO LEN(C1$)/3-Z part of the line which takes the length of the string, converts it to the number of commands and subtracts one. The next part of the line proceeds to test each group of three characters to see if they match the command that was input by the player. BASIC contains a means of extracting certain characters from a string. For any string such as C1$ all or part of it may be extracted and processed. The LEFT$(C1$,L),RIGHT$(C1$,L) and MID$(C1$,N,L) instructions can be used to perform thes operations. The LEFT$(C1$,L) function extracts the first L characters from C1$. Thus for example LEFT$(C1$,3) is equal to 'NAV'. The MID$(C1$,N,L) also extracts L characters from C1$, but starts N characters along the string, so for example, MID$(C1$,6,3) would be 'LRS'. The RIGHT$(C1$,L) function extracts the last L characters from the string. Thus RIGHT$(C1$,3) is 'SCA'. these three instructions allow the programmer to manipulate strings at will. Consider for example the statements L=3 : A$ = LEFT$(C1$,L) + RIGHT$(C1$,L) When it is performed, A$ will end up as 'NAVSCA'. To demonstrate the LEFT$,RIGHT$ and MID$ functions consider the following short program. However before you play with these routines, save the game program and clear the memory using the 'NEW' command. Consider the following short program. 10 A$ = "STARTREK By Joe Kasser" 20 FOR I = 1 TO LEN(A$) 30 PRINT LEFT$(A$,I) 40 NEXT Line 10 defines the contents of A$. Line 20 sets up a loop to display something. The limits of the loop are 1 and the number of characters in A$. Remember that the 'LEN(A$)' function returns the number of characters in A$. Line 30 does the job of displaying the contents of A$, while line 40 terminates the loop. Type this program into your computer and run it. You should see the following on your screen. Copyright (c) Joe Kasser 1989 Chapter 4 page 21 STARTREK THE COMPUTER PROGRAM By Joe Kasser S ST STA STAR START STARTR STARTRE STARTREK STARTREK STARTREK B STARTREK By STARTREK By STARTREK By J STARTREK By Jo STARTREK By Joe STARTREK By Joe STARTREK By Joe K STARTREK By Joe Ka STARTREK By Joe Kas STARTREK By Joe Kass STARTREK By Joe Kasse STARTREK By Joe Kasser OK If you now change line 30 to 30 PRINT RIGHT$(A$,I) we will see the effect of the 'RIGHT($) function. RUN the program and you should get the following. r er ser sser asser Kasser Kasser e Kasser oe Kasser Joe Kasser Joe Kasser y Joe Kasser By Joe Kasser By Joe Kasser K By Joe Kasser EK By Joe Kasser REK By Joe Kasser TREK By Joe Kasser RTREK By Joe Kasser ARTREK By Joe Kasser TARTREK By Joe Kasser STARTREK By Joe Kasser If you now change line 30 to 30 PRINT MID$(A$,I,1) Copyright (c) Joe Kasser 1989 Chapter 4 page 22 STARTREK THE COMPUTER PROGRAM By Joe Kasser and RUN this version, you will see the effect of the 'MID$(A$,N,L) function. You should see S T A R T R E K B y J o e K a s s e r Change the 1 in line 30 to pick out a different number of characters and play with these routines. When you have finished, clear the memory by typing in the word 'NEW' and read on. In order to match the player's input stored in A$ with the commands stored in C1$, the MID$(C1,N,L) function is used in line 3060. The statement takes the form IF A$=MID$(C1$,(I*3)+Z,3) THEN 3080 which causes the program to branch out of the loop (lines 3060/3070) to line 3080 when a match is found. In order to understand how it works, consider the contents of MID$(C1$,(I*3)+Z,3) for different values of I as follows I (I*3)+Z (I*3)+Z,3) MID$(C1$,(I*3)+Z,3) ------------------------------------------------- 0 1 1,3 NAV 1 4 4,3 SRS 2 7 7,3 LRS and so on. The statement will thus sequentially and systematically try to match the players's reply with each three letter group in turn. If a match is not found by the time it tests the last group, the loop terminates in the natural manner, and the program flow continues along line 3070 after the NEXT statement with a further Copyright (c) Joe Kasser 1989 Chapter 4 page 23 STARTREK THE COMPUTER PROGRAM By Joe Kasser loop that displays the choice of commands available to the player. In this case the loop limits are 0 and C1. For each value of I, the three letter group for the caommand choice is displayed using the MID$(C1,N,L) function just as before, that is PRINT MID$(C1$,(I*3)+Z,3); The cursor is then positioned further along the line and the command function stored in the command string array D$(X) is displayed by the TAB(8);D$(I) part of the statement: The NEXT statement terminates the loop, then C9 is set to 99 so as not to confuse line 3020 and the GOTO 3000 instruction causes the computer to re-issue the prompt message. The semi colon and TAB(N) that were used in the instruction line are display formatting statements. BASIC has a number of different ways of controlling the format of a display. The display resulting when line 3070 is performed looks like NAV WARP ENGINES SRS SHORT RANGE SENSORS LRS LONG RANGE SENSORS PHA PHASERS TOR PHOTON TORPEDOES COM COMPUTER SHE SHIELDS LRP LONG RANGE PROBES TRA TRANSPORTER SHU SHUTTLECRAFT DAM DAMAGE CONTROL VIS VISUAL RES RESIGN SAV SAVE THE STATE OF THE GAME LSG LOAD A SAVED GAME When the program gets to line 3080, the value of I is equal to the number of tests that have been performed before the match was found. At this time we could perform a sequence of IF I = X THEN statements such as the following. 3081 IF I = 0 THEN GOSUB 1300 : GOTO 3090 3082 IF I = 1 THEN GOSUB 400 : GOTO 3090 3083 IF I = 2 THEN GOSUB 300 : GOTO 3090 3084 IF I = 3 THEN GOSUB 800 : GOTO 3090 3085 IF I = 4 THEN GOSUB 1000 : GOTO 3090 3086 IF I = 5 THEN GOSUB 1900 : GOTO 3090 3087 IF I = 6 THEN GOSUB 2700 : GOTO 3090 ETC. If we are going to use this technique, there was no point in going through the rigmarole to convert the contents of A$ to a number in I, we could have just performed the tests as decribed above. The reason for converting the contents of A$ to a number in I is so that we can use the 'ON' statement which is a sequential test statement built into BASIC. It is used in two Copyright (c) Joe Kasser 1989 Chapter 4 page 24 STARTREK THE COMPUTER PROGRAM By Joe Kasser ways, either as ON I GOT0 11,21,31,41 or ON I GOSUB 11,21,31,41. Here BASIC tests the value of I and if I=1, branches to line 11, if I=2, branches to line 21, if I=3, branches to line 31, etc. The 'GOTO' or 'GOSUB' variations determine if the branch is a direct 'JUMP' or a subroutine call. In the event that it was a subroutine call, when the 'RETURN' statement is encountered, the program picks up at the instruction following the 'ON' statement. Line 3080 first sets the "last command flag (C9) to the value of I and then uses the 'ON' statment to proceed to the desired command subroutine. Snce the 'ON' statement counts from 1 and the value of I statrs from 0, the test is performed on 'I+Z' (or I+1). When the command has been carried out, the relevant 'RETURN' statment is encountered which brings the program flow back to line 3090 which tests to see if the 'End of Game' flag (F9) has been set. If it has, the program jumps forward to line 9000 and goes into the end game sequence. If the game isn't over a PRINT instruction is performed. The value of C9 is then tested. If it was 1, ie a Short Range Sensor Scan was the last command carried out, the command loop jumps forward to line 3110. If not, it continues at the next line which is line number 3100. Line 3100 moves the Klingons using the subroutine starting at line 700 and then allows them to fire at the Enterprise using the BACKFIRE subroutine starting at line 600 if any Klingons are present in the quadrant. Note that if a Short Range Sensor Scan was the last command carried out, this line is bypassed so that the enemy don't get to move and shoot back. There would be no point in doing a Short Range Sensor scan to see where the enemy are (in the static game), if they are allowed to move before the player can take any action. The last part of line 3100 tests the state of the "Game Over" flag (F9) to see if the player has wiped out all the enemy or if the enemy has destroyed the Enterprise. If either of those conditions are present, the value of F9 is greater than zero and the program branches forward to the end game sequence starting at line line 9000. Line 3110 causes the command loop perform another iteration by virtue of the 'GOTO 3000' statement. The subroutines of lines 600 and 700 have not yet been developed, hence the 'dummy' entries of lines 600,680,700 and 740 to make the program playable. Add the program statements in figure 4.7 to your version of the game, change line 10 to reflect the date. Carefully verify that there are no typographical errors because some of the statements are pretty complicated, and save the program before reading on. Now type in the word RUN followed by a carriage return, and the program should run. You should see the screen clear and Copyright (c) Joe Kasser 1989 Chapter 4 page 25 STARTREK THE COMPUTER PROGRAM By Joe Kasser after some seconds the messages from lines 10/40 appear on the screen. Then a few seconds later, the messages from lines 4530/4560 will follow them and be displayed. Finally you should see FUNCTION NOT AVAILABLE COMMAND ? Anytime you enter a valid command, you should get FUNCTION NOT AVAILABLE as a response. The exception to that rule is if you enter the 'MOV' command. You should then see it appear twice. The dual response is due to the fact that the 'MOV' command function does not yet exist. That gives you the first one. The second one is due to the fact that a 'SRS' is automatically invoked after a 'MOV' (see line 3020). The 'SRS' function will also return the 'missing' function message. If you enter less than three characters as a response to the command prompt, you should get the error message from line 3050. If you enter three or more characters which don't make up a valid command, you should be prompted with the "help" information, ie. the whole list as follows NAV WARP ENGINES SRS SHORT RANGE SENSORS LRS LONG RANGE SENSORS PHA PHASERS TOR PHOTON TORPEDOES COM COMPUTER SHE SHIELDS LRP LONG RANGE PROBES TRA TRANSPORTER SHU SHUTTLECRAFT DAM DAMAGE CONTROL VIS VISUAL RES RESIGN SAV SAVE THE STATE OF THE GAME LSG LOAD A SAVED GAME After you have corrected any errors that may have shown up, you will find that you cannot stop the program. Break the program flow by holding down the "CNTRL" (control) key and then touching the 'C' key at the same time. This combination known as Control C (^C), will stop the program. Save the corrected program. As an aid to ensuring that you have picked up all the relevant lines of code, your program listing should appear identical to that shown in figure 4.8 except for any changes you have had to make to customise the program for your computer. Did you notice that you were instructed to save the program before and after the debugging session ? That was because there is the remote possibility that you may blow the program during the session for one reason or another. In that event, you can always reload the saved version and continue from there. That beats having to re-enter lines of code that were added before the session and then lost. Copyright (c) Joe Kasser 1989