Alan Pratt cooks up a method of organising program structure MenuMaster For anyone who wishes to use a Spectrum for other purposes than playing games there is a wealth of literature aimed at teaching the Basic language. Many who have mastered aspects of that find themselves directed towards writing games pro- grams rather than more serious applications. The main reason for that is the lack of direction in the literature towards developing business or educational type programs. We can, however, develop a technique for writing non- games type programs which is both simple in concept and in widespread use already. The resulting programs come under the general category of Menu Driven Programs. Many who have decided to delve into the Basic language quickly become disillusioned because of its apparent un- structured nature. That means the majority of programs are difficult to break down into definite sections. They are like a book with only one paragraph covering several pages. It is possible to write a program in Basic as a series of sections each of which can be RUN as a separate program without necessarily relying on all the other sections. That is exactly how a menu driven program is constructed, with the menu section forming the skeleton from which other sections are accessed. Many commercial programs for large computers are menu driven as the operators are not program- mers. From the programmer's point of view the advantages of having separate sections is that each can be made error free before the next is written. The main section containing the menu displays a set of options available to the user with some means of selecting between them. That can take various forms but the simplest is to number each option. All the user has to do is to press the correct number of the keyboard. It is the options which constitute the independent sections. We will develop a program which is menu driven whilst describing the techniques used and how they apply to any program of that type. The program is a graphics character generator which allows you to develop a large character composed of 12 normal-sized characters arranged as three rows of four columns. The large character is drawn eight times full size on a grid occupying the full screen with a 'pen' which is capable of both drawing and rubbing out. The final large character can be displayed full size and saved on tape for future use by Program 1 and Program 2. The second program uses the characters to illustrate the letters of the alphabet and is intended as a teaching pro- gram for young children. It is also menu driven to further illustrate the techniques. You will probably have met a menu driven program on the second side of the Horizons tape in the form of "character" which is also a graphics character generating program. In this case only one screen display is used with the menu occupying the lower portion of the screen. Options are selected by pressing the appropriate letter keys. Before describing Program 1 it is worthwhile looking at some general practices which can be adopted when writing any program. Those result in the program lines 10 to 150 and are arranged as follows. Lines 10 to 30 are REM lines which contain the title, and a copyright notice enabling the program to be easily identified by the first few lines. Frequently used numbers are assigned to variables in line 40. The screen colours are established in line 50 whilst all the arrays are DIMensioned in lines 60 and 70. The POKE in line 100 is a useful [I disagree vehemently - RLB] means of setting CAPS LOCK on. Lines 120 to 130 read in the necessary data for the user defined graphics and array d() with line 110 reminding you where to find the first data line. Line 140 contains a frequently used message which is held in w$. The most commonly used GO TO statement, at the end of each option section, refers to the start of the Main menu. To assist in identifying those the variable 'menu' is set equal to 200, which is then treated as a line number in the statement 'GO TO menu'. The variable 'new' in line 40 is used to detect if a character has been generated and prevents you using options 4, 5 or 6 before you have selected 1, 7 or 8. Now to the main program section which contains the menu. That occupies lines 200 to 390 and starts with a REM line to identify the section. It is good practice to use as many REM lines as possible when writing a program to make it easier for others to understand. The fully developed pro- gram was written to run on a 16K Spectrum which still has enough memory to support an adequate number of REM lines. You will notice the use of the PRINT AT function in all the following lines in order to produce a screen display which is both tidy and easy to understand. Whenever I write a program containing screen displays I always sit down with a 32 by 24 grid representing the full screen and plan the layout with a pencil and rubber before attempting to write any program lines. Following the title and the instruction 'Select your option' which are both highlighted, the various options are spelt out in short titles which are intended to be self explanatory. As with all good menu programs there is a choice titled 'Instructions' which will enable the first time user to understand how to use the program. The final choice allows the user to stop the program without reverting to using the BREAK key or pulling the plug out. After printing all the titles the program needs to identify each option. In this case the numbers 1 to 9 are printed down the left hand side. That is done with lines 360 to 380 which may appear complicated but the effect produced is of a flashing band travelling quickly down the numbers and reminds the user the machine is waiting for a choice to be made. Line 390 completes the program loop which is another essential part of a menu program. The lines 350 to 390 are repeated endlessly until a valid key is pressed which identifies a menu option. Line 350 is used to detect when that happens by looking at the contents of INKEY$. In Appendix A of the Spectrum Manual is the ASCII table which gives the code for each character. The codes for the numbers start at 48 and go to 57, hence by subtracting 48 from CODE INKEY$ a number corresponding to the key pressed is obtained. That is then tested to see if it falls within the range of the options, in this case 1 to 9, and if it does the machine jumps to line 400. Line 410 directs the machine to the correct program section using the Spectrum capability of GO TO a variable - or in this case a formula using a variable - which is treated as a line number. Type in the program lines 10 to 410 and 6000 to 6020 and save them on tape. RUN the program and you will obtain the menu on the screen with the numbers on the left flickering in sequence. Try pressing any key except the numbers 1 to 9 and nothing should happen. Now press a number between 1 and 9 and the report 0 OK, 6020:1 should appear. If it does not you can correct your mistakes and RUN the program as many times as you like until it does what it is supposed to do. As all programmers realise writing a program may seem easy but getting it to work and do what you intend is something completely different. By allowing the program to be broken into separate sections the whole program need not be written at once. The menu section is relatively easy to write and you can copy lines 100 to 410 of this program with changes to the titles for your own menu program. Although each option section is ideally independent of all the others, there are always some routines which are used by two or more sections. The best example in Program 1 is the routine which returns the machine to the Main menu and consists of a subroutine at line 5000. In general all common routines should be separated from the options section and written as subroutines near the end of the program. As well as using subroutines, the program also contains two routines which are used by two option sections but are entered at different points by each section. The first of those has been taken out of the option section and located between lines 4000 and 4690. That is the main drawing routine and is itself separated into sections by REM lines which identify entry points. It also contains its own sub- routine at line 4600 which changes the characters in a$. The array represents the full character using '0' for an empty square and '1' for a filled-in square. The drawing loop, lines 4250 to 4490, is repeated until the character is complete and key 'C' is pressed. The second routine is associated with using either a SAVEd character or the existing characters USR "A" to USR "L". It contains two routines for filling the arrays c() and a$() and is located within the section associated with option 7. The other option, 8, enters the routine at line 2380. When developing the program the drawing routine was written after the main menu section. To check its operation it was necessary to include an option section to access it. When developing your own menu program you will probably follow the technique adopted of writing each option section and then testing it before proceeding with the next. Rather than use that approach now, type in the whole of the remainder of the program. Note the letters S and T in lines 680, 4050, 4090, 4100 and 4460, and letters A to L in lines 1450 and 5110 are graphics characters. Once comlete RUN the program again and press key 2. That will produce a set of instructions to enable you to use the program. Option 9 will allow you to stop the program and correct any mistakes in options 2 and 9 before continuing. Once you are happy RUN again and select options 4, 5 and 6 in turn. In each case you should be told to select options 1, 7 or 8 first since the variable 'new' = 0. Now select option 1 to test the drawing routine. After initialising a set of instructions will be displayed. Memorise those and then press a letter key. The screen will now fill with a grid of black lines defining 32 by 24 squares with the top left square highlighted containing the '+' symbol. That is the tip of the drawing pen. Check the operation of the '0' and cursor keys to draw a shape before pressing the 'C' key. After calculating, the program will automatically select option 4 and display three of the characters full size and a further five joined together. Once options 1 and 4 work properly select option 4 and your character will be reconstructed on the full screen line by line. The keys used in option 1 again become oper- ative and you may modify the character and view it full size before returning to the menu. Now select option 5 which produces its own instructions followed by a list of the numbers. The options 6 and 7 allow you to save a character on tape - by saving the graphic characters USR "A" to USR "L"+7 - and recall a character from tape. Generate a character using option 1 and then select option 6. You will be asked for a name which is entered in capital letters automatic- ally - do not change the cursor to lower case letters. [As mentioned above, I find this habit repulsive. Feel free to use decent human mixed case, just remember to change back to Caps Lock before pressing Enter, because the programmer was too stupid to handle both cases - RLB.] Instructions will be given on starting and stopping the tape and I suggest you record the characters on the second side. Now select option 7 and enter the same name you used in section 6. Rewind the tape and again follow the instruc- tions on the screen. Once loaded, the machine uses two routines to fill the arrays. Lines 2390 to 2450 are used to fill c() and lines 2460 to 2530 use a relatively quick method to convert the numbers from array c() into binary and insert the result in a$(). The last option, 8, allows you to use the existing graph- ics characters A to L. It is intended to be chosen instead of option 1 when the program is first RUN and allows you to use characters from another program which are preserved when NEW is used to delete that program. Once you are happy with the program and all the mistakes are corrected type RUN 7000 and press ENTER. The routines in lines 7000 to 7020 has been included to automatically SAVE the program for you. One final word of explanation; you will notice extensive use of PRINT #0 in this program. That allows printing on the two lines of the screen normally reserved for input and messages, enabling a grid of 24 lines to be drawn. Program 2 is an educational program which illustrates the letters of the alphabet with graphic characters. If you have used Program 1 to generate and save some characters on tape, you will be able to load those characters into a block of data associated with this alphabet learner program. You will recognise the initialisation and title section in lines 10 to 100. Line 40 contains CLEAR and LOAD state- ments which allow the data associated with the graphics characters to be held in memory from location 30100 on- wards. The saved program will automatically RUN itself when loaded and all the data is then loaded as a single block of numbers. The main menu is located from lines 200 to 290 with the options listed in lines 240 to 280. It is similar in layout to the menu in Program 1 and could be adapted to any requirement simply by changing the words. Line 290 contains a GO SUB 4300 statement which handles the input of a valid option number. The reason for using a subroutine is because two of the main menu options also contain their own menus, hence a common routine can be used. Remember that any common routines should be separated out as subroutines and placed near the end of the program. The variable 'j' is set to the row number containing the first option and 'lim'; is the number of options. Each menu option is defined by a REM statement as are the subroutines at the end. Once again the program will fit in the 16K Spectrum. For those of you who do not have any characters saved on tape a load program is included in Program 3 which will illustrate the letters A to D. It is limited to letter D since a longer listing would be laborious to type in and there is more enjoyment to be had in creating your own cha- racters. This program should be typed in before Program 2 and RUN. Once complete type NEW which will delete the program but preserve the numbers in high memory. If you do not wish to use the loader program you should type CLEAR 30099 and press ENTER. Now type in the program "Alpha" listing in Fig. 2. Note the letters A to L in lines 1450, 1770 and 2280 are graphics characters. Once you have corrected any mistakes type GO TO 8000 and follow the instructions for saving the program and then the data. After VERIFYing the tape copy type RANDOMIZE USR 0 to reset the Spectrum. Now type LOAD "Alpha" and load in the program. Once loaded the main menu will appear and I suggest you select option 1 first. Now you can load all those charac- ters you saved on tape and begin to teach your children the Sinclair character set.