## CS 115 Lab 6 Random numbers and If statements (Team)

### 80 Points

Due Date: Monday October 16, end of lab

Educational goals of this lab - verify that every student can

• write some graphics code
• using random numbers
• using if statements
• using the Line object
• using the Text object to display text messages on the graphics window
• work with their teammates to finish a project

INSTRUCTIONS:

This problem is due during lab.

(80 points) Team Problem:

• There is a concept called "The Drunkard's Walk" or a "random walk". The idea is that some things in life are not predictable but follow a random path. In mathematical terms, it is a path that consists of a succession of random steps. For example, the path traced by a molecule as it travels thorugh a liquid or a gas, the search path of a foraging animal, the price of a stock, the financial status of a gambler. ref
• Your program is going to simulate this using random numbers.
• Here is a sample run.
• (64 points) The team's job is to implement this design:
1. Write a usual header, Team members' names, section, team number, 3 P's
2. Document with the design given; add to it as needed
3. Create a graphics window with the title "Random Walk" and your choice of dimensions.
4. Create two variables (prevx and prevy) which represent the middle of the window. Use getWidth() and getHeight().
5. Create a variable called stepsize and set it to 10.
6. Store the values of prevx and prevy in two variables called currx and curry.
7. Create a Text object which is near the bottom of the screen, centered horizontally, with "0" as the text. Use getWidth() and getHeight(). Draw it.
8. Repeat 100 times (a for loop!)
1. Make a variable which is a randomly selected character from the string "UDLR" (stands for Up, Down, Left, Right). That is, the variable should be either "U", "D", "L", or "R". (There is a random library function that will do this nicely.)
2. Write an if/elif structure which uses the random string variable to decide which of the current point's coordinates to change (x or y) and in which direction, e.g. Up would cause the y coordinate to change in the negative direction (which is towards the top of the screen) and Right would cause the x coordinate to change in the positive direction (towards the right of the screen). The size of the change is always stepsize. All that each branch (of the 4 branches) has to do is change either curry or currx by stepsize. Use one of the augmented assignment operators.
3. Create a Point whose location is at prevx, prevy.
4. Create another Point whose location is at currx, curry
5. Draw a line between the two points just created
6. Store the value of currx in prevx and the value of curry in prevy
7. Using the Text object created in step 5, change its text to be the value of the counter of the loop (use setText)
9. Change the Text object so that its text is "Click to close"
10. Cause the program to pause until the user clicks
11. Don't forget to close the graphics window!
• Now run your program. Run it again. Does it give the same result? why or why not?
• Change the number of times the loop executes - make it 1000. Run it. Make it 10000. Run it. How do the two runs differ?
• (10 points) Note that the program may actually run off the side of the graphics window (clipping). Sometimes the path will eventually come back into the window, sometimes it will not ever. Let's fix that so the path never needs to be clipped.
• The key point is that the currx and curry variables have to be checked at every iteration of the loop. This checking is done by adding more if's inside the if/elif structure.
• For each of the four branches (four directions), ONE if statement (else not required) is required to check to make sure that the variable that was just changed is not past the boundary of the window. That's the ONLY check that needs to be done. Only one variable (currx or curry) is changed, so only one variable could possibly have gone outside the bounds.
• If the variable does cross the boundary of the window, instead make the variable value the value it would be at the boundary. Example: if it were going to go past the edge on the left side of the window, it is becoming less than 0. Instead just make it zero.
• As appropriate, use getHeight() and getWidth() instead of hard-coded numbers.
• Run it after you make the changes. You may have to run a few times before the path goes over one of the boundaries.
• How could you change the program to be more interesting? How about changing colors at random? color_rgb might be useful there or a list of colors like ["red", "blue", "green"]. Or how about changing the size of the step that is taken? instead of always being 10, use a random number between 1 and 20?
• (6 points) Change the size of the GraphWin. You should only have to change the two numbers in one line. The rest of the code should work unchanged. If that's not true of your code, then make it true.
• Submit this final program with your team members' names (the ones who were present today) using the link at the top of the page. Use menu choices Lab 6 and Code.
• Make sure that all members of the team get a copy of the team result. Also make sure that everyone understands the team result.

## If you do not attend lab, you will NOT get the points for the Team Problem, even if your team does submit a solution!

Make sure you sign the attendance sheet.

Log off properly - you don't want your account misused by someone else!

Remember NOT to leave files on the local hard drives in this lab or anywhere else on campus! Make sure you save your projects on a portable storage device you take with you!