DOC PREVIEW
UW CSE 142 - Study Guide

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1 of 3 CSE 142, Autumn 2011 Programming Assignment #5: Random Walk (20 points) Due: Tuesday, November 1, 2011, 11:30 PM This assignment focuses on using while loops to draw two-dimensional random pixel "walks" on a DrawingPanel. Two runs of the program are reproduced below with console output on the left and DrawingPanel output on the right: Welcome to the CSE 142 random walk simulator. Radius? 2 Time between steps (ms)? 1000 Walker starting at x=2, y=2 x=1, y=2, steps=1 x=1, y=3, steps=2 x=1, y=2, steps=3 x=0, y=2, steps=4 Walker escaped in 4 step(s). Walk again (yes/no)? y Radius? 4 Time between steps (ms)? 100 Walker starting at x=4, y=4 x=4, y=3, steps=1 x=4, y=4, steps=2 x=5, y=4, steps=3 x=5, y=5, steps=4 x=5, y=4, steps=5 x=6, y=4, steps=6 x=7, y=4, steps=7 x=6, y=4, steps=8 x=7, y=4, steps=9 x=8, y=4, steps=10 Walker escaped in 10 step(s). Walk again (yes/no)? YAP Radius? 2 Time between steps (ms)? 0 Walker starting at x=2, y=2 x=1, y=2, steps=1 x=1, y=3, steps=2 x=0, y=3, steps=3 Walker escaped in 3 step(s). Walk again (yes/no)? Nope Total walks = 3 Total steps = 17 Best walk = 3 Welcome to the CSE 142 random walk simulator. Radius? 20 Time between steps (ms)? 0 Walker starting at x=20, y=20 x=20, y=21, steps=1 x=20, y=22, steps=2 x=20, y=21, steps=3 ... <412 lines of output omitted to save paper> ... x=0, y=24, steps=416 Walker escaped in 416 step(s). Walk again (yes/no)? cheese Total walks = 1 Total steps = 416 Best walk = 4162 of 3 Program Description: This assignment focuses on while loops, random numbers, and using objects. Turn in a file named RandomWalk.java. Your program will draw a pixel-sized "random walker" that moves in random directions on a DrawingPanel until it has moved a certain distance away from its starting location. Read more about interesting properties of random walks here: • http://en.wikipedia.org/wiki/Random_walk When the program runs, a 100x100 DrawingPanel appears with a white background and an introduction is displayed on the console. The DrawingPanel should be zoomed in by a factor of 5 (syntax for doing this is shown later in this document). Then your program should perform one or more random walks. A walk begins by asking the user for the radius (in pixels) of the walk area and the time (in milliseconds) between steps. The DrawingPanel draws a black-outlined circle with the radius r provided by the user, centered at point (r, r). (In other words, the top-left corner of the circle's bounding box is at (0, 0), and its width and height are twice as large as the radius r the user types.) Next, the steps of the random walk are drawn on the DrawingPanel and displayed on the console. On the DrawingPanel, the walker is a single black pixel that begins in the center of the circle. At each step, the walker randomly steps its position up, down, left, or right by 1 pixel. The walker should choose between these four choices randomly with equal probability. After each move, a message should print to the console indicating the walker's (x, y) position and the number of steps made so far. Between steps, the walker will pause for the amount of time specified by the user. The walk ends when the walker reaches the perimeter of the circle (i.e., when its distance from the center is greater than or equal to the circle's radius). When the walk ends, the program reports how many steps were made. The previous output demonstrates your program's behavior. Yours will generate different random steps, but your output structure should match exactly. If you like, you may assume that no run will require ≥ 999,999,999 steps. After each run, the program asks the user if they want to do another walk. Assume the user will give a one-word answer. The program should walk again if the user's response begins with Y. That is, answers such as "y", "Y", "YES", "yes", "Yes", or "yeehaw" all indicate that the user wants another walk. If the user wants to do another walk, the DrawingPanel's contents clear out to white, and the steps described above repeat. (To clear out the contents of the DrawingPanel, use the DrawingPanel’s clear method.) If the response does not start with Y, assume that the user does not want to walk again. For example, responses such as "n", "N", "no", "okay", "0", and "cheese" mean that the user doesn't want any more walks. If the user chooses not to walk again, the program prints the total runs, total steps for all runs, and the steps for the best run (the one that required the fewest steps). These random walk images also look a little like Rorshach ink blot tests. Once you're done, you may wish to optionally save your output as an image, and submit it along with what you think your "ink blot" looks like. Implementation Guidelines: Your program must zoom everything displayed on the DrawingPanel by a factor of 5 to help you see each individual step made by the random walker. Use the zoom method from DrawingPanel. For example, given a DrawingPanel called panel, the following line of code will zoom in by a factor of 5: panel.zoom(5); // zoom the DrawingPanel by a factor of 5 This will make each individual pixel appear on the screen as a group of 5x5 pixels. The DrawingPanel will do this scaling for you, so you do not need to manually scale any of your drawing commands by 5. The user may also change the zoom level manually from the DrawingPanel's zoom menu. Note that circles do not look very round when zoomed in. The repetition in this program should be done using while loops. We suggest that you develop this program in stages: • Work on a text-only version of the program before adding graphics. • Initially write a version that does just one random walk. • Initially test using very small radius values such as 3 or 5. • Initially test using large values for the time between steps such as 1000 or 2000 (1 or 2 seconds). Use a single class constant for the DrawingPanel's size (default 100). By changing this constant and recompiling, it should be possible to run your program with a larger or smaller window and have all behavior update correctly.3 of 3 Examine distances between points to determine whether the walker has exited the circle. The formula to compute the distance between two points is to take the square root of the sum of the squares


View Full Document

UW CSE 142 - Study Guide

Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?