Solution

// Programmer: Clayton Price

// File: hans_walk.cpp

// Purpose: this file contains the main function for the program that will

// generate random walks for Hans Moleman.

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

/*------------------ DECLARATIONS --------------- */

const int MAX_DECISIONS = 500; //turning points in walk

const int DESTINATION_X = 1000; //x-coord of target

const int DESTINATION_Y = -1000; //y-coord of target

const int NUM_WALKS = 200; //max num walks in simulation

const float MIN_MILEAGE_OUTPUT = 1;

const int START_LEG = 20; //first advance from house

const int LEFT_FREQ = 7; //frequency of left turns

const int CLOSE_TO_HOME = 30;

const int CLOSE_TO_DEST = 50;

const int FEET_IN_MILE = 5280;

const int MIN_DIST_FROM_HOME = 5;

const string DESTINATION = "Nick's";

int decisions; //turning points

int x_loc = 0; //coords of actor - start (0,0)

int y_loc = 0;

int first_step = START_LEG; //first leg of each walk

int dist = 0; //distance for each leg of walk

int last_dir_indicator;

//to figure out which way to go

// if left turn next

int turn_indicator; //determines how to add or subtract

// walking distance

int traversal = 0; //number of feet traveled

float dist2home; //home at (0,, 0)

float dist2dest;

int term1cnt = 0, term2cnt = 0, term3cnt = 0; //termination counts

bool term1=false, term2=false, term3=false; //termination signals

float miles; //number of miles traveled

int max_walk = 0; //stores max dist walked

// for output formatting

cout<<"\n\n Ended walk at total dist traveled (ft)"<<endl;

cout<<" ------------- ------------------------"<<endl;

//begin simulation ........

for(int i=1; i<=NUM_WALKS; i++)

{

x_loc = first_step; //initialize Hans loc - east

y_loc = 0;

decisions = 1;

traversal = x_loc;

term1 = false; //ended up at home false

term2 = false; //got to destination false

term3 = false; //wandered off the map false

dist = x_loc; //initialize distance walked

turn_indicator = 1; //turn_indicator will determine

// how to advance walker

// start walking.......

do

{

dist = (((dist * 214013 + 2531011)%2147483647)%200) + 100;//magic formula

if (decisions%LEFT_FREQ != 0) //for determining how to move

{

if (turn_indicator%4 == 1)

{

y_loc -= dist;

last_dir_indicator = 0;

}

else if (turn_indicator%4 == 2)

{

x_loc -= dist;

last_dir_indicator = 1;

}

else if (turn_indicator%4 == 3)

{

y_loc += dist;

last_dir_indicator = 2;

}

else

{

x_loc += dist;

last_dir_indicator = 3;

}

}

else //turn left

{

if(last_dir_indicator == 0)

x_loc += dist;

else if (last_dir_indicator == 1)

y_loc -= dist;

else if (last_dir_indicator == 2)

x_loc -= dist;

else

y_loc += dist;

turn_indicator+=2; //accounts for left turn rather

} // than right turn

traversal += dist; //tally total dist moved

dist2home = sqrt(x_loc * x_loc + y_loc * y_loc); //calc distances

dist2dest = sqrt((x_loc - DESTINATION_X)*(x_loc - DESTINATION_X) +

(y_loc - DESTINATION_Y)*(y_loc - DESTINATION_Y));

term1 = (dist2home < CLOSE_TO_HOME) //still near home

&& (decisions > MIN_DIST_FROM_HOME);//determine how walk term'd

term2 = dist2dest < CLOSE_TO_DEST; //close to destination

term3 = decisions > MAX_DECISIONS-1; //walking off to the ozone

decisions++;

turn_indicator++;

} while(!term1 && !term2 && !term3);

miles = traversal/static_cast<float>(FEET_IN_MILE);//compute miles traveled

if (term1) //output formatting

cout<<"\thome "<<traversal;

else if (term2)

cout<<"\t"<<DESTINATION<<" "<<traversal;

// else

// cout<<"\tOZONE "<<traversal<<endl;

if ((term1 || term2) && (miles > MIN_MILEAGE_OUTPUT))

cout<<" (~ "<<miles<<" miles)";

if (term1 || term2)

cout<<endl;

first_step++;

// tally

if (term1) //increments the different

term1cnt++; // terminations

else if(term2)

term2cnt++;

else

term3cnt++;

if (traversal > max_walk) //compute max walk length for

max_walk = traversal; // laughs

}

// output stats

cout<<"\nThe experiment ended with these outcomes: "<<endl;

cout<<"\n\tHans got nowhere: "<<100*static_cast<float>(term1cnt)/NUM_WALKS

<<"% of the time"<<endl

<<"\tHans made it "<<DESTINATION<<": "

<<100*static_cast<float>(term2cnt)/NUM_WALKS

<<"% of the time"<<endl

<<"\tHans wandered off: "<<100*static_cast<float>(term3cnt)/NUM_WALKS

<<"% of the time"<<endl<<endl

<<"\tMax walk length = "<<static_cast<float>(max_walk)/FEET_IN_MILE

<<" miles!"<<endl<<endl;

cout<<"Good job, Hans!!.....Bye for now!"<<endl;

return 0;

}