home *** CD-ROM | disk | FTP | other *** search
/ World of Ham Radio 1994 January / AMSOFT_1994.iso / misc / zczbasic / prog / chapter.004 < prev    next >
Encoding:
Text File  |  1993-12-31  |  78.4 KB  |  1,651 lines

  1.         Chapter 4 page 1 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  2.  
  3.  
  4.                                     CHAPTER 4 
  5.  
  6.              Now  that the top and first intermediate level  flow  charts 
  7.         have been defined they have to be broken down into the low  level 
  8.         charts  and  then finally converted into a program.   This  would 
  9.         normally be performed in two distinct steps.  The first is to lay 
  10.         out the flow chart completely and only then implement the  second 
  11.         step  which  is the writing of the code.  In this case,  to  keep 
  12.         track  of  what is going on, and to avoid the need  for  constant 
  13.         references between chapters, the flow chart will be discussed the 
  14.         code  will  be  immediately written  and  tested.   A  comparison 
  15.         between  the flow chart and the code can then be  made  directly.  
  16.         It provides for an easier way for the beginner to relate the  two 
  17.         together.
  18.  
  19.              Each of the modules will be tested on a "stand alone"  basis 
  20.         (that  is, by itself) and the whole program slowly built up  from 
  21.         the  bottom  within the framework defined by the top  level  flow 
  22.         chart.   If you thus enter the code into your computer as we  go, 
  23.         you  will  slowly build up the game command function  by  command 
  24.         function and will be able to debug any errors with relative ease.
  25.  
  26.              BASIC  executes instructions in sequence in  ascending  line 
  27.         number order.  There is also no requirement for the numbers to be 
  28.         contiguous.   In  fact it is advantageous to  leave  some  spares 
  29.         between the various lines so that additions or changes can  later 
  30.         be  made  to  the  program without affecting  the  whole  of  the 
  31.         program.    Most dialects of BASIC however include a  renumbering 
  32.         function which can be used to free up space in the event that  no 
  33.         space exists to enter a new instruction.   The renumber function, 
  34.         however   changes  everything, and will  make  the  documentation 
  35.         invalid  if  the text is written about a version which  is  later 
  36.         renumbered.   
  37.  
  38.  
  39.         4.1  Line Numbering Convention
  40.  
  41.              The convention will be established that program lines ending 
  42.         in a 0 are program lines to be used in the final game.  Any lines 
  43.         ending  with any other number are temporary lines used  for  some 
  44.         intermediate reason explained at the time of use.
  45.  
  46.         4.2  Allocating Space for the Program
  47.  
  48.              A  guess is now made as to how much space (how  many  lines) 
  49.         will  be  needed to write the various parts of  the  program  and 
  50.         space is allocated accordingly.   The main outline of the program 
  51.         showing  which routines begin at what lines is thus as  shown  in 
  52.         figure 4.1.
  53.  
  54.              The   program   starts  with  the  lowest   numbered   line.   
  55.         Conventionally  lines  start at number 10 and  proceed  in  10's.  
  56.         thus the program lines will be numbered as 10, 20, 30 and so  on.  
  57.         This  allows  you  to add extra lines if you  forget  one.   This 
  58.         convention grew up before BASIC interpreters recognised the RENUM 
  59.  
  60.  
  61.         Copyright (c) Joe Kasser 1989
  62.  
  63.  
  64.  
  65.  
  66.  
  67.         Chapter 4 page 2 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  68.  
  69.  
  70.         (Renumber)  console command.   Note that a console command  is  a 
  71.         command  used  by the programmer during the program  writing  and 
  72.         debugging  phases  and may not execute when put  inside  a  line.   
  73.         The  RENUM command renumbers all line numbers and  references  to 
  74.         line numbers.  Thus a short program of the form
  75.  
  76.         1   REM  THIS IS LINE 1
  77.         2   REM  THIS IS LINE 2
  78.         3   REM  THIS IS LINE 3
  79.  
  80.         after being subjected to the RENUM command would be displayed as
  81.  
  82.         10   REM  THIS IS LINE 1
  83.         20   REM  THIS IS LINE 2
  84.         30   REM  THIS IS LINE 3
  85.  
  86.         when  the  LIST  statement is invoked.    LIST  causes  BASIC  to 
  87.         display  a listing of the program statements at the  console.   A 
  88.         variation  LLIST causes the statements to be printed out  by  the 
  89.         printer attached to the computer.
  90.  
  91.              When  typing  things into the computer you must  always  end 
  92.         with a "carriage return" character to tell the computer that  you 
  93.         have finished.  The computer will wait patiently for you to enter 
  94.         your  input, then when you touch the carriage return key on  your 
  95.         keyboard  it knows that you have finished and begins  to  process 
  96.         your input.   From now on, we will assume that you remember about 
  97.         the carriage return key.
  98.  
  99.              Copy  lines 1, 2 and 3 into the computer, and then type  the 
  100.         word  LIST  followed  by a carriage return.  Now  type  in  RENUM 
  101.         (carriage return) and after the computer has said "OK" again type 
  102.         in  LIST and examine the result.   Now type in NEW to  clear  the 
  103.         memory.   Some dialects of BASIC don't recognise the word  "NEW", 
  104.         and use something else such as "SCR" (SCRatch).  If your computer 
  105.         des  not recognise the word NEW, look up the equivalent  word  in 
  106.         the instruction manual.
  107.  
  108.              Before  you start entering lines of code into  the  computer 
  109.         first  familiarise  yourself with the SAVE and LOAD  commands  of 
  110.         BASIC.  The SAVE command allows you to save what you have entered 
  111.         on disk or cassette.  The LOAD command allows you to retreive the 
  112.         program.  Read the computer manual to determine exactly how these 
  113.         two  commands  work  and then practice  them  frequently  as  you 
  114.         proceed  to enter lines.  Never walk away from the  computer  for 
  115.         any  reason without performing a SAVE operation.  You  will  feel 
  116.         more  than  upset if the power fails or your two year  old  child 
  117.         kills  the power  or presses the reset switch on the machine  the 
  118.         instant you walk away from it after hours of working on it.
  119.  
  120.              The  commands usually take the form SAVE  "PROGRAM.NAME"  or 
  121.         LOAD  "PROGRAM.NAME"  where  the  program  name  is  included  in 
  122.         quotation marks.  Variations exist which determine the format  of 
  123.         the  SAVE/LOAD  operation.   Read  the  instruction  manual   and 
  124.         practice  them, even if you don't understand them at  this  time.  
  125.  
  126.  
  127.         Copyright (c) Joe Kasser 1989
  128.  
  129.  
  130.  
  131.  
  132.  
  133.         Chapter 4 page 3 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  134.  
  135.  
  136.         If you can them work for you , that will be enough for now.   The 
  137.         rest of the knowledge will come.
  138.  
  139.              We are now ready to begin entering the program sections into 
  140.         the computer.  First consider the framework for the BASIC program 
  141.         as shown in figure 4.2.
  142.  
  143.              The program starts with a REMark or comment telling us  what 
  144.         version  it is.  The name of the program is given as well as  the 
  145.         version.   It  is good practice to date each version of  the  the 
  146.         program.   When you eventually type up the program,  replace  the 
  147.         XX-YY-ZZ in line 10 by the date that you do the typing.    Later, 
  148.         when  you  read further into the book, each time you add  to  the 
  149.         program,  change  the date in line 10 to show the date  that  you 
  150.         updated  the program.  In this manner you will always know  which 
  151.         version  of  the  program you are working with when  you  have  a 
  152.         number of different versions or "back ups".
  153.  
  154.         Each  function  then begins at its assigned line  number  with  a 
  155.         REMark or comment statement identifying what is happenning.   The 
  156.         subsequent  line is a GOTO statement that takes the program  down 
  157.         to  line 10000.   The PRINT statement in line 10001 displays  the 
  158.         message "FUNCTION NOT PROGRAMMED AT THIS TIME" at the console.
  159.  
  160.              The GOTO statement causes the program to goto a defined line 
  161.         number.   Thus  lines can be bypassed if required.   It  will  be 
  162.         discussed at length later.
  163.  
  164.              The  End  (of the) game sequence begins at line  9000.   The 
  165.         'END'  statement  in line 9010 tells BASIC that the  program  has 
  166.         finished and to stop performing the program.  In many dialects of 
  167.         BASIC,  if  the last line of the program is the last line  to  be 
  168.         executed,  there is no need to use the 'END' statement.   If  the 
  169.         last line is not the actual last line, as in this case, the 'END' 
  170.         statement is required.
  171.  
  172.              The PRINT statement is BASIC's way of getting information to 
  173.         the  console.   You  can PRINT a variable  or  a  constant.   The 
  174.         statement
  175.                   PRINT "FUNCTION NOT PROGRAMMED AT THIS TIME" 
  176.         in line 10001 will print a String constant.  This String constant 
  177.         is an ASCII string.  The PRINT statement appears in many forms as 
  178.         you will see later.
  179.  
  180.              Line  10001  ends  with  a  RETURN  statment.   The   RETURN 
  181.         statments  cause the program to return from a subroutine  to  the 
  182.         main  flow of the program.   We have not yet seen any  subroutine 
  183.         calls because each command function is in itself a subroutine and 
  184.         is called by the main command loop as discussed later.
  185.  
  186.              Now  copy  the program statements of figure  4.2  into  your 
  187.         computer, because having defined the structure of the program, we 
  188.         are  ready to begin to flesh it out.  SAVE what you have  entered 
  189.         and read on.
  190.  
  191.  
  192.  
  193.         Copyright (c) Joe Kasser 1989
  194.  
  195.  
  196.  
  197.  
  198.  
  199.         Chapter 4 page 4 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  200.  
  201.  
  202.         4.3  Initialising the Variables
  203.  
  204.              The  first  thing to do is to set up all the  variables  and 
  205.         flags used in the game, and set up the contents of the  quadrants 
  206.         in the gfalaxy. However, before they can be set up, we must  have 
  207.         some  idea  of what kind of information is used to  describe  the 
  208.         contents  of the quadrants and how and where that information  is 
  209.         stored.
  210.  
  211.              Each  quadrant  may contain different  quantities  of  three 
  212.         different items (Klingons, stars and starbases). These items have 
  213.         to  be  stored in some kind of matrix or array.    We  could  for 
  214.         example use three different arrays one for the Klingons, one  for 
  215.         the  stars  and  one for the location of  the  starbases.    This 
  216.         technique  would  use  up more memory than  somehow  putting  the 
  217.         contents of each quadrant in one array.  
  218.  
  219.              How can this be done, remembering that there can be numerous 
  220.         stars and Klingons in the Quadrant?  Well remember the format  in 
  221.         which the contents of the quadrant were displayed. The Long Range 
  222.         Sensor scan display for example was,
  223.  
  224.         LONG RANGE SENSORS FOR QUADRANT 4,6
  225.              001 012 104
  226.              002 206 317
  227.              717 001 003
  228.  
  229.         where  the  contents of each quadrant are described  as  a  three 
  230.         digit number with the following convention.
  231.  
  232.         The 100's digit     = number of Klingons 
  233.         the  10's digit     = number of Starbases
  234.         the   1's digit     = number of Stars,
  235.  
  236.              thus  for  example, 317 means that the quadrant  contains  3 
  237.         Klingons,  1  Starbase  and  7 stars.  If  the  contents  of  the 
  238.         quadrant  were stored in the array just as they  were  displayed, 
  239.         that is 
  240.  
  241.         the 100's digit     = number of Klingons
  242.         the  10's digit     = number of Starbases
  243.         the   1's digit     = number of Stars.
  244.  
  245.              As long as the number of stars or the number of Klingons  do 
  246.         not  each  exceed  nine, the value stored in the  array  will  be 
  247.         unambiguous.   The  Long Range Sensor and Map displays  can  then 
  248.         just display the value of the quadrant directly.
  249.  
  250.              Operating  on  the  contents of the  quadrant  then  becomes 
  251.         simple.   To  remove  a  Klingon, subtract  100  from  the  value 
  252.         assigned to the quadrant.  For example, for any quadrant in the Q 
  253.         or  Quadrant  array, which is located in the I th row  and  J  th 
  254.         column , and is referenced as Q(I,J), the BASIC statement
  255.                            LET  Q(I,J)  = Q(I,J) - 100 
  256.         will delete one Klingon from the quadrant.
  257.  
  258.  
  259.         Copyright (c) Joe Kasser 1989
  260.  
  261.  
  262.  
  263.  
  264.  
  265.         Chapter 4 page 5 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  266.  
  267.  
  268.  
  269.              Detection of the presence of the enemy is also simple.   For 
  270.         any quadrant, if there are Klingons present in it, it must have a 
  271.         value greater or equal to 100. Thus the statement
  272.                               IF Q(I,J) => 100 THEN
  273.         will detect the presence of the enemy.
  274.  
  275.              In  order to differentiate between quadrants that have  been 
  276.         scanned and those that have not, the unscanned ones can be  given 
  277.         a negative sign. Thus for example, if Q(I,J) = -317 the  contents 
  278.         of  the quadrant still show 3 Klingons, 1 starbase and  7  stars, 
  279.         but  the negative sign shows that the quadrant has not  yet  been 
  280.         scanned.    The  scanning  procedure is also  made  simple.   The 
  281.         program  does  not need to check if a quadrant has  been  scanned 
  282.         before, it just converts the value stored in the array associated 
  283.         with  the  quadrant to a positive number.    BASIC  contains  the 
  284.         ABS(X) function which converts any number to its positive  value.  
  285.         It  does not care what the starting value was, thus LET Q(I,J)  = 
  286.         ABS(Q(I,J))  will give the same result no matter if the  starting 
  287.         value  was  negative or positive.  For example, if  the  starting 
  288.         value of Q(I,J) was either -317 or 317 the result will always  be 
  289.         317.   Note  the  use  of double parentheses  since  the  ABS(  ) 
  290.         function requires them, as does the Q(I,J).  BASIC is fussy about 
  291.         their  use and will give you a SYNTAX ERROR message if you  don't 
  292.         ballance the parentheses across a line.
  293.  
  294.  
  295.         4.4  Storing the Quadrant Data
  296.  
  297.              The  galaxy consists of 64 quadrants arranged in eight  rows 
  298.         of eight columns.  Each quadrant contains at least one star,  and 
  299.         may  also  contain Klingons, and and possibly  a  starbase.   The 
  300.         contents  of the quadrants are stored in an array Q(I,J) where  I 
  301.         is  the row and J the column. I and J may have values  between  0 
  302.         and  7  (8  rows  or columns).  This array is  known  as  a  "Two 
  303.         Dimensional Array" because it has two attributes (I and J).
  304.  
  305.         4.5  Setting Up the Galaxy
  306.  
  307.              Having defined how and where the information describing  the 
  308.         contents  of each of the quadrants within the galaxy  are  stored 
  309.         (and some of the ramifications of the techniques used) we can now 
  310.         go on to set them up at the beginning of a game.    In accordance 
  311.         with  good programming practice the flow chart for the  operation 
  312.         is  first developed and then the language  statements  performing 
  313.         that operation written.
  314.  
  315.              The flow chart for setting up the galaxy is shown in  figure 
  316.         4.3  where  the  numbers in parantheses in  some  of  the  lines, 
  317.         correspond  to  the numbers of the lines in  the  BASIC  language 
  318.         program.
  319.  
  320.              The   procedure  begins  displaying  the  "start   of   game 
  321.         messages".   It  continues  by  setting  up  all  the  constants, 
  322.         variables  and  parameters that will be used in the  game.   Next 
  323.  
  324.  
  325.         Copyright (c) Joe Kasser 1989
  326.  
  327.  
  328.  
  329.  
  330.  
  331.         Chapter 4 page 6 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  332.  
  333.  
  334.         follows  a  loop that is executed once for each quadrant  in  the 
  335.         galaxy.  It is in fact a double loop, one inside the other.   The 
  336.         practice of placing one loop inside another is called  "nesting".  
  337.         The inside loop sets up all the columns in one row.  The  outside 
  338.         loop advances the row pointer so that the inside loop can set  up 
  339.         all  the  columns in each row in sequence.   For  each  quadrant, 
  340.         first  the  number  of stars is computed, by  choosing  a  random 
  341.         number  between  1 and 7.  A test is next performed to see  if  a 
  342.         starbase is present in the quadrant takes place.  This test has a 
  343.         very  low  probability  of  success  because  we  want  very  few 
  344.         starbases  in  the  game.  Since we don't want more  than  say  5 
  345.         starbases  in the galaxy, we could for example put a test in  the 
  346.         loop  to say that if 5 starbase have already been  positioned  in 
  347.         the galaxy, not to bother to set up any more.  This would however 
  348.         tend to position the bases in the lower number quadrants and  the 
  349.         player  would  soon notice that fact and use it.   The  technique 
  350.         chosen here is to set the probability of occurrence to be so low, 
  351.         that  the  probability  of having more than 5  starbases  in  the 
  352.         galaxy  is almost non existant.  In fact, the probability  is  so 
  353.         low,  that having zero Starbases is a real possibility and  so  a 
  354.         test has to be put into the flow chart later to determine if that 
  355.         condition is present after the loop terminates.  Then the  number 
  356.         of Klingons in the quadrant is computed.  Here a low  probability 
  357.         is used because although there are 64 quadrants in the galaxy, we 
  358.         don't  want more than about 30 Klingons in the whole  game.   The 
  359.         total number of Klingons is then updated because we wish to  keep 
  360.         track of how many there are.
  361.  
  362.              If  no  starbases  have been allocated,  a  random  quadrant 
  363.         within the galaxy is chosen and a starbase positioned within  it.   
  364.         The  total number of starbases is then set to 1.  The  number  of 
  365.         Klingons within the galaxy is then examined.  If more than 30 are 
  366.         present,  the number of stardates available to complete the  game 
  367.         is  made  equal to the number of Klingons, if less  than  30  are 
  368.         present, the time is set to 30 Stardates.  This allows the player 
  369.         adequate  time to search the galaxy for the enemy and also  gives 
  370.         extra time if the number of Klingons is large.
  371.  
  372.              Putting a Starbase into a quadrant containing Klingons is  a 
  373.         philosophical  point that has some effect on the  game  strategy.  
  374.         Exactly  what is the function of a starbase?  In this  game,  its 
  375.         purpose is to provide a source of supplies to the Enterprise.  It 
  376.         is  provided with defensive armament but no  offensive  armament.  
  377.         The Klingons can thus blockade a base to stop the player reaching 
  378.         it  and  refuelling.  Thus if the Enterprise tries to dock  at  a 
  379.         starbase when Klingons are present in the quadrant (and shooting) 
  380.         it  may take the base commander a short time to  synchronise  the 
  381.         shields  to  those of the Enterprise so that the  enterprise  can 
  382.         approach the base.  In the mean time the enemy may be shooting at 
  383.         the ship and cause damage or even destroy it.  On the other hand, 
  384.         once  docked and protected by the starbase's shields  the  player 
  385.         can  tap into the energy banks of the starbase and can blast  the 
  386.         Klingons from within the protection of the starbases's shields.
  387.  
  388.              The loops terminate when all the quadrants have been set up.  
  389.  
  390.  
  391.         Copyright (c) Joe Kasser 1989
  392.  
  393.  
  394.  
  395.  
  396.  
  397.         Chapter 4 page 7 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  398.  
  399.  
  400.         A further loop is then performed to ensure that a minimum  number 
  401.         of  Klingons are set up so as to ensure that the game is not  too 
  402.         easy.  This loop computes a random quadrant within the galaxy and 
  403.         tests  it to determine its contents.  If it only contains  stars, 
  404.         then  a  starbase or Klingons can be set up in it at  this  time.  
  405.         This  choice  of  random quadrants continues  until  the  minimum 
  406.         number  (20) of Klingons have been set up.  If no starbases  have 
  407.         been allocated, a random quadrant within the galaxy is chosen and 
  408.         a starbase positioned within it.   The total number of  starbases 
  409.         is then set to 1.
  410.  
  411.              The Enterprise is then placed in a random quadrant somewhere 
  412.         inside the galaxy.  The contents of the quadrant are then set  up 
  413.         and  the "condition" established to complete  the  initialisation 
  414.         process.
  415.  
  416.         4.6  Implementing the Flow Chart in BASIC
  417.  
  418.              Implementing  the  flow chart in BASIC could take  the  form 
  419.         shown in figure 4.4.  
  420.  
  421.              The  program begins at line 10 which has been changed.   The 
  422.         REMark  is changed to two PRINT statements.   CHR$(26)  causes  a 
  423.         control  character to be output to the CRT terminal to clear  the 
  424.         screen.  Some dialects of BASIC used in computers that have built 
  425.         in  screens  may use a special word such as CLRS to do  the  job.   
  426.         The  XX-YY-ZZ is replaced by an actual date.  The line ends  with 
  427.         an error trapping statement which will be discussed later.  
  428.  
  429.              The  colon (:) is used to separate two instructions  in  the 
  430.         same line.  There is no necessity for every instruction to have a 
  431.         separate  line  number.  There are times when it  is  logical  or 
  432.         convenient to have more than on instruction on any given line.  
  433.  
  434.              Line  20  displays  a little more of the  credits  and  then 
  435.         institutes the initialisation process by calling the  subroutines  
  436.         starting  at  lines 4500 and 4660 using the GOSUB  (line  number) 
  437.         statement.   A subroutine is a sequence of instructions  that  is 
  438.         performed  more than once in a program.  It is convenient to  use 
  439.         just  one  module of code to perform the operation.   This  saves 
  440.         memory  and  facilitates  debugging since  if  the  operation  is 
  441.         performed by one piece of code no matter when it takes place,  if 
  442.         the  piece of code is correct (ie. debuged), the  operation  will 
  443.         always be performed correctly.  Subroutines always terminate with 
  444.         a RETURN statement.
  445.  
  446.              Lines  30  and  40  display  further  introductory  messages 
  447.         telling  the player the scope of the mission.  The  PRINT  K9/100 
  448.         part displays the number of Klingons that are located within  the 
  449.         galaxy  but multiplied by 100: hence the K9/100  statement  which 
  450.         then displays the actual number.  The reason that K9 contains one 
  451.         hundred  times  the  number of Klingons in  the  galaxy  will  be 
  452.         discussed later.  
  453.  
  454.              The   semi  colon  (;)  character  is  a  print   formatting 
  455.  
  456.  
  457.         Copyright (c) Joe Kasser 1989
  458.  
  459.  
  460.  
  461.  
  462.  
  463.         Chapter 4 page 8 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  464.  
  465.  
  466.         statement.   BASIC  contains  a  number  of  different  ways   of 
  467.         displaying  messages at the console.  The two most commonly  used 
  468.         formatting   statements  are  the  comma  and  the  semi   colon.   
  469.         Consider the following statements
  470.  
  471.         11 A=3 : B=6 : C=6
  472.  
  473.         12 PRINT A,B,C 
  474.  
  475.         when this short program is run, the terminal would display
  476.  
  477.         3         6         9.
  478.  
  479.              If  on the other hand, the program was changed so that  line 
  480.         12 became
  481.  
  482.         12 PRINT A;B;C 
  483.  
  484.         when the new version is run, the terminal display would be 
  485.  
  486.         3 6 9.
  487.  
  488.              The  semi  colon makes BASIC display  the  data  immediately 
  489.         after  any previous piece of data.  The  comma causes the  cursor 
  490.         to  advance to the next built in tab position.  BASIC  will  also 
  491.         automatically  issue a carriage return line feed  sequence  after 
  492.         the  last data have been displayed.  That implies that a  'PRINT' 
  493.         statement  by  itselft will perform a carriage return  line  feed 
  494.         sequence,  or move the cursor down to the beggining of  the  next 
  495.         line on the CRT screen.
  496.  
  497.              The  initialisation  module is split into two  halves.   The 
  498.         first  section contained in lines 4500 to 4650 is performed  once 
  499.         per  session  while  the second part in lines  4660  to  4830  is 
  500.         performed  for every new game that is played.  The main  sequence 
  501.         is listed in figure 4.4
  502.  
  503.              Consider in detail the task that each line of the subroutine 
  504.         performs.   Line  4500 is the comment statement to  identify  the 
  505.         subroutine.  Line 4510 sets up Z as a parameter equal to 1  using 
  506.         the  implied  LET  statement.  Different dialects  of  BASIC  may 
  507.         require a different value when called by the RND or random number 
  508.         generation  function.   The parameter technique is thus  used  so 
  509.         that  it  can  be  readily converted  to  a  different  value  if 
  510.         necessary.  Microsoft BASIC needs Z to be assigned a value of  1. 
  511.         Note that Z is also generally used instead of 1 from now on.   If 
  512.         your  dialect of BASIC requires a different value for the  random 
  513.         number  generator, change Z when used in the RND(Z) to  what  you 
  514.         need.   Use  Z1  instead  of Z for  example,  and  then  add  the 
  515.         definition  for  Z1  in  line 4510.  When you  come  to  run  the 
  516.         program,  if you don't get random numbers when you  expect  them, 
  517.         then read the book that came with the computer to see what  value 
  518.         to  assign  to  Z1.   Since most  personal  computers  speak  the 
  519.         Microsoft  dialect  of BASIC the odds are that you will  have  no 
  520.         trouble using Z equal to 1.
  521.  
  522.  
  523.         Copyright (c) Joe Kasser 1989
  524.  
  525.  
  526.  
  527.  
  528.  
  529.         Chapter 4 page 9 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  530.  
  531.  
  532.  
  533.              A string variable S$ is then defined such that its  contents 
  534.         can  be  used  to  display the contents of  the  sectors  in  the 
  535.         quadrant.    It  will be described later in the Chapter  6  which 
  536.         deals  with the Short Range Sensor Routine.  The E0 parameter  is 
  537.         then  set  to 4000 (Units of energy).  The parameter  C1  is  now 
  538.         defined.  It  is  a parameter equal to  the  number  of  commands 
  539.         allowed.   Using a parameter for the number of commands  allowed, 
  540.         allows  new  ones  to  be added  later  without  having  to  make 
  541.         extensive changes in the program.  By changing the value assigned 
  542.         to the parameter C1 and later adding the line numbers  associated 
  543.         with  the new command, any new command can be added with  minimal 
  544.         changes  to the existing program.  Note this technique  lets  you 
  545.         add a new command as yet undefined,  with only two changes to the 
  546.         program.   No further changes/debugging of the  existing  program 
  547.         are  required.   It  can be considered a general  rule  that  any 
  548.         changes  made to a working program will introduce bugs into  that 
  549.         program.   for that reason alone, it is always best  to  minimise 
  550.         any changes to be made to existing programs.
  551.  
  552.              Space  is next allocated for the Damage Control arrays  D(I) 
  553.         and D$(I) using the DIM (Dimension) statement in line 9020.  They 
  554.         are also allocated space as a function of the number of  commands 
  555.         in  the  game using the parameter C1.  Their  functions  will  be 
  556.         discussed  later.  We next allocate space for an array  to  store 
  557.         the   quadrant  information  (Q(I,J)).   The  array  is   a   two 
  558.         dimentional  array,  having rows and columns.  Since  the  galaxy 
  559.         contains 8 rows of 8 columns, the space allocated in the array is 
  560.         for  8  rows  and  8 columns. BASIC counts from  0  so  that  the 
  561.         allocation  of 7 places in this line really  allocates  positions 
  562.         0,1 ,2 ,3 ,4 ,5 ,6 and 7 or 8 spaces.  This means the computer is 
  563.         going to count quadrants from 0 to 7 while the player will see  a 
  564.         display of co-ordinates from 1 to 8.  The functions performed  by 
  565.         the  other  arrays will be discussed in detail later.   For  now, 
  566.         just type them into the program.
  567.  
  568.              Lines 4530 to 4560 display the start-up message that set the 
  569.         scene  for the game.  Lines 4570 to 4600 define the  command/sub-
  570.         system  strings  into  the  Damage  string  D$(I)  array.   Their 
  571.         function  will be discussed in Chapter 5 which discusses   Damage 
  572.         Control.  Line 4610 contain the Command Strings for both the main 
  573.         commands  (C1$)  and the Computer commands(C2$).  Lines  4620  to 
  574.         4630 define the contents of the computer command function  string 
  575.         array (C3$(I)) and line 4650 terminates the first subroutine with 
  576.         a 'RETURN' statement.
  577.  
  578.              The  second initialisation subroutine starts at  line  4660.  
  579.         Everything that should be set to zero at the start of a game  is.  
  580.         Most  dialects of BASIC will automatically set variables to  zero 
  581.         when  the 'RUN' statement is invoked to start the program.   This 
  582.         line  ensures that it is so, and also ensures the zero state  for 
  583.         those variables when subsequent games are played.  The  variables 
  584.         set to zero in this line are
  585.  
  586.         C9   The last command excercised
  587.  
  588.  
  589.         Copyright (c) Joe Kasser 1989
  590.  
  591.  
  592.  
  593.  
  594.  
  595.         Chapter 4 page 10 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  596.  
  597.  
  598.         K4   The number of Klingons captured
  599.         K5   The number of Klingons that destroyed themselves
  600.         F9   The end of game flag
  601.         G9   The Inside/outside the Galaxy flag
  602.         K9   The number of Klingons in the Galaxy
  603.         B9   The number of Starbases in the Galaxy
  604.         L3   The number of Long Range Probes launched.
  605.  
  606.              The last item on the line invokes a subroutine beginning  at 
  607.         line  3550  which sets the Damage Control status of all  the  on-
  608.         board sub-systems to zero.
  609.  
  610.              Line 4670 initiates two loops.  The loop using J as the loop 
  611.         counter performs the set up operation on each column in one  row.  
  612.         The loop using I as the loop counter advances the row counter  so 
  613.         that  by using the two loops together all the columns in all  the 
  614.         rows are set up.  
  615.  
  616.              Loops  can  be peformed in a number of ways in  BASIC.   The 
  617.         FOR/NEXT   construction  uses  a  loop  counter  and   tests   it 
  618.         automatically.  The programmer sets up the limits of the loop  in 
  619.         the FOR statement.  For example, the statement
  620.                                  FOR I = 0 TO 7 
  621.         sets up a loop.  The loop counter is first set to the lower limit 
  622.         (0  in  this example).  Everything in subsequent  lines  will  be 
  623.         performed until a NEXT statement is encountered.  When it is, the 
  624.         value  of the loop counter (I in this case) will  be  incremented 
  625.         and  tested  to  see if it exceeds the second  limit  (7  in  the 
  626.         example).   If it is, the loop automatically terminates  and  the 
  627.         program continues with the instruction immediately following  the 
  628.         NEXT statement.  If the loop counter is still less than the upper 
  629.         limit,  the program flow goes back to the  instruction  statement 
  630.         immediately  following  the FOR statement and does  the  sequence 
  631.         again.   The process repeats until the loop counter  exceeds  the 
  632.         upper limit and the loop times out.
  633.  
  634.              Line 4670 calls a subroutine which begins at line 4790.  The 
  635.         GOSUB statement is used to invoke a subroutine.  The program flow 
  636.         now  branches forward to line 4790 and will continue  from  there 
  637.         interpreting  subsequent lines sequentially (such as  4800,  4810 
  638.         etc)  until a RETURN statement is encountered.  When  the  return 
  639.         statement  is encountered the program flow returns or comes  back 
  640.         to  the  calling  line, and the program  continues  at  the  next 
  641.         instruction  following  the  GOSUB  statement.   The   subroutine 
  642.         starting at line 4790 sets up the number of Stars, Starbases  and 
  643.         Klingons for each quadrant and will be described below.  The next 
  644.         instruction  in the line, adds together the numbers  assigned  to 
  645.         the Klingons (K), the starbase (B) and the stars (S), then  makes 
  646.         the number negative (an unscanned quadrant) and stores it in  the 
  647.         Quadrant array using the implied LET statement 
  648.                               Q(I,J) = -(K + B + S).
  649.  
  650.              The  NEXT statements for both loops (row and  column)  which 
  651.         then  terminate  the  loops close out the line.  The  J  loop  is 
  652.         terminated  first, then the I loop.   If the order was  inverted, 
  653.  
  654.  
  655.         Copyright (c) Joe Kasser 1989
  656.  
  657.  
  658.  
  659.  
  660.  
  661.         Chapter 4 page 11 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  662.  
  663.  
  664.         there would be a nesting error and the computer would (hopefully) 
  665.         flag it.
  666.  
  667.              Line  4710 tests to see if the value representing the  total 
  668.         number  of  Klingons in the game is greater than 20.  If  it  is,  
  669.         the program branches forward to pick up again at line 4740.   The 
  670.         value of the variable representing the number of Klingons in  the 
  671.         galaxy  (K9)  was set up to be one hundred times  the  number  of 
  672.         enemy  ships  in the subroutine starting at line 4790  called  in 
  673.         line 4760.  Hence the test uses the statement K9>2000.  This line 
  674.         tests that a minimum number of Klingons have been set up so as to 
  675.         ensure that the game is not too easy.  Remember, the value of  K9 
  676.         is the number of Klingons left multiplied by 100.
  677.  
  678.              Line  4720  now  invokes a subroutine starting  at  line  50 
  679.         (described  below) which computes two random co-ordinates  within 
  680.         the galaxy.  The result is passed as the variables X and Y.   The 
  681.         actual  quadrant  associated with those co-ordinates  is  Q(X,Y).  
  682.         The contents of the quadrant are tested to see if any Klingons or 
  683.         a Starbase are present in it.  If they are, the program  branches 
  684.         back to the begining of the line to locate a different  quadrant.  
  685.         We are really trying to find a quadrant that does not contain any 
  686.         Klingons,  however,  on  the other hand,  the  probability  of  a 
  687.         starbase being present in the quadrant is so low, that if one is, 
  688.         we don't want to delete it.  The discussion about how the data is 
  689.         stored  in  the quadrant showed that there are always  less  than 
  690.         eight stars in the quadrant.  Since the value of the quadrant  is 
  691.         also negative (unscanned) a simple test of the form
  692.  
  693.         IF Q(X,Y) < -9 THEN
  694.  
  695.         will  indicate  if anything other than stars are present  in  the 
  696.         quadrant.  
  697.  
  698.              Line  4730 invokes the subroutine starting at line  4790  to 
  699.         set up a new set of stars, Klingons and starbases, and stores the 
  700.         data  in the quadrant matrix using the LET Q(X,Y) = -(K + B +  S) 
  701.         statement.   The loop then continues setting up more Klingons  by 
  702.         virtue  of the GOTO 4710 statement at the end of the  line  until 
  703.         line 4710 determines that the minimum number have been placed  in 
  704.         the galaxy.
  705.  
  706.              Lines  4710 to 4720 contain a loop that is  terminated  when 
  707.         the number of Klingons in the game (K9) is greater than a certain 
  708.         minimum.  This loop is not programmed by a FOR/NEXT sequence  but 
  709.         is  taken care of by the programmer with the test in  line  4710, 
  710.         and the GOTO at the end of line 4730.
  711.  
  712.              Line  4740 sets the amount of time available for the  player 
  713.         to  complete  the game.  The variable T is used  for  Time.   The 
  714.         player  normally  has  30 stardates to  complete  the  game.   If 
  715.         however more than 30 Klingons have been positioned in the galaxy, 
  716.         the  number of Stardates is set to be the same as the  number  of 
  717.         Klingons  to give the player more time to find and destroy  them.  
  718.         Since the variable K9 contains the number of Klingons  multiplied 
  719.  
  720.  
  721.         Copyright (c) Joe Kasser 1989
  722.  
  723.  
  724.  
  725.  
  726.  
  727.         Chapter 4 page 12 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  728.  
  729.  
  730.         by  100  (see line 4810) the value of K9/100 has to  be  compared 
  731.         with  30.  An alternative valid way of performing the test  would 
  732.         be to state
  733.         IF K9<3000 THEN T=30 ELSE T=K9/100.
  734.  
  735.              Line  4750  sets  the  initial time  factor  (T9)  and  then  
  736.         ensures that at least one starbase is present in the galaxy.   B9 
  737.         is  the  variable used to store the number of  Starbases  in  the  
  738.         galaxy.   If  it  is still equal to zero at this  time,  then  no 
  739.         Starbases  have  been set up in any quadrant of the  galaxy,  and 
  740.         line 4750 proceeds to set one up.  A quadrant is chosen at random 
  741.         by the subroutine starting at line 50 and a starbase is placed in 
  742.         that quadrant irrespective of whatever else may already be there.  
  743.         Remember  that  the  sign of the value of the  contents  of  that 
  744.         quadrant  is  negative,  signifying  that it  has  not  yet  been 
  745.         scanned.
  746.  
  747.              Line  4760  determines  which  quadrant is  to  be  the  the 
  748.         starting  quadrant  for  the Enterprise.  A  random  set  of  co-
  749.         ordinates is selected by calling the subroutine starting at  line 
  750.         50.  The two random values are then loaded into the row (Q1)  and 
  751.         column  (Q2)  co-ordinates  of the Enterprise.   Q1  and  Q2  are 
  752.         variables  used  to store the position of the Enterprise  in  the 
  753.         galaxy.   If we want to move the Enterprise to  another  quadrant 
  754.         inside  or even outside the galaxy, all we need to do  is  change 
  755.         the values stored in Q1 and Q2.
  756.  
  757.              In  order  to set up the contents of the quadrant  that  the 
  758.         Enterprise  has been positioned in, line 4770 calls a  subroutine 
  759.         beginning  at  line  3200.  Then  it  continues  and  calls   the 
  760.         subroutine  beginning at line 90 to set up the initial  state  of 
  761.         the  Enterprise  itself,  and sets the amount of  energy  in  the 
  762.         Shields (represented by the variable E1) to a tenth of the  total 
  763.         energy allocated to the ship at the start of the game  (expressed 
  764.         by  the  parameter  E0).  Line 4780  next  calls  the  subroutine 
  765.         starting  at  line  3400 which determines the  condition  of  the 
  766.         Enterprise  (RED,  YELLOW or GREEN), and  lastly  terminates  the 
  767.         initialisation subroutine with the RETURN statement.
  768.  
  769.              The  subroutine to set up the exact number of  Stars,  Bases 
  770.         and  Klingons  begins  at line 4790.  This is  an  example  of  a 
  771.         subroutine being called by a subroutine, or "nested subroutines".  
  772.         Line  4790  sets  up  the number of  Stars,  represented  by  the 
  773.         variable S to an integer value between 1 and 7 inclusive by using 
  774.         the  expression  S = INT(RND(Z)*7+Z).  The complex  operation  is 
  775.         performed  in one statement.  In order to explain what  is  being 
  776.         done, let us consider the operation in stages.  
  777.  
  778.              We  require  a  random  number  between  1  and  7,  for  by 
  779.         definition, there cannot be more than 7 stars in a quadrant,  and 
  780.         there  must  always be at least 1 star in  any  quadrant  located 
  781.         inside the galaxy.
  782.  
  783.              The procedure starts with the generation of a random number.  
  784.         The  RND(X)  function  returns a  floating  point  random  number 
  785.  
  786.  
  787.         Copyright (c) Joe Kasser 1989
  788.  
  789.  
  790.  
  791.  
  792.  
  793.         Chapter 4 page 13 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  794.  
  795.  
  796.         between  0  and  0.9999  ( just less than  1  for  all  practical 
  797.         purposes).   The random number is then multiplied by 7, giving  a 
  798.         value  between 0 and just less than 7.  When a 1 is added to  the 
  799.         number, the result is a floating point number between 1 and  just 
  800.         less  than  8.   This floating point number is  converted  to  an 
  801.         integer number between 1 and 7 (inclusive of those values) by the 
  802.         INT(X)  function  which returns the INTeger part  of  a  floating 
  803.         point  number.   The whole operation could have been  written  in 
  804.         individual steps as follows,
  805.         4970 S = RND(Z) : REM generate a random number (0 to .9999)
  806.         4971 S = S * 7  : REM now convert it to between 0 and 6.99
  807.         4972 S = S + 1  : REM now make it something between 1 and 7.99
  808.         4973 S = INT(S) : REM and finally make it an integer value between 1 and 7.
  809.  
  810.              Line  4800 determines if a Starbase is to be present in  the 
  811.         quadrant or not.  The value assigned to B is first set to zero to 
  812.         clear  up  any residue from the previous quadrant.   It  is  good 
  813.         practice to set a variable to a known state at the beginning of a 
  814.         procedure rather than rely on it being cleared at the end of  the 
  815.         last time the procedure was performed.  A random number between 0 
  816.         and 0.9999 is then generated.  If it is less than 0.02 a starbase 
  817.         is placed in the quadrant, by setting the value of B to 10.   The 
  818.         total  count  of Bases in the galaxy (B9)  is  then  incremented, 
  819.         keeping track of the number of starbases that have been set up.
  820.  
  821.              The  Klingon  sequence begins at line 4810.  The  number  of 
  822.         Klingons in the quadrant is reset to 0 (just like the value of  B 
  823.         in the previous line). An other random value is then obtained and 
  824.         if  it  is  greater than 0.06 (a six  percent  probability),  the 
  825.         Klingon  sequence  is  bypassed completely as  the  program  flow 
  826.         branches  forward  to the next line.  There is thus  only  a  six 
  827.         percent probability that at least one Klingon will be present  in 
  828.         any quadrant.
  829.  
  830.              The  statements used to set up the Klingons in the  quadrant 
  831.         in  line 4810 follow an IF statement.  The IF/THEN statement  can 
  832.         be used in a number of ways.  The test in line 4810 uses 
  833.         IF RND(Z)<.06 THEN 
  834.         yet  it  was stated in the previous paragraph  that  the  Klingon 
  835.         sequence  is bypassed if the random number is greater than  0.06.   
  836.         The two statements say the same thing but in different ways.
  837.  
  838.              This  construction  demonstrates  an  important   difference 
  839.         between the various dialects of BASIC.  In the Microsoft  dialect 
  840.         if  an  IF  test is performed  and it  fails,  the  program  flow 
  841.         continues  with  the  next line number  in  sequence.   In  other 
  842.         dialects, (such as Northstar BASIC) the program flow may continue 
  843.         with the statement immediately following the IF test in the  same 
  844.         line.   
  845.  
  846.              In  Microsoft BASIC, if the IF test fails, the program  flow 
  847.         continues on the following line and all statements after the THEN 
  848.         are not performed.  In Northstar BASIC the K=INT(RND(Z)*8)*100 is 
  849.         not  performed when the test fails, but everything following  the 
  850.         ':'  is.  This difference has to be carefully  investigated  when 
  851.  
  852.  
  853.         Copyright (c) Joe Kasser 1989
  854.  
  855.  
  856.  
  857.  
  858.  
  859.         Chapter 4 page 14 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  860.  
  861.  
  862.         converting  the program to different dialects of BASIC.  If  line 
  863.         4810 is changed from 
  864.         4810 K=0 : IF RND(Z)<.06 THEN K=INT(RND(Z)*8)*100 : K9=K9+K : K8=K9
  865.         to
  866.         4810 K=0 : IF RND(Z)>.06 THEN 4830
  867.         4820 K=INT(RND(Z)*8)*100 : K9=K9+K : K8=K9
  868.         4830 Continue here
  869.  
  870.         both  dialects of BASIC will perform in the same  manner  because 
  871.         there are no further instructions on line 4810 after the 'THEN'.
  872.  
  873.              Another  way of doing the job is to use  the  'IF/THEN-ELSE' 
  874.         construction.  Line 4810 could be written as 
  875.  
  876.         4810 K=0:IF RND(Z)>.06 THEN 4830 ELSE K=INT(RND(Z)*8)*100:K9=K9+K:K8=K9
  877.  
  878.         which  is also unambiguous. The line now states that if the  test 
  879.         is  good (ie. RND(Z) is greater than .06) then continue  at  line 
  880.         4830,  ELSE  if  it  is less than  0.06  set  up  some  Klingons.  
  881.         Northstar BASIC implies the ELSE function by continuing following 
  882.         the : on the same line.  This discussion on the IF/THEN statement 
  883.         has been included herein since not all versions of BASIC  contain 
  884.         the ELSE capability.
  885.  
  886.              The last section of line 4810 sets up the number of Klingons 
  887.         in  the quadrant.  K is used as a temporary  variable  containing 
  888.         the  exact number of enemy ships in the quadrant.  The  operation 
  889.         could have been written in a number of lines as
  890.  
  891.         4811 K = RND(Z)     : REM     Generate a random number (0 to .999)
  892.         4812 K = K * 8      : REM     convert it to between 0 and 7.99
  893.         4813 K = INT(K)     : REM     now turn it into an integer between 0 and 7
  894.         4814 K = K * 100    : REM     and multiply it by 100
  895.         4815 K9 = K9 + K    : REM     sum total number of Klingons in the game
  896.         4816 K8 = K9        : REM     set up the parameter representing the 
  897.                                       number of Klingons at the start of the game. 
  898.  
  899.              Line  4810  has thus set up the number of  Klingons  in  the 
  900.         quadrant, and also adjusted the total number for the galaxy.
  901.  
  902.              This  technique  for  setting up the  enemy  ships  has  one 
  903.         previously  unmentioned  characteristic.  By  first  deciding  if 
  904.         enemy ships are to be placed in a quadrant, and then  positioning 
  905.         them  there, they will tend to show up in bunches.   It  is  felt 
  906.         that this is not so unreasonable, because it is quite logical for 
  907.         raiding  ships  to travel in fleets particularly when  they  know 
  908.         that  the  Federation (the player) has a  slight  superiority  in 
  909.         weapons technology.  The subroutine exits in line 4830.
  910.  
  911.              The Initialisation subroutines themselves called a number of 
  912.         other  subroutines.   These  "nested" subroutines  are  shown  in 
  913.         figure 4.5
  914.  
  915.              The  subroutine  called  by the  initialisation  routine  to 
  916.         return  two  random  co-ordinates begins at line  50.   The  most 
  917.  
  918.  
  919.         Copyright (c) Joe Kasser 1989
  920.  
  921.  
  922.  
  923.  
  924.  
  925.         Chapter 4 page 15 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  926.  
  927.  
  928.         commonly  used subroutines are placed into the program  with  low 
  929.         numbers to speed up the execution time. There are many  instances 
  930.         in the program, especially during the initialisation phase  where 
  931.         two integer random values are required.  They can for example  be 
  932.         used  to  set  up co-ordinates of objects.  Line  50  contains  a 
  933.         REMark  to  that effect while line 60 does the  work.   The  same 
  934.         procedure  is used twice.  the first time returns a value for  X, 
  935.         the  second  time  returns a value for Y.  X and Y  are  used  as 
  936.         temporary variables for the purpose of communicating between  the 
  937.         calling  program and the subroutine.  The code for the  procedure 
  938.         is  X  =  INT(RND(Z)*8)  which is the joining  of  the  following 
  939.         sequences
  940.  
  941.         X  =  RND(Z)  : X = X * 8 : X = INT(X).  It  is  more  convenient 
  942.         however to write the operation in one statement. 
  943.  
  944.              The subroutine starting in line 90 sets up the Enterprise on 
  945.         board   parameters.    Line  90  contains  the   REMark   comment 
  946.         identifying  the  task  carried out by the  subroutine,  and  the 
  947.         action  takes place in line 100.  The amount of  on-board  energy 
  948.         (E)  is  set to the value represented by the parameter  E0  (4000 
  949.         units  as per line 4510).  The energy in the shields  represented 
  950.         by  the  variable  E1 is set to zero and  the  number  of  Photon 
  951.         Torpedoes, represented by the variable P is set to 10.  The  last 
  952.         statement  in the line is the RETURN statement which  closes  the 
  953.         subroutine.   This  subroutine  is  also  used  in  the   docking 
  954.         operation described later on.
  955.  
  956.              Three    other   subroutines   were   called   during    the 
  957.         initialisation process.  The first two set up the contents of the 
  958.         quadrant  and the condition of the Enterprise.  For now they  are 
  959.         "dummied  up " by lines 3200, 3340, 3400 and 3540.  The last  one 
  960.         begins  at  line  3550  with  the  usual  REMark  statement  that 
  961.         describes the function of the subroutine.  Line 3560 then uses  a 
  962.         FOR/NEXT loop to set the stat of all the on-board damageable sub-
  963.         systems (represented by the contents of the D(I) array) to  zero.  
  964.         C1 defines the number of commands, C2 is the number that are  not 
  965.         damageable, so the total number that is damageable is C1-C2.
  966.  
  967.              These  lines  insure that if the program is run  before  the 
  968.         subroutines  are inserted, the main sequence will work,  but  the 
  969.         functions  performed by the subroutines will not be carried  out.  
  970.         As long as we remember to complete the subroutines before we  try 
  971.         to  use any data they are supposed to set up, everything will  be 
  972.         fine.
  973.  
  974.              Now  add the lines of program listed in figures 4.4 and  4.5 
  975.         to the lines of figure 4.2 that you entered earlier.  Change  the 
  976.         date  in line 10 if the date has changed.  Carefully  check  each 
  977.         line  you  have  typed  in against the  listings,  and  save  the 
  978.         program.  You may compare parts of the program by using the  LIST 
  979.         instruction  to  list part of the program (to  stop  the  earlier 
  980.         lines  from scrolling uo the screen out of sight).   Try  typeing 
  981.         'LIST  10-4500' instead of 'LIST' and see what happens.   In  the 
  982.         event  you get an error message try 'LIST 10,4500'  because  some 
  983.  
  984.  
  985.         Copyright (c) Joe Kasser 1989
  986.  
  987.  
  988.  
  989.  
  990.  
  991.         Chapter 4 page 16 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  992.  
  993.  
  994.         dialects of BASIC need a comma instead of a hyphen (dash).   Once 
  995.         all is well, save the program and read on.
  996.  
  997.         4.7 The Play the Game Module  
  998.  
  999.              Having laid out the structure of the program and initialised 
  1000.         the  galaxy  we  can  now begin to do  things  in  it.   This  is 
  1001.         represented by the PLAY.THE.GAME module first outlined in Chapter 
  1002.         3.   Consider the flowchart for the module in detail as shown  in 
  1003.         figure 4.6.
  1004.  
  1005.              The  module  begins  by checking to determine  if  the  last 
  1006.         command  was a 'MOV' command.  If it was, it is assumed that  the 
  1007.         Enterprise has moved into a new sector, and a Short Range  Sensor 
  1008.         display is to be shown.  The state of the Short Range Sensors  is 
  1009.         then  checked and if they are working, a Short Range Sensor  scan 
  1010.         is  performed.   If  the sensors are damaged, a  Visual  scan  is 
  1011.         performed instead.
  1012.  
  1013.              The Command dialog function now takes place.  The player  is 
  1014.         prompted  to  input  a command, and the computer  waits  for  the 
  1015.         reply.   The  reply is then tested for validity.  If it  is  less 
  1016.         than three characters, the player is prompted with a message that 
  1017.         signifyies that three characters are to be input to the computer.  
  1018.         If  the  reply  is valid it, is carried  out  by  the  PERFORM.IT  
  1019.         module.   If  the  command  is not valid,  then   the  player  is 
  1020.         prompted  with  a list of all the valid commands, and  a  minimal 
  1021.         amount of information what they do by the  DISPLAY.COMMAND.PROMPT 
  1022.         function.
  1023.  
  1024.              At  this stage the command has been carried out, and a  test 
  1025.         is next performed to determine if the game is over.  If it isn't, 
  1026.         and  if the last command carried out by the player was not  Short 
  1027.         Range Sensor, Visual or Klingon Status. and there are Klingons in 
  1028.         the  quadrant,  the  klingons in the sector  are  moved   by  the 
  1029.         MOVE.KLINGONS  module and allowed to take hostile action  at  the 
  1030.         Enterprise in the BACKFIRE module.
  1031.  
  1032.              The  loop  continues as long as the game is not  over.   The 
  1033.         BASIC  language implementation of the flow chart could  take  the 
  1034.         form shown in figure 4.7.  
  1035.  
  1036.              The  routine begins at line 3000 with the REMark or  comment 
  1037.         statement.  Line 3020 tests the value of the "last command  "flag 
  1038.         (C9) to see if the last command was a 'MOV' command.  If it  was, 
  1039.         the  program  flow continues along the line, it  if  wasn't,  the 
  1040.         program picks up at the next line number in sequence (line  3040) 
  1041.         in accordance with the rules of the IF/THEN statement.
  1042.  
  1043.              If  the test passes, the value of I is set to 1 by  the  I=Z 
  1044.         statement.  The IF D(Z)=0 THEN statement tests the state  of  the 
  1045.         Short Range Sensors.  How it does so will be discussed in Chapter 
  1046.         5 as part of the discussion on the Damage Control function.   The 
  1047.         D(I) array contains the state of repair of each of the Enterprise 
  1048.         on-board systems.  If the test passes, that is, the value of D(1) 
  1049.  
  1050.  
  1051.         Copyright (c) Joe Kasser 1989
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.         Chapter 4 page 17 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  1058.  
  1059.  
  1060.         is  zero,  then  the  program  invokes  the  Short  Range  Sensor 
  1061.         subroutine which begins at line 400.  If the test fails,  namely, 
  1062.         the Short Range Sensors are damaged, the ELSE statement sets  the 
  1063.         value  of I to 11 and the Visual Command subroutine beginning  at 
  1064.         line  2500  is invoked.  The value of I is set to 11, so  that  a 
  1065.         subroutine called by the Visual routine can be invoked correctly.  
  1066.         This nested subroutine will be discussed later. 
  1067.  
  1068.              Line  3040  contains a PRINT statement.  A  PRINT  statement 
  1069.         without anything following it causes the terminal cursor to  move 
  1070.         down to the beginning of the following line.  It is equivalent to 
  1071.         a carriage return and line feed combination.
  1072.  
  1073.              The  player  is  prompted for an input in  line  3050.   The 
  1074.         'INPUT'  statement is used by BASIC to accept an input  from  the 
  1075.         console.  When it is encountered, BASIC will wait, and accept all 
  1076.         characters entered at the keyboard until the 'ENTER' or  'RETURN' 
  1077.         key  is depressed.  It will then process the data that  has  just 
  1078.         been entered.  Anything input to the program has to be stored  in 
  1079.         a  variable.   The  INPUT  statement  is  always  followed  by  a 
  1080.         variable.  A plain INPUT statement will cause a question mark  to 
  1081.         be  displayed  at the console.  If something in  quotation  marks 
  1082.         such as "COMMAND " is placed immediately after the word  'INPUT', 
  1083.         the  computer  will display that something  before  the  question 
  1084.         mark.   Thus INPUT "COMMAND " will cause the computer  to  prompt 
  1085.         the player with 
  1086.         COMMAND ?.
  1087.  
  1088.              Notice  that  in  line  3050 there is  a  space  inside  the 
  1089.         quotation  marks  after the word COMMAND.  BASIC will  print  the 
  1090.         question  mark immediately after whatever is inside  the  quotes, 
  1091.         thus if we wanted the prompt to be
  1092.         COMMAND?
  1093.  
  1094.         we  would not put the space inside the quotes, but as we do  want 
  1095.         the prompt to be 'COMMAND ?', we have placed the space inside the 
  1096.         pair of quotation marks.   
  1097.  
  1098.              The  INPUT statement is separated from the variable  (A$  in 
  1099.         this  case) by the semi colon.  The characters are  entered  into 
  1100.         the computer and the entry sequence is terminated by the ENTER or 
  1101.         the  RETURN  key.  This last action tells the computer  that  the 
  1102.         player  is done entering the data, and the computer may start  to 
  1103.         process  the input.  The response to the INPUT statement  may  be 
  1104.         numeric or a string.  In this case it is a string, and the player 
  1105.         input is stored in the string variable A$.
  1106.  
  1107.              The next part of the line tests A$ to see if it is less than 
  1108.         3 characters by using the LEN(A$) function.  The LEN(A$) function 
  1109.         returns  the length of the string.  If it less than 3  characters 
  1110.         long, the program displays an error message on the console as 
  1111.         'BEG PARDON CAPTAIN (3 letters please)' 
  1112.         which not only tells the player that a mistake has been made, but 
  1113.         also  hints at for the correct reply.  The GOTO 3050  instruction 
  1114.         at the end of the line makes the program go back to the beginning 
  1115.  
  1116.  
  1117.         Copyright (c) Joe Kasser 1989
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.         Chapter 4 page 18 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  1124.  
  1125.  
  1126.         of  the  line  and ask the player to have another  try.   If  the 
  1127.         length  of A$ is 3 or more characters, the program  continues  at 
  1128.         line  3060  which contains the loop that  validates  the  command 
  1129.         entry.
  1130.  
  1131.           There are a number of different techniques that can be used  to 
  1132.         implement the command matching function,  so let us now  consider 
  1133.         some of them.
  1134.  
  1135.              The possible commands are as follows.
  1136.  
  1137.         NAV     WARP ENGINES 
  1138.         SRS     SHORT RANGE SENSORS 
  1139.         LRS     LONG RANGE SENSORS 
  1140.         PHA     PHASERS 
  1141.         TOR     PHOTON TORPEDOES
  1142.         COM     COMPUTER 
  1143.         SHE     SHIELDS 
  1144.         LRP     LONG RANGE PROBES 
  1145.         TRA     TRANSPORTER 
  1146.         SHU     SHUTTLECRAFT 
  1147.         DAM     DAMAGE CONTROL 
  1148.         VIS     VISUAL 
  1149.         RES     RESIGN
  1150.         SAV     SAVE THE STATE OF THE GAME
  1151.         LSG     LOAD A SAVED GAME
  1152.  
  1153.              We  will  also  allow the player to  directly  access  three 
  1154.         computer  commands directly from the main command  level.   These 
  1155.         three commands are as follows
  1156.  
  1157.         MAP     DISPLAY A MAP OF THE GALAXY
  1158.         KST     SHOW THE KLINGON STATUS
  1159.         SCA     SCAN A QUADRANT
  1160.  
  1161.  
  1162.              The  simplest  technique of finding out  which  command  was 
  1163.         entered  by  the player, and now residing in the  string  variale 
  1164.         called  A$, is to try and match them all in turn using  something 
  1165.         like the following sequence.
  1166.         3081 IF A$ = "NAV" THEN 1300
  1167.         3082 IF A$ = "SRS" THEN 400
  1168.         3083 IF A$ = "LRS" THEN 300
  1169.         3084 IF A$ = "PHA" THEN 800
  1170.         3085 IF A$ = "TOR" THEN 1000
  1171.         3086 IF A$ = "COM" THEN 1900
  1172.         3087 IF A$ = "SHE" THEN 2700
  1173.         ETC.
  1174.  
  1175.              That set of instructions tests the contents of A$ and  tries 
  1176.         in  turn to match it with the various allowable  commands.   This 
  1177.         technique is perfectly valid.  It does however require the use of 
  1178.         a  lot of 'matching' instructions.  Also, a match in any  of  the 
  1179.         lines  will  cause a branch to the section of  the  program  that 
  1180.         performs the actual command.  The command sequence must then  end 
  1181.  
  1182.  
  1183.         Copyright (c) Joe Kasser 1989
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.         Chapter 4 page 19 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  1190.  
  1191.  
  1192.         with another 'GOTO' instruction which will take the program  flow 
  1193.         back to the main sequence.  A modification of the technique which 
  1194.         allows the command modules to be subroutines and just end with  a 
  1195.         'RETURN' statement is to use the 'GOSUB' statement as follows.
  1196.         3081 IF A$ = "NAV" THEN GOSUB 1300
  1197.         3082 IF A$ = "SRS" THEN GOSUB 400
  1198.         3083 IF A$ = "LRS" THEN GOSUB 300
  1199.         3084 IF A$ = "PHA" THEN GOSUB 800
  1200.         3085 IF A$ = "TOR" THEN GOSUB 1000
  1201.         3086 IF A$ = "COM" THEN GOSUB 1900
  1202.         3087 IF A$ = "SHE" THEN GOSUB 2700
  1203.         ETC.
  1204.  
  1205.              Using  this approach, as long as the command  modules  don't 
  1206.         change the contents of A$, or at least don't set them to a  value 
  1207.         equal to an other command, the only module that will be performed 
  1208.         is the desired one.  One exception however exists.  Since we want 
  1209.         a  Shthe command modules don't change the contents of A$,  or  at 
  1210.         least  don't set them to a value equal to an other  command,  the 
  1211.         only  module  that  will be performed is the  desired  one.   One 
  1212.         exception however exists.  Since we want a Shce will perform  the 
  1213.         SRS  command immediately after performing the "MOV"  because  the 
  1214.         test  for 'MOV' is in line 3081 while the test for 'SRS'  follows 
  1215.         it in line 3082.  (It still doesn't automatically switch to 'VIS' 
  1216.         in  the event that the Short Range Sensors are damaged, but  that 
  1217.         can be taken care of using a statement such as
  1218.         IF D(Z) = 0 THEN A$ = "SRS" ELSE A$ = "VIS"
  1219.         somewhat similar to the statemnt in line 3020 already discussed.
  1220.  
  1221.              If this test technique is to be used, a better version is to 
  1222.         bypass  any undesired lines using a 'GOTO' statement.   Here  the 
  1223.         test on A$ is again performed sequentially for each command, and, 
  1224.         when  a match has been found and the command carried  (using  the 
  1225.         'GOSUB/RETURN'  structure), the 'GOTO' then bypasses the rest  of 
  1226.         the tests.  This modification also speeds up the operation of the 
  1227.         program,  since  once the match is  found,  further  un-necessary 
  1228.         matching  tests are not carried out.  The 'GOTO' statement  would 
  1229.         be added in the following manner
  1230.         3081 IF A$ = "NAV" THEN GOSUB 1300 : GOTO 3090
  1231.         3082 IF A$ = "SRS" THEN GOSUB 400 : GOTO 3090
  1232.         3083 IF A$ = "LRS" THEN GOSUB 300 : GOTO 3090
  1233.         3084 IF A$ = "PHA" THEN GOSUB 800 : GOTO 3090
  1234.         3085 IF A$ = "TOR" THEN GOSUB 1000 : GOTO 3090
  1235.         3086 IF A$ = "COM" THEN GOSUB 1900 : GOTO 3090
  1236.         3087 IF A$ = "SHE" THEN GOSUB 2700 : GOTO 3090 
  1237.         ETC.
  1238.  
  1239.              The FOR/NEXT loop in lines 3060 and 3070 tries to match  the 
  1240.         command entered by the player with the ones stored in memory.
  1241.  
  1242.              An  aother technique which seems more complicated  at  first 
  1243.         but is really simpler uses the 'ON' statement. Before we look  at 
  1244.         the  'ON' statemnt, let consider how it can be used. Each of  the 
  1245.         three  letter commands was stored in a string (C1$) back in  line 
  1246.         4610 by the statement
  1247.  
  1248.  
  1249.         Copyright (c) Joe Kasser 1989
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.         Chapter 4 page 20 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  1256.  
  1257.  
  1258.         4610 C1$="NAVSRSLRSPHATORCOMSHELRPTRASHUDAMVISRESSAVLSGMAPKSTSCA"
  1259.  
  1260.         Since each of the commands are three letters long, the length  of 
  1261.         the  string is equal to the number of possible choices  available 
  1262.         to the player multiplied by three.  Since the player counts  from 
  1263.         1 to the length of the string, and the computer counts from zero, 
  1264.         the  loop  has to be equal to the number of commands  minus  one.  
  1265.         That  is  done in the FOR I=0 TO LEN(C1$)/3-Z part  of  the  line 
  1266.         which  takes the length of the string, converts it to the  number 
  1267.         of  commands  and  subtracts one.   The next  part  of  the  line 
  1268.         proceeds  to test each group of three characters to see  if  they 
  1269.         match the command that was input by the player.  
  1270.  
  1271.              BASIC contains a means of extracting certain characters from 
  1272.         a  string.  For any string such as C1$ all or part of it  may  be 
  1273.         extracted  and  processed.  The  LEFT$(C1$,L),RIGHT$(C1$,L)   and 
  1274.         MID$(C1$,N,L)   instructions   can  be  used  to   perform   thes 
  1275.         operations.
  1276.  
  1277.              The  LEFT$(C1$,L) function extracts the first  L  characters 
  1278.         from  C1$. Thus for example LEFT$(C1$,3) is equal to 'NAV'.   The 
  1279.         MID$(C1$,N,L)  also extracts L characters from C1$, but starts  N 
  1280.         characters along the string, so for example, MID$(C1$,6,3)  would 
  1281.         be  'LRS'.   The  RIGHT$(C1$,L)  function  extracts  the  last  L 
  1282.         characters from the string.  Thus RIGHT$(C1$,3) is 'SCA'.   these 
  1283.         three instructions allow the programmer to manipulate strings  at 
  1284.         will.  Consider for example the statements
  1285.         L=3 : A$ = LEFT$(C1$,L) + RIGHT$(C1$,L)   
  1286.         When it is performed, A$ will end up as 'NAVSCA'.
  1287.  
  1288.              To demonstrate the LEFT$,RIGHT$ and MID$ functions  consider 
  1289.         the following short program.  However before you play with  these 
  1290.         routines,  save the game program and clear the memory  using  the 
  1291.         'NEW' command.
  1292.  
  1293.              Consider the following short program.
  1294.  
  1295.         10 A$ = "STARTREK By Joe Kasser"
  1296.         20 FOR I = 1 TO LEN(A$)
  1297.         30 PRINT LEFT$(A$,I)
  1298.         40 NEXT
  1299.  
  1300.         Line  10 defines the contents of A$.  Line 20 sets up a  loop  to 
  1301.         display  something.  The limits of the loop are 1 and the  number 
  1302.         of  characters  in  A$.  Remember  that  the  'LEN(A$)'  function 
  1303.         returns the number of characters in A$.  Line 30 does the job  of 
  1304.         displaying the contents of A$, while line 40 terminates the loop.  
  1305.         Type this program into your computer and run it.  You should  see 
  1306.         the following on your screen.
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.         Copyright (c) Joe Kasser 1989
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.         Chapter 4 page 21 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  1322.  
  1323.  
  1324.         S
  1325.         ST
  1326.         STA
  1327.         STAR
  1328.         START
  1329.         STARTR
  1330.         STARTRE
  1331.         STARTREK
  1332.         STARTREK
  1333.         STARTREK B
  1334.         STARTREK By
  1335.         STARTREK By 
  1336.         STARTREK By J
  1337.         STARTREK By Jo
  1338.         STARTREK By Joe
  1339.         STARTREK By Joe 
  1340.         STARTREK By Joe K
  1341.         STARTREK By Joe Ka
  1342.         STARTREK By Joe Kas
  1343.         STARTREK By Joe Kass
  1344.         STARTREK By Joe Kasse
  1345.         STARTREK By Joe Kasser
  1346.         OK
  1347.  
  1348.              If you now change line 30 to
  1349.         30 PRINT RIGHT$(A$,I)
  1350.  
  1351.         we  will  see  the effect of the  'RIGHT($)  function.   RUN  the 
  1352.         program and you should get the following.
  1353.  
  1354.         r
  1355.         er
  1356.         ser
  1357.         sser
  1358.         asser
  1359.         Kasser
  1360.          Kasser
  1361.         e Kasser
  1362.         oe Kasser
  1363.         Joe Kasser
  1364.          Joe Kasser
  1365.         y Joe Kasser
  1366.         By Joe Kasser
  1367.          By Joe Kasser
  1368.         K By Joe Kasser
  1369.         EK By Joe Kasser
  1370.         REK By Joe Kasser
  1371.         TREK By Joe Kasser
  1372.         RTREK By Joe Kasser
  1373.         ARTREK By Joe Kasser
  1374.         TARTREK By Joe Kasser
  1375.         STARTREK By Joe Kasser
  1376.  
  1377.         If you now change line 30 to 
  1378.         30 PRINT MID$(A$,I,1)
  1379.  
  1380.  
  1381.         Copyright (c) Joe Kasser 1989
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.         Chapter 4 page 22 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  1388.  
  1389.  
  1390.  
  1391.         and   RUN  this  version,  you  will  see  the  effect   of   the 
  1392.         'MID$(A$,N,L) function.
  1393.  
  1394.         You should see
  1395.  
  1396.         S
  1397.         T
  1398.         A
  1399.         R
  1400.         T
  1401.         R
  1402.         E
  1403.         K
  1404.  
  1405.         B
  1406.         y
  1407.  
  1408.         J
  1409.         o
  1410.         e
  1411.  
  1412.         K
  1413.         a
  1414.         s
  1415.         s
  1416.         e
  1417.         r
  1418.  
  1419.         Change  the  1  in  line 30 to pick out  a  different  number  of 
  1420.         characters and play with these routines.  When you have finished, 
  1421.         clear the memory by typing in the word 'NEW' and read on.
  1422.  
  1423.              In  order to match the player's input stored in A$ with  the 
  1424.         commands stored in C1$, the MID$(C1,N,L) function is used in line 
  1425.         3060.   The statement takes the form 
  1426.         IF A$=MID$(C1$,(I*3)+Z,3) THEN 3080
  1427.  
  1428.         which  causes  the  program  to branch out  of  the  loop  (lines 
  1429.         3060/3070)  to  line  3080 when a match is found.   In  order  to 
  1430.         understand    how   it   works,   consider   the   contents    of 
  1431.         MID$(C1$,(I*3)+Z,3) for different values of I as follows
  1432.  
  1433.         I    (I*3)+Z   (I*3)+Z,3)     MID$(C1$,(I*3)+Z,3)
  1434.         -------------------------------------------------
  1435.         0         1         1,3            NAV
  1436.         1         4         4,3            SRS
  1437.         2         7         7,3            LRS
  1438.         and so on.  
  1439.  
  1440.         The  statement will thus sequentially and systematically  try  to 
  1441.         match  the players's reply with each three letter group in  turn.  
  1442.         If a match is not found by the time it tests the last group,  the 
  1443.         loop  terminates  in  the natural manner, and  the  program  flow 
  1444.         continues along line 3070 after the NEXT statement with a further 
  1445.  
  1446.  
  1447.         Copyright (c) Joe Kasser 1989
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.         Chapter 4 page 23 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  1454.  
  1455.  
  1456.         loop  that  displays  the choice of  commands  available  to  the 
  1457.         player.   In  this case the loop limits are 0 and C1.   For  each 
  1458.         value  of  I, the three letter group for the caommand  choice  is 
  1459.         displayed using the MID$(C1,N,L) function just as before, that is
  1460.         PRINT MID$(C1$,(I*3)+Z,3); 
  1461.  
  1462.         The  cursor  is then positioned further along the  line  and  the 
  1463.         command  function  stored in the command string  array  D$(X)  is 
  1464.         displayed  by the TAB(8);D$(I) part of the statement:   The  NEXT 
  1465.         statement terminates the loop, then C9 is set to 99 so as not  to 
  1466.         confuse  line  3020  and the GOTO  3000  instruction  causes  the 
  1467.         computer to re-issue the prompt message.  
  1468.  
  1469.              The semi colon and TAB(N) that were used in the  instruction 
  1470.         line  are display formatting statements.  BASIC has a  number  of 
  1471.         different  ways  of  controlling the format of  a  display.   The 
  1472.         display resulting when line 3070 is performed looks like
  1473.  
  1474.         NAV     WARP ENGINES 
  1475.         SRS     SHORT RANGE SENSORS 
  1476.         LRS     LONG RANGE SENSORS 
  1477.         PHA     PHASERS 
  1478.         TOR     PHOTON TORPEDOES
  1479.         COM     COMPUTER 
  1480.         SHE     SHIELDS 
  1481.         LRP     LONG RANGE PROBES 
  1482.         TRA     TRANSPORTER 
  1483.         SHU     SHUTTLECRAFT 
  1484.         DAM     DAMAGE CONTROL 
  1485.         VIS     VISUAL 
  1486.         RES     RESIGN
  1487.         SAV     SAVE THE STATE OF THE GAME
  1488.         LSG     LOAD A SAVED GAME
  1489.  
  1490.              When the program gets to line 3080, the value of I is  equal 
  1491.         to the number of tests that have been performed before the  match 
  1492.         was found.  At this time we could perform a sequence of 
  1493.         IF I = X THEN 
  1494.         statements such as the following.
  1495.  
  1496.         3081 IF I = 0 THEN GOSUB 1300 : GOTO 3090
  1497.         3082 IF I = 1 THEN GOSUB 400 : GOTO 3090
  1498.         3083 IF I = 2 THEN GOSUB 300 : GOTO 3090
  1499.         3084 IF I = 3 THEN GOSUB 800 : GOTO 3090
  1500.         3085 IF I = 4 THEN GOSUB 1000 : GOTO 3090
  1501.         3086 IF I = 5 THEN GOSUB 1900 : GOTO 3090
  1502.         3087 IF I = 6 THEN GOSUB 2700 : GOTO 3090 
  1503.         ETC.
  1504.  
  1505.              If we are going to use this technique, there was no point in 
  1506.         going  through the rigmarole to convert the contents of A$  to  a 
  1507.         number  in I, we could have just performed the tests as  decribed 
  1508.         above.  The reason for converting the contents of A$ to a  number 
  1509.         in  I  is  so  that we can use the  'ON'  statement  which  is  a 
  1510.         sequential  test statement built into BASIC.  It is used  in  two 
  1511.  
  1512.  
  1513.         Copyright (c) Joe Kasser 1989
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.         Chapter 4 page 24 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  1520.  
  1521.  
  1522.         ways, either as
  1523.              ON I GOT0 11,21,31,41
  1524.         or   ON I GOSUB 11,21,31,41.
  1525.  
  1526.              Here BASIC tests the value of I and if I=1, branches to line 
  1527.         11,  if  I=2, branches to line 21, if I=3, branches to  line  31, 
  1528.         etc.  The 'GOTO' or 'GOSUB' variations determine if the branch is 
  1529.         a direct 'JUMP' or a subroutine call.  In the event that it was a 
  1530.         subroutine call, when the 'RETURN' statement is encountered,  the 
  1531.         program picks up at the instruction following the 'ON' statement.
  1532.  
  1533.              Line  3080  first sets the "last command flag  (C9)  to  the 
  1534.         value  of  I and then uses the 'ON' statment to  proceed  to  the 
  1535.         desired command subroutine.  Snce the 'ON' statement counts  from 
  1536.         1  and  the value of I statrs from 0, the test  is  performed  on 
  1537.         'I+Z' (or I+1).
  1538.  
  1539.              When the command has been carried out, the relevant 'RETURN' 
  1540.         statment  is  encountered which brings the program flow  back  to 
  1541.         line  3090 which tests to see if the 'End of Game' flag (F9)  has 
  1542.         been set.  If it has, the program jumps forward to line 9000  and 
  1543.         goes  into the end game sequence. If the game isn't over a  PRINT 
  1544.         instruction is performed.  The value of C9 is then tested.  If it 
  1545.         was 1, ie a Short Range Sensor Scan was the last command  carried 
  1546.         out,  the  command loop jumps forward to line 3110.  If  not,  it 
  1547.         continues at the next line which is line number 3100.  Line  3100 
  1548.         moves the Klingons using the subroutine starting at line 700  and 
  1549.         then  allows  them to fire at the Enterprise using  the  BACKFIRE 
  1550.         subroutine  starting at line 600 if any Klingons are  present  in 
  1551.         the  quadrant.   Note that if a Short Range Sensor Scan  was  the 
  1552.         last command carried out, this line is bypassed so that the enemy 
  1553.         don't  get  to move and shoot back.  There would be no  point  in 
  1554.         doing  a Short Range Sensor scan to see where the enemy  are  (in 
  1555.         the  static game), if they are allowed to move before the  player 
  1556.         can take any action.  The last part of line 3100 tests the  state 
  1557.         of  the "Game Over" flag (F9) to see if the player has wiped  out 
  1558.         all  the enemy or if the enemy has destroyed the Enterprise.   If 
  1559.         either  of  those  conditions are present, the  value  of  F9  is 
  1560.         greater  than  zero and the program branches forward to  the  end 
  1561.         game sequence starting at line line 9000.
  1562.  
  1563.              Line 3110 causes the command loop perform another  iteration 
  1564.         by virtue of the 'GOTO 3000' statement.  The subroutines of lines 
  1565.         600  and  700  have not yet been  developed,  hence  the  'dummy' 
  1566.         entries  of  lines  600,680,700  and  740  to  make  the  program 
  1567.         playable.
  1568.  
  1569.              Add the program statements in figure 4.7 to your version  of 
  1570.         the  game, change line 10 to reflect the date.  Carefully  verify 
  1571.         that  there  are  no typographical errors  because  some  of  the 
  1572.         statements  are pretty complicated, and save the  program  before 
  1573.         reading on.  
  1574.  
  1575.              Now type in the word RUN followed by a carriage return,  and 
  1576.         the  program  should run.  You should see the  screen  clear  and 
  1577.  
  1578.  
  1579.         Copyright (c) Joe Kasser 1989
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.         Chapter 4 page 25 STARTREK THE COMPUTER PROGRAM By Joe Kasser
  1586.  
  1587.  
  1588.         after  some seconds the messages from lines 10/40 appear  on  the 
  1589.         screen.   Then  a  few seconds later,  the  messages  from  lines 
  1590.         4530/4560 will follow them and be displayed.  Finally you  should 
  1591.         see
  1592.         FUNCTION NOT AVAILABLE
  1593.         COMMAND ?
  1594.  
  1595.         Anytime you enter a valid command, you should get 
  1596.         FUNCTION NOT AVAILABLE
  1597.         as  a response.  The exception to that rule is if you  enter  the 
  1598.         'MOV'  command.  You should then see it appear twice.   The  dual 
  1599.         response is due to the fact that the 'MOV' command function  does 
  1600.         not yet exist.  That gives you the first one.  The second one  is 
  1601.         due  to  the fact that a 'SRS' is automatically invoked  after  a 
  1602.         'MOV'  (see line 3020).  The 'SRS' function will also return  the 
  1603.         'missing'  function  message.    If you  enter  less  than  three 
  1604.         characters  as a response to the command prompt, you  should  get 
  1605.         the  error  message from line 3050.  If you enter three  or  more 
  1606.         characters  which  don't make up a valid command, you  should  be 
  1607.         prompted  with  the  "help" information, ie. the  whole  list  as 
  1608.         follows
  1609.  
  1610.         NAV     WARP ENGINES 
  1611.         SRS     SHORT RANGE SENSORS 
  1612.         LRS     LONG RANGE SENSORS 
  1613.         PHA     PHASERS 
  1614.         TOR     PHOTON TORPEDOES
  1615.         COM     COMPUTER 
  1616.         SHE     SHIELDS 
  1617.         LRP     LONG RANGE PROBES 
  1618.         TRA     TRANSPORTER 
  1619.         SHU     SHUTTLECRAFT 
  1620.         DAM     DAMAGE CONTROL 
  1621.         VIS     VISUAL 
  1622.         RES     RESIGN
  1623.         SAV     SAVE THE STATE OF THE GAME
  1624.         LSG     LOAD A SAVED GAME
  1625.  
  1626.              After you have corrected any errors that may have shown  up, 
  1627.         you  will  find  that you cannot stop  the  program.   Break  the 
  1628.         program flow  by holding down the "CNTRL" (control) key and  then 
  1629.         touching the 'C' key at the same time.  This combination known as 
  1630.         Control  C  (^C),  will stop the  program.   Save  the  corrected 
  1631.         program.   As an aid to ensuring that you have picked up all  the 
  1632.         relevant  lines  of  code, your  program  listing  should  appear 
  1633.         identical to that shown in figure 4.8 except for any changes  you 
  1634.         have had to make to customise the program for your computer.
  1635.  
  1636.              Did you notice that you were instructed to save the  program 
  1637.         before and after the debugging session ?  That was because  there 
  1638.         is  the remote possibility that you may blow the  program  during 
  1639.         the  session for one reason or another.  In that event,  you  can 
  1640.         always  reload the saved version and continue from  there.   That 
  1641.         beats having to re-enter lines of code that were added before the 
  1642.         session and then lost.
  1643.  
  1644.  
  1645.         Copyright (c) Joe Kasser 1989
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.