walk.cc From Astrachan, pp 316-317 ----------------------------------- #include "walk.h" #include "dice.h" RandomWalk::RandomWalk(int maxSteps) // postcondition: all private data fields initialized { myPosition = mySteps = 0; myMaxSteps = maxSteps; } void RandomWalk::TakeStep() // postcondition: one step of random walk taken { Dice coin(2); switch (coin.Roll()) { case 1: myPosition--; break; case 2: myPosition++; break; } mySteps++; } void RandomWalk::First() // postcondition: first step of random walk taken { myPosition = 0; mySteps = 0; TakeStep(); } bool RandomWalk::IsDone() // postcondition: returns true when random walk is finished // i.e., when # of steps taken == max. # of steps { return mySteps >= myMaxSteps; } void RandomWalk::Next() // postcondition: next step in random walk simulated { TakeStep(); } void RandomWalk::Simulate() // postcondition: one simulation completed { for(First(); ! IsDone(); Next()) { // simulation complete using iterator methods } } int RandomWalk::GetPosition() // postcondition: returns position of "frog" (x coordinate) { return myPosition; } int RandomWalk::GetSteps() // postcondition: returns number of steps taken by "frog" { return mySteps; }