// File: p4main2.cpp // // Last update: 02/26/2007 20:02 // Last update: 04/15/2007 01:45 // // See Project 2 & 4 description for documentation. // #include #include #include #include #include "Thing.h" #include "RaceCourse.h" #include "ScriptedCar.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?? // // 01234567890123456789012345678901234567890123456789012345678901 V.push_back("##############################################################") ; //0 V.push_back("# # # #") ; //1 V.push_back("# #") ; //2 V.push_back("# ################################################ #") ; //3 V.push_back("# ## #") ; //4 V.push_back("## ############### #################### ##") ; //5 V.push_back("# ### #") ; //6 V.push_back("# ############### #") ; //7 V.push_back("# ################# ##################") ; //8 V.push_back("# #") ; //9 V.push_back("# ### # #") ; //0 V.push_back("####### # # #") ; //1 V.push_back("# # # # ##") ; //2 V.push_back("# #") ; //3 V.push_back("# # # #") ; //4 V.push_back("# # # # #") ; //5 V.push_back("# # #") ; //6 V.push_back("# # # #") ; //7 V.push_back("## # #") ; //8 V.push_back("# ######## #### #### ######### #") ; //9 V.push_back("# # % # #") ; //0 V.push_back("# ########### #### #### ##### ########## #") ; //1 V.push_back("# # # #") ; //2 V.push_back("# # # # #") ; //3 V.push_back("# # #") ; //4 V.push_back("# #") ; //5 V.push_back("# # # # #") ; //6 V.push_back("# # # #") ; //7 V.push_back("# # #") ; //8 V.push_back("# #") ; //9 V.push_back("# # # #") ; //0 V.push_back("# # # #") ; //1 V.push_back("# #") ; //2 V.push_back("# # # # #") ; //3 V.push_back("# #") ; //4 V.push_back("# #") ; //5 V.push_back("# # #") ; //6 V.push_back("# ################################################ #") ; //7 V.push_back("# #") ; //8 V.push_back("# #") ; //9 V.push_back("# #") ; //0 V.push_back("##############################################################") ; // Set obstacles // No need to set outside perimeter unsigned int i, j, rows, cols ; 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) == '#') { obs.push_back(new Wall(i,j)) ; } } } } void UpdateScreen(RaceCourse& R, vector& Cars) { #ifdef VT100 cout << "\033[2J\033[H" ; // VT100 erase screen, goto HOME #endif R.PrettyPrintCourse() ; RaceStatus s = R.GetRaceStatus() ; switch(s) { case WAITING : cout << "Status: WAITING\n" ; break ; case STARTED : cout << "Status: STARTED\n" ; break ; case FINISHED : cout << "Status: FINISHED\n" ; break ; default: cout << "Status: UNKNOWN (Bad)\n" ; } for(unsigned int i=0 ; i < Cars.size() ; i++) { cout << "Car #" << Cars[i]->DrawMe() << " at " ; cout << "(" << Cars[i]->GetRow() << "," << Cars[i]->GetCol() << ")" ; cout << " fuel=" << Cars[i]->GetFuelLevel() << "\n" ; } } int main() { vector obs, morethings ; vector Cars ; ScriptedCar *cptr ; Goal *gptr ; bool AddOK ; // Initialize RaceCourse R RaceCourse R(40, 60) ; // Set up some walls SetObstacles(obs) ; // Set up winning location gptr = new Goal(20, 29) ; obs.push_back(gptr) ; // Tell RaceCourse R to put in the walls, etc. AddOK = R.AddThings(obs) ; if (!AddOK) { cerr << "Something went wrong with AddThings()\n" ; exit(1) ; } // Now we set up some cars // Scripted paths for each car to follow // vector script(4) ; // 0123456789012345678901234567890 script[0] = "drdrldrdluldruru" ; script[1] = "dldrdldlruldrurd" ; // Make some Cars // We need both Thing pointers and ScriptedCar pointers // because AddThings takes vector of Thing * only // cptr = new ScriptedCar(1, 1, '0', 185, script[0]) ; Cars.push_back(cptr) ; morethings.push_back(cptr) ; cptr = new ScriptedCar(1, 60, '1', 253, script[1]) ; Cars.push_back(cptr) ; morethings.push_back(cptr) ; // Tell RaceCourse R to put in the walls, etc. AddOK = R.AddThings(morethings) ; if (!AddOK) { cerr << "Something went wrong with AddThings()\n" ; exit(1) ; } // Print out starting line up // UpdateScreen(R, Cars) ; cout << "\n\nInitial Race Course: \n" ; cout << "#Rows=" << R.GetNumOfRows() << endl ; cout << "#Cols=" << R.GetNumOfCols() << endl ; cout << "-----------------------------------------\n\n" ; #ifdef VT100 // #define VT100 for "interactive" version. cout << "Type \"enter\" to continue\n" ; cin.ignore(10000, '\n') ; #endif R.StartRace() ; for (unsigned int move=0 ; move < 16 ; move++) { for (unsigned int i=0 ; i < Cars.size() ; i++) { Cars[i]->MakeMove() ; UpdateScreen(R, Cars) ; cout << "\nCar #" << Cars[i]->DrawMe() << ", Move #" << move << " was '" << script[i][move] << "'\n\n" ; // Check for winner if (gptr->GetWinner() != NULL) { cout << "And the winer is, Car #" << gptr->GetWinner()->DrawMe() << "!!\n" ; } cout << "-----------------------------------------\n\n\n" ; #ifdef VT100 // #define VT100 for "interactive" version. cout << "Type \"enter\" to continue\n" ; cin.ignore(10000, '\n') ; #endif } } // Deallocate memory for cars & Walls // for (unsigned int i=1; i < Cars.size() ; i++) { delete Cars[i] ; } for (unsigned int j=1; j < obs.size() ; j++) { delete obs[j] ; } }