Classic Computer Magazine Archive ANTIC VOL. 4, NO. 11 / MARCH 1986


How to program a computer simulation


Computer simulations are an important practical application for science and industry. The following article demonstrates the steps involved in creating a computer simulation of how plants would grow on an all-metal planet. Dilithium Dahlias works with disk or cassette, on all Atari 8-bit computers having Graphics 9 capability.

The most beautiful art often comes from the everyday workings of science and nature. A computer simulation is an attempt to copy and anticipate the workings of nature by scientific means, incorporating elements of both realms.
   At my job, I am regularly called upon to make up computerized simulations for various new ideas. I have found that I follow a fairly set routine in doing so, and would like to share it with you.
   For this simulation, I chose a type of plant from my wife's flower garden and decided to find out what this plant would look like if it had grown on another planet-say, a planet where everything is made out of metal.
   Type in Listing 1, DAHLIAS.BAS, check it with TYPO II and SAVE a copy before you RUN it. NOTE: Some older Atari 400/800 models don't have the Graphics 9 capability required for this program. To test your computer, type in and RUN the following: 10 GRAPHICS 9:GOTO 10. If your screen turns black, you have the GTIA chip needed for Graphics 9. If the screen remains blue, you have the older CTIA chip.
   When you RUN the program, a metallic plant will slowly grow right before your eyes. Each time you run the program, a different plant will develop. But in order to achieve this end result, let's look at the steps we needed to go through.

STEP 1. Collect the - necessary data. To start, I measured every inch of the plant, and compressed the important elements of its form into numbers, as follows:
   1. For every millimeter of plant height, there was a branch 14 percent of the time.
   2. The first branch was always a double branch (left and right) on every such plant in the garden.
   3. If a branch began within 10mm of an already existing branch, it didn't grow, but remained a stub.
   4. Of the other branches, 60 percent branched outward from the stem at a 30-degree angle.
   5. 40 percent branched inward toward the stem at a 10-degree angle.
   6. Of these branches, 3 percent didn't grow, but remained a stub.
   7. The higher up on the plant, the thinner the stem.
   8. Every one of these plants in the garden was randomly different, but maintained the same general parameters. Any computer simulation would have to provide me with a different Dilithium Dahlia every time the program was RUN, but would have to follow the same general parameters.
   Wow! I want to simulate all that in a program? Right! Piece of cake! In other words:

   STEP 2. Don't allow the task ahead of you to be greater than the enthusiasm behind you!

   STEP 3. Think through what you want the program to do:
   When I start a simulation, I find it easiest to first make up a general outline of the whole program in REM statements, so I will know what is necessary at any given time.
   This also helps if you cannot write the whole program in one sitting. In the Dilithium Dahlias listing, note that the REM statements are all in increments of 10, and the program lines are written between them. Luckily, in this one, the program lines fit. If they don't, you just have to renumber.
   Note that a few of the 10-step increment groups are not there. Often, after you begin to write the program, you will see that you can delete steps which seemed important when you were doing your outline.
   Note also the use of well-named variables for the major portions of the program. By using these you can renumber at will, and just change the value of the variable, without having to go back through your program to check every line for GOTOs and GOSUBs.
   This also makes the program easier to read if you refer back to it at some later date. When you assign the variable values for all GOTO and GOSUB routines, be sure you do not assign the value of your REM line. That way, the REM line can be removed later, to streamline the program.

   STEP 4. Write the main programming lines to meet each REM line (without getting fancy):
   To begin this simulation, I just looked at each REM line and wrote programming below it which would do what was requested. I usually go straight through, without RUNning the program, and write all the code at once.

   STEP 5. See if it works (it won't!) or why not:
   The best laid plans of mice & men ... Well, you know. From here, just work on the program to get it to do what you need it to.

STEP 6. Once it does work, improve it:
   Any good programmer, looking at the enclosed listing will immediately see a thousand ways to streamline it (get rid of the REM lines, etc.), enhance it (add a title page, more colors, etc.), and generally make it more professional.
   Please feel free to change this program any way you want, and to call it your own. I have left the program at this stage of development so you can do just that. In fact, I have always felt that one of the best ways to sharpen your programming skills is by RE-programming something which already works. It allows you to play around with things, and see immediate results.
   Once you have improved this program to your own satisfaction, think up a simulation of your own. (Martian animals? Underwater jet planes?) Once you have tried a few, you'll see a new world of programming capabilities opening up to you.

Leonard Buchanan is Computer Operations Manager for Secure Systems Planning and teaches Computer Science classes at Anne Arundel Community College, Annapolis, MD.

Listing 1   DAHLIAS.BAS Download