CSCI 261 Programming Concepts (C++)

Winter/Spring 2012

Homework 30: Random (aka Drunkard's) Walk

Right-click me and select "Save target as..." to save.

Concepts

For this assignment, focus on using method chaining when affecting objects stored inside vectors.

DRY: Don't Repeat Yourself & Private "Helper" Functions

Whenever you notice some code that is exactly the same in your programs, it makes sense to "refactor" (improve) your code by extracting the duplication into a function. We provided some examples of this in class and won't re-emphasize it here.

If you follow the DRY principle (Don't Repeat Yourself) you'll start to see areas in your program that can be improved. One goal of DRYness is also to reduce the lines of code in your program: every line of code you don't write is bug-free. But be careful, this doesn't mean that you should try to be clever in order to reduce lines of code -- it's more about reducing duplication.

Method Chaining

Since you spent the time reading the testApp.cpp code in 28_movingSquares (yes, you did) you saw some examples of method chaining. We also discussed some examples of this in class. As such, we'll leave you with a simple example.

Assume we have a vector trolls that has been filled with Troll objects. How might we print the length of each Troll's name to the screen?

vector<Troll> trolls;
// .. assuming the vector is filled with N Troll objects

for (int i = 0; i < N; ++i) {
    cout << trolls.at(i).name.size();
}

What is troll.at(i)? It's a Troll object. And Troll's have names. What is a Troll's name? It's a string. And strings support the size() method. The above code is like saying "Give me the size of the name of the thing in the trolls vector at index i." Or, from left to right, "Get the thing in the trolls vector at index i, then ask it it's name, then ask name it's size."

Instructions

For this assignment, we'd like you to imagine that your friend is lost. They find themselves in the middle of a field and are a bit inebriated from programming too much. As such, as they walk they will take just one step in any of four random directions (forward, right, back, or left). What happens to your friend's position over time? After taking 1000 steps, will your friend just end up right back where they started?

This process is lovingly known as the Random Walk phenomena, also known as "Drunkard's Walk" or Brownian motion.

Your goal for this assignment is to create a simulation that illustrates the effect of Brownian motion, to help answer the question: what happens to the walker over time?

Create a graphical program that draws the position of the walker over time as a colored square on the screen. Then, increase the number of walkers (10? 100? 400?) and see what happens to all of the walkers.

Click each below to see a video.

  

Hints

You must design and implement a Walker class.

In testApp.cpp you should probably declare two globals: a const int NUMBER_OF_WALKERS representing the number of Walkers you wish to draw, and a vector of Walker objects.

Your setup, update and draw functions should probably create the walkers, have each walker take one step, and draw each walker on the screen, respectively.

Requirements and Rubric

A friendly message from The Terminator, our grading program.

Hello ag *bzzzt* again. I will be visiting my cyb-b-borg family for turkey day and will not be evaluating this assignment.

Compilation for this assignment will be automated, but we will be manually evaluating your programs.

This work is worth 300 points.

Requirement Points Notes
Place your name in the comment header in main.cpp 5
Correct submission as .zip file 5
Walker class defined 30
Walker has x attribute 10
Walker has y attribute 10
Walker has red, green and blue attributes 30 use random numbers between 0 and 255
Walker constructor sets initial position to center of screen 20
Walker has walk() method that changes either x or y by +1 or -1 20
testApp.cpp declares NUMBER_OF_WALKERS constant for number of Walkers 10
testApp.cpp declares vector of Walker objects 10
setup adds NUMBER_OF_WALKERS Walker objects to vector 30
update causes each Walker in the vector to walk() 30
draw draws each Walker as a 10x10 square at position x and y, with color red, green, blue 30
program successfully exhibits graphical behavior described in the instructions 60

Concepts Exercised: simulation, algorithms, classes, OOP, graphics, animation, fun, vectors, data structures, repeated tasks, making decisions