Herman the Cube Crawler
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 Herman the Fly, who can only exist on the edges of a cube. At the start of a trial, Herman is randomly placed on one of the eight corners of a cube and poison is placed on one of the eight corners. Herman starts moving along the edges. When he comes to a corner, he chooses one of the directions and continues. How many edges will Herman visit until he hits the poison? Note that Herman may visit zero to an infinite number of edges in a given trial.
Develop a simulation of Hermans travels around the cube. The visual basic form for your Herman simulation should include two buttons, three labels, and two list boxes:
The buttons include a quit button and a start (or "Go
Use a label to display
Herman's initial position, and a label
to display the position of the
poison on the cube.
should be used to track Herman's movements by displaying Herman's current
position after each move.
After he has hit the poison and that simulation trial
is finished, you should display the number of edges Herman
for the completed trial in the other listbox.
That listbox tracks the total number of
edges that Herman 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 Herman'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.
Use a delay routine to slow Herman'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 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 Herman. 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 Herman's previous location as an argument and generates a new location for Herman. Note that this function will call the "Random" function written above to generate the new location. Remember that Herman can only move to an adjacent location on the cube.
A procedure called "initializePoisonPosition" that generates an initial position for the poison. Note that this procedure will call the "Random" function written above to generate the positions. *
A procedure called "initializeHermanPosition" that generates an initial position for Herman. 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 Herman'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. Each function (as well as the program itself) requires a block comment at its top stating your name and one (or more if necessary) sentence(s) stating the exact purpose of that function.
Include a flowchart or pseudocode detailing your solution. It can be a hand-drawn, rough draft and does not have to be fancy.
REMOVE THIS FOR 2006
Extra Credit: Using graphics techniques, write a subroutine, drawCube, to draw a blue cube in a picture box below the output specified above. Create a subroutine, drawCircle, that accepts the coordinates of one of the cube corners and a color, and draws a colored circle around that corner. Then modify your Herman solution from above to show a simulation of Herman going around the cube. Use the drawCircle routine to draw a red circle around the corner where the poison is located. Track Herman's movements around the cube by using the drawCircle to draw a green circle at the node that he visits. Only the current node should be so indicated. When Herman hits the poison draw a black circle around that point. See the extra-credit demo for an example. A sample interface is shown in the following figure.