// File: main3.cpp // // Last update: 02/26/2007 20:02 // // See Project 2 description for documentation. // #include #include #include #include #include "proj2.h" #include "Car.h" #include "RaceCourse.h" using namespace std ; // This function initializes the obstacle vector. // The "input" is almost WYSIWYG. // // Warning! Does not check for size compatibility // with RaceCourse constructor. // // Make sure all the rows are the same length. void SetObstacles(vector& obs) { vector V ; // WYSIWYG Obstacle Constructor?? // // 0123456789012345678901 V.push_back("######################") ; V.push_back("# #################") ; V.push_back("# # #") ; V.push_back("# ############ # #") ; V.push_back("# ## # #") ; V.push_back("# ########## ## # #") ; V.push_back("# ## ## # #") ; V.push_back("# ######## ## ## # #") ; V.push_back("# ### ## ## # #") ; V.push_back("###### ### ## ## #####") ; V.push_back("# #") ; V.push_back("# #### ##### ### #") ; V.push_back("# # # #") ; V.push_back("# # ### #########") ; V.push_back("### # # #") ; V.push_back("# ######### #") ; V.push_back("####### # #") ; V.push_back("# # ## #####") ; V.push_back("# ###### # #") ; V.push_back("### # # ###### #") ; V.push_back("# #") ; V.push_back("######################") ; // Set obstacles // No need to set outside perimeter unsigned int i, j, rows, cols ; Position X ; rows = V.size() ; assert(rows > 0) ; cols = V.at(0).length() ; for (i=1 ; i < rows-1 ; i++) { assert( cols == V.at(i).length() ); for (j = 1 ; j < cols-1 ; j++) { if (V.at(i).at(j) == '#') { X.row = i ; X.col = j ; obs.push_back(X) ; } } } } // Make car zoom in given direction // void DoZoom(Car *Cptr, char dir) { switch(dir) { case 'u' : Cptr->ZoomUp() ; break ; case 'd' : Cptr->ZoomDown() ; break ; case 'l' : Cptr->ZoomLeft() ; break ; case 'r' : Cptr->ZoomRight() ; break ; default: cerr << "Someone messed up!\n" ; exit(1) ; } ; } int main() { vector obs ; Car *CarPtr1, *CarPtr2, *CarPtr3, *CarPtr4 ; SetObstacles(obs) ; RaceCourse R(20, 20, 4, obs) ; // Get Car pointers // CarPtr1 = R.GetCar(1) ; CarPtr2 = R.GetCar(2) ; CarPtr3 = R.GetCar(3) ; CarPtr4 = R.GetCar(4) ; // Scripted paths for each car to follow // Make them equal length or else ... // // 123456789012345 char script1[] = "drdldrdlrdldrdl" ; char script2[] = "drdrdldrdldrdld" ; char script3[] = "drdrdldrdldldll" ; char script4[] = "drdrdldrrdllldl" ; int len1 = strlen(script1) ; int len2 = strlen(script2) ; int len3 = strlen(script3) ; int len4 = strlen(script4) ; // Print out starting line up // cout << "Initial Race Course: \n" ; R.PrettyPrintCourse() ; cout << "#Cars=" << R.GetNumOfCars() << endl ; cout << "#Rows=" << R.GetNumOfRows() << endl ; cout << "#Cols=" << R.GetNumOfCols() << endl ; R.StartRace() ; // Compute length of longest script int maxlen = len1 ; if (maxlen < len2) maxlen = len2 ; if (maxlen < len3) maxlen = len3 ; if (maxlen < len4) maxlen = len4 ; int i ; for (int k=0 ; k < maxlen ; k++) { i = (k % len1) ; // script wraps around if too short cout << "\nCar 1 Step #" << k << endl ; DoZoom(CarPtr1, script1[i]) ; R.PrettyPrintCourse() ; cout << "Car 1 is at (" << CarPtr1->GetPosition().row << "," << CarPtr1->GetPosition().col << ") with fuel level=" << CarPtr1->GetFuelLevel() << "\n" ; i = (k % len2) ; cout << "\nCar 2 Step #" << k << endl ; DoZoom(CarPtr2, script2[i]) ; R.PrettyPrintCourse() ; cout << "Car 2 is at (" << CarPtr2->GetPosition().row << "," << CarPtr2->GetPosition().col << ") with fuel level=" << CarPtr2->GetFuelLevel() << "\n" ; i = (k % len3) ; cout << "\nCar 3 Step #" << k << endl ; DoZoom(CarPtr3, script3[i]) ; R.PrettyPrintCourse() ; cout << "Car 3 is at (" << CarPtr3->GetPosition().row << "," << CarPtr3->GetPosition().col << ") with fuel level=" << CarPtr3->GetFuelLevel() << "\n" ; i = (k % len4) ; cout << "\nCar 4 Step #" << k << endl ; DoZoom(CarPtr4, script4[i]) ; R.PrettyPrintCourse() ; cout << "Car 4 is at (" << CarPtr4->GetPosition().row << "," << CarPtr4->GetPosition().col << ") with fuel level=" << CarPtr4->GetFuelLevel() << "\n" ; } cout << "\n\n\nTHE WINNER IS: Car " << R.GetWinner() << endl ; }