CIS 220 - Program 6
Subs, Functions, Parameter Passing

Objectives: Gain experience modularizing your programs. 

Description: This program is intended to provide you with experience using subroutines, functions, and parameter passing, as well as the randomize function provided by Visual Basic.  It is also the first analytically challenging program.

Simulations are very important for the development of business applications. They are used to locate bottlenecks in factory operations, used to evaluate pricing policy given consumer reactions, and the College of Business uses simulations as a teaching tool in a number of advanced and graduate courses.

This simulation is designed to provide you with more experience using functions and subs, looping, random numbers, and invoking procedures from other procedures.  You will be simulating the life of Hank the Roach, a lab cockroach, who can only skitter along the edges of a cube. At the start of a trial, Hank is randomly placed on one of the eight corners of a cube and a sedative is placed on one of the eight corners. Hank starts moving along the edges. When he comes to a corner, he chooses one of the directions and continues. How many edges will Hank visit until he hits the sedative? Note that Hank may visit zero to an infinite number of edges in a given trial.

Develop a simulation of Hank’s travels around the cube. The visual basic form for your Hank simulation should include two buttons, three labels, and two list boxes:

  • The buttons include a quit button and a start (or "Poke Hank") button.  

  • Use a label to display Hank's initial position, and a label to display the position of the sedative on the cube.  

  • One listbox should be used to track Hank's movements by displaying Hank's current position after each move.  

  • After he has hit the sedative and that simulation trial is finished, you should display the number of edges Hank visited for the completed trial in the other listbox.  That listbox tracks the total number of edges that Hank visits for each trial.  

  • You should also use a label to display the average for all the simulation trials the user has run since the start of the program.    

The program uses listboxes because they can hold multiple values, and thus can be used to record or trace Hank's movements during a single trial, or to record the number of edges visited for all the trials.  

The most recent addition to a list box should be highlighted and displayed.  See list box clues and/or scrolling clues for help.

Use a delay routine to slow Hank's movements so that they can be observed.  The interface that you should use is shown in the following figure.

The use of labels in a form, instead of textboxes, will speed up larger programs because they have fewer properties. However, you may need to refresh the label with a command like lblNumEdges.Refresh before the new value will be displayed.

Use two integers declared as static variables, to track the running average. Name the variables "totalEdges" and "totalTrials." (Add the code for the running average after the rest of the project is operational – doing things a little bit at a time is key to rapidly generating bug free code.)

Use the modularization approach in  developing a solution to Hank.  Four or more procedures will help make the code tractable.  Implement and test the procedures individually, before developing the rest of the code (it's called incremental testing).  Do not use any global variables to pass variables into or out of functions or subroutines.  The procedures should include:

  • A function called "randomNumber" that accepts a positive integer, and randomly generates a number from zero to one less than that integer. For example, if you pass the argument 8 to the function, it should return a number between 0 and 7.

  • A function called "newLocation" that accepts Hank's previous location as an argument and generates a new location for Hank.  Note that this function will call the "Random" function written above to generate the new location.  Remember that Hank can only skitter to an adjacent location on the cube.

  • A procedure called "initializeSedativePosition" that generates an initial position for the sedative.  Note that this procedure will call the "Random" function written above to generate the positions.  *

  • A procedure called "initializeHankPosition" that generates an initial position for Hank.  Note that this procedure will also call the "Random" function written above to generate the positions.  *

            * If you wish, you can replace these two functions with a single procedure (perhaps a function) called  
               initializePositions that can be called both to initialize the poison position as well as Hank's initial position.

Numbering your corners as they are numbered in the figure below may help.

Insufficiently commented code, poorly chosen variable or function names, not using functions, or not passing the correct information into a function will be graded harshly. The program requires a block comment at the top stating your name, the date, the program name and program description or purpose.  Each function requires a block comment at its top with one (or more if necessary) sentence(s) stating the exact purpose of that function. You may want to indicate incoming and outgoing parameters.

Include a flowchart or pseudocode detailing your solution.  It can be a hand-drawn, rough draft and does not have to be fancy.