Change into your "cps6" directory using the "cd" command and create another directory called "lab5" using the "mkdir" command. Change into the "lab5" directory. If you did this correctly, when you type "pwd" you should see a long path name that ends with "/cps6/lab5"
In order to do this lab, you need to copy some files using the following "cp" (for "copy") command (don't forget the trailing period, or dot):
cp ~dr/cps6/lab5/* .
If you type "ls" to list the files now in your lab5 directory, you should see the following files: Makefile, gwindow.h, gwindow.cc, gwalk.cc, gwalk.h and frogwalk.cc.
For each of the programming problems that follow, you should use the style rules discussed in class, which includes indentation, meaningful variable names and comments at the top of the file and for each function.
Remember, to compile these programs, use the command
Note on the Window class
DO NOT MODIFY THE WINDOW CLASS!
Copy the files above before doing this part. Show one of your lab TA's the following using Unix commands. These commands have appeared in previous labs.
Use emacs to find/load the file frogwalk.cc and lets take a look at it before compiling it.
This program simulates a one dimensional random walk of a frog on a dock using a modified version of the RandomWalk class described on pages 292-296. The modifed RandomWalk class is located in the files gwalk.cc and gwalk.h. Some of the functions have been modified to create an animation of the frog walking. Some additional member functions and variables have also been added.
Looking at frogwalk.cc,
a randomly walking frog is created (which also
creates a graphic window for the animation),
and the new member function DisplayMessage is called which displays the
message "ONE DIMENSIONAL RANDOM WALK" at the top of the graphic window.
The frog always starts in the middle of the dock and takes its first
step by calling the member function First
and randomly moves one position
right or left (with equal probability)
each time by calling the member function Next.
The member function IsDone() returns true after the frog has moved the
specified number of times.
Since this is a one dimensional walk, the frog's y-position never changes.
Compile this program by typing
make frogwalkand run it to watch the one dimensional walk using the Samba animator on high speed and enter 500 in the xterm window for the number of steps. To run it, type:
frogwalk | samba
The frog may go off the dock and keep moving, it may even go off the screen and you won't be able to see it. When the message DONE appears at the top of the graphic window, the frog has stopped moving. If you get tired of waiting for it, you can click on QUIT to remove the Xtango window.
Modify frogwalk.cc in the following manner.
NOTE: All output to the xterm window must be done using a cout statement that first prints out the word "comment " and ends in an endl;
cout << "comment " << ... << endl;
Now recompile the program, fix any mistakes, and run it to make sure it is correct. As soon as the frog goes off the dock, the frog should halt. If the frog doesn't go off the dock, then it should halt after it has moved the specified number of times.
In this section you will modify two of the member functions in the
RandomWalk class. Even though you are modifying gwalk.cc and
gwalk.h, you will still type make frogwalk to recompile
the frogwalk program. It doesn't make sense to type
make gwalk because gwalk.cc doesn't have a main function!
Make the following modifications to convert to a two dimensional walk.
frogwalk.cc,
change the message displayed to "TWO DIMENSIONAL
RANDOM WALK".
You might want to compile the program to make sure this works correctly before moving to the next step. Remember to type: make frogwalk to compile.
Note: You will have to modify the function GetPosition in
gwalk.cc, the function protoype
for GetPosition in gwalk.h,
and its
use in frogwalk.cc
to now print out both the x and y positions of the frog.
Now recompile the program, fix any mistakes, and run it to make sure it is correct. You should now see a 2-dimensional random walk. Click here to see what it might look like when the frog is moving around. Not too much to see different in a picture, a different message and the y-value of the frog is no longer in the middle of the dock. You'll notice the real difference in the animation, since the frog will now be moving all over the dock.
In this section, you'll make the frog move around more and make a mark where it has been.
Make the following modifications.
In gwalk.cc, the function CopyMark should be defined with the following header.
void RandomWalk::CopyMark(string color)
Notice that the the Window variable w has already been declared in the RandomWalk class. You'll need to use Window member functions to draw the filled in circle at the current position of the frog. See gwindow.h for a description of the member functions.
Note that you will also have to add a function prototype for this new function in gwalk.h.
Modify frogwalk.cc
to use the member function CopyMark. Ask the user
how often to mark where the frog is, and then call CopyMark to mark a
copy of the frog in "yellow". For example, if
the user enters 20, show the mark
every 20 steps. If the user enters 0, do not show the
mark.
For example, Click here to see a picture where the frog is hopping around and putting a yellow mark every 20 steps.
frogwalk.cc), make sure you fill in the comment block (name, etc)
at the top of all three of these files.
Now recompile the program by typing make frogwalk , fix any mistakes, and run it to make sure it is correct.
Remove the line
frog.DisplayMessage("Done");
from frogwalk.cc.
(Otherwise it will quickly write over the message
you have displayed indicating where the frog landed).
frogwalk.cc
to call CopyMark to draw a red mark if the frog
does not fall off the dock.
Now recompile the program by typing make frogwalk , fix any mistakes, and run it to make sure it is correct.
The extra credit will further modify the files frogwalk.cc,
gwalk.cc and gwalk.h.
If you do the extra credit, be sure to include
a note in your README file that says you did the extra credit so the
grader will look for it.
Make the following modifications.
frogwalk.cc,
ask the user how many
simulations to run, and
then add a loop so
that the frog starts over again (by calling First) for each simulation.
Each time the frog starts over at the middle of the dock, it will run a simulation of numSteps steps. By restarting the frog several times you can get a more reliable estimate on the simulation results.
Caution: Because the RandomWalk class has been modified to create an animation of one frog hopping, you can only create one frog. If you create another frog, your program will not work correctly. Make sure the declaration of the RandomWalk frog is not in a loop. If it is inside a loop, that has the same effect of creating a new frog for each occurence of the loop, and your program will not work correctly.
Submission of programs:
When your program compiles and produces the correct output, create a README file. This file should include your name, section number, the date, how long you worked on these programs, and anyone you received significant help from. Remember to include a note in your README file if you did the extra credit! . You can then turn everything in by typing (where N is your section number (1, 2, ..., or 7):
submit6 lab5 README frogwalk.cc gwalk.cc gwalk.h
You should receive a message telling you that the programs were submitted correctly.